@expressive/mvc 0.86.0 → 0.88.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/component.d.ts +33 -11
- package/dist/component.js +88 -53
- package/dist/component.js.map +1 -1
- package/dist/context.d.ts +3 -1
- package/dist/context.js +30 -12
- package/dist/context.js.map +1 -1
- package/dist/dispatch.d.ts +6 -1
- package/dist/dispatch.js +14 -6
- package/dist/dispatch.js.map +1 -1
- package/dist/field/def.js +4 -3
- package/dist/field/def.js.map +1 -1
- package/dist/field/get.js +21 -5
- package/dist/field/get.js.map +1 -1
- package/dist/field/has.js +1 -1
- package/dist/field/map.js +1 -1
- package/dist/field/ref.js +1 -1
- package/dist/field/set.js +1 -1
- package/dist/hot.d.ts +29 -0
- package/dist/hot.js +225 -0
- package/dist/hot.js.map +1 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +1 -2
- package/dist/jsx-dev-runtime.js +1 -1
- package/dist/jsx-runtime.d.ts +78 -5
- package/dist/jsx-runtime.js +69 -2
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/observable.js +3 -1
- package/dist/observable.js.map +1 -1
- package/dist/state.d.ts +78 -32
- package/dist/state.js +143 -89
- package/dist/state.js.map +1 -1
- package/package.json +7 -5
- package/dist/caught.d.ts +0 -5237
- package/dist/caught.js +0 -64
- package/dist/caught.js.map +0 -1
- package/dist/runtime.d.ts +0 -71
- package/dist/runtime.js +0 -68
- package/dist/runtime.js.map +0 -1
package/dist/jsx-runtime.js
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { compose } from "./component.js";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
//#region src/jsx-runtime.ts
|
|
4
|
+
/**
|
|
5
|
+
* Agnostic Fragment sentinel. The runtime entries translate it to the host's
|
|
6
|
+
* Fragment on element creation; `typeOf` translates back, so identity checks
|
|
7
|
+
* against this value hold for fragments from either pragma.
|
|
8
|
+
*/
|
|
9
|
+
const Fragment = Symbol.for("@expressive/mvc.Fragment");
|
|
10
|
+
/** Default for seams only a registered host can provide. */
|
|
11
|
+
function absent() {
|
|
12
|
+
throw new Error("No JSX host is registered for @expressive/mvc. Import a host adapter (e.g. '@expressive/react') before anything renders - typically as the first import of your application entry module.");
|
|
13
|
+
}
|
|
14
|
+
const HOST = {
|
|
15
|
+
childrenOf: absent,
|
|
16
|
+
isElement: absent,
|
|
17
|
+
jsx: absent,
|
|
18
|
+
jsxs: absent,
|
|
19
|
+
propsOf: absent,
|
|
20
|
+
typeOf: absent,
|
|
21
|
+
Fragment
|
|
22
|
+
};
|
|
23
|
+
let registered;
|
|
24
|
+
/**
|
|
25
|
+
* Register the host runtime. Idempotent for the same runtime - re-registering
|
|
26
|
+
* picks up members added since (e.g. an optional dev seam). Element mechanics
|
|
27
|
+
* the host leaves unset keep defaults that throw a setup-pointing error;
|
|
28
|
+
* `jsxDEV` is routed around at the call site instead.
|
|
29
|
+
*/
|
|
30
|
+
function host(runtime) {
|
|
31
|
+
if (registered && registered !== runtime) throw new Error("A different JSX host is already registered for @expressive/mvc. Only one host adapter may be active per build.");
|
|
32
|
+
registered = runtime;
|
|
33
|
+
Object.assign(HOST, runtime);
|
|
34
|
+
}
|
|
35
|
+
/** Translate the agnostic Fragment sentinel to the host's Fragment. */
|
|
36
|
+
function hostType(type) {
|
|
37
|
+
return type === Fragment ? HOST.Fragment : type;
|
|
38
|
+
}
|
|
39
|
+
function jsx(type, props, key) {
|
|
40
|
+
return HOST.jsx(hostType(type), props, key);
|
|
41
|
+
}
|
|
42
|
+
function jsxs(type, props, key) {
|
|
43
|
+
return HOST.jsxs(hostType(type), props, key);
|
|
44
|
+
}
|
|
45
|
+
/** Dev-transpiled element creation. Hosts without a `jsxDEV` of their own fall
|
|
46
|
+
* back to the production entries (prod-only host under a dev build). */
|
|
47
|
+
function jsxDEV(type, props, key, isStatic, source, self) {
|
|
48
|
+
return HOST.jsxDEV ? HOST.jsxDEV(hostType(type), props, key, isStatic, source, self) : isStatic ? jsxs(type, props, key) : jsx(type, props, key);
|
|
49
|
+
}
|
|
50
|
+
/** Flatten `children` to an array of nodes, per the host's semantics. */
|
|
51
|
+
function childrenOf(children) {
|
|
52
|
+
return HOST.childrenOf(children);
|
|
53
|
+
}
|
|
54
|
+
/** Is `node` a host element? */
|
|
55
|
+
function isElement(node) {
|
|
56
|
+
return HOST.isElement(node);
|
|
57
|
+
}
|
|
58
|
+
/** Element type of `node`; host Fragments surface as the agnostic `Fragment`. */
|
|
59
|
+
function typeOf(node) {
|
|
60
|
+
const type = HOST.typeOf(node);
|
|
61
|
+
return type === HOST.Fragment ? Fragment : type;
|
|
62
|
+
}
|
|
63
|
+
/** Props carried by element `node`. */
|
|
64
|
+
function propsOf(node) {
|
|
65
|
+
return HOST.propsOf(node);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { Fragment, childrenOf, compose, host, isElement, jsx, jsxDEV, jsxs, propsOf, typeOf };
|
|
70
|
+
//# sourceMappingURL=jsx-runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsx-runtime.js","names":[],"sources":["../src/jsx-runtime.ts"],"sourcesContent":["import type { Component } from './component';\n\nexport { compose } from './component';\n\n/**\n * Per-adapter interpretation manifest. Each adapter augments this interface to\n * declare how it renders; the first member is `node` - the element type produced\n * by `Component.render` (surfaced as `Component.Node`) - alongside `intrinsics`,\n * the host's tag map (surfaced as `JSX.IntrinsicElements`), `elementClass`,\n * the instance a class must construct to render as an element, and optionally\n * `element`, further element types the host renders (joined into\n * `JSX.ElementType`).\n *\n * ```ts\n * declare module '@expressive/mvc/jsx-runtime' {\n * interface Host {\n * node: React.ReactNode;\n * intrinsics: React.JSX.IntrinsicElements;\n * elementClass: React.Component<any, any>;\n * }\n * }\n * ```\n *\n * Only one adapter is expected per compilation; two augmenting the same member\n * with different types in the same build would conflict - by design.\n */\nexport interface Host { }\n\n/**\n * Host runtime, registered by an adapter (e.g. `@expressive/react`) as an\n * import side effect. One host per build - a second registration with a\n * different runtime throws.\n *\n * Members are ambient host capabilities: plain functions the host uniquely\n * owns, callable outside render - element mechanics always, plus optional\n * extras that fall back sanely when absent. Hook-shaped (render-resident)\n * capabilities do not belong here; those live with the adapter.\n */\nexport interface HostRuntime {\n childrenOf(children: unknown): Component.Node[];\n isElement(node: unknown): boolean;\n jsx(type: unknown, props: object, key?: unknown): Component.Node;\n jsxDEV?(type: unknown, props: object, key?: unknown, isStatic?: boolean, source?: object, self?: unknown): Component.Node;\n jsxs(type: unknown, props: object, key?: unknown): Component.Node;\n propsOf(node: unknown): Record<string, unknown>;\n typeOf(node: unknown): unknown;\n Fragment: unknown;\n}\n\n/**\n * Agnostic Fragment sentinel. The runtime entries translate it to the host's\n * Fragment on element creation; `typeOf` translates back, so identity checks\n * against this value hold for fragments from either pragma.\n */\nexport const Fragment = Symbol.for('@expressive/mvc.Fragment');\n\n/** Default for seams only a registered host can provide. */\nfunction absent(): never {\n throw new Error(\n 'No JSX host is registered for @expressive/mvc. ' +\n \"Import a host adapter (e.g. '@expressive/react') before anything renders - \" +\n 'typically as the first import of your application entry module.'\n );\n}\n\nconst HOST: HostRuntime = {\n childrenOf: absent,\n isElement: absent,\n jsx: absent,\n jsxs: absent,\n propsOf: absent,\n typeOf: absent,\n Fragment\n};\n\nlet registered: HostRuntime | undefined;\n\n/**\n * Register the host runtime. Idempotent for the same runtime - re-registering\n * picks up members added since (e.g. an optional dev seam). Element mechanics\n * the host leaves unset keep defaults that throw a setup-pointing error;\n * `jsxDEV` is routed around at the call site instead.\n */\nexport function host(runtime: HostRuntime) {\n if (registered && registered !== runtime)\n throw new Error(\n 'A different JSX host is already registered for @expressive/mvc. ' +\n 'Only one host adapter may be active per build.'\n );\n\n registered = runtime;\n Object.assign(HOST, runtime);\n}\n\n/** Translate the agnostic Fragment sentinel to the host's Fragment. */\nfunction hostType(type: unknown): unknown {\n return type === Fragment ? HOST.Fragment : type;\n}\n\nexport function jsx(type: unknown, props: object, key?: unknown): Component.Node {\n return HOST.jsx(hostType(type), props, key);\n}\n\nexport function jsxs(type: unknown, props: object, key?: unknown): Component.Node {\n return HOST.jsxs(hostType(type), props, key);\n}\n\n/** Dev-transpiled element creation. Hosts without a `jsxDEV` of their own fall\n * back to the production entries (prod-only host under a dev build). */\nexport function jsxDEV(\n type: unknown,\n props: object,\n key?: unknown,\n isStatic?: boolean,\n source?: object,\n self?: unknown\n): Component.Node {\n return HOST.jsxDEV\n ? HOST.jsxDEV(hostType(type), props, key, isStatic, source, self)\n : isStatic\n ? jsxs(type, props, key)\n : jsx(type, props, key);\n}\n\n/** Flatten `children` to an array of nodes, per the host's semantics. */\nexport function childrenOf(children: unknown): Component.Node[] {\n return HOST.childrenOf(children);\n}\n\n/** Is `node` a host element? */\nexport function isElement(node: unknown): boolean {\n return HOST.isElement(node);\n}\n\n/** Element type of `node`; host Fragments surface as the agnostic `Fragment`. */\nexport function typeOf(node: unknown): unknown {\n const type = HOST.typeOf(node);\n return type === HOST.Fragment ? Fragment : type;\n}\n\n/** Props carried by element `node`. */\nexport function propsOf(node: unknown): Record<string, unknown> {\n return HOST.propsOf(node);\n}\n\n\nexport declare namespace JSX {\n type Element = Component.Node;\n type ElementType =\n | keyof IntrinsicElements\n | ((props: any) => Component.Node)\n | (new (...args: any[]) => Host extends { elementClass: infer I } ? I : ElementClass)\n | (Host extends { element: infer T } ? T : never);\n /**\n * Class instance contract when no host declares `elementClass`. Gates on the\n * `render` contract alone - requiring full\n * `Component` assignability trips contravariance on `props` members (e.g.\n * the `is` callback narrows per subclass), rejecting every subclass.\n */\n interface ElementClass { render(props?: any): Component.Node }\n interface ElementAttributesProperty { props: {} }\n interface ElementChildrenAttribute { children: {} }\n interface IntrinsicAttributes { key?: string | number | null }\n /**\n * Host tag manifest, via the {@link Host} seam. Resolves to `{}` until an\n * adapter augments `Host` with `intrinsics` - agnostic code is\n * components-only by design, since it cannot know what tags a host renders.\n */\n type IntrinsicElements =\n Host extends { intrinsics: infer T extends object } ? T : {};\n}\n"],"mappings":";;;;;;;;AAsDA,MAAa,WAAW,OAAO,IAAI,2BAA2B;;AAG9D,SAAS,SAAgB;AACvB,OAAM,IAAI,MACR,4LAGD;;AAGH,MAAM,OAAoB;CACxB,YAAY;CACZ,WAAW;CACX,KAAK;CACL,MAAM;CACN,SAAS;CACT,QAAQ;CACR;CACD;AAED,IAAI;;;;;;;AAQJ,SAAgB,KAAK,SAAsB;AACzC,KAAI,cAAc,eAAe,QAC/B,OAAM,IAAI,MACR,iHAED;AAEH,cAAa;AACb,QAAO,OAAO,MAAM,QAAQ;;;AAI9B,SAAS,SAAS,MAAwB;AACxC,QAAO,SAAS,WAAW,KAAK,WAAW;;AAG7C,SAAgB,IAAI,MAAe,OAAe,KAA+B;AAC/E,QAAO,KAAK,IAAI,SAAS,KAAK,EAAE,OAAO,IAAI;;AAG7C,SAAgB,KAAK,MAAe,OAAe,KAA+B;AAChF,QAAO,KAAK,KAAK,SAAS,KAAK,EAAE,OAAO,IAAI;;;;AAK9C,SAAgB,OACd,MACA,OACA,KACA,UACA,QACA,MACgB;AAChB,QAAO,KAAK,SACR,KAAK,OAAO,SAAS,KAAK,EAAE,OAAO,KAAK,UAAU,QAAQ,KAAK,GAC/D,WACE,KAAK,MAAM,OAAO,IAAI,GACtB,IAAI,MAAM,OAAO,IAAI;;;AAI7B,SAAgB,WAAW,UAAqC;AAC9D,QAAO,KAAK,WAAW,SAAS;;;AAIlC,SAAgB,UAAU,MAAwB;AAChD,QAAO,KAAK,UAAU,KAAK;;;AAI7B,SAAgB,OAAO,MAAwB;CAC7C,MAAM,OAAO,KAAK,OAAO,KAAK;AAC9B,QAAO,SAAS,KAAK,WAAW,WAAW;;;AAI7C,SAAgB,QAAQ,MAAwC;AAC9D,QAAO,KAAK,QAAQ,KAAK"}
|
package/dist/observable.js
CHANGED
|
@@ -3,6 +3,8 @@ import { enqueue, hold } from "./dispatch.js";
|
|
|
3
3
|
//#region src/observable.ts
|
|
4
4
|
const Observer = Symbol("Observer");
|
|
5
5
|
const Observing = Symbol("Observing");
|
|
6
|
+
/** Event every observer treats as watched, to force a refresh. */
|
|
7
|
+
const REFRESH = Symbol.for("@expressive/mvc.refresh");
|
|
6
8
|
function observer(state, create) {
|
|
7
9
|
const o = state[Observer];
|
|
8
10
|
if (!o && create) {
|
|
@@ -29,7 +31,7 @@ function observe(object, callback, required, cleanup = []) {
|
|
|
29
31
|
release: cleanup
|
|
30
32
|
};
|
|
31
33
|
const release = listener(object, (key) => {
|
|
32
|
-
if (watching.has(key)) return observing.callback();
|
|
34
|
+
if (watching.has(key) || key === REFRESH) return observing.callback();
|
|
33
35
|
});
|
|
34
36
|
cleanup.push(release);
|
|
35
37
|
if (EffectContext) EffectContext.add((update) => {
|
package/dist/observable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observable.js","names":[],"sources":["../src/observable.ts"],"sourcesContent":["import { enqueue, hold } from './dispatch';\n\ndeclare namespace Observer {\n /**\n * Update callback function.\n *\n * @param key -\n * - `string` - property which has updated.\n * - `false` - a normal update has completed.\n * - `true` - initial event; instance is ready.\n * - `null` - terminal event; instance is expired.\n */\n type Notify = (key: Signal) => (() => void) | null | void;\n\n type EffectCleanup = (update: boolean | null) => void;\n\n type Effect<T extends object> = (\n this: T,\n proxy: T,\n changed: readonly Event[]\n ) => EffectCleanup | Promise<void> | null | void;\n\n type Event = number | string | symbol;\n\n type Signal = Event | true | false | null;\n\n type Callback = () => void | null;\n}\n\ninterface Observer {\n /** Becomes `true` once the subject has emitted its initial event. */\n ready?: true;\n listeners: Map<Observer.Notify, Set<Observer.Signal> | undefined>;\n /** Recursion guard for an in-progress emit pass. */\n pending: Set<Observer.Signal>;\n /** Accumulator drained by the next microtask. */\n events: Set<Observer.Event>;\n}\n\ninterface Observing {\n callback: Observer.Callback;\n watching: Set<Observer.Signal>;\n required?: boolean;\n release: (() => void)[];\n}\n\nconst Observer: unique symbol = Symbol('Observer');\nconst Observing: unique symbol = Symbol('Observing');\n\ninterface Observable { [Observer]?: Observer | null }\ninterface Observed { [Observing]?: Observing }\n\n/**\n * Fetch the dispatch bundle attached to an object.\n * - returns the bundle if active\n * - returns `null` if the subject has been terminated\n * - returns `undefined` if the subject was never observable\n *\n * With `create: true`, attaches a fresh bundle iff the slot is undefined.\n * A terminated (null) slot is not resurrected.\n */\nfunction observer(state: object, create: true): Observer;\nfunction observer(state: object, create?: boolean): Observer | undefined | null;\nfunction observer(state: object, create?: boolean) {\n const o = (state as Observable)[Observer];\n\n if (!o && create) {\n if (o === null)\n throw new Error(\n `${state} was destroyed - cannot be rendered, watched or updated.`\n );\n\n const value: Observer = {\n listeners: new Map(),\n pending: new Set(),\n events: new Set()\n };\n Object.defineProperty(state, Observer, { value, writable: true });\n return value;\n }\n\n return o;\n}\n\nfunction observe<T extends object>(\n object: T,\n callback: Observer.Callback,\n required?: boolean,\n cleanup: (() => void)[] = []\n): T {\n const watching = new Set<Observer.Signal>();\n const observing: Observing = { callback, watching, required, release: cleanup };\n\n const release = listener(object, (key) => {\n if (watching.has(key)) return observing.callback();\n });\n\n cleanup.push(release);\n\n if (EffectContext)\n EffectContext.add((update) => {\n if (update !== true) release();\n });\n\n const proxy = Object.create(object) as T;\n\n if ('is' in object)\n Object.defineProperty(proxy, 'is', {\n value: (object as { is?: unknown }).is,\n writable: true\n });\n\n return Object.defineProperty(proxy, Observing, { value: observing });\n}\n\nfunction touch(from: object, key: any): void;\nfunction touch<T>(from: object, key: any, value?: T): T;\nfunction touch(from: object, key: any, value?: any) {\n const active = (from as Observed)[Observing];\n\n if (active) {\n if (value === undefined && arguments.length === 3 && active.required)\n throw new Error(\n `${Object.getPrototypeOf(from)}.${String(key)} is required in this context.`\n );\n\n active.watching.add(key);\n\n if (value instanceof Object && observer(value))\n return observe(\n value,\n () => active.callback(),\n active.required,\n active.release\n );\n }\n\n return value;\n}\n\nfunction unobserve(observing: Observing) {\n for (const release of observing.release) release();\n}\n\nfunction listener<T extends object>(\n subject: T,\n callback: Observer.Notify,\n select?: Observer.Signal | Set<Observer.Signal>\n) {\n const o = observer(subject, true);\n\n if (select !== undefined && !(select instanceof Set))\n select = new Set([select]);\n\n if (o.ready && !select) callback(true);\n\n o.listeners.set(callback, select);\n\n return () => o.listeners.delete(callback);\n}\n\nconst EMPTY = Object.assign([], {\n then: Promise.prototype.then.bind(Promise.resolve([]))\n} as PromiseLike<never[]>);\n\nfunction queued<K extends Observer.Event>(\n state: object\n): K[] & PromiseLike<K[]> {\n const o = observer(state);\n if (!o?.events.size) return EMPTY;\n\n const current = o.events as Set<K>;\n const promise = new Promise<K[]>((res) => {\n const callback: Observer.Notify = () => {\n o.listeners.delete(callback);\n return () => res([...current]);\n };\n o.listeners.set(callback, undefined);\n });\n\n return Object.assign([...current], {\n then: promise.then.bind(promise)\n });\n}\n\nfunction event(state: object, key?: Observer.Event | null, silent?: boolean) {\n const o = observer(state, key === undefined);\n\n if (!o) return;\n\n if (key === null) {\n if (!Object.getOwnPropertyDescriptor(state, Observer)) return;\n\n (state as Observable)[Observer] = null;\n return emit(o, key, state);\n }\n\n if (key === undefined) return emit(o, true, state);\n\n if (!o.events.size && !silent)\n enqueue(() => {\n emit(o, false, state);\n o.events = new Set();\n }, undefined, state);\n\n o.events.add(key);\n\n if (!silent) emit(o, key, state);\n}\n\nfunction emit(o: Observer, key: Observer.Signal, subject: object): void {\n const { listeners, pending, ready } = o;\n\n if (key === ready) return;\n\n if (pending.size) {\n pending.add(key);\n return;\n }\n\n if (!ready && key !== null) pending.add(true);\n\n pending.add(key);\n\n for (const k of pending)\n for (const [callback, filter] of listeners)\n if (!filter || filter.has(k)) {\n const after = callback(k);\n if (after === null) listeners.delete(callback);\n else if (typeof after == 'function') enqueue(after, undefined, subject);\n }\n\n if (key === null) listeners.clear();\n else if (key === true) o.ready = true;\n\n pending.clear();\n}\n\n/**\n * Create a side-effect which will update whenever values accessed change.\n * Callback is called immediately and whenever values are stale.\n *\n * @param target - Instance of State to observe.\n * @param callback - Function to invoke when values change.\n * @param requireValues - If `true` will throw if accessing a value which is `undefined`.\n */\nfunction watch<T extends object>(\n target: T,\n callback: Observer.Effect<Required<T>>,\n requireValues: true,\n transition?: (work: () => void) => void\n): () => void;\n\nfunction watch<T extends object>(\n target: T,\n callback: Observer.Effect<T>,\n recursive?: boolean,\n transition?: (work: () => void) => void\n): () => void;\n\nfunction watch<T extends object>(\n target: T,\n callback: Observer.Effect<T>,\n argument?: boolean,\n transition?: (work: () => void) => void\n) {\n const o = observer(target, true);\n let events: readonly Observer.Event[] = [];\n let unset: ((update: boolean | null) => void) | undefined;\n let reset: (() => void) | null | undefined;\n let previous: Observing | undefined;\n let latest: Observing | undefined;\n let queued = false;\n let suspense: ReturnType<typeof hold>;\n\n function resume() {\n suspense?.();\n suspense = undefined;\n }\n\n function invoke() {\n if (reset === null || observer(target) === null) return;\n\n queued = false;\n let ignore: boolean = true;\n\n function onUpdate() {\n events = [...o.events];\n\n if (reset === null) return null;\n if (ignore) {\n if (queued) enqueue(invoke, transition, target);\n return;\n }\n\n ignore = true;\n queued = true;\n\n unset!(true);\n unset = undefined;\n\n enqueue(invoke, transition, target);\n }\n\n function run(release?: (update?: boolean | null) => void) {\n if (previous !== latest) unobserve(latest!);\n\n const proxy = observe(target, onUpdate, argument === true);\n const current = latest = (proxy as Observed)[Observing]!;\n const output = callback.call(proxy, proxy, events);\n\n if (observer(target) === null) {\n if (typeof output == 'function') output(null);\n if (release) release(null);\n return;\n }\n\n if (previous && argument === false && !current.watching.size)\n previous.callback = onUpdate;\n else {\n if (previous) unobserve(previous);\n previous = current;\n }\n\n ignore = false;\n reset = output === null ? null : invoke;\n unset = (key) => {\n if (typeof output == 'function') output(key);\n if (release) release(key);\n };\n\n events = [];\n }\n\n try {\n if (argument !== false) capture(run);\n else run();\n } catch (err) {\n if (err instanceof Promise) {\n reset = undefined;\n const suspended = suspense = hold(true, transition)!;\n const replay = () => {\n if (suspense !== suspended) return;\n suspense = undefined;\n suspended(invoke);\n };\n err.then(replay, replay);\n } else {\n throw err;\n }\n }\n }\n\n const unlisten = listener(target, (key) => {\n if (key === true) invoke();\n else if (key === null) {\n if (unset) unset(null);\n resume();\n reset = null;\n } else if (!reset) {\n suspense?.(false);\n return reset;\n }\n });\n\n function cleanup() {\n if (unset) unset(false);\n\n resume();\n reset = null;\n unlisten();\n }\n\n if (EffectContext && argument !== false) EffectContext.add(cleanup);\n\n return cleanup;\n}\n\nlet EffectContext: Set<(update?: boolean | null) => void> | undefined;\n\nfunction capture(scope: (release: (update?: boolean | null) => void) => void) {\n const parent = EffectContext;\n const context = (EffectContext = new Set());\n\n try {\n scope((update) => context.forEach((fn) => fn(update)));\n } finally {\n EffectContext = parent;\n }\n}\n\nexport {\n listener,\n event,\n Observer,\n touch,\n queued,\n observer,\n watch,\n capture\n};\n"],"mappings":";;;AA8CA,MAAM,WAA0B,OAAO,WAAW;AAClD,MAAM,YAA2B,OAAO,YAAY;AAgBpD,SAAS,SAAS,OAAe,QAAkB;CACjD,MAAM,IAAK,MAAqB;AAEhC,KAAI,CAAC,KAAK,QAAQ;AAChB,MAAI,MAAM,KACR,OAAM,IAAI,MACR,GAAG,MAAM,0DACV;EAEH,MAAM,QAAkB;GACtB,2BAAW,IAAI,KAAK;GACpB,yBAAS,IAAI,KAAK;GAClB,wBAAQ,IAAI,KAAK;GAClB;AACD,SAAO,eAAe,OAAO,UAAU;GAAE;GAAO,UAAU;GAAM,CAAC;AACjE,SAAO;;AAGT,QAAO;;AAGT,SAAS,QACP,QACA,UACA,UACA,UAA0B,EAAE,EACzB;CACH,MAAM,2BAAW,IAAI,KAAsB;CAC3C,MAAM,YAAuB;EAAE;EAAU;EAAU;EAAU,SAAS;EAAS;CAE/E,MAAM,UAAU,SAAS,SAAS,QAAQ;AACxC,MAAI,SAAS,IAAI,IA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,OAAO;AACjC,YAAS,QAAQ;AACf,QAAI,OAAO,UAAU,WAAY,QAAO,IAAI;AAC5C,QAAI,QAAS,SAAQ,IAAI;;AAG3B,YAAS,EAAE;;AAGb,MAAI;AACF,OAAI,aAAa,MAAO,SAAQ,IAAI;OAC/B,MAAK;WACH,KAAK;AACZ,OAAI,eAAe,SAAS;AAC1B,YAAQ;IACR,MAAM,YAAY,WAAW,KAAK,MAAM,WAAW;IACnD,MAAM,eAAe;AACnB,SAAI,aAAa,UAAW;AAC5B,gBAAW;AACX,eAAU,OAAO;;AAEnB,QAAI,KAAK,QAAQ,OAAO;SAExB,OAAM;;;CAKZ,MAAM,WAAW,SAAS,SAAS,QAAQ;AACzC,MAAI,QAAQ,KAAM,SAAQ;WACjB,QAAQ,MAAM;AACrB,OAAI,MAAO,OAAM,KAAK;AACtB,WAAQ;AACR,WAAQ;aACC,CAAC,OAAO;AACjB,cAAW,MAAM;AACjB,UAAO;;GAET;CAEF,SAAS,UAAU;AACjB,MAAI,MAAO,OAAM,MAAM;AAEvB,UAAQ;AACR,UAAQ;AACR,YAAU;;AAGZ,KAAI,iBAAiB,aAAa,MAAO,eAAc,IAAI,QAAQ;AAEnE,QAAO;;AAGT,IAAI;AAEJ,SAAS,QAAQ,OAA6D;CAC5E,MAAM,SAAS;CACf,MAAM,UAAW,gCAAgB,IAAI,KAAK;AAE1C,KAAI;AACF,SAAO,WAAW,QAAQ,SAAS,OAAO,GAAG,OAAO,CAAC,CAAC;WAC9C;AACR,kBAAgB"}
|
|
1
|
+
{"version":3,"file":"observable.js","names":[],"sources":["../src/observable.ts"],"sourcesContent":["import { enqueue, hold } from './dispatch';\n\ndeclare namespace Observer {\n /**\n * Update callback function.\n *\n * @param key -\n * - `string` - property which has updated.\n * - `false` - a normal update has completed.\n * - `true` - initial event; instance is ready.\n * - `null` - terminal event; instance is expired.\n */\n type Notify = (key: Signal) => (() => void) | null | void;\n\n type EffectCleanup = (update: boolean | null) => void;\n\n type Effect<T extends object> = (\n this: T,\n proxy: T,\n changed: readonly Event[]\n ) => EffectCleanup | Promise<void> | null | void;\n\n type Event = number | string | symbol;\n\n type Signal = Event | true | false | null;\n\n type Callback = () => void | null;\n}\n\ninterface Observer {\n /** Becomes `true` once the subject has emitted its initial event. */\n ready?: true;\n listeners: Map<Observer.Notify, Set<Observer.Signal> | undefined>;\n /** Recursion guard for an in-progress emit pass. */\n pending: Set<Observer.Signal>;\n /** Accumulator drained by the next microtask. */\n events: Set<Observer.Event>;\n}\n\ninterface Observing {\n callback: Observer.Callback;\n watching: Set<Observer.Signal>;\n required?: boolean;\n release: (() => void)[];\n}\n\nconst Observer: unique symbol = Symbol('Observer');\nconst Observing: unique symbol = Symbol('Observing');\n\n/** Event every observer treats as watched, to force a refresh. */\nconst REFRESH = Symbol.for('@expressive/mvc.refresh');\n\ninterface Observable { [Observer]?: Observer | null }\ninterface Observed { [Observing]?: Observing }\n\n/**\n * Fetch the dispatch bundle attached to an object.\n * - returns the bundle if active\n * - returns `null` if the subject has been terminated\n * - returns `undefined` if the subject was never observable\n *\n * With `create: true`, attaches a fresh bundle iff the slot is undefined.\n * A terminated (null) slot is not resurrected.\n */\nfunction observer(state: object, create: true): Observer;\nfunction observer(state: object, create?: boolean): Observer | undefined | null;\nfunction observer(state: object, create?: boolean) {\n const o = (state as Observable)[Observer];\n\n if (!o && create) {\n if (o === null)\n throw new Error(\n `${state} was destroyed - cannot be rendered, watched or updated.`\n );\n\n const value: Observer = {\n listeners: new Map(),\n pending: new Set(),\n events: new Set()\n };\n Object.defineProperty(state, Observer, { value, writable: true });\n return value;\n }\n\n return o;\n}\n\nfunction observe<T extends object>(\n object: T,\n callback: Observer.Callback,\n required?: boolean,\n cleanup: (() => void)[] = []\n): T {\n const watching = new Set<Observer.Signal>();\n const observing: Observing = { callback, watching, required, release: cleanup };\n\n const release = listener(object, (key) => {\n if (watching.has(key) || key === REFRESH) return observing.callback();\n });\n\n cleanup.push(release);\n\n if (EffectContext)\n EffectContext.add((update) => {\n if (update !== true) release();\n });\n\n const proxy = Object.create(object) as T;\n\n if ('is' in object)\n Object.defineProperty(proxy, 'is', {\n value: (object as { is?: unknown }).is,\n writable: true\n });\n\n return Object.defineProperty(proxy, Observing, { value: observing });\n}\n\nfunction touch(from: object, key: any): void;\nfunction touch<T>(from: object, key: any, value?: T): T;\nfunction touch(from: object, key: any, value?: any) {\n const active = (from as Observed)[Observing];\n\n if (active) {\n if (value === undefined && arguments.length === 3 && active.required)\n throw new Error(\n `${Object.getPrototypeOf(from)}.${String(key)} is required in this context.`\n );\n\n active.watching.add(key);\n\n if (value instanceof Object && observer(value))\n return observe(\n value,\n () => active.callback(),\n active.required,\n active.release\n );\n }\n\n return value;\n}\n\nfunction unobserve(observing: Observing) {\n for (const release of observing.release) release();\n}\n\nfunction listener<T extends object>(\n subject: T,\n callback: Observer.Notify,\n select?: Observer.Signal | Set<Observer.Signal>\n) {\n const o = observer(subject, true);\n\n if (select !== undefined && !(select instanceof Set))\n select = new Set([select]);\n\n if (o.ready && !select) callback(true);\n\n o.listeners.set(callback, select);\n\n return () => o.listeners.delete(callback);\n}\n\nconst EMPTY = Object.assign([], {\n then: Promise.prototype.then.bind(Promise.resolve([]))\n} as PromiseLike<never[]>);\n\nfunction queued<K extends Observer.Event>(\n state: object\n): K[] & PromiseLike<K[]> {\n const o = observer(state);\n if (!o?.events.size) return EMPTY;\n\n const current = o.events as Set<K>;\n const promise = new Promise<K[]>((res) => {\n const callback: Observer.Notify = () => {\n o.listeners.delete(callback);\n return () => res([...current]);\n };\n o.listeners.set(callback, undefined);\n });\n\n return Object.assign([...current], {\n then: promise.then.bind(promise)\n });\n}\n\nfunction event(state: object, key?: Observer.Event | null, silent?: boolean) {\n const o = observer(state, key === undefined);\n\n if (!o) return;\n\n if (key === null) {\n if (!Object.getOwnPropertyDescriptor(state, Observer)) return;\n\n (state as Observable)[Observer] = null;\n return emit(o, key, state);\n }\n\n if (key === undefined) return emit(o, true, state);\n\n if (!o.events.size && !silent)\n enqueue(() => {\n emit(o, false, state);\n o.events = new Set();\n }, undefined, state);\n\n o.events.add(key);\n\n if (!silent) emit(o, key, state);\n}\n\nfunction emit(o: Observer, key: Observer.Signal, subject: object): void {\n const { listeners, pending, ready } = o;\n\n if (key === ready) return;\n\n if (pending.size) {\n pending.add(key);\n return;\n }\n\n if (!ready && key !== null) pending.add(true);\n\n pending.add(key);\n\n for (const k of pending)\n for (const [callback, filter] of listeners)\n if (!filter || filter.has(k)) {\n const after = callback(k);\n if (after === null) listeners.delete(callback);\n else if (typeof after == 'function') enqueue(after, undefined, subject);\n }\n\n if (key === null) listeners.clear();\n else if (key === true) o.ready = true;\n\n pending.clear();\n}\n\n/**\n * Create a side-effect which will update whenever values accessed change.\n * Callback is called immediately and whenever values are stale.\n *\n * @param target - Instance of State to observe.\n * @param callback - Function to invoke when values change.\n * @param requireValues - If `true` will throw if accessing a value which is `undefined`.\n */\nfunction watch<T extends object>(\n target: T,\n callback: Observer.Effect<Required<T>>,\n requireValues: true,\n transition?: (work: () => void) => void\n): () => void;\n\nfunction watch<T extends object>(\n target: T,\n callback: Observer.Effect<T>,\n recursive?: boolean,\n transition?: (work: () => void) => void\n): () => void;\n\nfunction watch<T extends object>(\n target: T,\n callback: Observer.Effect<T>,\n argument?: boolean,\n transition?: (work: () => void) => void\n) {\n const o = observer(target, true);\n let events: readonly Observer.Event[] = [];\n let unset: ((update: boolean | null) => void) | undefined;\n let reset: (() => void) | null | undefined;\n let previous: Observing | undefined;\n let latest: Observing | undefined;\n let queued = false;\n let suspense: ReturnType<typeof hold>;\n\n function resume() {\n suspense?.();\n suspense = undefined;\n }\n\n function invoke() {\n if (reset === null || observer(target) === null) return;\n\n queued = false;\n let ignore: boolean = true;\n\n function onUpdate() {\n events = [...o.events];\n\n if (reset === null) return null;\n if (ignore) {\n if (queued) enqueue(invoke, transition, target);\n return;\n }\n\n ignore = true;\n queued = true;\n\n unset!(true);\n unset = undefined;\n\n enqueue(invoke, transition, target);\n }\n\n function run(release?: (update?: boolean | null) => void) {\n if (previous !== latest) unobserve(latest!);\n\n const proxy = observe(target, onUpdate, argument === true);\n const current = latest = (proxy as Observed)[Observing]!;\n const output = callback.call(proxy, proxy, events);\n\n if (observer(target) === null) {\n if (typeof output == 'function') output(null);\n if (release) release(null);\n return;\n }\n\n if (previous && argument === false && !current.watching.size)\n previous.callback = onUpdate;\n else {\n if (previous) unobserve(previous);\n previous = current;\n }\n\n ignore = false;\n reset = output === null ? null : invoke;\n unset = (key) => {\n if (typeof output == 'function') output(key);\n if (release) release(key);\n };\n\n events = [];\n }\n\n try {\n if (argument !== false) capture(run);\n else run();\n } catch (err) {\n if (err instanceof Promise) {\n reset = undefined;\n const suspended = suspense = hold(true, transition)!;\n const replay = () => {\n if (suspense !== suspended) return;\n suspense = undefined;\n suspended(invoke);\n };\n err.then(replay, replay);\n } else {\n throw err;\n }\n }\n }\n\n const unlisten = listener(target, (key) => {\n if (key === true) invoke();\n else if (key === null) {\n if (unset) unset(null);\n resume();\n reset = null;\n } else if (!reset) {\n suspense?.(false);\n return reset;\n }\n });\n\n function cleanup() {\n if (unset) unset(false);\n\n resume();\n reset = null;\n unlisten();\n }\n\n if (EffectContext && argument !== false) EffectContext.add(cleanup);\n\n return cleanup;\n}\n\nlet EffectContext: Set<(update?: boolean | null) => void> | undefined;\n\nfunction capture(scope: (release: (update?: boolean | null) => void) => void) {\n const parent = EffectContext;\n const context = (EffectContext = new Set());\n\n try {\n scope((update) => context.forEach((fn) => fn(update)));\n } finally {\n EffectContext = parent;\n }\n}\n\nexport {\n listener,\n event,\n Observer,\n touch,\n queued,\n observer,\n watch,\n capture\n};\n"],"mappings":";;;AA8CA,MAAM,WAA0B,OAAO,WAAW;AAClD,MAAM,YAA2B,OAAO,YAAY;;AAGpD,MAAM,UAAU,OAAO,IAAI,0BAA0B;AAgBrD,SAAS,SAAS,OAAe,QAAkB;CACjD,MAAM,IAAK,MAAqB;AAEhC,KAAI,CAAC,KAAK,QAAQ;AAChB,MAA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OAA6D;CAC5E,MAAM,SAAS;CACf,MAAM,UAAW,gCAAgB,IAAI,KAAK;AAE1C,KAAI;AACF,SAAO,WAAW,QAAQ,SAAS,OAAO,GAAG,OAAO,CAAC,CAAC;WAC9C;AACR,kBAAgB"}
|
package/dist/state.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Context } from "./context.js";
|
|
2
2
|
import { event } from "./observable.js";
|
|
3
|
-
import { Caught } from "./caught.js";
|
|
4
3
|
|
|
5
4
|
//#region src/state.d.ts
|
|
6
5
|
/** Internal state assigned to states. */
|
|
@@ -8,6 +7,17 @@ declare const STORE: WeakMap<State, Record<string | number | symbol, unknown>>;
|
|
|
8
7
|
/** States under construction - added on `new`, removed when activated or released.
|
|
9
8
|
* Also carries work to run at the deadline, which `def` uses to bound its registry. */
|
|
10
9
|
declare const PENDING: Set<State | (() => void)>;
|
|
10
|
+
/** External lifecycle listeners for any given State class. */
|
|
11
|
+
type Handler = State.On<any>;
|
|
12
|
+
declare const SETUP: WeakMap<State.Extends<State>, Set<Handler>>;
|
|
13
|
+
/** Reference bound instance methods, or their getter, to real one. */
|
|
14
|
+
declare const UNBIND: WeakMap<any, any>;
|
|
15
|
+
/** List of methods defined by a given type. */
|
|
16
|
+
declare const METHODS: WeakMap<Function, Map<string, (value: any) => void>>;
|
|
17
|
+
/** List of reactive getters defined by a given type. */
|
|
18
|
+
declare const GETTERS: WeakMap<Function, Map<string, () => unknown>>;
|
|
19
|
+
/** Replacement for a getter captured by live computed properties, for hot patching. */
|
|
20
|
+
declare const LATEST: WeakMap<Function, Function>;
|
|
11
21
|
declare namespace State {
|
|
12
22
|
/** Any type of State, using own class constructor as its identifier. */
|
|
13
23
|
type Extends<T extends State = State> = (abstract new (...args: any[]) => T) & typeof State;
|
|
@@ -30,10 +40,7 @@ declare namespace State {
|
|
|
30
40
|
* will observe). Returned function will run when state is destroyed.
|
|
31
41
|
*/
|
|
32
42
|
type Init<T extends State = State> = (this: T, thisArg: T) => Promise<void> | (() => void) | Args<T> | Assign<T> | void;
|
|
33
|
-
/**
|
|
34
|
-
* Lifecycle handlers for `State.on`, keyed by when they run. A bare `Init`
|
|
35
|
-
* function passed to `on` is sugar for `{ before }`.
|
|
36
|
-
*/
|
|
43
|
+
/** Lifecycle handlers for `State.on`, keyed by when they run. */
|
|
37
44
|
interface On<T extends State = State> {
|
|
38
45
|
/**
|
|
39
46
|
* Per-class setup, run once when the class is first bootstrapped - before
|
|
@@ -43,29 +50,39 @@ declare namespace State {
|
|
|
43
50
|
*/
|
|
44
51
|
type?(type: State.Extends<T>): void;
|
|
45
52
|
/**
|
|
46
|
-
* Per-instance
|
|
47
|
-
*
|
|
48
|
-
*
|
|
53
|
+
* Per-instance, once constructed - before own values are observed and
|
|
54
|
+
* before constructor args and `new()`. May return a cleanup, constructor
|
|
55
|
+
* args, or an assign overlay.
|
|
56
|
+
*/
|
|
57
|
+
setup?(this: T, self: T): void | (() => void) | Promise<void> | Args<T> | Assign<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Per-instance, once ready - after own values are observed, constructor
|
|
60
|
+
* args applied and `new()` has run. May return a cleanup function.
|
|
49
61
|
*/
|
|
50
|
-
|
|
62
|
+
ready?(this: T, self: T): void | (() => void);
|
|
51
63
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
64
|
+
* Runs each time a method is bound to an instance - first access,
|
|
65
|
+
* reassignment, and the rebind after a hot patch - with the key, the bound
|
|
66
|
+
* function and the instance. Tooling and development use.
|
|
54
67
|
*/
|
|
55
|
-
|
|
68
|
+
method?(this: T, key: string, fn: Function, self: T): void;
|
|
56
69
|
/**
|
|
57
|
-
* Receives
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
70
|
+
* Receives what an effect, refreshing getter or async initializer of this State
|
|
71
|
+
* or a subclass threw, a destroyed write, or an instance never activated - with
|
|
72
|
+
* its `kind` (`effect`, `getter`, `setup`, `dead`, `unused`) and `key` where
|
|
73
|
+
* one applies. Most-derived class first, last registered first, each once. Return the error
|
|
74
|
+
* (or another) to pass it on; return nothing to handle it; throw to let it escape.
|
|
75
|
+
* Passed off the end, a destroyed write outputs nothing, `unused` warns,
|
|
76
|
+
* anything else escapes uncaught.
|
|
62
77
|
*/
|
|
63
|
-
catch?(this: T, error:
|
|
78
|
+
catch?(this: T, error: unknown, kind: 'effect' | 'getter' | 'setup' | 'dead' | 'unused', key?: string): unknown;
|
|
64
79
|
}
|
|
65
80
|
/** Object overlay to override values and methods on a state. */
|
|
66
|
-
type Assign<T> = Record<string, unknown> & { [K in
|
|
81
|
+
type Assign<T> = Record<string, unknown> & { [K in Property<T>]?: T[K] extends ((...args: infer A) => infer R) ? (this: T, ...args: A) => R : T[K] };
|
|
67
82
|
/** Subset of `keyof T` not defined by base State. **/
|
|
68
|
-
type
|
|
83
|
+
type Property<T> = Exclude<keyof T, keyof State>;
|
|
84
|
+
/** Managed subset of Property<T> - excludes unmanaged `_` keys. **/
|
|
85
|
+
type Field<T> = Exclude<Property<T>, `_${string}`>;
|
|
69
86
|
/** Any valid key for state, including but not limited to Field<T>. */
|
|
70
87
|
type Event<T = State> = Field<T> | number | symbol | (string & {});
|
|
71
88
|
/** Any event signal dispatched to listeners, including lifecycle meta events. */
|
|
@@ -206,6 +223,18 @@ declare abstract class State {
|
|
|
206
223
|
* @returns Function to cancel listener.
|
|
207
224
|
*/
|
|
208
225
|
get<T extends State>(type: State.Type<T>, callback: Context.Expect<T>, downstream?: boolean): () => void;
|
|
226
|
+
/** Owner of this State. Throws if it has none. */
|
|
227
|
+
get<T extends typeof State>(type: T & (typeof State extends T ? unknown : never), required?: true): State;
|
|
228
|
+
/** Owner of this State. Undefined if it has none. */
|
|
229
|
+
get<T extends typeof State>(type: T & (typeof State extends T ? unknown : never), required: boolean): State | undefined;
|
|
230
|
+
/**
|
|
231
|
+
* Run a callback for each State this one owns - those owned now, then each
|
|
232
|
+
* one as it activates. Callback may return a function, called when that State
|
|
233
|
+
* is destroyed.
|
|
234
|
+
*
|
|
235
|
+
* @returns Function to stop watching.
|
|
236
|
+
*/
|
|
237
|
+
get<T extends typeof State>(type: T & (typeof State extends T ? unknown : never), callback: (child: State) => void | (() => void), downstream: true): () => void;
|
|
209
238
|
/**
|
|
210
239
|
* Get update in progress.
|
|
211
240
|
*
|
|
@@ -217,7 +246,7 @@ declare abstract class State {
|
|
|
217
246
|
* Properties which are not managed by this state will be ignored.
|
|
218
247
|
*
|
|
219
248
|
* @param assign - Object with properties to update.
|
|
220
|
-
* @param silent - If true, listeners will not be notified. If state is destroyed,
|
|
249
|
+
* @param silent - If true, listeners will not be notified. If state is destroyed, stores the update without reporting it.
|
|
221
250
|
* @returns Array of keys updated, syncronously contains keys updated immediately and may be resolved (to itself) when all updates are settled.
|
|
222
251
|
*/
|
|
223
252
|
set(assign?: State.Assign<this>, silent?: boolean): State.Updated<this>;
|
|
@@ -251,7 +280,7 @@ declare abstract class State {
|
|
|
251
280
|
*/
|
|
252
281
|
set(key: State.Event<this>): State.Updated<this>;
|
|
253
282
|
/**
|
|
254
|
-
* Declare an end to updates. This event is final
|
|
283
|
+
* Declare an end to updates. This event is final - later writes are stored but never dispatched.
|
|
255
284
|
* This event can be watched for as well, to run cleanup logic and internally will remove all listeners.
|
|
256
285
|
*
|
|
257
286
|
* @param status - `null` to end updates.
|
|
@@ -300,16 +329,18 @@ declare abstract class State {
|
|
|
300
329
|
/**
|
|
301
330
|
* Register a lifecycle handler for this State and its subclasses.
|
|
302
331
|
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* `after` (per-instance, at the `new()` slot).
|
|
332
|
+
* Hooks by cadence - `type` (per-class, at bootstrap), `setup` (per-instance,
|
|
333
|
+
* before values are observed), `ready` (per-instance, after `new()`), and
|
|
334
|
+
* `method` (per method binding). A function alone is a `setup` handler. A
|
|
335
|
+
* function returned from `setup` or `ready` runs when the instance is destroyed.
|
|
308
336
|
*
|
|
309
337
|
* @returns Function to remove the handler.
|
|
310
338
|
*/
|
|
311
|
-
static on<T extends State>(this: State.Extends<T>,
|
|
339
|
+
static on<T extends State>(this: State.Extends<T>, setup: NonNullable<State.On<T>['setup']>): () => boolean;
|
|
340
|
+
static on<T extends State>(this: State.Extends<T>, handler: State.On<T>): () => boolean;
|
|
312
341
|
}
|
|
342
|
+
/** Bind own configurable methods of a type and collect its reactive getters. */
|
|
343
|
+
declare function classify(type: State.Extends, keys: Map<string, (value: any) => void>, getters: Map<string, () => unknown>): void;
|
|
313
344
|
/**
|
|
314
345
|
* Install a reactive computed property on a state instance, derived from a prototype
|
|
315
346
|
* getter or an arity-bearing `set` factory.
|
|
@@ -330,22 +361,37 @@ declare function compute(this: State, getter: (self: any) => unknown, key: strin
|
|
|
330
361
|
* @param silent - If an update does occur, listeners will not be refreshed automatically.
|
|
331
362
|
*/
|
|
332
363
|
declare function apply(state: State, key: string, config: State.Apply, silent?: boolean): void;
|
|
364
|
+
/**
|
|
365
|
+
* Pending States constructed after this one - in a parent, the products of
|
|
366
|
+
* its own initializers, until the last one it kept.
|
|
367
|
+
*/
|
|
368
|
+
declare function trailing(state: State): State[];
|
|
333
369
|
/**
|
|
334
370
|
* Report States adopted by a parent - those already held, then each one
|
|
335
371
|
* claimed later. Returns a callback to stop watching.
|
|
336
372
|
*/
|
|
337
373
|
declare function children(state: State, callback: (child: State) => void): () => boolean;
|
|
374
|
+
/**
|
|
375
|
+
* Report States owned by this one - those active now, then each one as it
|
|
376
|
+
* activates. A function returned by `callback` runs when that State is destroyed.
|
|
377
|
+
*/
|
|
378
|
+
declare function owned(state: State, callback: (child: State) => void | (() => void)): () => undefined;
|
|
338
379
|
declare function access(state: State, property: string, required?: boolean): any;
|
|
339
380
|
/**
|
|
340
381
|
* Update a property on a state instance and notify listeners.
|
|
341
382
|
*
|
|
342
383
|
* This is used internally to update properties, but can also be used to update properties which are not managed by state, or to update values without triggering setters.
|
|
343
384
|
*
|
|
344
|
-
* A destroyed state
|
|
385
|
+
* A destroyed state stores the write without dispatch, reports it unless `silent`, and returns `false`.
|
|
345
386
|
*/
|
|
346
387
|
declare function update<T>(state: State, key: State.Event<T>, value: T, silent?: boolean, own?: boolean): boolean;
|
|
347
|
-
/**
|
|
348
|
-
|
|
388
|
+
/**
|
|
389
|
+
* Hand a value stored by an owning writer to that property's adopter,
|
|
390
|
+
* creating one the first time a State is stored there. A property which has
|
|
391
|
+
* held a State keeps its adopter, so a later non-State value releases the
|
|
392
|
+
* previous child.
|
|
393
|
+
*/
|
|
394
|
+
declare function adopt(state: State, key: unknown, value: unknown): void;
|
|
349
395
|
/** Random alphanumberic of length 6; always starts with a letter. */
|
|
350
396
|
declare function uid(): string;
|
|
351
397
|
declare function unbind<T extends Function>(fn: T): T;
|
|
@@ -359,5 +405,5 @@ declare function unbind<T extends Function>(fn?: T): T | undefined;
|
|
|
359
405
|
declare function parent(child: object): State | null | undefined;
|
|
360
406
|
declare function parent(child: object, value: State | null): boolean;
|
|
361
407
|
//#endregion
|
|
362
|
-
export { PENDING, STORE, State, access, apply, children, compute, event,
|
|
408
|
+
export { GETTERS, type Handler, LATEST, METHODS, PENDING, SETUP, STORE, State, UNBIND, access, adopt, apply, children, classify, compute, event, owned, parent, trailing, uid, unbind, update };
|
|
363
409
|
//# sourceMappingURL=state.d.ts.map
|