@expressive/mvc 0.19.2 → 0.20.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/lib/index.d.ts CHANGED
@@ -14,35 +14,128 @@ declare class Issue extends Error {
14
14
  }
15
15
  declare function issues<M extends Issue.Messages>(register: M): Issue.Output<M>;
16
16
 
17
- declare const LOCAL = "__local";
17
+ type Async<T = any> = (...args: any[]) => Promise<T>;
18
+ /**
19
+ * Sets an exotic method with managed ready-state. Property accepts an async function.
20
+ *
21
+ * When a run-method is invoked, its `active` property to true for duration of call.
22
+ * This is emitted as an update to property, both when called and after returns (or throws).
23
+ *
24
+ * **Note:** Subsequent calls will immediately throw if one is still pending.
25
+ *
26
+ * @param action - Action to fire when resulting property is invoked.
27
+ */
28
+ declare function run(action: Async): typeof action & {
29
+ active: boolean;
30
+ };
31
+ declare function run<S>(action: Async<S>): typeof action & {
32
+ active: boolean;
33
+ };
34
+
35
+ type Callback = () => void;
36
+
37
+ declare global {
38
+ namespace jest {
39
+ interface Matchers<R> {
40
+ /** Assert model does have one or more updates pending. */
41
+ toUpdate(): Promise<R>;
42
+
43
+ /** Assert model did update with keys specified. */
44
+ toHaveUpdated<R>(keys: string[]): Promise<R>;
45
+ }
46
+ }
47
+ }
48
+
49
+ type Observer<T extends Model = any> = (key: Model.Event<T> | null | undefined, source: Control) => Callback | null | void;
50
+ declare namespace Control {
51
+ /**
52
+ * Property initializer, will run upon instance creation.
53
+ * Optional returned callback will run when once upon first access.
54
+ */
55
+ type Instruction<T> = (this: Control, key: string, thisArg: Control) => PropertyDescriptor<T> | Getter<T> | void;
56
+ type Getter<T> = (source: Model) => T;
57
+ type Setter<T> = (value: T) => boolean | void;
58
+ type PropertyDescriptor<T = any> = {
59
+ enumerable?: boolean;
60
+ value?: T;
61
+ get?: Getter<T>;
62
+ set?: Setter<T> | false;
63
+ };
64
+ /**
65
+ * Callback for Controller.for() static method.
66
+ * Returned callback is forwarded.
67
+ */
68
+ type OnReady<T extends Model> = (control: Control<T>) => Callback | void;
69
+ type GetAdapter<T> = (update: () => void, source: (request: (got: Model | undefined) => void) => void) => {
70
+ mount: () => (() => void) | void;
71
+ render: () => T;
72
+ } | void;
73
+ type GetHook = <T extends Model, R>(type: Model.Type<T>, adapter: GetAdapter<R>) => R | null;
74
+ type UseAdapter<T extends Model> = (update: () => void) => {
75
+ instance: T;
76
+ mount: () => (() => void) | void;
77
+ render: (props: Model.Compat<T>) => T;
78
+ };
79
+ type UseHook = <T extends Model>(adapter: UseAdapter<T>) => (props: Model.Compat<T>) => T;
80
+ type HasHook = (type: typeof Model, relativeTo: Model, callback: (got: Model | undefined) => void) => void;
81
+ }
82
+ declare class Control<T extends Model = any> {
83
+ subject: T;
84
+ id: string | number;
85
+ static after: Set<Callback>;
86
+ static before: Set<Callback>;
87
+ static pending: Set<Callback>;
88
+ static get: Control.GetHook;
89
+ static use: Control.UseHook;
90
+ static has: Control.HasHook;
91
+ static for: typeof control;
92
+ static apply: typeof apply;
93
+ static watch: typeof watch;
94
+ parent?: Model;
95
+ state: {
96
+ [key: string]: any;
97
+ };
98
+ latest?: Model.Event<T>[];
99
+ frame: Set<string>;
100
+ followers: Set<Observer<any>>;
101
+ observers: Map<string, Set<Observer<any>>>;
102
+ constructor(subject: T, id?: string | number);
103
+ add(key: Extract<keyof T, string>): void;
104
+ watch(key: keyof T & string, output: Control.PropertyDescriptor<any>): void;
105
+ ref<K extends Model.Key<T>>(key: K, callback?: (this: T, value: T[K]) => boolean | void): (value: any) => void;
106
+ update(key: string): void;
107
+ addListener(fn: Observer): () => boolean;
108
+ clear(): void;
109
+ }
110
+ declare function control<T extends Model>(subject: T, ready: Control.OnReady<T>): Callback;
111
+ declare function control<T extends Model>(subject: T, ready?: boolean): Control<T>;
112
+ declare function watch<T extends Model>(on: T, cb: Observer): T;
113
+ declare function apply<T = any>(instruction: Control.Instruction<any>): T;
114
+
18
115
  declare const STATE = "__state";
