observable

package
v0.2.2-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package observable is the framework primitive that lets backing stores (task list, subagent panel, future "notes" / "todos" / ...) publish state changes through a single uniform stream.

A store gains pub/sub by embedding Observable and satisfying Store. The owning container (toolset.ToolState) registers the store once, then any number of subscribers (the agent's event sink, persistence, tests) consume every store's changes through one Subscribe call.

Payload is intentionally untyped at this layer. Each store publishes a domain-typed snapshot ([]todo.Todo, meta.SubagentSnapshot, ...) in Change.Payload; consumers switch on Domain and type-assert. This trades a small amount of compile-time safety at the boundary for the ability to add a new domain without touching the event or agent packages at all.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	Domain  string
	Op      string
	ID      string
	Payload any
	Time    time.Time
}

Change is a single state-change notification a Store emits to its observers.

Domain identifies the emitting store ("task", "subagent", ...). Op names the verb ("created", "updated", "removed", "phase", ...). ID is the affected entity. Payload carries a domain-typed snapshot the consumer type-asserts on.

type Observable

type Observable struct {
	// contains filtered or unexported fields
}

Observable(被觀察者) is the embeddable pub/sub primitive. Zero value is ready to use; safe for concurrent Subscribe and Notify from any goroutine.

func (*Observable) Notify

func (o *Observable) Notify(c Change)

Notify fans c out to every subscriber. Time is filled in when zero. The observer slice is snapshotted under read-lock so a Subscribe racing with Notify is safe and observers receive a stable view.

func (*Observable) Subscribe

func (o *Observable) Subscribe(fn Observer)

Subscribe appends fn to the observer list. nil fns are ignored. (被訂閱)

type Observer

type Observer func(Change)

Observer(觀察者) is the callback shape Subscribe accepts. Observers run on the goroutine that called Notify and must not block — slow consumers buffer internally.

type Store

type Store interface {
	Domain() string
	Subscribe(Observer) // Store 必須可被觀察
}

Store is the contract every observable backing store satisfies. The Observable mixin below provides Subscribe and a Notify helper, so a store typically only needs to declare Domain().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL