@expressive/mvc 1.0.0-beta.9 → 1.0.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 +594 -0
- package/lib/index.esm.js +1606 -0
- package/lib/index.js +1498 -881
- package/package.json +20 -22
- package/types/context.d.ts +0 -59
- package/types/dispatch.d.ts +0 -55
- package/types/index.d.ts +0 -9
- package/types/instructions.d.ts +0 -101
- package/types/lifecycle.d.ts +0 -30
- package/types/model.d.ts +0 -421
- package/types/selector.ts +0 -29
- package/types/types.d.ts +0 -14
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type Callback = () => void;
|
|
4
|
+
type Class = new (...args: any[]) => any;
|
|
5
|
+
type InstanceOf<T> = T extends { prototype: infer U } ? U : never;
|
|
6
|
+
|
|
7
|
+
/** Type may not be undefined - instead will be null. */
|
|
8
|
+
type NoVoid<T> = T extends undefined ? null : T;
|
|
9
|
+
|
|
10
|
+
declare type Listener = {
|
|
11
|
+
commit(): void;
|
|
12
|
+
release(): void;
|
|
13
|
+
};
|
|
14
|
+
declare namespace Subscriber {
|
|
15
|
+
type OnEvent<T = any> = (key: Model.Event<T> | null, source: Control) => Callback | void;
|
|
16
|
+
}
|
|
17
|
+
declare class Subscriber<T extends {} = any> {
|
|
18
|
+
parent: Control<T>;
|
|
19
|
+
onUpdate: Subscriber.OnEvent;
|
|
20
|
+
proxy: T;
|
|
21
|
+
release: Callback;
|
|
22
|
+
latest?: Model.Event<T>[];
|
|
23
|
+
active: boolean;
|
|
24
|
+
dependant: Set<Listener>;
|
|
25
|
+
watch: Map<any, boolean | (() => true | void)>;
|
|
26
|
+
constructor(parent: Control<T>, onUpdate: Subscriber.OnEvent);
|
|
27
|
+
get using(): Model.Key<T>[];
|
|
28
|
+
add(key: any, value?: boolean | Callback): void;
|
|
29
|
+
commit(): () => void;
|
|
30
|
+
private onEvent;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare const LOCAL = "$debug local";
|
|
34
|
+
declare const STATE = "$debug state";
|
|
35
|
+
declare const UPDATE = "$debug update";
|
|
36
|
+
declare const CONTROL = "$debug controller";
|
|
37
|
+
declare const Debug: {
|
|
38
|
+
readonly CONTROL: "$debug controller";
|
|
39
|
+
readonly LOCAL: "$debug local";
|
|
40
|
+
readonly STATE: "$debug state";
|
|
41
|
+
readonly UPDATE: "$debug update";
|
|
42
|
+
};
|
|
43
|
+
declare type Debug<T extends {}> = T & {
|
|
44
|
+
/** Controller for this instance. */
|
|
45
|
+
[CONTROL]?: Control<T>;
|
|
46
|
+
/** Current subscriber (if present) while used in a live context (e.g. hook or effect). */
|
|
47
|
+
[LOCAL]?: Subscriber<T>;
|
|
48
|
+
/** Current state of this instance. */
|
|
49
|
+
[STATE]?: Model.Export<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Last update causing a refresh to subscribers.
|
|
52
|
+
*
|
|
53
|
+
* If accessed directly, will contain all keys from last push.
|
|
54
|
+
* If within a subscribed function, will contain only keys which explicitly caused a refresh.
|
|
55
|
+
*/
|
|
56
|
+
[UPDATE]?: readonly Model.Event<T>[];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
declare namespace Model {
|
|
60
|
+
export { Control };
|
|
61
|
+
export { Subscriber };
|
|
62
|
+
/** Including but not limited to T. */
|
|
63
|
+
type Extends<T> = T | (string & Record<never, never>);
|
|
64
|
+
export type Type<T extends Model = Model> = typeof Model & (new () => T);
|
|
65
|
+
export type Effect<T> = (this: T, argument: T) => Callback | Promise<any> | void;
|
|
66
|
+
/** Exotic value, actual value is contained. */
|
|
67
|
+
export type Ref<T = any> = {
|
|
68
|
+
(next: T): void;
|
|
69
|
+
current: T | null;
|
|
70
|
+
};
|
|
71
|
+
/** Properties of T which are methods. */
|
|
72
|
+
export type Methods<T> = {
|
|
73
|
+
[K in keyof T]: T[K] extends Ref ? never : T[K] extends Function ? K : never;
|
|
74
|
+
}[keyof T];
|
|
75
|
+
type BuiltIn = keyof Model | keyof Debug<{}>;
|
|
76
|
+
/**
|
|
77
|
+
* Subset of `keyof T` which are not methods or defined by base Model U.
|
|
78
|
+
*
|
|
79
|
+
* **Note**: This excludes all keys which are not of type `string` (only those are managed).
|
|
80
|
+
*
|
|
81
|
+
* TODO: Should exclude methods
|
|
82
|
+
**/
|
|
83
|
+
export type Key<T> = Exclude<keyof T, BuiltIn> & string;
|
|
84
|
+
/**
|
|
85
|
+
* Including but not limited to `keyof T` which are not methods or defined by base Model.
|
|
86
|
+
**/
|
|
87
|
+
export type Event<T> = Extends<Key<T>>;
|
|
88
|
+
/** Object containing managed entries found in T. */
|
|
89
|
+
export type Entries<T> = {
|
|
90
|
+
[K in Key<T>]: T[K];
|
|
91
|
+
};
|
|
92
|
+
/** Object comperable to data found in T. */
|
|
93
|
+
export type Compat<T> = {
|
|
94
|
+
[K in Key<T>]?: T[K];
|
|
95
|
+
};
|
|
96
|
+
/** Actual value stored in state. */
|
|
97
|
+
export type Value<R> = R extends Ref<infer T> ? T : R;
|
|
98
|
+
/** Actual value belonging to a managed property. */
|
|
99
|
+
export type ValueOf<T extends {}, K> = K extends keyof T ? Value<T[K]> : undefined;
|
|
100
|
+
/**
|
|
101
|
+
* Values from current state of given controller.
|
|
102
|
+
*
|
|
103
|
+
* Differs from `Entries` as values here will drill into "real" values held by exotics like ref.
|
|
104
|
+
*/
|
|
105
|
+
export type Get<T, K extends Key<T> = Key<T>> = {
|
|
106
|
+
[P in K]: Value<T[P]>;
|
|
107
|
+
};
|
|
108
|
+
export type Export<T, E extends Model = Model> = {
|
|
109
|
+
[P in Key<T>]: Value<T[P]>;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
declare class Model {
|
|
113
|
+
/**
|
|
114
|
+
* Circular reference to `this` controller.
|
|
115
|
+
*
|
|
116
|
+
* Useful to obtain full reference where one has already destructured.
|
|
117
|
+
*/
|
|
118
|
+
is: this;
|
|
119
|
+
constructor();
|
|
120
|
+
on<P extends Model.Event<this>>(keys?: P | Iterable<P>, timeout?: number): Promise<P[]>;
|
|
121
|
+
on<P extends Model.Event<this>>(keys: P | Iterable<P>, listener: (this: this, keys: Model.Event<this>[]) => void, once?: boolean): Callback;
|
|
122
|
+
on(effect: Model.Effect<this>): Callback;
|
|
123
|
+
on(effect: Model.Effect<this>, watch?: []): Callback;
|
|
124
|
+
on(effect: Model.Effect<this>, watch?: Model.Event<this>[]): Callback;
|
|
125
|
+
on(key?: undefined, timeout?: number): Promise<Model.Event<this>[]>;
|
|
126
|
+
on(current?: boolean): Promise<Model.Event<this>[] | null>;
|
|
127
|
+
on(current: true): Promise<Model.Event<this>[]>;
|
|
128
|
+
on(current: null): Promise<null>;
|
|
129
|
+
get(): Model.Export<this>;
|
|
130
|
+
get<P extends Model.Key<this>>(property: P): this[P];
|
|
131
|
+
get<P extends Model.Key<this>>(property: P, timeout: number): Promise<this[P]>;
|
|
132
|
+
get<P extends Model.Key<this>>(property: P, onChange: true): Promise<this[P]>;
|
|
133
|
+
get<P extends Model.Key<this>>(property: P, onChange: boolean): this[P] | Promise<this[P]>;
|
|
134
|
+
get<P extends Model.Key<this>>(property: P, listener: (this: this, value: this[P], key: P[]) => void): Callback;
|
|
135
|
+
get<P extends Model.Key<this>>(select: Iterable<P>): Model.Get<this, P>;
|
|
136
|
+
get<P extends Model.Key<this>>(select: Iterable<P>, timeout: number): Promise<Model.Get<this, P>>;
|
|
137
|
+
get<P extends Model.Key<this>>(select: Iterable<P>, onChange: true): Promise<Model.Get<this, P>>;
|
|
138
|
+
get<P extends Model.Key<this>>(select: Iterable<P>, onChange: boolean): Model.Get<this, P> | Promise<Model.Get<this, P>>;
|
|
139
|
+
get<P extends Model.Key<this>>(select: Iterable<P>, listener: (this: this, value: Model.Get<this, P>, key: P[]) => void): Callback;
|
|
140
|
+
set(key: Model.Event<this>): PromiseLike<readonly Model.Event<this>[]>;
|
|
141
|
+
set<K extends Model.Event<this>>(key: Model.Event<this>, value: Model.ValueOf<this, K>): PromiseLike<readonly Model.Event<this>[]>;
|
|
142
|
+
set<T extends Model.Compat<this>>(source: T, select: (keyof T)[]): PromiseLike<(keyof T)[]>;
|
|
143
|
+
set<T extends Model.Compat<this>>(source: T, force?: boolean): PromiseLike<(keyof T)[]>;
|
|
144
|
+
/**
|
|
145
|
+
* Clean up side effects and mark this instance for garbage-collection.
|
|
146
|
+
*/
|
|
147
|
+
end(): void;
|
|
148
|
+
/**
|
|
149
|
+
* Creates a new instance of this controller.
|
|
150
|
+
*
|
|
151
|
+
* Beyond `new this(...)`, method will activate managed-state.
|
|
152
|
+
*
|
|
153
|
+
* @param args - arguments sent to constructor
|
|
154
|
+
*/
|
|
155
|
+
static new<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
|
|
156
|
+
/**
|
|
157
|
+
* Static equivalent of `x instanceof this`.
|
|
158
|
+
*
|
|
159
|
+
* Will determine if provided class is a subtype of this one.
|
|
160
|
+
*/
|
|
161
|
+
static isTypeof<T extends Model.Type>(this: T, maybe: any): maybe is T;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
declare namespace Control {
|
|
165
|
+
/**
|
|
166
|
+
* Called immediately when any key is changed or emitted.
|
|
167
|
+
* Returned callback is notified when update is complete.
|
|
168
|
+
*/
|
|
169
|
+
type OnSync<T = any> = (key: Model.Event<T> | null, source: Control) => OnAsync<T> | void;
|
|
170
|
+
/** Called at the end of an event frame with all keys affected. */
|
|
171
|
+
type OnAsync<T = any> = (keys: Model.Event<T>[]) => void;
|
|
172
|
+
type OnValue<T = any> = (this: T, value: any) => boolean | void;
|
|
173
|
+
}
|
|
174
|
+
declare class Control<T extends {} = any> {
|
|
175
|
+
subject: T;
|
|
176
|
+
state: Map<any, any>;
|
|
177
|
+
frame: Set<string>;
|
|
178
|
+
waiting: Set<Model.Control.OnAsync<T>>;
|
|
179
|
+
followers: Set<Model.Control.OnSync<any>>;
|
|
180
|
+
latest?: Model.Event<T>[];
|
|
181
|
+
constructor(subject: T);
|
|
182
|
+
subscribe(callback: Subscriber.OnEvent<T>): Model.Subscriber<T>;
|
|
183
|
+
watch(key: keyof T & string): void;
|
|
184
|
+
addListener(listener: Control.OnSync<T>): () => void;
|
|
185
|
+
ref<K extends Model.Key<T>>(key: K, handler?: (this: T, value: T[K]) => boolean | void): (value: any) => void;
|
|
186
|
+
update(key: Model.Key<T>): void;
|
|
187
|
+
clear(): void;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Property initializer, will run upon instance creation.
|
|
192
|
+
* Optional returned callback will run when once upon first access.
|
|
193
|
+
*/
|
|
194
|
+
declare type Instruction<T> = (this: Control, key: string, thisArg: Control) => Instruction.Getter<T> | Instruction.ExplicitDescriptor | Instruction.Descriptor<T> | Instruction.RecursiveDescriptor<T> | boolean | void;
|
|
195
|
+
declare namespace Instruction {
|
|
196
|
+
type Getter<T> = (within?: Subscriber) => T;
|
|
197
|
+
type Setter<T> = (value: T) => boolean | void;
|
|
198
|
+
type Runner<T> = (this: Control, key: string, on: Control) => Instruction.Descriptor<T> | boolean | undefined;
|
|
199
|
+
interface Descriptor<T> {
|
|
200
|
+
configurable?: boolean;
|
|
201
|
+
enumerable?: boolean;
|
|
202
|
+
value?: T;
|
|
203
|
+
writable?: boolean;
|
|
204
|
+
get?: Getter<T>;
|
|
205
|
+
set?: Setter<T> | false;
|
|
206
|
+
suspend?: boolean;
|
|
207
|
+
destroy?: () => void;
|
|
208
|
+
}
|
|
209
|
+
interface ExplicitDescriptor extends PropertyDescriptor {
|
|
210
|
+
explicit: true;
|
|
211
|
+
}
|
|
212
|
+
interface RecursiveDescriptor<T> {
|
|
213
|
+
recursive: true;
|
|
214
|
+
enumerable?: boolean;
|
|
215
|
+
value?: T;
|
|
216
|
+
set?: Setter<T> | false;
|
|
217
|
+
suspend?: boolean;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Run instruction as controller sets itself up.
|
|
222
|
+
* This will specialize the behavior of a given property.
|
|
223
|
+
*/
|
|
224
|
+
declare function add<T = any>(instruction: Instruction<any>, label?: string): T;
|
|
225
|
+
|
|
226
|
+
declare type Async<T = any> = (...args: any[]) => Promise<T>;
|
|
227
|
+
/**
|
|
228
|
+
* Sets an exotic method with managed ready-state. Property accepts an async function.
|
|
229
|
+
*
|
|
230
|
+
* When a run-method is invoked, its `active` property to true for duration of call.
|
|
231
|
+
* This is emitted as an update to property, both when called and after returns (or throws).
|
|
232
|
+
*
|
|
233
|
+
* **Note:** Subsequent calls will immediately throw if one is still pending.
|
|
234
|
+
*
|
|
235
|
+
* @param action - Action to fire when resulting property is invoked.
|
|
236
|
+
*/
|
|
237
|
+
declare function run(action: Async): typeof action & {
|
|
238
|
+
active: boolean;
|
|
239
|
+
};
|
|
240
|
+
declare function run<S>(action: Async<S>): typeof action & {
|
|
241
|
+
active: boolean;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
declare namespace get {
|
|
245
|
+
type Function<T, S = any> = (this: S, on: S) => T;
|
|
246
|
+
type Factory<T, S = unknown> = (this: S, key: string, thisArg: S) => T;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Implement a computed value; output will be generated by provided function.
|
|
250
|
+
*
|
|
251
|
+
* @param source - Source model from which computed value will be a subscriber.
|
|
252
|
+
* @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value. Will update automatically as input values change.
|
|
253
|
+
* @param suspend - Value will throw suspense when evaulating to undefined.
|
|
254
|
+
*/
|
|
255
|
+
declare function get<R, T>(source: T, compute: (this: T, on: T) => R, suspend: true): Exclude<R, undefined>;
|
|
256
|
+
declare function get<R, T>(source: T, compute: (this: T, on: T) => R, suspend?: boolean): R;
|
|
257
|
+
/**
|
|
258
|
+
* Implement a computed value; output is returned by function from provided factory.
|
|
259
|
+
*
|
|
260
|
+
* @param compute - Factory function to generate a getter to subscribe dependancies.
|
|
261
|
+
* @param suspend - Value will throw suspense when evaulating to undefined.
|
|
262
|
+
*/
|
|
263
|
+
declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R, suspend: true): Exclude<R, undefined>;
|
|
264
|
+
declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R, suspend?: boolean): R;
|
|
265
|
+
/**
|
|
266
|
+
* Set property with an async function.
|
|
267
|
+
*
|
|
268
|
+
* Property cannot be accessed until factory resolves, yeilding a result.
|
|
269
|
+
* If accessed while processing, React Suspense will be thrown.
|
|
270
|
+
*
|
|
271
|
+
* - `required: true` (default) -
|
|
272
|
+
* Run factory immediately upon creation of model instance.
|
|
273
|
+
* - `required: false` -
|
|
274
|
+
* Run factory only if/when accessed.
|
|
275
|
+
* Value will always throw suspense at least once - use with caution.
|
|
276
|
+
*
|
|
277
|
+
* @param factory - Callback run to derrive property value.
|
|
278
|
+
* @param required - (default: true) Run factory immediately on creation, otherwise on access.
|
|
279
|
+
*/
|
|
280
|
+
declare function get<T>(factory: get.Factory<Promise<T>>, required: false): T | undefined;
|
|
281
|
+
declare function get<T, S>(factory: get.Factory<Promise<T>, S>, required: false): T | undefined;
|
|
282
|
+
declare function get<T>(factory: get.Factory<Promise<T>>, required?: boolean): T;
|
|
283
|
+
declare function get<T, S>(factory: get.Factory<Promise<T>, S>, required?: boolean): T;
|
|
284
|
+
/**
|
|
285
|
+
* Set property with a factory function.
|
|
286
|
+
*
|
|
287
|
+
* - `required: true` (default) -
|
|
288
|
+
* Run factory immediately upon creation of model instance.
|
|
289
|
+
* - `required: false` -
|
|
290
|
+
* Run factory only if/when accessed.
|
|
291
|
+
* Value will always throw suspense at least once - use with caution.
|
|
292
|
+
*
|
|
293
|
+
* @param factory - Callback run to derrive property value.
|
|
294
|
+
* @param required - (default: true) Run factory immediately on creation, otherwise on access.
|
|
295
|
+
*/
|
|
296
|
+
declare function get<T>(factory: get.Factory<T>, required: false): T | undefined;
|
|
297
|
+
declare function get<T, S>(factory: get.Factory<T, S>, required: false): T | undefined;
|
|
298
|
+
declare function get<T>(factory: get.Factory<T>, required?: boolean): T;
|
|
299
|
+
declare function get<T, S>(factory: get.Factory<T, S>, required?: boolean): T;
|
|
300
|
+
/**
|
|
301
|
+
* Assign a property with result of a promise.
|
|
302
|
+
*/
|
|
303
|
+
declare function get<T>(factory: Promise<T>, required: false): T | undefined;
|
|
304
|
+
declare function get<T>(factory: Promise<T>, required?: boolean): T;
|
|
305
|
+
|
|
306
|
+
declare namespace ref {
|
|
307
|
+
type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<void> | void | boolean;
|
|
308
|
+
interface Object<T = any> {
|
|
309
|
+
(next: T): void;
|
|
310
|
+
current: T | null;
|
|
311
|
+
}
|
|
312
|
+
/** Object with references to all managed values of `T`. */
|
|
313
|
+
type Proxy<T extends Model> = {
|
|
314
|
+
[P in Model.Key<T>]-?: Model.Ref<T[P]>;
|
|
315
|
+
};
|
|
316
|
+
type CustomProxy<T extends Model, R> = {
|
|
317
|
+
[P in Model.Key<T>]-?: R;
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Creates an object with references to all managed values.
|
|
322
|
+
* Each property is a function to set value in state when invoked.
|
|
323
|
+
*
|
|
324
|
+
* *Properties are simultaneously a ref-function and ref-object, use as needed.*
|
|
325
|
+
*
|
|
326
|
+
* @param target - Source model from which to reference values.
|
|
327
|
+
*/
|
|
328
|
+
declare function ref<T extends Model>(target: T): ref.Proxy<T>;
|
|
329
|
+
/**
|
|
330
|
+
* Creates an object with values based on managed values.
|
|
331
|
+
* Each property will invoke mapper function on first access supply
|
|
332
|
+
* the its return value going forward.
|
|
333
|
+
*
|
|
334
|
+
* @param target - Source model from which to reference values.
|
|
335
|
+
* @param mapper - Function producing the placeholder value for any given property.
|
|
336
|
+
*/
|
|
337
|
+
declare function ref<O extends Model, R>(target: O, mapper: (key: Model.Key<O>) => R): ref.CustomProxy<O, R>;
|
|
338
|
+
/**
|
|
339
|
+
* Creates a ref-compatible property.
|
|
340
|
+
* Will persist value, and updates to this are made part of controller event-stream.
|
|
341
|
+
*
|
|
342
|
+
* *Output is simultaneously a ref-function and ref-object, use as needed.*
|
|
343
|
+
*
|
|
344
|
+
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
345
|
+
*/
|
|
346
|
+
declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>;
|
|
347
|
+
declare function ref<T, S>(callback?: ref.Callback<T, S>): ref.Object<T>;
|
|
348
|
+
|
|
349
|
+
declare namespace set {
|
|
350
|
+
type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<any> | void | boolean;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Set property with a placeholder.
|
|
354
|
+
*
|
|
355
|
+
* Property cannot be accessed until it is defined. If accessed while undefined, a hybrid
|
|
356
|
+
* `Promise`/`Error` (ala: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.
|
|
357
|
+
*/
|
|
358
|
+
declare function set<T = any>(): T;
|
|
359
|
+
declare function set<T>(onUpdate: set.Callback<T>): T;
|
|
360
|
+
declare function set<T, S>(onUpdate: set.Callback<T, S>): T;
|
|
361
|
+
declare function set<T>(value: undefined, onUpdate: set.Callback<T>): T;
|
|
362
|
+
declare function set<T, S>(value: undefined, onUpdate: set.Callback<T, S>): T;
|
|
363
|
+
declare function set<T>(value: T, onUpdate: set.Callback<T>): T;
|
|
364
|
+
declare function set<T, S>(value: T, onUpdate: set.Callback<T, S>): T;
|
|
365
|
+
|
|
366
|
+
declare 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;
|
|
367
|
+
declare type Managed<T> = Exclude<T, "forEach"> & {
|
|
368
|
+
from?: T;
|
|
369
|
+
forEach<R>(mapFunction: MapFunction<T, R>, thisArg?: any): Exclude<R, undefined>[] | void;
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
declare function use<K = any>(initial: SetConstructor): Managed<Set<K>>;
|
|
373
|
+
declare function use<K = any, V = any>(initial: MapConstructor): Managed<Map<K, V>>;
|
|
374
|
+
declare function use(initial: Set<unknown>): Managed<Set<any>>;
|
|
375
|
+
declare function use(initial: Map<unknown, unknown>): Managed<Map<any, any>>;
|
|
376
|
+
declare function use(from: () => Set<unknown>): Managed<Set<any>>;
|
|
377
|
+
declare function use(from: () => Map<unknown, unknown>): Managed<Map<any, any>>;
|
|
378
|
+
declare function use<T extends Set<any> | Map<any, any>>(initial: T): Managed<T>;
|
|
379
|
+
declare function use<T extends Set<any> | Map<any, any>>(from: () => T): Managed<T>;
|
|
380
|
+
/**
|
|
381
|
+
* Create a placeholder for specified Model type.
|
|
382
|
+
*/
|
|
383
|
+
declare function use<T extends Model>(): T | undefined;
|
|
384
|
+
declare function use<T extends Model>(from: () => T, required: false): T | undefined;
|
|
385
|
+
declare function use<T extends Model>(from: () => T, required: boolean): T;
|
|
386
|
+
/**
|
|
387
|
+
* Create a new child instance of model.
|
|
388
|
+
*/
|
|
389
|
+
declare function use<T extends Model>(Type: Model.Type<T>, callback?: (i: T) => void): T;
|
|
390
|
+
/**
|
|
391
|
+
* Create a managed child from factory function.
|
|
392
|
+
*/
|
|
393
|
+
declare function use<T extends {}>(from: () => Promise<T>, required: false): T | undefined;
|
|
394
|
+
declare function use<T extends {}>(from: () => Promise<T>, required: boolean): T;
|
|
395
|
+
declare function use<T extends {}>(from: () => Promise<T>, callback?: (i: T) => void): T;
|
|
396
|
+
declare function use<T extends {}>(from: () => T, callback?: (i: T) => void): T;
|
|
397
|
+
/**
|
|
398
|
+
* Create child-instance relationship with provided model.
|
|
399
|
+
*
|
|
400
|
+
* Note: If `peer` is not already initialized before parent is
|
|
401
|
+
* (created with `new` as opposed to create method), that model will
|
|
402
|
+
* attach this via `parent()` instruction. It will not, however, if
|
|
403
|
+
* already active.
|
|
404
|
+
*/
|
|
405
|
+
declare function use<T extends {}>(peer: T, callback?: (i: T) => void): T;
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Fetches and assigns the controller which spawned this host.
|
|
409
|
+
* When parent assigns an instance via `use()` directive, it
|
|
410
|
+
* will be made available to child upon this request.
|
|
411
|
+
*
|
|
412
|
+
* @param Type - Type of controller compatible with this class.
|
|
413
|
+
* @param required - Throw if controller is created independantly.
|
|
414
|
+
*/
|
|
415
|
+
declare function has<T extends Model>(Type: Model.Type<T>, required: false): T | undefined;
|
|
416
|
+
declare function has<T extends Model>(Type: Model.Type<T>, required?: true): T;
|
|
417
|
+
|
|
418
|
+
declare namespace MVC {
|
|
419
|
+
type EffectCallback<T = MVC> = (found: T) => Callback | void;
|
|
420
|
+
type ForceUpdate = {
|
|
421
|
+
(): void;
|
|
422
|
+
<T = void>(passthru?: Promise<T>): Promise<T>;
|
|
423
|
+
};
|
|
424
|
+
type TapCallback<T extends Model, R> = (this: T, model: T, update: MVC.ForceUpdate) => R;
|
|
425
|
+
}
|
|
426
|
+
declare class MVC extends Model {
|
|
427
|
+
end(force?: boolean): boolean | void;
|
|
428
|
+
static global?: boolean;
|
|
429
|
+
static keepAlive?: boolean;
|
|
430
|
+
/**
|
|
431
|
+
* Create a new instance of this model and activate its managed state.
|
|
432
|
+
*
|
|
433
|
+
* @param args - arguments sent to constructor
|
|
434
|
+
*/
|
|
435
|
+
static new<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
|
|
436
|
+
/**
|
|
437
|
+
* **React Hook** - Fetch most instance of this controller from context, if it exists.
|
|
438
|
+
*
|
|
439
|
+
* @param required - Unless false, will throw where instance cannot be found.
|
|
440
|
+
*/
|
|
441
|
+
static get<T extends MVC>(this: Model.Type<T>, required?: boolean, effectCallback?: MVC.EffectCallback<T>): T;
|
|
442
|
+
/**
|
|
443
|
+
* **React Hook** - Fetch most instance of this controller from context.
|
|
444
|
+
*
|
|
445
|
+
* @param required - If false, may return undefined.
|
|
446
|
+
*/
|
|
447
|
+
static get<T extends MVC>(this: Model.Type<T>, required: false, effectCallback?: MVC.EffectCallback<T | undefined>): T | undefined;
|
|
448
|
+
/**
|
|
449
|
+
* **React Hook** - Fetch instance.
|
|
450
|
+
*
|
|
451
|
+
* Effect callback will run once if found, throw if not found.
|
|
452
|
+
* Returned function is called on unmount.
|
|
453
|
+
*/
|
|
454
|
+
static get<I extends MVC>(this: Model.Type<I>, effectCallback: MVC.EffectCallback<I>): I | undefined;
|
|
455
|
+
/**
|
|
456
|
+
* **React Hook** - Fetch specific value from instance of this controller in context.
|
|
457
|
+
*/
|
|
458
|
+
static get<I extends MVC, K extends Model.Key<I>>(this: Model.Type<I>, key: K): I[K];
|
|
459
|
+
/**
|
|
460
|
+
* **React Hook** - Fetch and subscribe to instance of this controller within ambient component.
|
|
461
|
+
*/
|
|
462
|
+
static tap<T extends MVC>(this: Model.Type<T>): T;
|
|
463
|
+
static tap<T extends MVC, K extends Model.Key<T>>(this: Model.Type<T>, key: K, expect: true): Exclude<T[K], undefined>;
|
|
464
|
+
static tap<T extends MVC, K extends Model.Key<T>>(this: Model.Type<T>, key: K, expect?: boolean): NoVoid<T[K]>;
|
|
465
|
+
static tap<T extends MVC, R>(this: Model.Type<T>, connect: MVC.TapCallback<T, () => R>): NoVoid<R>;
|
|
466
|
+
static tap<T extends MVC, R>(this: Model.Type<T>, connect: MVC.TapCallback<T, (() => R) | null>): NoVoid<R> | null;
|
|
467
|
+
static tap<T extends MVC, R>(this: Model.Type<T>, compute: MVC.TapCallback<T, Promise<R> | R>, expect: true): Exclude<R, undefined>;
|
|
468
|
+
static tap<T extends MVC, R>(this: Model.Type<T>, compute: MVC.TapCallback<T, Promise<R>>, expect?: boolean): NoVoid<R> | null;
|
|
469
|
+
static tap<T extends MVC, R>(this: Model.Type<T>, compute: MVC.TapCallback<T, R>, expect?: boolean): NoVoid<R>;
|
|
470
|
+
static use<I extends MVC>(this: Model.Type<I>, watch: Model.Key<I>[], callback?: (instance: I) => void): I;
|
|
471
|
+
static use<I extends MVC>(this: Model.Type<I>, callback?: (instance: I) => void): I;
|
|
472
|
+
static use<I extends MVC>(this: Model.Type<I>, apply: Model.Compat<I>, keys?: Model.Event<I>[]): I;
|
|
473
|
+
static meta<T extends Class>(this: T): T;
|
|
474
|
+
static meta<T extends Class, K extends keyof T>(this: T, key: K, expect: true): Exclude<T[K], undefined>;
|
|
475
|
+
static meta<T extends Class, K extends keyof T>(this: T, key: K, expect?: boolean): T[K];
|
|
476
|
+
static meta<T, M extends Class>(this: M, from: (this: M, state: M) => Promise<T>, expect: true): Exclude<T, undefined>;
|
|
477
|
+
static meta<T, M extends Class>(this: M, from: (this: M, state: M) => Promise<T>, expect?: boolean): T;
|
|
478
|
+
static meta<T, M extends Class>(this: M, from: (this: M, state: M) => T, expect: true): Exclude<T, undefined>;
|
|
479
|
+
static meta<T, M extends Class>(this: M, from: (this: M, state: M) => T, expect?: boolean): T;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
declare function useContext<T extends Model>(Type: Model.Type<T>, required: false): T | undefined;
|
|
483
|
+
declare function useContext<T extends Model>(Type: Model.Type<T>, required?: boolean): T;
|
|
484
|
+
|
|
485
|
+
declare function useNew<T extends Model>(source: Model.Type<T> | (() => T), callback?: (instance: T) => void): T;
|
|
486
|
+
declare function useNew<T extends Model>(source: Model.Type<T> | (() => T), watch: Model.Key<T>[], callback?: (instance: T) => void): T;
|
|
487
|
+
declare function useNew<T extends Model>(source: Model.Type<T> | (() => T), apply: Model.Compat<T>, keys?: Model.Event<T>[]): T;
|
|
488
|
+
|
|
489
|
+
declare namespace useTap {
|
|
490
|
+
type Source<T extends Model> = T | Model.Type<T> | (() => T | Model.Type<T>);
|
|
491
|
+
type Callback<T extends Model, R> = (this: T, model: T, update: MVC.ForceUpdate) => R;
|
|
492
|
+
}
|
|
493
|
+
declare function useTap<T extends Model>(source: useTap.Source<T>): T;
|
|
494
|
+
declare function useTap<T extends Model, K extends Model.Key<T>>(source: useTap.Source<T>, path: K, expect: true): Exclude<T[K], undefined>;
|
|
495
|
+
declare function useTap<T extends Model, K extends Model.Key<T>>(source: useTap.Source<T>, path: K, expect?: boolean): NoVoid<T[K]>;
|
|
496
|
+
declare function useTap<T extends Model, R>(source: useTap.Source<T>, connect: useTap.Callback<T, () => R>): NoVoid<R>;
|
|
497
|
+
declare function useTap<T extends Model, R>(source: useTap.Source<T>, connect: useTap.Callback<T, (() => R) | null>): NoVoid<R> | null;
|
|
498
|
+
declare function useTap<T extends Model, R>(source: useTap.Source<T>, compute: useTap.Callback<T, Promise<R> | R>, expect: true): Exclude<R, undefined>;
|
|
499
|
+
declare function useTap<T extends Model, R>(source: useTap.Source<T>, compute: useTap.Callback<T, Promise<R>>, expect?: boolean): NoVoid<R> | null;
|
|
500
|
+
declare function useTap<T extends Model, R>(source: useTap.Source<T>, compute: useTap.Callback<T, R>, expect?: boolean): NoVoid<R>;
|
|
501
|
+
|
|
502
|
+
declare namespace Consumer {
|
|
503
|
+
type HasProps<E extends Class> = {
|
|
504
|
+
/** @deprecated - use for instead */
|
|
505
|
+
of?: never;
|
|
506
|
+
/** Type of controller to fetch from context. */
|
|
507
|
+
for: E;
|
|
508
|
+
/**
|
|
509
|
+
* Getter function. Is called on every natural render of this component.
|
|
510
|
+
* Will throw if usable instance cannot be found in context.
|
|
511
|
+
*/
|
|
512
|
+
has: (value: InstanceType<E>) => void;
|
|
513
|
+
};
|
|
514
|
+
type GetProps<E extends Class> = {
|
|
515
|
+
/** @deprecated - use for instead */
|
|
516
|
+
of?: never;
|
|
517
|
+
/** Type of controller to fetch from context. */
|
|
518
|
+
for: E;
|
|
519
|
+
/** Getter function. Is called on every natural render of this component. */
|
|
520
|
+
get: (value: InstanceType<E> | undefined) => void;
|
|
521
|
+
};
|
|
522
|
+
type RenderProps<E extends Class> = {
|
|
523
|
+
/** @deprecated - use for instead */
|
|
524
|
+
of?: never;
|
|
525
|
+
/** Type of controller to fetch from context. */
|
|
526
|
+
for: E;
|
|
527
|
+
/**
|
|
528
|
+
* Render function, will receive instance of desired controller.
|
|
529
|
+
*
|
|
530
|
+
* Similar to `tap()`, updates to properties accessed in
|
|
531
|
+
* this function will cause a refresh when they change.
|
|
532
|
+
*/
|
|
533
|
+
children: (value: InstanceType<E>) => React.ReactElement<any, any> | null;
|
|
534
|
+
};
|
|
535
|
+
type Props<T extends Class> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
536
|
+
}
|
|
537
|
+
declare function Consumer<T extends Class>(props: Consumer.Props<T>): any;
|
|
538
|
+
|
|
539
|
+
declare namespace Provider {
|
|
540
|
+
type Item = Model | Model.Type;
|
|
541
|
+
type Multiple<T extends Item = Item> = T[] | {
|
|
542
|
+
[key: string]: T;
|
|
543
|
+
};
|
|
544
|
+
type Instance<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
|
|
545
|
+
type NormalProps<E, I = Instance<E>> = {
|
|
546
|
+
for: E;
|
|
547
|
+
fallback?: React.ReactNode;
|
|
548
|
+
children?: React.ReactNode | ((instance: I) => React.ReactNode);
|
|
549
|
+
and?: Model.Compat<I>;
|
|
550
|
+
};
|
|
551
|
+
type MultipleProps<T extends Item> = {
|
|
552
|
+
for: Multiple<T>;
|
|
553
|
+
fallback?: React.ReactNode;
|
|
554
|
+
children?: React.ReactNode;
|
|
555
|
+
and?: Model.Compat<Instance<T>>;
|
|
556
|
+
};
|
|
557
|
+
type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
|
|
558
|
+
}
|
|
559
|
+
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): JSX.Element;
|
|
560
|
+
|
|
561
|
+
declare class Global extends MVC {
|
|
562
|
+
static global: boolean;
|
|
563
|
+
static keepAlive: boolean;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
declare namespace tap {
|
|
567
|
+
type Callback<T extends Model> = (instance: T | undefined) => void | boolean;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Find and attach most applicable instance of Model via context.
|
|
571
|
+
*
|
|
572
|
+
* Host controller will search element-hierarchy relative to where it spawned.
|
|
573
|
+
*
|
|
574
|
+
* @param Type - Type of model to find from context
|
|
575
|
+
* @param callback -
|
|
576
|
+
* - Invoked after context is scanned, is passed result - either found or undefined.
|
|
577
|
+
* - If argument is inadequate, but required, your implemention should simply throw.
|
|
578
|
+
* - If inadequate and not required, conditionally return false.
|
|
579
|
+
*/
|
|
580
|
+
declare function tap<T extends Model>(Type: Model.Type<T>, callback?: (instance?: T) => void | true): T;
|
|
581
|
+
declare function tap<T extends Model>(Type: Model.Type<T>, callback?: (instance?: T) => void | boolean): T | undefined;
|
|
582
|
+
/**
|
|
583
|
+
* Find and attach most applicable instance of Model via context.
|
|
584
|
+
*
|
|
585
|
+
* Expects a `<Provider>` of target controller to exist.
|
|
586
|
+
* Host controller will search element-hierarchy relative to where it spawned.
|
|
587
|
+
*
|
|
588
|
+
* @param Type - Type of controller to attach to property.
|
|
589
|
+
* @param required - Throw if instance of Type cannot be found.
|
|
590
|
+
*/
|
|
591
|
+
declare function tap<T extends Model>(Type: Model.Type<T>, required: true): T;
|
|
592
|
+
declare function tap<T extends Model>(Type: Model.Type<T>, required?: boolean): T | undefined;
|
|
593
|
+
|
|
594
|
+
export { Consumer, Debug, Global, MVC, MVC as Model, Provider, add, MVC as default, get, has, ref, run, set, tap, use, useContext, useNew, useTap };
|