19
116
  declare const UPDATE = "__update";
20
117
  declare const CONTROL = "__control";
118
+ declare const PARENT = "__parent";
21
119
  declare const Debug: {
22
- /** Use to access Model's local Subscriber (if exists). */
23
- LOCAL: "__local";
24
120
  /** Use to access snapshot of Model's current state. */
25
121
  STATE: "__state";
26
122
  /**
27
123
  * Use to access snapshot of Model's latest update.
28
- *
29
- * Note: If used within a Subscriber, update will be narrowed to the specific keys
30
- * which triggered a refresh (if one did).
31
124
  */
32
125
  UPDATE: "__update";
33
126
  /** Use to access a Model's controller. */
34
127
  CONTROL: "__control";
128
+ /** Use to access a Model's parent, if exists. */
129
+ PARENT: "__parent";
35
130
  };
36
131
  /**
37
132
  * Generic will declare debug properties on a given Model.
38
133
  *
39
134
  * These properties always exist at runtime, however are hidden unless you cast your Model as `Debug<T>`.
40
135
  */
41
- type Debug<T extends Model> = T & {
136
+ type Debug<T extends Model = Model> = T & {
42
137
  /** Controller for this instance. */
43
138
  [CONTROL]?: Control<T>;
44
- /** Current subscriber (if present) while used in a live context (e.g. hook or effect). */
45
- [LOCAL]?: Subscriber<T>;
46
139
  /** Current state of this instance. */
47
140
  [STATE]?: Model.Export<T>;
48
141
  /**
@@ -52,75 +145,78 @@ type Debug<T extends Model> = T & {
52
145
  * If within a subscribed function, will contain only keys which explicitly caused a refresh.
53
146
  */
54
147
  [UPDATE]?: readonly Model.Event<T>[];
148
+ /**
149
+ * Parent currently assigned to parent. Usually the model which this one is a property of.
150
+ */
151
+ [PARENT]?: Model;
55
152
  };
56
153
 
57
- type Callback = () => void;
58
- type Class = new (...args: any[]) => any;
59
- type InstanceOf<T> = T extends { prototype: infer U } ? U : never;
60
-
61
- type MaybePromise<T> = T | Promise<T>;
62
-
63
154
  /** Type may not be undefined - instead will be null. */
64
155
  type NoVoid<T> = T extends undefined ? null : T;
156
+ declare function get$1<T extends Model>(this: Model.Type<T>): T;
157
+ /** Fetch instance of this class in passive mode. Will not subscribe to events. */
158
+ declare function get$1<T extends Model>(this: Model.Type<T>, ignoreUpdates?: true): T;
159
+ /** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
160
+ declare function get$1<T extends Model>(this: Model.Type<T>, required: boolean): T | undefined;
161
+ declare function get$1<T extends Model, R extends []>(this: Model.Type<T>, factory: Model.GetCallback<T, R | (() => R)>, expect?: boolean): R;
162
+ declare function get$1<T extends Model, R extends []>(this: Model.Type<T>, factory: Model.GetCallback<T, Promise<R> | (() => R) | null>, expect?: boolean): R | null;
163
+ declare function get$1<T extends Model, R extends []>(this: Model.Type<T>, factory: Model.GetCallback<T, Promise<R> | R>, expect: true): Exclude<R, undefined>;
164
+ declare function get$1<T extends Model, R>(this: Model.Type<T>, init: Model.GetCallback<T, () => R>): NoVoid<R>;
165
+ declare function get$1<T extends Model, R>(this: Model.Type<T>, init: Model.GetCallback<T, (() => R) | null>): NoVoid<R> | null;
166
+ declare function get$1<T extends Model, R>(this: Model.Type<T>, compute: Model.GetCallback<T, Promise<R> | R>, expect: true): Exclude<R, undefined>;
167
+ declare function get$1<T extends Model, R>(this: Model.Type<T>, compute: Model.GetCallback<T, Promise<R>>, expect?: boolean): NoVoid<R> | null;
168
+ declare function get$1<T extends Model, R>(this: Model.Type<T>, compute: Model.GetCallback<T, R>, expect?: boolean): NoVoid<R>;
169
+
170
+ declare function use$1<T extends Model>(this: Model.New<T>, callback?: (instance: T) => void): T;
171
+ declare function use$1<T extends Model>(this: Model.New<T>, apply?: Model.Compat<T>, repeat?: boolean): T;
65
172
 
173
+ type InstanceOf<T> = T extends {
174
+ prototype: infer U;
175
+ } ? U : never;
66
176
  declare namespace Model {
67
- export { Control };
68
- export { Subscriber };
69
- /** Including but not limited to T. */
70
- type Extends<T> = T | (string & Record<never, never>);
71
177
  /** Any typeof Model, using class constructor as the reference. */
72
- export type Type<T extends Model = Model> = abstract new () => T;
73
- export type Class<T extends Model> = (abstract new () => T) & typeof Model;
74
- /** A typeof Model, specifically one which may be created without arguments. */
75
- export type New<T extends Model = Model> = (new () => T) & typeof Model;
76
- export type Effect<T> = (this: T, argument: T) => Callback | Promise<any> | void;
178
+ type Type<T extends Model = Model> = abstract new (...args: any[]) => T;
179
+ /** A typeof Model, specifically one which can be created without any arguments. */
180
+ type New<T extends Model = Model> = (new () => T) & typeof Model;
181
+ type Effect<T> = (this: T, argument: T) => Callback | Promise<any> | void;
77
182
  /** Exotic value, actual value is contained. */
78
- export type Ref<T = any> = {
183
+ type Ref<T = any> = {
79
184
  (next: T): void;
80
185
  current: T | null;
81
186
  };
82
- /** Properties of T which are methods. */
83
- export type Methods<T> = {
84
- [K in keyof T]: T[K] extends Ref ? never : T[K] extends Function ? K : never;
85
- }[keyof T];
86
- type BuiltIn = keyof Model | keyof Debug<Model>;
87
187
  /**
88
188
  * Subset of `keyof T` which are not methods or defined by base Model U.
89
- *
90
- * **Note**: This excludes all keys which are not of type `string` (only those are managed).
91
- *
92
- * TODO: Should exclude methods
93
189
  **/
94
- export type Key<T> = Exclude<keyof T, BuiltIn> & string;
95
- /**
96
- * Including but not limited to `keyof T` which are not methods or defined by base Model.
97
- **/
98
- export type Event<T> = Extends<Key<T>>;
190
+ type Key<T, U = Model> = Extract<Exclude<keyof T, keyof U | keyof Debug>, string>;
191
+ /** Including but not limited to `keyof T` which are not methods or defined by base Model. */
192
+ type Event<T> = Key<T> | (string & {});
193
+ /** Actual value stored in state. */
194
+ type Value<R> = R extends Ref<infer T> ? T : R;
195
+ /** Actual value belonging to a managed property. */
196
+ type ValueOf<T extends {}, K> = K extends keyof T ? Value<T[K]> : undefined;
197
+ type Export<T> = {
198
+ [P in Key<T>]: Value<T[P]>;
199
+ };
99
200
  /** Object containing managed entries found in T. */
100
- export type Entries<T> = {
201
+ type Entries<T> = {
101
202
  [K in Key<T>]: T[K];
102
203
  };
103
204
  /** Object comperable to data found in T. */
104
- export type Compat<T> = {
205
+ type Compat<T> = {
105
206
  [K in Key<T>]?: T[K];
106
207
  };
107
- /** Actual value stored in state. */
108
- export type Value<R> = R extends Ref<infer T> ? T : R;
109
- /** Actual value belonging to a managed property. */
110
- export type ValueOf<T extends {}, K> = K extends keyof T ? Value<T[K]> : undefined;
111
208
  /**
112
209
  * Values from current state of given controller.
113
210
  *
114
211
  * Differs from `Entries` as values here will drill into "real" values held by exotics like ref.
115
212
  */
116
- export type Get<T, K extends Key<T> = Key<T>> = {
213
+ type Get<T, K extends Key<T> = Key<T>> = {
117
214
  [P in K]: Value<T[P]>;
118
215
  };
119
- export type Export<T> = {
120
- [P in Key<T>]: Value<T[P]>;
121
- };
122
- export type FindFunction = <T extends Model>(type: Model.Type<T>, relativeTo: Model, required: boolean) => (_refresh: (x: T) => void) => T | undefined;
123
- export type GetCallback<T extends Model, R> = (this: T, model: T, update: ForceUpdate) => R;
216
+ /** Promise thrown by something which is not yet ready. */
217
+ type Suspense = Promise<void> & Error;
218
+ type OnCallback<T extends Model> = (this: T, keys: Model.Event<T>[] | null) => void;
219
+ type GetCallback<T extends Model, R> = (this: T, model: T, update: ForceUpdate) => R;
124
220
  type ForceUpdate = {
125
221
  /** Force an update in current component. */
126
222
  (): void;
@@ -128,51 +224,38 @@ declare namespace Model {
128
224
  * Force an update and again after promise either resolves or rejects.
129
225
  * Will return a duplicate of given Promise, which resolves after refresh.
130
226
  */
131
- <T = void>(passthru?: Promise<T>): Promise<T>;
227
+ <T = void>(passthru: Promise<T>): Promise<T>;
132
228
  /**
133
229
  * Force a update while calling async function.
134
230
  * A refresh will occur both before and after given function.
135
231
  * Any actions performed before first `await` will occur before refresh!
136
232
  */
137
- <T = void>(invoke?: () => Promise<T>): Promise<T>;
233
+ <T = void>(invoke: () => Promise<T>): Promise<T>;
138
234
  };
139
- export type Suspense = Promise<void> & Error;
140
235
  }
141
236
  declare class Model {
237
+ constructor(id?: string | number);
142
238
  /**
143
239
  * Reference to `this` without a subscription.
144
240
  * Use to obtain full reference from a destructure.
145
241
  */
146
- is: this;
147
- /** Unique identifier for Model instance. */
148
- id: string;
149
- constructor(id?: string);
150
- on<P extends Model.Event<this>>(keys?: P | Iterable<P>, timeout?: number): Promise<P[]>;
151
- on<P extends Model.Event<this>>(keys: P | Iterable<P>, listener: (this: this, keys: Model.Event<this>[]) => void, once?: boolean): Callback;
242
+ get is(): this;
243
+ on(): Promise<Model.Event<this>[]>;
244
+ on(timeout?: number): Promise<Model.Event<this>[] | false>;
245
+ on<P extends Model.Event<this>>(keys?: P | Iterable<P>, timeout?: number): Promise<P[] | false>;
246
+ on<P extends Model.Event<this>>(keys: P | Iterable<P>, listener: Model.OnCallback<this>, once?: boolean): Callback;
152
247
  on(effect: Model.Effect<this>): Callback;
153
- on(effect: Model.Effect<this>, watch?: []): Callback;
154
- on(effect: Model.Effect<this>, watch?: Model.Event<this>[]): Callback;
155
- on(key?: undefined, timeout?: number): Promise<Model.Event<this>[]>;
156
- on(current?: boolean): Promise<Model.Event<this>[] | null>;
157
- on(current: true): Promise<Model.Event<this>[]>;
158
- on(current: null): Promise<null>;
159
248
  get(): Model.Export<this>;
160
249
  get<P extends Model.Key<this>>(select: P): this[P];
161
250
  get<P extends Model.Key<this>>(select: Iterable<P>): Model.Get<this, P>;
162
- get<P extends Model.Key<this>>(select: P, timeout: number): Promise<this[P]>;
163
- get<P extends Model.Key<this>>(select: Iterable<P>, timeout: number): Promise<Model.Get<this, P>>;
164
- get<P extends Model.Key<this>>(select: P, onChange: true): Promise<this[P]>;
165
- get<P extends Model.Key<this>>(select: Iterable<P>, onChange: true): Promise<Model.Get<this, P>>;
166
- get<P extends Model.Key<this>>(select: P, onChange: boolean): MaybePromise<this[P]>;
167
- get<P extends Model.Key<this>>(select: Iterable<P>, onChange: boolean): MaybePromise<Model.Get<this, P>>;
168
- get<P extends Model.Key<this>>(select: P, listener: (this: this, value: this[P], key: P[]) => void): Callback;
169
- get<P extends Model.Key<this>>(select: Iterable<P>, listener: (this: this, value: Model.Get<this, P>, key: P[]) => void): Callback;
170
- set(key: Model.Event<this>): PromiseLike<readonly Model.Event<this>[]>;
171
- set<K extends Model.Event<this>>(key: Model.Event<this>, value: Model.ValueOf<this, K>): PromiseLike<readonly Model.Event<this>[]>;
172
- set<T extends Model.Compat<this>>(source: T, select: (keyof T)[]): PromiseLike<(keyof T)[]>;
173
- set<T extends Model.Compat<this>>(source: T, force?: boolean): PromiseLike<(keyof T)[]>;
251
+ get<P extends Model.Key<this>>(select: P, listener: (this: this, value: this[P]) => void): Callback;
252
+ get<P extends Model.Key<this>>(select: Iterable<P>, listener: (this: this, value: Model.Get<this, P>) => void): Callback;
253
+ set<T extends Model.Compat<this>>(source: T, select?: (keyof T)[]): Promise<Model.Key<T>[]>;
254
+ set<K extends Model.Event<this>>(key: K, value?: Model.ValueOf<this, K>): Promise<readonly Model.Event<this>[]>;
174
255
  /** Mark this instance for garbage collection. */
175
- gc(): void;
256
+ null(): void;
257
+ static get: typeof get$1;
258
+ static use: typeof use$1;
176
259
  /**
177
260
  * Creates a new instance of this controller.
178
261
  *
@@ -180,23 +263,7 @@ declare class Model {
180
263
  *
181
264
  * @param args - arguments sent to constructor
182
265
  */
183
- static new<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
184
- static find<T extends Model>(this: Model.Type<T>, required?: boolean): T;
185
- static find<T extends Model>(this: Model.Type<T>, required: false): T | undefined;
186
- static get<T extends Model>(this: Model.Type<T>): T;
187
- static get<T extends Model>(this: Model.Type<T>, passive: true): T;
188
- static get<T extends Model>(this: Model.Type<T>, required: false): T | undefined;
189
- static get<T extends Model, R extends []>(this: Model.Type<T>, compute: Model.GetCallback<T, R | (() => R)>, expect?: boolean): R;
190
- static get<T extends Model, R extends []>(this: Model.Type<T>, compute: Model.GetCallback<T, Promise<R> | (() => R) | null>, expect?: boolean): R | null;
191
- static get<T extends Model, R extends []>(this: Model.Type<T>, compute: Model.GetCallback<T, Promise<R> | R>, expect: true): Exclude<R, undefined>;
192
- static get<T extends Model, R>(this: Model.Type<T>, init: Model.GetCallback<T, () => R>): NoVoid<R>;
193
- static get<T extends Model, R>(this: Model.Type<T>, init: Model.GetCallback<T, (() => R) | null>): NoVoid<R> | null;
194
- static get<T extends Model, R>(this: Model.Type<T>, compute: Model.GetCallback<T, Promise<R> | R>, expect: true): Exclude<R, undefined>;
195
- static get<T extends Model, R>(this: Model.Type<T>, compute: Model.GetCallback<T, Promise<R>>, expect?: boolean): NoVoid<R> | null;
196
- static get<T extends Model, R>(this: Model.Type<T>, compute: Model.GetCallback<T, R>, expect?: boolean): NoVoid<R>;
197
- static use<I extends Model>(this: Model.Type<I>, watch: Model.Key<I>[], callback?: (instance: I) => void): I;
198
- static use<I extends Model>(this: Model.Type<I>, callback?: (instance: I) => void): I;
199
- static use<I extends Model>(this: Model.Type<I>, apply: Model.Compat<I>, keys?: Model.Event<I>[]): I;
266
+ static new<T extends new (...args: any[]) => any>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
200
267
  /**
201
268
  * Static equivalent of `x instanceof this`.
202
269
  *
@@ -205,120 +272,10 @@ declare class Model {
205
272
  static isTypeof<T extends Model.Type>(this: T, maybe: any): maybe is T;
206
273
  }
207
274
 
208
- type Listener = {
209
- commit(): void;
210
- release(): void;
211
- };
212
- declare namespace Subscriber {
213
- type OnEvent<T = any> = (key: Model.Event<T> | null, source: Control) => Callback | void;
214
- type Getter<T extends Model> = (within?: Subscriber<T> | undefined) => any;
215
- }
216
- declare class Subscriber<T extends Model = any> {
217
- onUpdate: Subscriber.OnEvent;
218
- proxy: T;
219
- clear: () => void;
220
- latest?: Model.Event<T>[];
221
- active: boolean;
222
- dependant: Set<Listener>;
223
- watch: Map<any, boolean | (() => boolean | void)>;
224
- parent: Control<T>;
225
- constructor(parent: Control<T> | T, onUpdate: Subscriber.OnEvent);
226
- get using(): Model.Key<T>[];
227
- get(key: string, using?: Subscriber.Getter<T>): any;
228
- follow(key: any, value?: boolean | (() => boolean | void)): void;
229
- commit(): void;
230
- release(): void;
231
- private notify;
232
- }
233
-
234
- /**
235
- * Property initializer, will run upon instance creation.
236
- * Optional returned callback will run when once upon first access.
237
- */
238
- type Instruction<T> = (this: Control, key: string, thisArg: Control) => Instruction.Getter<T> | Instruction.Descriptor<T> | void;
239
- declare namespace Instruction {
240
- type Getter<T> = (within?: Subscriber) => T;
241
- type Setter<T> = (value: T) => boolean | void;
242
- type Runner = (on: Control, key: string) => Instruction.Descriptor<any> | undefined;
243
- interface Descriptor<T> {
244
- enumerable?: boolean;
245
- value?: T;
246
- get?: Getter<T>;
247
- set?: Setter<T> | false;
248
- }
249
- }
250
- /**
251
- * Run instruction as controller sets itself up.
252
- * This will specialize the behavior of a given property.
253
- */
254
- declare function add<T = any>(instruction: Instruction<any>, label?: string): T;
255
-
256
- declare namespace Control {
257
- /**
258
- * Called immediately when any key is changed or emitted.
259
- * Returned callback is notified when update is complete.
260
- */
261
- type OnSync<T = any> = (key: Model.Event<T> | null, source: Control) => OnAsync<T> | void;
262
- /** Called at the end of an event frame with all keys affected. */
263
- type OnAsync<T = any> = (keys: Model.Event<T>[]) => void;
264
- type OnValue<T = any> = (this: T, value: any) => boolean | void;
265
- }
266
- declare class Control<T extends {} = any> {
267
- subject: T;
268
- state: Map<any, any>;
269
- frame: Set<string>;
270
- waiting: Set<Model.Control.OnAsync<T>>;
271
- followers: Set<Model.Control.OnSync<any>>;
272
- latest?: Model.Event<T>[];
273
- constructor(subject: T);
274
- watch(key: keyof T & string): void;
275
- /** Throw suspense which resolves when key is set. */
276
- waitFor(key: string): never;
277
- addListener(listener: Control.OnSync<T>): () => void;
278
- ref<K extends Model.Key<T>>(key: K, handler?: (this: T, value: T[K]) => boolean | void): (value: any) => void;
279
- update(key: Model.Key<T>): void;
280
- clear(): void;
281
- static get: typeof controller;
282
- static for: typeof control;
283
- }
284
- declare function controller<T extends Model>(from: T): Control<T> | undefined;
285
- declare namespace control {
286
- /**
287
- * Callback for Controller.for() static method.
288
- * Returned callback is forwarded.
289
- */
290
- type OnReady<T extends Model> = (control: Control<T>) => Callback | void;
291
- }
292
- declare function control<T extends Model>(subject: T): Control<T>;
293
- declare function control<T extends Model>(subject: T, cb: control.OnReady<T>): Callback;
294
-
295
- declare function getParent<T extends Model>(from: Model, type?: Model.Class<T>): T;
296
-
297
- type Async<T = any> = (...args: any[]) => Promise<T>;
298
- /**
299
- * Sets an exotic method with managed ready-state. Property accepts an async function.
300
- *
301
- * When a run-method is invoked, its `active` property to true for duration of call.
302
- * This is emitted as an update to property, both when called and after returns (or throws).
303
- *
304
- * **Note:** Subsequent calls will immediately throw if one is still pending.
305
- *
306
- * @param action - Action to fire when resulting property is invoked.
307
- */
308
- declare function run(action: Async): typeof action & {
309
- active: boolean;
310
- };
311
- declare function run<S>(action: Async<S>): typeof action & {
312
- active: boolean;
313
- };
314
-
315
275
  declare namespace get {
316
276
  type Function<T, S = any> = (this: S, on: S) => T;
317
277
  type Factory<R, T> = (this: T, property: string, on: T) => Function<R, T>;
318
- type FindFunction<T extends Model = Model> = (type: Model.Class<T>, relativeTo: Model, required: boolean) => Source<T>;
319
278
  type Source<T extends Model = Model> = (callback: (x: T) => void) => void;
320
- /** Assign fetch algorithm for get instruction. */
321
- let using: (fn: FindFunction) => typeof get;
322
279
  }
323
280
  /**
324
281
  * Fetches and assigns the controller which spawned this host.
@@ -338,8 +295,8 @@ declare function get<R, T extends Model>(Type: Model.Type<T>, compute: (this: T,
338
295
  * @param source - Source model from which computed value will be a subscriber.
339
296
  * @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value. Will update automatically as input values change.
340
297
  */
341
- declare function get<R, T>(source: T, compute: (this: T, on: T) => R): Exclude<R, undefined>;
342
- declare function get<R, T>(source: T, compute: (this: T, on: T) => R): R;
298
+ declare function get<R, T extends Model>(source: T, compute: (this: T, on: T) => R): Exclude<R, undefined>;
299
+ declare function get<R, T extends Model>(source: T, compute: (this: T, on: T) => R): R;
343
300
  /**
344
301
  * Implement a computed value; output is returned by function from provided factory.
345
302
  *
@@ -349,7 +306,7 @@ declare function get<R, T>(compute: (property: string, on: T) => (this: T, state
349
306
  declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R): R;
350
307
 
351
308
  declare namespace ref {
352
- type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<void> | void | boolean;
309
+ type Callback<T> = (argument: T) => ((next: T) => void) | Promise<void> | void | boolean;
353
310
  interface Object<T = any> {
354
311
  (next: T): void;
355
312
  current: T | null;
@@ -389,11 +346,10 @@ declare function ref<O extends Model, R>(target: O, mapper: (key: Model.Key<O>)
389
346
  * @param callback - Optional callback to synchronously fire when reference is first set or does update.
390
347
  */
391
348
  declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>;
392
- declare function ref<T, S>(callback?: ref.Callback<T, S>): ref.Object<T>;
393
349
 
394
350
  declare namespace set {
395
351
  type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<any> | void | boolean;
396
- type Factory<T, S = unknown> = (this: S, key: string, thisArg: S) => T;
352
+ type Factory<T, S = any> = (this: S, key: string, thisArg: S) => Promise<T> | T;
397
353
  }
398
354
  /**
399
355
  * Set property with a placeholder.
@@ -402,28 +358,12 @@ declare namespace set {
402
358
  * `Promise`/`Error` (ala: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.
403
359
  */
404
360
  declare function set<T = any>(): T;
405
- /**
406
- * Set property with an async function.
407
- *
408
- * Property cannot be accessed until factory resolves, yeilding a result.
409
- * If accessed while processing, React Suspense will be thrown.
410
- *
411
- * - `required: true` (default) -
412
- * Run factory immediately upon creation of model instance.
413
- * - `required: false` -
414
- * Run factory only if/when accessed.
415
- * Value will always throw suspense at least once - use with caution.
416
- *
417
- * @param factory - Callback run to derrive property value.
418
- * @param required - (default: true) Run factory immediately on creation, otherwise on access.
419
- */
420
- declare function set<T>(factory: set.Factory<Promise<T>>, required: false): T | undefined;
421
- declare function set<T, S>(factory: set.Factory<Promise<T>, S>, required: false): T | undefined;
422
- declare function set<T>(factory: set.Factory<Promise<T>>, required?: boolean): T;
423
- declare function set<T, S>(factory: set.Factory<Promise<T>, S>, required?: boolean): T;
424
361
  /**
425
362
  * Set property with a factory function.
426
363
  *
364
+ * If async, poperty cannot be accessed until resolves, yeilding a result.
365
+ * If accessed while still processing, React Suspense will be thrown.
366
+ *
427
367
  * - `required: true` (default) -
428
368
  * Run factory immediately upon creation of model instance.
429
369
  * - `required: false` -
@@ -433,70 +373,39 @@ declare function set<T, S>(factory: set.Factory<Promise<T>, S>, required?: boole
433
373
  * @param factory - Callback run to derrive property value.
434
374
  * @param required - (default: true) Run factory immediately on creation, otherwise on access.
435
375
  */
436
- declare function set<T>(factory: set.Factory<T>, required: false): T | undefined;
437
- declare function set<T, S>(factory: set.Factory<T, S>, required: false): T | undefined;
438
- declare function set<T>(factory: set.Factory<T>, required?: boolean): T;
439
- declare function set<T, S>(factory: set.Factory<T, S>, required?: boolean): T;
440
- declare function set<T>(factory: Promise<T>, required: false): T | undefined;
441
- declare function set<T>(factory: Promise<T>, required?: boolean): T;
376
+ declare function set<T>(factory: set.Factory<T> | Promise<T>, required: false): T | undefined;
377
+ declare function set<T>(factory: set.Factory<T> | Promise<T>, required?: boolean): T;
442
378
  declare function set<T>(value: undefined, onUpdate: set.Callback<T>): T;
443
- declare function set<T, S>(value: undefined, onUpdate: set.Callback<T, S>): T;
444
379
  declare function set<T>(value: T, onUpdate: set.Callback<T>): T;
445
- declare function set<T, S>(value: T, onUpdate: set.Callback<T, S>): T;
446
-
447
- type MapFunction<T, R> = T extends Map<infer K, infer V> ? (value: V, key: K, map: T) => R | void : T extends Set<infer K> ? (value: K, key: K, set: T) => R | void : never;
448
- type Managed<T> = Exclude<T, "forEach"> & {
449
- from?: T;
450
- forEach<R>(mapFunction: MapFunction<T, R>, thisArg?: any): Exclude<R, undefined>[] | void;
451
- };
452
380
 
453
- declare function use<K = any>(initial: SetConstructor): Managed<Set<K>>;
454
- declare function use<K = any, V = any>(initial: MapConstructor): Managed<Map<K, V>>;
455
- declare function use(initial: Set<unknown>): Managed<Set<any>>;
456
- declare function use(initial: Map<unknown, unknown>): Managed<Map<any, any>>;
457
- declare function use(from: () => Set<unknown>): Managed<Set<any>>;
458
- declare function use(from: () => Map<unknown, unknown>): Managed<Map<any, any>>;
459
- declare function use<T extends Set<any> | Map<any, any>>(initial: T): Managed<T>;
460
- declare function use<T extends Set<any> | Map<any, any>>(from: () => T): Managed<T>;
461
- /**
462
- * Create a placeholder for specified Model type.
463
- */
381
+ /** Create a placeholder for specified Model type. */
464
382
  declare function use<T extends Model>(): T | undefined;
465
- declare function use<T extends Model>(from: () => T, required: false): T | undefined;
466
- declare function use<T extends Model>(from: () => T, required: boolean): T;
467
- /**
468
- * Create a new child instance of model.
469
- */
383
+ /** Create a new child instance of model. */
470
384
  declare function use<T extends Model>(Type: Model.New<T>, callback?: (i: T) => void): T;
385
+ /** Assign the result of a factory as a child model. */
386
+ declare function use<T extends Model>(from: () => Promise<T> | T, required?: true): T;
387
+ /** Assign the result of a factory as a child model. */
388
+ declare function use<T extends Model>(from: () => Promise<T> | T, required: boolean): T | undefined;
389
+ /** Create a managed child from factory function. */
390
+ declare function use<T extends Model>(from: () => Promise<T> | T, callback?: (i: T) => void): T;
471
391
  /**
472
- * Create a managed child from factory function.
473
- */
474
- declare function use<T extends {}>(from: () => Promise<T>, required: false): T | undefined;
475
- declare function use<T extends {}>(from: () => Promise<T>, required: boolean): T;
476
- declare function use<T extends {}>(from: () => Promise<T>, callback?: (i: T) => void): T;
477
- declare function use<T extends {}>(from: () => T, callback?: (i: T) => void): T;
478
- /**
479
- * Create child-instance relationship with provided model.
480
- *
481
- * Note: If `peer` is not already initialized before parent is
482
- * (created with `new` as opposed to create method), that model will
483
- * attach this via `parent()` instruction. It will not, however, if
484
- * already active.
485
- */
486
- declare function use<T extends {}>(peer: T, callback?: (i: T) => void): T;
392
+ * Create child-instance relationship with provided model.
393
+ *
394
+ * Note: If `peer` is not already initialized before parent is
395
+ * (created with `new` as opposed to create method), that model will
396
+ * attach this via `parent()` instruction. It will not, however, if
397
+ * already active.
398
+ */
399
+ declare function use<T extends Model>(peer: T, callback?: (i: T) => void): T;
487
400
 
488
- declare class Register {
401
+ declare class Context {
489
402
  private table;
490
- register: Map<string | number, Model | Model.Type>;
403
+ private input;
491
404
  private has;
492
405
  get<T extends Model>(Type: Model.Type<T>): T | undefined;
493
- add(input: Model.New | Model, key?: number | string): Model;
406
+ add<T extends Model>(input: T | Model.New<T>, key?: number | string): T;
494
407
  push(): this;
495
408
  pop(): void;
496
409
  }
497
410
 
498
- declare const Internal: {
499
- getParent: typeof getParent;
500
- };
501
-
502
- export { Control, Debug, Internal, Model, Register, Subscriber, add, Model as default, get, issues, ref, run, set, use };
411
+ export { Context, Control, Debug, Model, Model as default, get, issues, ref, run, set, use };