@expressive/mvc 0.73.0 → 0.78.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 +137 -0
- package/dist/component.d.ts +87 -0
- package/dist/component.js +111 -0
- package/dist/component.js.map +1 -0
- package/dist/context.d.ts +56 -0
- package/dist/context.js +201 -0
- package/dist/context.js.map +1 -0
- package/dist/field/def.d.ts +17 -0
- package/dist/field/def.js +29 -0
- package/dist/field/def.js.map +1 -0
- package/dist/field/get.d.ts +57 -0
- package/dist/field/get.js +90 -0
- package/dist/field/get.js.map +1 -0
- package/dist/field/hot.d.ts +16 -0
- package/dist/field/hot.js +98 -0
- package/dist/field/hot.js.map +1 -0
- package/dist/field/ref.d.ts +78 -0
- package/dist/field/ref.js +85 -0
- package/dist/field/ref.js.map +1 -0
- package/dist/field/set.d.ts +59 -0
- package/dist/field/set.js +84 -0
- package/dist/field/set.js.map +1 -0
- package/dist/index.d.ts +10 -502
- package/dist/index.js +11 -872
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +90 -0
- package/dist/jsx-runtime.js +57 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/observable.d.ts +58 -0
- package/dist/observable.js +184 -0
- package/dist/observable.js.map +1 -0
- package/dist/state.d.ts +311 -0
- package/dist/state.js +424 -0
- package/dist/state.js.map +1 -0
- package/package.json +35 -18
- package/dist/esm/context.d.ts +0 -62
- package/dist/esm/context.js +0 -158
- package/dist/esm/index.d.ts +0 -8
- package/dist/esm/index.js +0 -9
- package/dist/esm/instruction/get.d.ts +0 -36
- package/dist/esm/instruction/get.js +0 -77
- package/dist/esm/instruction/ref.d.ts +0 -71
- package/dist/esm/instruction/ref.js +0 -68
- package/dist/esm/instruction/set.d.ts +0 -69
- package/dist/esm/instruction/set.js +0 -118
- package/dist/esm/instruction/use.d.ts +0 -23
- package/dist/esm/instruction/use.js +0 -56
- package/dist/esm/observable.d.ts +0 -42
- package/dist/esm/observable.js +0 -121
- package/dist/esm/state.d.ts +0 -234
- package/dist/esm/state.js +0 -293
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set.js","names":[],"sources":["../../src/field/set.ts"],"sourcesContent":["import { listener, capture } from '../observable';\nimport { access, event, State, update } from '../state';\nimport { def } from './def';\n\ndeclare namespace set {\n type Callback<T, S = any> = (\n this: S,\n next: T,\n previous: T\n ) => ((next: T) => void) | Promise<any> | void | boolean;\n\n type Factory<T, S = any> = (this: S, property: string) => Promise<T> | T;\n}\n\n/**\n * Set a property as a placeholder, initially `undefined` but required.\n *\n * Property cannot be accessed until it is defined. If accessed while undefined, a hybrid\n * `Promise`/`Error` (aka: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.\n *\n * Non-enumerable; writable only if `onUpdate` callback is provided.\n *\n * @param value - Starting value (undefined for suspense placeholder).\n * @param onUpdate - Callback run when property is set.\n */\nfunction set<T>(value?: undefined, onUpdate?: set.Callback<T>): T;\n\n/**\n * Set property with an async factory function.\n * If required is not defined, factory runs lazily on first access.\n * If async, or returns a promise, suspense is thrown upon access until resolved.\n *\n * Non-enumerable and read-only.\n *\n * @param factory - Zero-arg async callback to produce property value.\n * @param required - Pass `true` to run factory immediately and require value.\n */\nfunction set<T>(factory: () => T | Promise<T>, required?: true): T;\n\n/**\n * Set property with a lazy factory, no suspense.\n *\n * Non-enumerable and read-only.\n *\n * @param factory - Zero-arg callback to produce property value.\n * @param required - Pass `false` to allow undefined while pending.\n */\nfunction set<T>(\n factory: () => T | Promise<T>,\n required: boolean\n): T | undefined;\n\n/**\n * Set property with a factory and update callback.\n *\n * Non-enumerable but writable via the callback.\n *\n * @param factory - Callback to produce initial value.\n * @param onUpdate - Callback run when property is updated.\n */\nfunction set<T>(factory: () => T | Promise<T>, onUpdate: set.Callback<T>): T;\n\n/**\n * Set a property with a non-function, non-Promise value.\n *\n * Non-enumerable but writable. Unlike plain assignment (`= value`), this\n * property is excluded from snapshots and `ref(this)`.\n *\n * @param value - Starting value for property.\n * @param onUpdate - Optional callback run when property is set.\n */\n// TODO: if onUpdate is not defined, should this have behavior unique to simple assignment?\n// I'm thinking default behavior should be to assign value directly to property, without triggering any additional updates.\n// All this requires is a default callback which throws true, silent updates are already implemented.\nfunction set<T>(\n value: T extends Promise<any> ? never : T,\n onUpdate?: set.Callback<T>\n): T;\n\nfunction set<T = any>(value?: unknown, argument?: unknown): any {\n return def<T>((key, subject) => {\n if (value instanceof Promise)\n throw new TypeError(\n `Direct promises are not supported in set(${subject}.${key}). Use set(() => promise) instead.`\n );\n\n const config: State.Apply = {\n enumerable: false,\n set: false\n };\n\n let computed = false;\n\n // One-shot factory\n if (typeof value == 'function') {\n const factory = value.bind(subject, key)\n computed = true;\n\n function init() {\n let output: unknown;\n\n try {\n output = attempt(factory);\n } catch (err) {\n console.warn(\n `Generating initial value for ${subject}.${key} failed.`\n );\n throw err;\n }\n\n config.get = argument !== false;\n\n function assign(next: any, silent?: boolean) {\n if (typeof argument == 'function') subject[key] = next;\n else update(subject, key, next, silent);\n }\n\n if (output instanceof Promise)\n output.then(assign, (error) => {\n event(subject, key);\n config.get = () => {\n throw error;\n };\n });\n else assign(output, true);\n\n if (argument) return null;\n\n if (output instanceof Promise && argument !== false)\n return access(subject, key, true);\n\n return subject[key];\n }\n\n if (argument) {\n listener(subject, () => {\n init();\n return null;\n });\n } else {\n config.get = init;\n }\n } else if (value !== undefined) {\n config.value = value;\n }\n\n if (typeof argument == 'function') {\n let unset: ((next: T) => void) | undefined;\n\n config.set = function (this: any, value: any, previous: any) {\n capture((release) => {\n const returns = argument.call(this, value, previous);\n\n if (unset) unset(value);\n\n unset = (next: T) => {\n if (typeof returns == 'function') returns(next);\n release();\n };\n });\n };\n } else if (!computed)\n config.set = () => {\n config.get = undefined;\n config.set = undefined;\n };\n\n return config;\n });\n}\n\nfunction attempt(fn: () => any): any {\n function retry(err: unknown) {\n if (err instanceof Promise) return err.then(compute);\n else throw err;\n }\n\n function compute(): any {\n try {\n const output = fn();\n\n return output instanceof Promise ? output.catch(retry) : output;\n } catch (err) {\n return retry(err);\n }\n }\n\n return compute();\n}\n\nexport { set };\n"],"mappings":";;;;;AA+EA,SAAS,IAAa,OAAiB,UAAyB;AAC9D,QAAO,KAAQ,KAAK,YAAY;AAC9B,MAAI,iBAAiB,QACnB,OAAM,IAAI,UACR,4CAA4C,QAAQ,GAAG,IAAI,oCAC5D;EAEH,MAAM,SAAsB;GAC1B,YAAY;GACZ,KAAK;GACN;EAED,IAAI,WAAW;AAGf,MAAI,OAAO,SAAS,YAAY;GAC9B,MAAM,UAAU,MAAM,KAAK,SAAS,IAAI;AACxC,cAAW;GAEX,SAAS,OAAO;IACd,IAAI;AAEJ,QAAI;AACF,cAAS,QAAQ,QAAQ;aAClB,KAAK;AACZ,aAAQ,KACN,gCAAgC,QAAQ,GAAG,IAAI,UAChD;AACD,WAAM;;AAGR,WAAO,MAAM,aAAa;IAE1B,SAAS,OAAO,MAAW,QAAkB;AAC3C,SAAI,OAAO,YAAY,WAAY,SAAQ,OAAO;SAC7C,QAAO,SAAS,KAAK,MAAM,OAAO;;AAGzC,QAAI,kBAAkB,QACpB,QAAO,KAAK,SAAS,UAAU;AAC7B,WAAM,SAAS,IAAI;AACnB,YAAO,YAAY;AACjB,YAAM;;MAER;QACC,QAAO,QAAQ,KAAK;AAEzB,QAAI,SAAU,QAAO;AAErB,QAAI,kBAAkB,WAAW,aAAa,MAC5C,QAAO,OAAO,SAAS,KAAK,KAAK;AAEnC,WAAO,QAAQ;;AAGjB,OAAI,SACF,UAAS,eAAe;AACtB,UAAM;AACN,WAAO;KACP;OAEF,QAAO,MAAM;aAEN,UAAU,OACnB,QAAO,QAAQ;AAGjB,MAAI,OAAO,YAAY,YAAY;GACjC,IAAI;AAEJ,UAAO,MAAM,SAAqB,OAAY,UAAe;AAC3D,aAAS,YAAY;KACnB,MAAM,UAAU,SAAS,KAAK,MAAM,OAAO,SAAS;AAEpD,SAAI,MAAO,OAAM,MAAM;AAEvB,cAAS,SAAY;AACnB,UAAI,OAAO,WAAW,WAAY,SAAQ,KAAK;AAC/C,eAAS;;MAEX;;aAEK,CAAC,SACV,QAAO,YAAY;AACjB,UAAO,MAAM;AACb,UAAO,MAAM;;AAGjB,SAAO;GACP;;AAGJ,SAAS,QAAQ,IAAoB;CACnC,SAAS,MAAM,KAAc;AAC3B,MAAI,eAAe,QAAS,QAAO,IAAI,KAAK,QAAQ;MAC/C,OAAM;;CAGb,SAAS,UAAe;AACtB,MAAI;GACF,MAAM,SAAS,IAAI;AAEnB,UAAO,kBAAkB,UAAU,OAAO,MAAM,MAAM,GAAG;WAClD,KAAK;AACZ,UAAO,MAAM,IAAI;;;AAIrB,QAAO,SAAS"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,502 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
type Notify<T extends Observable = any> = (this: T, key: Event, source: T) => (() => void) | null | void;
|
|
13
|
-
type Effect<T extends Observable> = (proxy: T) => ((update: boolean | null) => void) | Promise<void> | null | void;
|
|
14
|
-
type Event = number | string | null | boolean | symbol;
|
|
15
|
-
type PromiseLite<T = void> = {
|
|
16
|
-
then: (callback: () => T) => T;
|
|
17
|
-
};
|
|
18
|
-
declare namespace Observable {
|
|
19
|
-
type Callback = () => void | PromiseLite;
|
|
20
|
-
}
|
|
21
|
-
interface Observable {
|
|
22
|
-
[Observable](callback: Observable.Callback, required?: boolean): this | void;
|
|
23
|
-
}
|
|
24
|
-
declare const Observable: unique symbol;
|
|
25
|
-
/** Update register. */
|
|
26
|
-
declare function addListener<T extends Observable>(subject: T, callback: Notify<T>, select?: Event | Set<Event>): () => boolean;
|
|
27
|
-
declare function event(subject: Observable, key?: string | number | symbol | null, silent?: boolean): void;
|
|
28
|
-
/**
|
|
29
|
-
* Create a side-effect which will update whenever values accessed change.
|
|
30
|
-
* Callback is called immediately and whenever values are stale.
|
|
31
|
-
*
|
|
32
|
-
* @param target - Instance of State to observe.
|
|
33
|
-
* @param callback - Function to invoke when values change.
|
|
34
|
-
* @param requireValues - If `true` will throw if accessing a value which is `undefined`.
|
|
35
|
-
*/
|
|
36
|
-
declare function watch<T extends Observable>(target: T, callback: Effect<Required<T>>, requireValues: true): () => void;
|
|
37
|
-
declare function watch<T extends Observable>(target: T, callback: Effect<T>, recursive?: boolean): () => void;
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/state.d.ts
|
|
40
|
-
/** Reference bound instance methods to real ones. */
|
|
41
|
-
declare const METHOD: WeakMap<any, any>;
|
|
42
|
-
declare namespace State {
|
|
43
|
-
/** Any type of State, using own class constructor as its identifier. */
|
|
44
|
-
type Extends<T extends State = State> = (abstract new (...args: any[]) => T) & typeof State;
|
|
45
|
-
/** A State constructor which may be instanciated. */
|
|
46
|
-
type Type<T extends State = State> = (new (...args: State.Args<T>) => T) & Omit<typeof State, never>;
|
|
47
|
-
/** State constructor arguments */
|
|
48
|
-
type Args<T extends State = any> = (Args<T> | Init<T> | Assign<T> | string | void)[];
|
|
49
|
-
/**
|
|
50
|
-
* State constructor callback - is called when State finishes intializing.
|
|
51
|
-
* Returned function will run when state is destroyed.
|
|
52
|
-
*/
|
|
53
|
-
type Init<T extends State = State> = (this: T, thisArg: T) => Promise<void> | (() => void) | Args<T> | Assign<T> | void;
|
|
54
|
-
/** Object overlay to override values and methods on a state. */
|
|
55
|
-
type Assign<T> = Record<string, unknown> & { [K in Field<T>]?: T[K] extends ((...args: infer A) => infer R) ? (this: T, ...args: A) => R : T[K] };
|
|
56
|
-
/** Subset of `keyof T` which are not methods or defined by base State U. **/
|
|
57
|
-
type Field<T> = Exclude<keyof T, keyof State>;
|
|
58
|
-
/** Any valid key for state, including but not limited to Field<T>. */
|
|
59
|
-
type Event<T> = Field<T> | number | symbol | (string & {});
|
|
60
|
-
/** Export/Import compatible value for a given property in a State. */
|
|
61
|
-
type Export<R> = R extends {
|
|
62
|
-
get(): infer T;
|
|
63
|
-
} ? T : R;
|
|
64
|
-
/** Value for a property managed by a state. */
|
|
65
|
-
type Value<T extends State, K extends State.Event<T>> = K extends keyof T ? Export<T[K]> : unknown;
|
|
66
|
-
type Setter<T> = (value: T, previous: T) => boolean | void | (() => T);
|
|
67
|
-
type OnEvent<T extends State> = (this: T, key: unknown, source: T) => void | (() => void) | null;
|
|
68
|
-
type OnUpdate<T extends State, K extends State.Event<T>> = (this: T, key: K, thisArg: K) => void;
|
|
69
|
-
/**
|
|
70
|
-
* Values from current state of given state.
|
|
71
|
-
* Differs from `Values` as values here will drill
|
|
72
|
-
* into "real" values held by exotics like ref.Object.
|
|
73
|
-
*/
|
|
74
|
-
type Values<T> = { [P in Field<T>]: Export<T[P]> };
|
|
75
|
-
/** Object comperable to data found in T. */
|
|
76
|
-
type Partial<T> = { [P in Field<T>]?: Export<T[P]> };
|
|
77
|
-
/** Exotic value, where actual value is contained within. */
|
|
78
|
-
type Ref<T = any> = {
|
|
79
|
-
(next: T): void;
|
|
80
|
-
current: T | null;
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* A callback function which is subscribed to parent and updates when accessed properties change.
|
|
84
|
-
*
|
|
85
|
-
* @param current - Current state of this state. This is a proxy which detects properties which
|
|
86
|
-
* where accessed, and thus depended upon to stay current.
|
|
87
|
-
*
|
|
88
|
-
* @param update - Set of properties which have changed, and events fired, since last update.
|
|
89
|
-
*
|
|
90
|
-
* @returns A callback function which will be called when this effect is stale.
|
|
91
|
-
*/
|
|
92
|
-
type Effect<T> = (this: T, current: T, update: Set<State.Event<T>>) => EffectCallback | Promise<void> | null | void;
|
|
93
|
-
/**
|
|
94
|
-
* A callback function returned by effect. Will be called when effect is stale.
|
|
95
|
-
*
|
|
96
|
-
* @param update - `true` if update is pending, `false` effect has been cancelled, `null` if state is destroyed.
|
|
97
|
-
*/
|
|
98
|
-
type EffectCallback = (update: boolean | null) => void;
|
|
99
|
-
}
|
|
100
|
-
interface State {
|
|
101
|
-
/**
|
|
102
|
-
* Loopback to instance of this state. This is useful when in a subscribed context,
|
|
103
|
-
* to keep write access to `this` after a destructure. You can use it to read variables silently as well.
|
|
104
|
-
**/
|
|
105
|
-
is: this;
|
|
106
|
-
}
|
|
107
|
-
declare abstract class State implements Observable {
|
|
108
|
-
constructor(...args: State.Args);
|
|
109
|
-
[Observable](callback: Observable.Callback, required?: boolean): this;
|
|
110
|
-
/**
|
|
111
|
-
* Optional lifecycle hook called during State initialization.
|
|
112
|
-
* Can return a cleanup function to run when state is destroyed.
|
|
113
|
-
*/
|
|
114
|
-
protected new?(): void | (() => void);
|
|
115
|
-
/**
|
|
116
|
-
* Pull current values from state. Flattens all states and exotic values recursively.
|
|
117
|
-
*
|
|
118
|
-
* @returns Object with all values from this state.
|
|
119
|
-
**/
|
|
120
|
-
get(): State.Values<this>;
|
|
121
|
-
/**
|
|
122
|
-
* Run a function which will run automatically when accessed values change.
|
|
123
|
-
*
|
|
124
|
-
* @param effect Function to run, and again whenever accessed values change.
|
|
125
|
-
* If effect returns a function, it will be called when a change occurs (syncronously),
|
|
126
|
-
* effect is cancelled, or parent state is destroyed.
|
|
127
|
-
* @returns Function to cancel listener.
|
|
128
|
-
*/
|
|
129
|
-
get(effect: State.Effect<this>): () => void;
|
|
130
|
-
/**
|
|
131
|
-
* Get value of a property.
|
|
132
|
-
*
|
|
133
|
-
* @param key - Property to get value of.
|
|
134
|
-
* @param required - If true, will throw an error if property is not available.
|
|
135
|
-
* @returns Value of property.
|
|
136
|
-
*/
|
|
137
|
-
get<T extends State.Event<this>>(key: T, required?: boolean): State.Value<this, T>;
|
|
138
|
-
/**
|
|
139
|
-
* Run a function when a property is updated.
|
|
140
|
-
*
|
|
141
|
-
* @param key - Property to watch for updates.
|
|
142
|
-
* @param callback - Function to call when property is updated.
|
|
143
|
-
* @returns Function to cancel listener.
|
|
144
|
-
*/
|
|
145
|
-
get<T extends State.Event<this>>(key: T, callback: State.OnUpdate<this, T>): () => void;
|
|
146
|
-
/**
|
|
147
|
-
* Check if state is expired.
|
|
148
|
-
*
|
|
149
|
-
* @param status - `null` to check if state is expired.
|
|
150
|
-
* @returns `true` if state is expired, `false` otherwise.
|
|
151
|
-
*/
|
|
152
|
-
get(status: null): boolean;
|
|
153
|
-
/**
|
|
154
|
-
* Callback when state is to be destroyed.
|
|
155
|
-
*
|
|
156
|
-
* @param callback - Function to call when state is destroyed.
|
|
157
|
-
* @returns Function to cancel listener.
|
|
158
|
-
*/
|
|
159
|
-
get(status: null, callback: () => void): () => void;
|
|
160
|
-
/**
|
|
161
|
-
* Get update in progress.
|
|
162
|
-
*
|
|
163
|
-
* @returns Promise which resolves object with updated values, `undefined` if there no update is pending.
|
|
164
|
-
**/
|
|
165
|
-
set(): PromiseLike<State.Event<this>[]> | undefined;
|
|
166
|
-
/**
|
|
167
|
-
* Update mulitple properties at once. Merges argument with current state.
|
|
168
|
-
* Properties which are not managed by this state will be ignored.
|
|
169
|
-
*
|
|
170
|
-
* @param assign - Object with properties to update.
|
|
171
|
-
* @param silent - If an update does occur, listeners will not be refreshed automatically.
|
|
172
|
-
* @returns Promise resolving an array of keys updated, `undefined` (immediately) if a noop.
|
|
173
|
-
*/
|
|
174
|
-
set(assign?: State.Assign<this>, silent?: boolean): PromiseLike<State.Event<this>[]> | undefined;
|
|
175
|
-
/**
|
|
176
|
-
* Push an update. This will not change the value of associated property.
|
|
177
|
-
*
|
|
178
|
-
* Useful where a property value internally has changed, but the object is the same.
|
|
179
|
-
* For example: An array has pushed a new value, or a nested property is updated.
|
|
180
|
-
*
|
|
181
|
-
* You can also use this to dispatch arbitrary events.
|
|
182
|
-
* Symbols are recommended as non-property events, however you can use any string.
|
|
183
|
-
* If doing so, be sure to avoid collisions with property names! An easy way to do this is
|
|
184
|
-
* to prefix an event with "!" and/or use dash-case. e.g. `set("!event")` or `set("my-event")`.
|
|
185
|
-
*
|
|
186
|
-
* @param key - Property or event to dispatch.
|
|
187
|
-
* @returns Promise resolves an array of keys updated.
|
|
188
|
-
*/
|
|
189
|
-
set(key: State.Event<this>): PromiseLike<State.Event<this>[]>;
|
|
190
|
-
/**
|
|
191
|
-
* Set a value for a property. This will update the value and notify listeners.
|
|
192
|
-
*
|
|
193
|
-
* **Use with caution!** Key nor value are checked for validity.
|
|
194
|
-
*
|
|
195
|
-
* This is meant for assigning values programmatically,
|
|
196
|
-
* where simple assignment is not practicable.
|
|
197
|
-
*
|
|
198
|
-
* For example: `(this as any)[myProperty] = value;`
|
|
199
|
-
*
|
|
200
|
-
* @param key - Property to set value for.
|
|
201
|
-
* @param value - Value to set for property.
|
|
202
|
-
* @param init - If true, state will begin to manage the property if it is not already.
|
|
203
|
-
* @returns Promise resolves an array of keys updated.
|
|
204
|
-
*/
|
|
205
|
-
set(key: State.Event<this>, value: unknown, init?: boolean): PromiseLike<State.Event<this>[]>;
|
|
206
|
-
/**
|
|
207
|
-
* Call a function when update occurs.
|
|
208
|
-
*
|
|
209
|
-
* Given function is called for every assignment (which changes value) or explicit `set`.
|
|
210
|
-
*
|
|
211
|
-
* To run logic on final value only, callback may return a function. Using the same
|
|
212
|
-
* function for one or more events will ensure it is called only when events are settled.
|
|
213
|
-
*
|
|
214
|
-
* @param callback - Function to call when update occurs.
|
|
215
|
-
* @returns Function to remove listener. Will return `true` if removed, `false` if inactive already.
|
|
216
|
-
*
|
|
217
|
-
*/
|
|
218
|
-
set(callback: State.OnEvent<this>): () => boolean;
|
|
219
|
-
/**
|
|
220
|
-
* Call a function when a property is updated.
|
|
221
|
-
* Unlike `get`, this calls synchronously and will fire as many times as the property is updated.
|
|
222
|
-
*
|
|
223
|
-
* @param callback - Function to call when property is updated.
|
|
224
|
-
* @param event - Property to watch for updates.
|
|
225
|
-
*/
|
|
226
|
-
set(callback: State.OnEvent<this>, event: State.Event<this> | null): () => boolean;
|
|
227
|
-
/**
|
|
228
|
-
* Declare an end to updates. This event is final and will freeze state.
|
|
229
|
-
* This event can be watched for as well, to run cleanup logic and internally will remove all listeners.
|
|
230
|
-
*
|
|
231
|
-
* @param status - `null` to end updates.
|
|
232
|
-
*/
|
|
233
|
-
set(status: null): void;
|
|
234
|
-
/**
|
|
235
|
-
* Iterate over managed properties in this instance of State.
|
|
236
|
-
* Yeilds the key and current value for each property.
|
|
237
|
-
*/
|
|
238
|
-
[Symbol.iterator](): Iterator<[string, unknown]>;
|
|
239
|
-
/**
|
|
240
|
-
* Create and activate a new instance of this state.
|
|
241
|
-
*
|
|
242
|
-
* **Important** - Unlike `new this(...)` - this method also activates state.
|
|
243
|
-
*
|
|
244
|
-
* @param args - arguments sent to constructor
|
|
245
|
-
*/
|
|
246
|
-
static new<T extends State>(this: State.Type<T>, ...args: State.Args<T>): T;
|
|
247
|
-
/**
|
|
248
|
-
* Static equivalent of `x instanceof this`.
|
|
249
|
-
* Determines if provided class is a subtype of this one.
|
|
250
|
-
* If so, language server will make available all static
|
|
251
|
-
* methods and properties of this class.
|
|
252
|
-
*/
|
|
253
|
-
static is<T extends State.Extends>(this: T, maybe: unknown): maybe is T;
|
|
254
|
-
/**
|
|
255
|
-
* Register a callback to run when any instance of this State is updated.
|
|
256
|
-
*/
|
|
257
|
-
static on<T extends State>(this: State.Extends<T>, listener: State.OnEvent<T>): () => boolean;
|
|
258
|
-
}
|
|
259
|
-
//#endregion
|
|
260
|
-
//#region src/instruction/use.d.ts
|
|
261
|
-
/**
|
|
262
|
-
* Property initializer, will run upon instance creation.
|
|
263
|
-
* Optional returned callback will run when once upon first access.
|
|
264
|
-
*/
|
|
265
|
-
type Instruction<T = any, M extends State = any> = (this: M, key: Extract<State.Field<M>, string>, thisArg: M, state: State.Values<M>) => Instruction.Descriptor<T> | ((source: M) => T) | void;
|
|
266
|
-
declare namespace Instruction {
|
|
267
|
-
type Getter<T> = (source: State) => T;
|
|
268
|
-
type Descriptor<T = any> = {
|
|
269
|
-
get?: Getter<T> | boolean;
|
|
270
|
-
set?: State.Setter<T> | boolean;
|
|
271
|
-
enumerable?: boolean;
|
|
272
|
-
value?: T;
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
declare function use<T>(instruction: Instruction<T>): T extends void ? unknown : T;
|
|
276
|
-
declare function use<T extends State>(Type: State.Type<T>, required: false): T | undefined;
|
|
277
|
-
declare function use<T extends State>(Type: State.Type<T>, ready?: (i: T) => void): T;
|
|
278
|
-
//#endregion
|
|
279
|
-
//#region src/instruction/get.d.ts
|
|
280
|
-
declare namespace get {
|
|
281
|
-
type Callback<T = any> = (state: T, subject: State) => void | boolean | (() => void);
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Fetches upstream State from context with optional lifecycle callback.
|
|
285
|
-
* Callback runs on mount and can return cleanup function for unmount.
|
|
286
|
-
*
|
|
287
|
-
* @param Type - Type of State to fetch.
|
|
288
|
-
* @param callback - Optional lifecycle callback, receives (state, subject). Can return cleanup function.
|
|
289
|
-
*/
|
|
290
|
-
declare function get<T extends State>(Type: State.Extends<T>, callback?: get.Callback<T>): T;
|
|
291
|
-
/**
|
|
292
|
-
* Fetches and assigns the controller which spawned this host.
|
|
293
|
-
* When parent assigns an instance via `use()` directive, it
|
|
294
|
-
* will be made available to child upon this request.
|
|
295
|
-
*
|
|
296
|
-
* @param Type - Type of controller compatible with this class.
|
|
297
|
-
* @param required - If false, property may be undefined. Otherwise will throw suspense.
|
|
298
|
-
*/
|
|
299
|
-
declare function get<T extends State>(Type: State.Extends<T>, required: false): T | undefined;
|
|
300
|
-
/**
|
|
301
|
-
* Collects downstream States, this will accumulate all instances of specified type for which this is an ancestor.
|
|
302
|
-
* Returns a frozen array that updates as States are added or removed.
|
|
303
|
-
* Callback runs on each State registration and can return cleanup or false to prevent registration.
|
|
304
|
-
*
|
|
305
|
-
* @param Type - Type of State to collect.
|
|
306
|
-
* @param downstream - Must be true to enable downstream mode.
|
|
307
|
-
* @param callback - Optional lifecycle callback, receives (state, subject). May return false to prevent registration.
|
|
308
|
-
*/
|
|
309
|
-
declare function get<T extends State>(Type: State.Extends<T>, downstream: true, callback?: get.Callback<T>): readonly T[];
|
|
310
|
-
//#endregion
|
|
311
|
-
//#region src/instruction/set.d.ts
|
|
312
|
-
declare namespace set {
|
|
313
|
-
type Callback<T, S = any> = (this: S, next: T, previous: T) => ((next: T) => void) | Promise<any> | void | boolean;
|
|
314
|
-
type Compute<T, S = any> = (on: S, key: string) => T;
|
|
315
|
-
type Factory<T, S = any> = (this: S, key: string) => Promise<T> | T;
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Set property as `undefined` but required.
|
|
319
|
-
*
|
|
320
|
-
* Property cannot be accessed until it is defined. If accessed while undefined, a hybrid
|
|
321
|
-
* `Promise`/`Error` (aka: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.
|
|
322
|
-
*/
|
|
323
|
-
declare function set<T = any>(): T;
|
|
324
|
-
/**
|
|
325
|
-
* Set property with a factory function.
|
|
326
|
-
*
|
|
327
|
-
* **Note** Factory is lazy! It will only run if/when property is accessed.
|
|
328
|
-
* Value will be undefined until factory resolves, which will also dispatch an update for the property.
|
|
329
|
-
*/
|
|
330
|
-
declare function set<T>(factory: set.Factory<T>, required: false): T | undefined;
|
|
331
|
-
/**
|
|
332
|
-
* Set property with a factory function.
|
|
333
|
-
*
|
|
334
|
-
* If async, property cannot be accessed until resolves, yeilding a result.
|
|
335
|
-
* If accessed while still processing, React Suspense will be thrown.
|
|
336
|
-
*
|
|
337
|
-
* @param factory - Callback run to derrive property value.
|
|
338
|
-
* @param required - If true run factory immediately on creation, otherwise on access.
|
|
339
|
-
*/
|
|
340
|
-
declare function set<T>(factory: set.Factory<T>, required?: boolean): T;
|
|
341
|
-
/**
|
|
342
|
-
* Set property with a factory function.
|
|
343
|
-
*
|
|
344
|
-
* If async, property cannot be accessed until resolves, yeilding a result.
|
|
345
|
-
* If accessed while still processing, React Suspense will be thrown.
|
|
346
|
-
*
|
|
347
|
-
* @param factory - Callback run to derrive property value.
|
|
348
|
-
* @param onUpdate - Callback run when property is finished computing or is set.
|
|
349
|
-
*/
|
|
350
|
-
declare function set<T>(factory: set.Factory<T>, onUpdate?: set.Callback<T>): T;
|
|
351
|
-
/**
|
|
352
|
-
* Set a property with empty placeholder and/or update callback.
|
|
353
|
-
*
|
|
354
|
-
* @param value - Starting value for property. If undefined, suspense will be thrown on access, until value is set and accepted by callback.
|
|
355
|
-
* @param onUpdate - Callback run when property is set. If returns false, update is not accepted and property will keep previous value.
|
|
356
|
-
*/
|
|
357
|
-
declare function set<T>(value: T | undefined, onUpdate?: set.Callback<T>): T;
|
|
358
|
-
/**
|
|
359
|
-
* Implement a reactive computed value using a method reference.
|
|
360
|
-
*
|
|
361
|
-
* The method will automatically re-run when any accessed properties change.
|
|
362
|
-
* Use `true` to indicate reactive mode with implicit `this` as the source.
|
|
363
|
-
*
|
|
364
|
-
* @param reactiveToThis - Pass `true` to enable reactive mode with implicit this
|
|
365
|
-
* @param method - Method reference or compute function to use
|
|
366
|
-
*/
|
|
367
|
-
declare function set<R, T = any>(reactiveToThis: true, method: set.Compute<R, T> | Function): R;
|
|
368
|
-
/**
|
|
369
|
-
* Implement a computed value; output will be generated by provided function.
|
|
370
|
-
*
|
|
371
|
-
* @param reactiveTo - Source state from which computed value will be a subscriber.
|
|
372
|
-
* @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value. Will update automatically as input values change.
|
|
373
|
-
*/
|
|
374
|
-
declare function set<R, T extends State>(reactiveTo: T, compute: set.Compute<R, T>): R;
|
|
375
|
-
//#endregion
|
|
376
|
-
//#region src/instruction/ref.d.ts
|
|
377
|
-
declare namespace ref {
|
|
378
|
-
type Callback<T> = (argument: T) => ((next: T | null) => void) | Promise<void> | void | boolean;
|
|
379
|
-
interface Object<T = any> {
|
|
380
|
-
/** Current value held by this reference. */
|
|
381
|
-
current: T;
|
|
382
|
-
/** State instance this reference belongs to. */
|
|
383
|
-
is: State;
|
|
384
|
-
/** Key of property on state this reference belongs to. */
|
|
385
|
-
key: string;
|
|
386
|
-
/**
|
|
387
|
-
* Subscribe to changes of this reference.
|
|
388
|
-
*
|
|
389
|
-
* @param callback - Callback to invoke with the current value when it changes.
|
|
390
|
-
* @returns Unsubscribe function to stop listening to changes.
|
|
391
|
-
*/
|
|
392
|
-
get(callback: (value: T) => void): () => void;
|
|
393
|
-
/** Retrieve current value. */
|
|
394
|
-
get(): T | null;
|
|
395
|
-
}
|
|
396
|
-
/** Object with references to all managed values of `T`. */
|
|
397
|
-
/** Object with references to all managed values of `T`. */
|
|
398
|
-
type Proxy<T extends State> = { [P in State.Field<T>]-?: Object<T[P]> } & {
|
|
399
|
-
get(): T;
|
|
400
|
-
};
|
|
401
|
-
type CustomProxy<T extends State, R> = { [P in State.Field<T>]-?: R } & {
|
|
402
|
-
get(): T;
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Creates an object with references to all managed values.
|
|
407
|
-
* Each property is a function to set value in state when invoked.
|
|
408
|
-
*
|
|
409
|
-
* *Properties are simultaneously a ref-function and ref-object, use as needed.*
|
|
410
|
-
*
|
|
411
|
-
* @param target - Source state from which to reference values.
|
|
412
|
-
*/
|
|
413
|
-
declare function ref<T extends State>(target: T): ref.Proxy<T>;
|
|
414
|
-
/**
|
|
415
|
-
* Creates an object with values based on managed values.
|
|
416
|
-
* Each property will invoke mapper function on first access supply
|
|
417
|
-
* the its return value going forward.
|
|
418
|
-
*
|
|
419
|
-
* @param target - Source state from which to reference values.
|
|
420
|
-
* @param mapper - Function producing the placeholder value for any given property.
|
|
421
|
-
*/
|
|
422
|
-
declare function ref<O extends State, R>(target: O, mapper: (key: State.Field<O>) => R): ref.CustomProxy<O, R>;
|
|
423
|
-
/**
|
|
424
|
-
* Creates a ref-compatible property.
|
|
425
|
-
* Will persist value, and updates to this are made part of controller event-stream.
|
|
426
|
-
*
|
|
427
|
-
* *Output is simultaneously a ref-function and ref-object, use as needed.*
|
|
428
|
-
*
|
|
429
|
-
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
430
|
-
*/
|
|
431
|
-
declare function ref<T>(callback?: ref.Callback<T>): ref.Object<T>;
|
|
432
|
-
/**
|
|
433
|
-
* Creates a ref-compatible property.
|
|
434
|
-
* Will persist value, and updates to this are made part of controller event-stream.
|
|
435
|
-
*
|
|
436
|
-
* *Output is simultaneously a ref-function and ref-object, use as needed.*
|
|
437
|
-
*
|
|
438
|
-
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
439
|
-
* @param ignoreNull - Default `true`. If `false`, will still callback with `null`.
|
|
440
|
-
*/
|
|
441
|
-
declare function ref<T>(callback: ref.Callback<T | null>, ignoreNull: boolean): ref.Object<T>;
|
|
442
|
-
//#endregion
|
|
443
|
-
//#region src/context.d.ts
|
|
444
|
-
declare namespace Context {
|
|
445
|
-
type Multiple<T extends State> = {
|
|
446
|
-
[key: string | number]: State.Type<T> | T;
|
|
447
|
-
};
|
|
448
|
-
type Accept<T extends State = State> = T | State.Type<T> | Multiple<T>;
|
|
449
|
-
type Expect<T extends State = State> = (state: T) => (() => void) | void;
|
|
450
|
-
}
|
|
451
|
-
interface Context {
|
|
452
|
-
[key: symbol]: State | Context.Expect | null | undefined;
|
|
453
|
-
}
|
|
454
|
-
declare class Context {
|
|
455
|
-
/**
|
|
456
|
-
* Get the context for a specified State. If a callback is provided, it will be run when
|
|
457
|
-
* the context becomes available.
|
|
458
|
-
*/
|
|
459
|
-
static get<T extends State>(on: State, callback: (got: Context) => void): void;
|
|
460
|
-
/**
|
|
461
|
-
* Get the context for a specified State. Returns undefined if none are found.
|
|
462
|
-
*/
|
|
463
|
-
static get<T extends State>(on: State): Context | undefined;
|
|
464
|
-
id: string;
|
|
465
|
-
protected inputs: Record<string | number, State | State.Extends>;
|
|
466
|
-
protected cleanup: Set<() => void>;
|
|
467
|
-
constructor(inputs?: Context.Accept);
|
|
468
|
-
/** Find specified type registered to a parent context. Throws if none are found. */
|
|
469
|
-
get<T extends State>(Type: State.Extends<T>, require: true): T;
|
|
470
|
-
/** Find specified type registered to a parent context. Returns undefined if none are found. */
|
|
471
|
-
get<T extends State>(Type: State.Extends<T>, require?: boolean): T | undefined;
|
|
472
|
-
/** Run callback when a specified type is registered in context downstream. */
|
|
473
|
-
get<T extends State>(Type: State.Extends<T>, callback: (state: T) => void): () => void;
|
|
474
|
-
/**
|
|
475
|
-
* Adds a State to this context.
|
|
476
|
-
*/
|
|
477
|
-
protected add<T extends State>(input: T | State.Type<T>, implicit?: boolean): T;
|
|
478
|
-
/**
|
|
479
|
-
* Register one or more States to this context.
|
|
480
|
-
*
|
|
481
|
-
* Context will add or remove States as needed to keep with provided input.
|
|
482
|
-
*
|
|
483
|
-
* @param inputs State, State class, or map of States / State classes to register.
|
|
484
|
-
* @param forEach Optional callback to run for each State registered.
|
|
485
|
-
*/
|
|
486
|
-
use<T extends State>(inputs: Context.Accept<T>, forEach?: Context.Expect<T>): void;
|
|
487
|
-
/**
|
|
488
|
-
* Create a child context, optionally registering one or more States to it.
|
|
489
|
-
*
|
|
490
|
-
* @param inputs State, State class, or map of States / State classes to register.
|
|
491
|
-
*/
|
|
492
|
-
push(inputs?: Context.Accept): this;
|
|
493
|
-
/**
|
|
494
|
-
* Remove all States from this context.
|
|
495
|
-
*
|
|
496
|
-
* Will also run any cleanup callbacks registered when States were added.
|
|
497
|
-
*/
|
|
498
|
-
pop(): void;
|
|
499
|
-
}
|
|
500
|
-
//#endregion
|
|
501
|
-
export { Context, type Instruction, METHOD, Observable, State, State as default, addListener, event, get, ref, set, use, watch };
|
|
502
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
import { Context } from "./context.js";
|
|
2
|
+
import { Observer, event, listener, observer, touch, watch } from "./observable.js";
|
|
3
|
+
import { State, unbind } from "./state.js";
|
|
4
|
+
import { Component } from "./component.js";
|
|
5
|
+
import { def } from "./field/def.js";
|
|
6
|
+
import { get } from "./field/get.js";
|
|
7
|
+
import { hot } from "./field/hot.js";
|
|
8
|
+
import { ref } from "./field/ref.js";
|
|
9
|
+
import { set } from "./field/set.js";
|
|
10
|
+
export { Component, Context, Observer, State, def, State as default, event, get, hot, listener, observer, ref, set, touch, unbind, watch };
|