[agent/codex:gpt-5.4-mini] Read ~/spec/code/core/go/store/RFC.md fully. Find features d... #60
2 changed files with 72 additions and 0 deletions
37
scope.go
37
scope.go
|
|
@ -279,6 +279,43 @@ func (scopedStore *ScopedStore) GetFields(group, key string) (iter.Seq[string],
|
|||
return storeInstance.GetFields(scopedStore.namespacedGroup(group), key)
|
||||
}
|
||||
|
||||
// Usage example: `events := scopedStore.Watch("config")`
|
||||
func (scopedStore *ScopedStore) Watch(group string) <-chan Event {
|
||||
storeInstance, err := scopedStore.storeInstance("store.Watch")
|
||||
if err != nil {
|
||||
return closedEventChannel()
|
||||
}
|
||||
return storeInstance.Watch(scopedStore.namespacedGroup(group))
|
||||
}
|
||||
|
||||
// Usage example: `scopedStore.Unwatch("config", events)`
|
||||
func (scopedStore *ScopedStore) Unwatch(group string, events <-chan Event) {
|
||||
storeInstance, err := scopedStore.storeInstance("store.Unwatch")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
storeInstance.Unwatch(scopedStore.namespacedGroup(group), events)
|
||||
}
|
||||
|
||||
// Usage example: `unregister := scopedStore.OnChange(func(event store.Event) { fmt.Println(event.Group, event.Key) })`
|
||||
func (scopedStore *ScopedStore) OnChange(callback func(Event)) func() {
|
||||
storeInstance, err := scopedStore.storeInstance("store.OnChange")
|
||||
if err != nil {
|
||||
return func() {}
|
||||
}
|
||||
if callback == nil {
|
||||
return func() {}
|
||||
}
|
||||
|
||||
namespacePrefix := scopedStore.namespacePrefix()
|
||||
return storeInstance.OnChange(func(event Event) {
|
||||
if !core.HasPrefix(event.Group, namespacePrefix) {
|
||||
return
|
||||
}
|
||||
callback(event)
|
||||
})
|
||||
}
|
||||
|
||||
// Usage example: `removedRows, err := scopedStore.PurgeExpired(); if err != nil { return }; fmt.Println(removedRows)`
|
||||
func (scopedStore *ScopedStore) PurgeExpired() (int64, error) {
|
||||
storeInstance, err := scopedStore.storeInstance("store.PurgeExpired")
|
||||
|
|
|
|||
|
|
@ -471,6 +471,41 @@ func TestScope_ScopedStore_Good_PurgeExpired_NamespaceLocal(t *testing.T) {
|
|||
assert.Equal(t, 1, rawEntryCount(t, storeInstance, "tenant-b:session"))
|
||||
}
|
||||
|
||||
func TestScope_ScopedStore_Good_WatchAndUnwatch(t *testing.T) {
|
||||
storeInstance, _ := New(":memory:")
|
||||
defer storeInstance.Close()
|
||||
|
||||
scopedStore := mustScoped(t, storeInstance, "tenant-a")
|
||||
events := scopedStore.Watch("config")
|
||||
scopedStore.Unwatch("config", events)
|
||||
|
||||
_, open := <-events
|
||||
assert.False(t, open, "channel should be closed after Unwatch")
|
||||
|
||||
require.NoError(t, scopedStore.SetIn("config", "theme", "dark"))
|
||||
}
|
||||
|
||||
func TestScope_ScopedStore_Good_OnChange(t *testing.T) {
|
||||
storeInstance, _ := New(":memory:")
|
||||
defer storeInstance.Close()
|
||||
|
||||
scopedStore := mustScoped(t, storeInstance, "tenant-a")
|
||||
|
||||
var seen []Event
|
||||
unregister := scopedStore.OnChange(func(event Event) {
|
||||
seen = append(seen, event)
|
||||
})
|
||||
defer unregister()
|
||||
|
||||
require.NoError(t, scopedStore.SetIn("config", "theme", "dark"))
|
||||
require.NoError(t, storeInstance.Set("other", "key", "value"))
|
||||
|
||||
require.Len(t, seen, 1)
|
||||
assert.Equal(t, "tenant-a:config", seen[0].Group)
|
||||
assert.Equal(t, "theme", seen[0].Key)
|
||||
assert.Equal(t, "dark", seen[0].Value)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Quota enforcement — MaxKeys
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue