@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
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { Context } from "./context.js";
|
|
2
|
+
import { event } from "./observable.js";
|
|
3
|
+
|
|
4
|
+
//#region src/state.d.ts
|
|
5
|
+
/** Internal state assigned to states. */
|
|
6
|
+
declare const STORE: WeakMap<State, Record<string | number | symbol, unknown>>;
|
|
7
|
+
declare namespace State {
|
|
8
|
+
/** Any type of State, using own class constructor as its identifier. */
|
|
9
|
+
type Extends<T extends State = State> = (abstract new (...args: any[]) => T) & typeof State;
|
|
10
|
+
/** A State constructor which may be instanciated. */
|
|
11
|
+
type Type<T extends State = State> = (new (...args: State.Args<T>) => T) & Omit<typeof State, never>;
|
|
12
|
+
/** State constructor arguments */
|
|
13
|
+
type Args<T extends State = any> = (Args<T> | Init<T> | Assign<T> | void)[];
|
|
14
|
+
/**
|
|
15
|
+
* State constructor callback - runs during activation, in argument order,
|
|
16
|
+
* before the `new()` lifecycle hook (so it may configure state `new()`
|
|
17
|
+
* will observe). Returned function will run when state is destroyed.
|
|
18
|
+
*/
|
|
19
|
+
type Init<T extends State = State> = (this: T, thisArg: T) => Promise<void> | (() => void) | Args<T> | Assign<T> | void;
|
|
20
|
+
/**
|
|
21
|
+
* Lifecycle handlers for `State.on`, keyed by when they run. A bare `Init`
|
|
22
|
+
* function passed to `on` is sugar for `{ before }`.
|
|
23
|
+
*/
|
|
24
|
+
interface On<T extends State = State> {
|
|
25
|
+
/**
|
|
26
|
+
* Per-class setup, run once when the class is first bootstrapped - before
|
|
27
|
+
* its members are classified and bound. Receives the class, so a handler
|
|
28
|
+
* may inspect or reshape the prototype first. A handler registered on a
|
|
29
|
+
* base class runs for each subclass too.
|
|
30
|
+
*/
|
|
31
|
+
type?(type: State.Extends<T>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Per-instance setup, run in the `prepare` phase before `observe` and the
|
|
34
|
+
* `new()` hook. Equivalent to passing a bare function to `on`. May return a
|
|
35
|
+
* cleanup, constructor args, or an assign overlay.
|
|
36
|
+
*/
|
|
37
|
+
before?(this: T, thisArg: T): void | (() => void) | Promise<void> | Args<T> | Assign<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Per-instance setup, run at the `new()` slot - after own values are
|
|
40
|
+
* observed and constructor args applied. May return a cleanup function.
|
|
41
|
+
*/
|
|
42
|
+
after?(this: T, self: T): void | (() => void);
|
|
43
|
+
}
|
|
44
|
+
/** Object overlay to override values and methods on a state. */
|
|
45
|
+
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] };
|
|
46
|
+
/** Subset of `keyof T` not defined by base State. **/
|
|
47
|
+
type Field<T> = Exclude<keyof T, keyof State>;
|
|
48
|
+
/** Any valid key for state, including but not limited to Field<T>. */
|
|
49
|
+
type Event<T = State> = Field<T> | number | symbol | (string & {});
|
|
50
|
+
/** Any event signal dispatched to listeners, including lifecycle meta events. */
|
|
51
|
+
type Signal<T = State> = Event<T> | true | false | null;
|
|
52
|
+
/** Export/Import compatible value for a given property in a State. */
|
|
53
|
+
type Export<R> = R extends State ? Values<R> : R extends {
|
|
54
|
+
get(): infer T;
|
|
55
|
+
} ? T : R;
|
|
56
|
+
/**
|
|
57
|
+
* Values from current state of given state.
|
|
58
|
+
* Differs from `Values` as values here will drill
|
|
59
|
+
* into "real" values held by exotics like ref.Object.
|
|
60
|
+
*/
|
|
61
|
+
type Values<T> = { [P in Field<T>]: Export<T[P]> };
|
|
62
|
+
/** Object comperable to data found in T. */
|
|
63
|
+
type Partial<T> = { [P in Field<T>]?: Export<T[P]> };
|
|
64
|
+
/** Value for a property applied 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) => T | void;
|
|
67
|
+
/** Descriptor config for a managed property. */
|
|
68
|
+
type Apply<T = any> = {
|
|
69
|
+
value?: T;
|
|
70
|
+
get?: ((source: State) => T) | boolean;
|
|
71
|
+
set?: Setter<T> | boolean;
|
|
72
|
+
enumerable?: boolean;
|
|
73
|
+
};
|
|
74
|
+
/** Descriptor config for set() overload - enforces type when key is a known field. */
|
|
75
|
+
type Define<T, K> = K extends Field<T> ? Apply<T[K] | Export<T[K]>> : Apply;
|
|
76
|
+
type OnEvent<T extends State> = (this: T, key: Signal<T>, source: T) => void | (() => void) | null;
|
|
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: readonly State.Event<T>[] | undefined) => 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
|
+
* A list of keys updated, or events fired, since last update.
|
|
101
|
+
* This may be awaited to get full list (same array) when update is settled.
|
|
102
|
+
*/
|
|
103
|
+
type Updated<T extends State> = readonly Event<T>[] & PromiseLike<readonly Event<T>[]>;
|
|
104
|
+
}
|
|
105
|
+
declare abstract class State {
|
|
106
|
+
/**
|
|
107
|
+
* Loopback to instance of this state. This is useful when in a subscribed context,
|
|
108
|
+
* to keep write access to `this` after a destructure. You can use it to read variables silently as well.
|
|
109
|
+
**/
|
|
110
|
+
is: this;
|
|
111
|
+
constructor(...args: State.Args);
|
|
112
|
+
/**
|
|
113
|
+
* Optional lifecycle hook called during State initialization.
|
|
114
|
+
* Can return a cleanup function to run when state is destroyed.
|
|
115
|
+
*
|
|
116
|
+
* It is recommended you protect this method.
|
|
117
|
+
*/
|
|
118
|
+
protected new?(): void | (() => void);
|
|
119
|
+
/**
|
|
120
|
+
* Pull current values from state. Flattens all states and exotic values recursively.
|
|
121
|
+
*
|
|
122
|
+
* @returns Object with all values from this state.
|
|
123
|
+
**/
|
|
124
|
+
get(): State.Values<this>;
|
|
125
|
+
/**
|
|
126
|
+
* Run a function to run automatically when accessed values change.
|
|
127
|
+
*
|
|
128
|
+
* @param effect Function to run, and whenever accessed values change.
|
|
129
|
+
* If effect returns a function, it will be called when a change occurs (syncronously),
|
|
130
|
+
* effect is cancelled, or parent state is destroyed.
|
|
131
|
+
* @returns Function to cancel listener.
|
|
132
|
+
*/
|
|
133
|
+
get(effect: State.Effect<this>): () => void;
|
|
134
|
+
/**
|
|
135
|
+
* Get value of a property. Will fetch underlying value from exotic values like ref.Object.
|
|
136
|
+
*
|
|
137
|
+
* Any value which implements `get()` (with no arguments) will be treated as such.
|
|
138
|
+
*
|
|
139
|
+
* @param key - Property to get value of.
|
|
140
|
+
* @param required - If true, will throw an error if property is not available.
|
|
141
|
+
* @returns Value of property.
|
|
142
|
+
*/
|
|
143
|
+
get<T extends State.Event<this>>(key: T, required?: boolean): State.Value<this, T>;
|
|
144
|
+
/**
|
|
145
|
+
* Check if state is destroyed.
|
|
146
|
+
*
|
|
147
|
+
* @param status - `null` to check if state is destroyed.
|
|
148
|
+
* @returns `true` if state is destroyed, `false` otherwise.
|
|
149
|
+
*/
|
|
150
|
+
get(status: null): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Callback when state is to be destroyed.
|
|
153
|
+
*
|
|
154
|
+
* @param callback - Function to call when state is destroyed.
|
|
155
|
+
* @returns Function to cancel listener.
|
|
156
|
+
*/
|
|
157
|
+
get(status: null, callback: () => void): () => void;
|
|
158
|
+
/** Fetch State of type from context. Throws if not found. */
|
|
159
|
+
get<T extends State>(type: State.Type<T>, required?: true): T;
|
|
160
|
+
/** Fetch a State from context. Undefined if not found. */
|
|
161
|
+
get<T extends State>(type: State.Type<T>, required: boolean): T | undefined;
|
|
162
|
+
/**
|
|
163
|
+
* Subscribe to State becoming available in context.
|
|
164
|
+
*
|
|
165
|
+
* Will search both up and downstream by default.
|
|
166
|
+
* Normally you can ignore this because State you expect is rarely both.
|
|
167
|
+
*
|
|
168
|
+
* Specify downstream if you only want to watch for State in children, which can be useful if you expect multiple of the same State in different branches of the tree.
|
|
169
|
+
*
|
|
170
|
+
* @param type - Type of State to watch for.
|
|
171
|
+
* @param callback - Function to call when State is found. Will be called immediately if State is already available.
|
|
172
|
+
* @param downstream - If true, will only watch for State in children, if false will only for parents.
|
|
173
|
+
* @returns Function to cancel listener.
|
|
174
|
+
*/
|
|
175
|
+
get<T extends State>(type: State.Type<T>, callback: Context.Expect<T>, downstream?: boolean): () => void;
|
|
176
|
+
/**
|
|
177
|
+
* Get update in progress.
|
|
178
|
+
*
|
|
179
|
+
* @returns Promise which resolves object with updated values, `undefined` if there no update is pending.
|
|
180
|
+
**/
|
|
181
|
+
set(): State.Updated<this>;
|
|
182
|
+
/**
|
|
183
|
+
* Merge argument with current state, updating one or more properties at once.
|
|
184
|
+
* Properties which are not managed by this state will be ignored.
|
|
185
|
+
*
|
|
186
|
+
* @param assign - Object with properties to update.
|
|
187
|
+
* @param silent - If true, listeners will not be notified. If state is destroyed, will squash update without throwing.
|
|
188
|
+
* @returns Array of keys updated, syncronously contains keys updated immediately and may be resolved (to itself) when all updates are settled.
|
|
189
|
+
*/
|
|
190
|
+
set(assign?: State.Assign<this>, silent?: boolean): State.Updated<this>;
|
|
191
|
+
/**
|
|
192
|
+
* Call a function when update occurs.
|
|
193
|
+
*
|
|
194
|
+
* Given function is called for every assignment (which changes value) or explicit `set`.
|
|
195
|
+
*
|
|
196
|
+
* To run logic on final value only, callback may return a function. The same
|
|
197
|
+
* function for one or more events will be called only once when update is settled.
|
|
198
|
+
*
|
|
199
|
+
* Return `null` from callback to stop listening.
|
|
200
|
+
*
|
|
201
|
+
* @param callback - Function to call when update occurs.
|
|
202
|
+
* @returns Function to remove listener. Will return `true` if removed, `false` if inactive already.
|
|
203
|
+
*/
|
|
204
|
+
set(callback: State.OnEvent<this>): () => boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Push an update. This will not change the value of associated property.
|
|
207
|
+
*
|
|
208
|
+
* Useful where a property value internally has changed, but the object is the same.
|
|
209
|
+
* For example: An array has pushed a new value, or a nested property is updated.
|
|
210
|
+
*
|
|
211
|
+
* You can also use this to dispatch arbitrary events.
|
|
212
|
+
* Symbols are recommended as non-property events, however you can use any string.
|
|
213
|
+
* If doing so, be sure to avoid collisions using property names. An easy way to do this is
|
|
214
|
+
* to prefix an event with "!" and/or use dash-case. e.g. `set("!event")` or `set("my-event")`.
|
|
215
|
+
*
|
|
216
|
+
* @param key - Property or event to dispatch.
|
|
217
|
+
* @returns Promise resolves an array of keys updated.
|
|
218
|
+
*/
|
|
219
|
+
set(key: State.Event<this>): State.Updated<this>;
|
|
220
|
+
/**
|
|
221
|
+
* Declare an end to updates. This event is final and will freeze state.
|
|
222
|
+
* This event can be watched for as well, to run cleanup logic and internally will remove all listeners.
|
|
223
|
+
*
|
|
224
|
+
* @param status - `null` to end updates.
|
|
225
|
+
*/
|
|
226
|
+
set(status: null): void;
|
|
227
|
+
/**
|
|
228
|
+
* Register a callback for a specific property or event.
|
|
229
|
+
*
|
|
230
|
+
* Callback receives the key, current value, and source instance.
|
|
231
|
+
*
|
|
232
|
+
* @param event - Property or event to watch. If `null`, will callback on destroy.
|
|
233
|
+
* @param callback - Function to call when event occurs.
|
|
234
|
+
* @returns Function to remove listener.
|
|
235
|
+
*/
|
|
236
|
+
set<K extends State.Event<this>>(event: K | null, callback: State.OnEvent<this>): () => boolean;
|
|
237
|
+
/**
|
|
238
|
+
* Define or update a managed property using a descriptor config.
|
|
239
|
+
* If the property already is managed, config will only accept value.
|
|
240
|
+
* If the property does not exist, it will be created and made reactive.
|
|
241
|
+
*
|
|
242
|
+
* @param key - Property to define or update.
|
|
243
|
+
* @param config - Descriptor config with value, get, set, enumerable, and/or destroy.
|
|
244
|
+
* @returns Promise resolves an array of keys updated.
|
|
245
|
+
*/
|
|
246
|
+
set<K extends State.Event<this>>(key: K, config: State.Define<this, K>): State.Updated<this>;
|
|
247
|
+
/**
|
|
248
|
+
* Iterate over managed properties in this instance of State.
|
|
249
|
+
* Yeilds the key and current value for each property.
|
|
250
|
+
*/
|
|
251
|
+
[Symbol.iterator](): Iterator<[string, unknown]>;
|
|
252
|
+
/**
|
|
253
|
+
* Create and activate a new instance of this state.
|
|
254
|
+
*
|
|
255
|
+
* **Important** - Unlike `new this(...)` - this method also activates state.
|
|
256
|
+
*
|
|
257
|
+
* @param args - arguments sent to constructor
|
|
258
|
+
*/
|
|
259
|
+
static new<T extends State>(this: State.Type<T>, ...args: State.Args<T>): T;
|
|
260
|
+
/**
|
|
261
|
+
* Static equivalent of `x instanceof this`.
|
|
262
|
+
* Determines if provided class is a subtype of this one.
|
|
263
|
+
* If so, language server will make available all static
|
|
264
|
+
* methods and properties of this class.
|
|
265
|
+
*/
|
|
266
|
+
static is<T extends State.Extends>(this: T, maybe: unknown): maybe is T;
|
|
267
|
+
/**
|
|
268
|
+
* Register a lifecycle handler for this State and its subclasses.
|
|
269
|
+
*
|
|
270
|
+
* A bare function is per-instance setup run in the `prepare` phase (sugar for
|
|
271
|
+
* `{ before }`); if it returns a function, that runs when the instance is
|
|
272
|
+
* destroyed. Pass a {@link State.On} object to hook by cadence - `type`
|
|
273
|
+
* (per-class, at bootstrap), `before` (per-instance, before `new()`), and
|
|
274
|
+
* `after` (per-instance, at the `new()` slot).
|
|
275
|
+
*
|
|
276
|
+
* @returns Function to remove the handler.
|
|
277
|
+
*/
|
|
278
|
+
static on<T extends State>(this: State.Extends<T>, handler: State.Init<T> | State.On<T>): () => boolean;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Define or update a managed property using a descriptor config.
|
|
282
|
+
* If the property already is managed, config will only accept value.
|
|
283
|
+
* If the property does not exist, it will be created and made reactive.
|
|
284
|
+
*
|
|
285
|
+
* @param state - State to apply property to.
|
|
286
|
+
* @param key - Property to define or update.
|
|
287
|
+
* @param config - Descriptor config with value, get, set, enumerable, and/or destroy.
|
|
288
|
+
* @param silent - If an update does occur, listeners will not be refreshed automatically.
|
|
289
|
+
*/
|
|
290
|
+
declare function apply(state: State, key: string, config: State.Apply, silent?: boolean): void;
|
|
291
|
+
declare function access(state: State, property: string, required?: boolean): any;
|
|
292
|
+
/**
|
|
293
|
+
* Update a property on a state instance and notify listeners.
|
|
294
|
+
*
|
|
295
|
+
* 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.
|
|
296
|
+
*
|
|
297
|
+
* If `silent` is true, the update will not dispatch events and will return `false` instead of throwing if state is destroyed.
|
|
298
|
+
*/
|
|
299
|
+
declare function update<T>(state: State, key: State.Event<T>, value: T, silent?: boolean): boolean;
|
|
300
|
+
/** Random alphanumberic of length 6; always starts with a letter. */
|
|
301
|
+
declare function uid(): string;
|
|
302
|
+
declare function unbind<T extends Function>(fn: T): T;
|
|
303
|
+
declare function unbind<T extends Function>(fn?: T): T | undefined;
|
|
304
|
+
/**
|
|
305
|
+
* Get or set a parent on given state. May only be assigned once; ignores new value.
|
|
306
|
+
* Returns parent of child, or `undefined` if not assigned yet.
|
|
307
|
+
*/
|
|
308
|
+
declare function parent(child: State, value?: State | null): State | null | undefined;
|
|
309
|
+
//#endregion
|
|
310
|
+
export { STORE, State, access, apply, event, parent, uid, unbind, update };
|
|
311
|
+
//# sourceMappingURL=state.d.ts.map
|