trellis
Version:
Agentic State Engine — event-sourced causal graph with branching, decision traces, and realtime sync for AI-native applications
60 lines (58 loc) • 1.14 kB
JavaScript
// src/client/reactive.ts
var Signal = class {
_value;
_subs = /* @__PURE__ */ new Set();
constructor(initial) {
this._value = initial;
}
get value() {
return this._value;
}
set value(v) {
if (Object.is(this._value, v)) return;
this._value = v;
for (const fn of this._subs) {
try {
fn(v);
} catch {
}
}
}
/** Subscribe to changes. Immediately called with current value. */
subscribe(fn) {
this._subs.add(fn);
try {
fn(this._value);
} catch {
}
return () => {
this._subs.delete(fn);
};
}
/** Read without tracking. */
peek() {
return this._value;
}
/** Whether any subscriber is registered. */
hasSubscribers() {
return this._subs.size > 0;
}
};
var BatchSignal = class extends Signal {
_raf = null;
constructor(initial) {
super(initial);
}
set value(v) {
if (Object.is(this.peek(), v)) return;
if (this._raf !== null) cancelAnimationFrame(this._raf);
this._raf = requestAnimationFrame(() => {
this._raf = null;
super.value = v;
});
}
};
export {
Signal,
BatchSignal
};