[agent/codex:gpt-5.4-mini] Read docs/RFC-STORE.md and docs/specs/core/go/RFC.md fully. ... #164
2 changed files with 47 additions and 0 deletions
21
scope.go
21
scope.go
|
|
@ -280,6 +280,27 @@ func (scopedStore *ScopedStore) PurgeExpired() (int64, error) {
|
|||
return removedRows, nil
|
||||
}
|
||||
|
||||
// Usage example: `unregister := scopedStore.OnChange(func(event store.Event) { fmt.Println(event.Group, event.Key, event.Value) })`
|
||||
// The callback receives the namespace-local group name, so a write to
|
||||
// `tenant-a:config` is reported as `config`.
|
||||
func (scopedStore *ScopedStore) OnChange(callback func(Event)) func() {
|
||||
if scopedStore == nil || callback == nil {
|
||||
return func() {}
|
||||
}
|
||||
if scopedStore.storeInstance == nil {
|
||||
return func() {}
|
||||
}
|
||||
|
||||
namespacePrefix := scopedStore.namespacePrefix()
|
||||
return scopedStore.storeInstance.OnChange(func(event Event) {
|
||||
if !core.HasPrefix(event.Group, namespacePrefix) {
|
||||
return
|
||||
}
|
||||
event.Group = core.TrimPrefix(event.Group, namespacePrefix)
|
||||
callback(event)
|
||||
})
|
||||
}
|
||||
|
||||
// ScopedStoreTransaction exposes namespace-local transaction helpers so callers
|
||||
// can work inside a scoped namespace without manually prefixing group names.
|
||||
//
|
||||
|
|
|
|||
|
|
@ -273,6 +273,32 @@ func TestScope_ScopedStore_Good_DeletePrefix(t *testing.T) {
|
|||
assert.Equal(t, "keep", otherValue)
|
||||
}
|
||||
|
||||
func TestScope_ScopedStore_Good_OnChange_NamespaceLocal(t *testing.T) {
|
||||
storeInstance, _ := New(":memory:")
|
||||
defer storeInstance.Close()
|
||||
|
||||
scopedStore, _ := NewScoped(storeInstance, "tenant-a")
|
||||
otherScopedStore, _ := NewScoped(storeInstance, "tenant-b")
|
||||
|
||||
var events []Event
|
||||
unregister := scopedStore.OnChange(func(event Event) {
|
||||
events = append(events, event)
|
||||
})
|
||||
defer unregister()
|
||||
|
||||
require.NoError(t, scopedStore.SetIn("config", "colour", "blue"))
|
||||
require.NoError(t, otherScopedStore.SetIn("config", "colour", "red"))
|
||||
require.NoError(t, scopedStore.Delete("config", "colour"))
|
||||
|
||||
require.Len(t, events, 2)
|
||||
assert.Equal(t, "config", events[0].Group)
|
||||
assert.Equal(t, "colour", events[0].Key)
|
||||
assert.Equal(t, "blue", events[0].Value)
|
||||
assert.Equal(t, "config", events[1].Group)
|
||||
assert.Equal(t, "colour", events[1].Key)
|
||||
assert.Equal(t, "", events[1].Value)
|
||||
}
|
||||
|
||||
func TestScope_ScopedStore_Good_GetAll(t *testing.T) {
|
||||
storeInstance, _ := New(":memory:")
|
||||
defer storeInstance.Close()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue