@expressive/mvc 0.33.0 → 0.35.0-alpha.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 DELETED
@@ -1,411 +0,0 @@
1
- type Async<T = any> = (...args: any[]) => Promise<T>;
2
- /**
3
- * Sets an exotic method with managed ready-state. Property accepts an async function.
4
- *
5
- * When a run-method is invoked, its `active` property to true for duration of call.
6
- * This is emitted as an update to property, both when called and after returns (or throws).
7
- *
8
- * **Note:** Subsequent calls will immediately throw if one is still pending.
9
- *
10
- * @param action - Action to fire when resulting property is invoked.
11
- */
12
- declare function run(action: Async): typeof action & {
13
- active: boolean;
14
- };
15
- declare function run<S>(action: Async<S>): typeof action & {
16
- active: boolean;
17
- };
18
-
19
- /** Type may not be undefined - instead will be null. */
20
- type NoVoid<T> = T extends undefined | void ? null : T;
21
- type ForceUpdate = {
22
- /** Force an update in current component. */
23
- (): void;
24
- /**
25
- * Force an update and again after promise either resolves or rejects.
26
- * Will return a duplicate of given Promise, which resolves after refresh.
27
- */
28
- <T = void>(passthru: Promise<T>): Promise<T>;
29
- /**
30
- * Force a update while calling async function.
31
- * A refresh will occur both before and after given function.
32
- * Any actions performed before first `await` will occur before refresh!
33
- */
34
- <T = void>(invoke: () => Promise<T>): Promise<T>;
35
- };
36
- type Factory<T extends Model, R> = (this: T, current: T, update: ForceUpdate) => R;
37
- /** Fetch instance of this class from context. */
38
- declare function get$1<T extends Model>(this: Model.Type<T>, ignoreUpdates?: true): Model.Focus<T>;
39
- /** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
40
- declare function get$1<T extends Model>(this: Model.Type<T>, required: boolean): Model.Focus<T> | undefined;
41
- declare function get$1<T extends Model, R>(this: Model.Type<T>, factory: Factory<T, (() => R) | R | Promise<R>>): NoVoid<R>;
42
- declare function get$1<T extends Model, R>(this: Model.Type<T>, factory: Factory<T, (() => R) | null>): NoVoid<R> | null;
43
-
44
- declare function use$1<T extends Model>(this: Model.New<T>, callback?: (instance: T) => void, repeat?: boolean): Model.Focus<T>;
45
- declare function use$1<T extends Model>(this: Model.New<T>, apply?: Model.Values<T>, repeat?: boolean): Model.Focus<T>;
46
-
47
- type Callback = () => void;
48
-
49
- declare global {
50
- namespace jest {
51
- interface Matchers<R> {
52
- /** Assert model does have one or more updates pending. */
53
- toUpdate(): Promise<R>;
54
-
55
- /** Assert model did update with keys specified. */
56
- toHaveUpdated<R>(keys: string[]): Promise<R>;
57
- }
58
- }
59
- }
60
-
61
- type SelectOne<T, K extends Model.Key<T>> = K;
62
- type SelectFew<T, K extends Model.Key<T>> = K[];
63
- type Select<T, K extends Model.Key<T> = Model.Key<T>> = K | K[];
64
- type Export<T, S> = S extends SelectOne<T, infer P> ? T[P] : S extends SelectFew<T, infer P> ? Model.Export<T, P> : never;
65
- type OnUpdate<T, S> = (this: T, value: Export<T, S>, updated: Model.Event<T>[]) => void;
66
- interface Observable {
67
- get(): Model.Export<this>;
68
- get<P extends Select<this>>(select: P): Export<this, P>;
69
- get<P extends Select<this>>(select: P, onUpdate: OnUpdate<this, P>, once?: true): Callback;
70
- get<P extends Select<this>>(select: P, onUpdate: OnUpdate<this, P>, initial?: false): Callback;
71
- get(effect: Model.Effect<this>): Callback;
72
- set(): Promise<Model.Event<this>[]>;
73
- set(timeout: number): Promise<Model.Event<this>[] | false>;
74
- set<T extends Model.Values<this>>(from: T, only?: (keyof T)[]): Promise<Model.Event<T>[] | false>;
75
- set<K extends Model.Key<this>>(key: K, value: Model.Value<this[K]>): Promise<Model.Event<this>[] | false>;
76
- set<K extends Model.Event<this>>(key: K): Promise<Model.Event<this>[] | false>;
77
- }
78
-
79
- type InstanceOf<T> = T extends {
80
- prototype: infer U;
81
- } ? U : never;
82
- declare namespace Model {
83
- /** Any typeof Model, using class constructor as the reference. */
84
- type Type<T extends Model = Model> = abstract new (...args: any[]) => T;
85
- /** A typeof Model, specifically one which can be created without any arguments. */
86
- type New<T extends Model = Model> = (new () => T) & typeof Model;
87
- /** A callback function which is subscribed to parent and updates when values change. */
88
- type Effect<T> = (this: T, argument: T) => Callback | Promise<void> | void;
89
- /** Subset of `keyof T` which are not methods or defined by base Model U. **/
90
- type Key<T, U = Observable> = Extract<Exclude<keyof T, keyof U>, string>;
91
- /** Including but not limited to `keyof T` which are not methods or defined by base Model. */
92
- type Event<T> = Key<T> | (string & {});
93
- /** Object comperable to data found in T. */
94
- type Values<T> = {
95
- [P in Key<T>]?: Value<T[P]>;
96
- };
97
- /**
98
- * Values from current state of given controller.
99
- * Differs from `Values` as values here will drill into "real" values held by exotics like ref.
100
- */
101
- type Export<T, K extends Key<T> = Key<T>> = {
102
- [P in K]: Value<T[P]>;
103
- };
104
- /**
105
- * Reference to `this` without a subscription.
106
- * Use to obtain full reference from a destructure.
107
- */
108
- type Focus<T> = T & {
109
- is: T;
110
- };
111
- /** Exotic value, actual value is contained. */
112
- type Ref<T = any> = {
113
- (next: T): void;
114
- current: T | null;
115
- };
116
- /** Actual value stored in state. */
117
- type Value<R> = R extends Ref<infer T> ? T : R extends Observable ? Export<R> : R;
118
- }
119
- interface Model extends Observable {
120
- }
121
- declare class Model {
122
- constructor(id?: string | number);
123
- /** Mark this instance for garbage collection. */
124
- null(): void;
125
- static get: typeof get$1;
126
- static use: typeof use$1;
127
- /**
128
- * Creates a new instance of this controller.
129
- *
130
- * Beyond `new this(...)`, method will activate managed-state.
131
- *
132
- * @param args - arguments sent to constructor
133
- */
134
- static new<T extends new (...args: any[]) => any>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
135
- /**
136
- * Static equivalent of `x instanceof this`.
137
- *
138
- * Will determine if provided class is a subtype of this one.
139
- */
140
- static is<T extends Model.Type>(this: T, maybe: any): maybe is T;
141
- }
142
-
143
- declare namespace get {
144
- type Function<T, S = any> = (this: S, on: S) => T;
145
- type Factory<R, T> = (this: T, property: string, on: T) => Function<R, T>;
146
- type Source<T extends Model = Model> = (callback: (x: T) => void) => void;
147
- }
148
- /**
149
- * Fetches and assigns the controller which spawned this host.
150
- * When parent assigns an instance via `use()` directive, it
151
- * will be made available to child upon this request.
152
- *
153
- * @param Type - Type of controller compatible with this class.
154
- * @param required - Throw if controller is created independantly.
155
- */
156
- declare function get<T extends Model>(Type: Model.Type<T>, required?: true): T;
157
- declare function get<T extends Model>(Type: Model.Type<T>, required: boolean): T | undefined;
158
- declare function get<R, T extends Model>(Type: Model.Type<T>, compute: (this: T, on: T) => R): Exclude<R, undefined>;
159
- declare function get<R, T extends Model>(Type: Model.Type<T>, compute: (this: T, on: T) => R): R;
160
- /**
161
- * Implement a computed value; output will be generated by provided function.
162
- *
163
- * @param source - Source model from which computed value will be a subscriber.
164
- * @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value. Will update automatically as input values change.
165
- */
166
- declare function get<R, T extends Model>(source: T, compute: (this: T, on: T) => R): Exclude<R, undefined>;
167
- declare function get<R, T extends Model>(source: T, compute: (this: T, on: T) => R): R;
168
- /**
169
- * Implement a computed value; output is returned by function from provided factory.
170
- *
171
- * @param compute - Factory function to generate a getter to subscribe dependancies.
172
- */
173
- declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R): Exclude<R, undefined>;
174
- declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R): R;
175
-
176
- declare namespace ref {
177
- type Callback<T> = (argument: T) => ((next: T | null) => void) | Promise<void> | void | boolean;
178
- interface Object<T = any> {
179
- (next: T | null): void;
180
- current: T | null;
181
- }
182
- /** Object with references to all managed values of `T`. */
183
- type Proxy<T extends Model> = {
184
- [P in Model.Key<T>]-?: Model.Ref<T[P]>;
185
- };
186
- type CustomProxy<T extends Model, R> = {
187
- [P in Model.Key<T>]-?: R;
188
- };
189
- }
190
- /**
191
- * Creates an object with references to all managed values.
192
- * Each property is a function to set value in state when invoked.
193
- *
194
- * *Properties are simultaneously a ref-function and ref-object, use as needed.*
195
- *
196
- * @param target - Source model from which to reference values.
197
- */
198
- declare function ref<T extends Model>(target: T): ref.Proxy<T>;
199
- /**
200
- * Creates an object with values based on managed values.
201
- * Each property will invoke mapper function on first access supply
202
- * the its return value going forward.
203
- *
204
- * @param target - Source model from which to reference values.
205
- * @param mapper - Function producing the placeholder value for any given property.
206
- */
207
- declare function ref<O extends Model, R>(target: O, mapper: (key: Model.Key<O>) => R): ref.CustomProxy<O, R>;
208
- /**
209
- * Creates a ref-compatible property.
210
- * Will persist value, and updates to this are made part of controller event-stream.
211
- *
212
- * *Output is simultaneously a ref-function and ref-object, use as needed.*
213
- *
214
- * @param callback - Optional callback to synchronously fire when reference is first set or does update.
215
- */
216
- declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>;
217
- /**
218
- * Creates a ref-compatible property.
219
- * Will persist value, and updates to this are made part of controller event-stream.
220
- *
221
- * *Output is simultaneously a ref-function and ref-object, use as needed.*
222
- *
223
- * @param callback - Optional callback to synchronously fire when reference is first set or does update.
224
- * @param ignoreNull - Default `true`. If `false`, will still callback with `null`.
225
- */
226
- declare function ref<T = HTMLElement>(callback: ref.Callback<T | null>, ignoreNull: boolean): ref.Object<T>;
227
-
228
- declare namespace set {
229
- type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<any> | void | boolean;
230
- type Factory<T, S = any> = (this: S, key: string, thisArg: S) => Promise<T> | T;
231
- }
232
- /**
233
- * Set property with a placeholder.
234
- *
235
- * Property cannot be accessed until it is defined. If accessed while undefined, a hybrid
236
- * `Promise`/`Error` (ala: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.
237
- */
238
- declare function set<T = any>(): T;
239
- /**
240
- * Set property with a factory function.
241
- *
242
- * If async, poperty cannot be accessed until resolves, yeilding a result.
243
- * If accessed while still processing, React Suspense will be thrown.
244
- *
245
- * - `required: true` (default) -
246
- * Run factory immediately upon creation of model instance.
247
- * - `required: false` -
248
- * Run factory only if/when accessed.
249
- * Value will always throw suspense at least once - use with caution.
250
- *
251
- * @param factory - Callback run to derrive property value.
252
- * @param required - (default: true) Run factory immediately on creation, otherwise on access.
253
- */
254
- declare function set<T>(factory: set.Factory<T> | Promise<T>, required: false): T | undefined;
255
- declare function set<T>(factory: set.Factory<T> | Promise<T>, required?: boolean): T;
256
- declare function set<T>(value: undefined, onUpdate: set.Callback<T>): T;
257
- declare function set<T>(value: T, onUpdate: set.Callback<T>): T;
258
-
259
- type Empty = Record<string, never>;
260
- declare namespace use {
261
- type Record<T> = {
262
- [key: string | number]: T;
263
- } & Observable;
264
- type Object<T extends {}> = T & Observable;
265
- }
266
- /** Create a placeholder for specified Model type. */
267
- declare function use<T extends Model>(): T | undefined;
268
- /** Create a new child instance of model. */
269
- declare function use<T extends Model>(Type: Model.New<T>, ready?: (i: T) => void): T;
270
- /**
271
- * Use existing model as a child of model assigned to.
272
- *
273
- * Note: If `peer` is not already initialized before parent is
274
- * (created with `new` as opposed to create method), that model will
275
- * attach this via `parent()` instruction. It will not, however, if
276
- * already active.
277
- **/
278
- declare function use<T extends Model>(model: T, ready?: (i: T) => void): T;
279
- /** Create a managed record with observable entries. */
280
- declare function use<T = any, C = use.Record<T>>(record: Empty, ready?: (object: C) => void): C;
281
- /** Create a managed object with observable entries. */
282
- declare function use<T extends {}, O = use.Object<T>>(data: T, ready?: (object: O) => void): O;
283
-
284
- declare namespace Context {
285
- type Inputs = {
286
- [key: string | number]: Model | Model.New;
287
- };
288
- }
289
- declare class Context {
290
- key: string;
291
- private table;
292
- private layer;
293
- private has;
294
- get<T extends Model>(Type: Model.Type<T>): T | undefined;
295
- include(inputs: Context.Inputs): Map<Model, boolean>;
296
- add<T extends Model>(input: T | Model.New<T>, implicit?: boolean): T;
297
- push(): this;
298
- pop(): void;
299
- }
300
-
301
- type Observer<T extends Model = any> = (key: Model.Event<T> | null | undefined, source: Control) => Callback | null | void;
302
- declare namespace Control {
303
- /**
304
- * Property initializer, will run upon instance creation.
305
- * Optional returned callback will run when once upon first access.
306
- */
307
- type Instruction<T> = (this: Control, key: string, thisArg: Control) => PropertyDescriptor<T> | Getter<T> | void;
308
- type Getter<T> = (source: Model) => T;
309
- type Setter<T> = (value: T) => boolean | void;
310
- type PropertyDescriptor<T = any> = {
311
- get?: Getter<T>;
312
- set?: Setter<T> | false;
313
- enumerable?: boolean;
314
- value?: T;
315
- };
316
- type Callback = (control: Control) => void;
317
- /**
318
- * Callback for Controller.for() static method.
319
- * Returned callback is forwarded.
320
- */
321
- type OnReady<T extends {}> = (control: Control<T>) => (() => void) | void;
322
- type RequestRefresh = (update: (tick: number) => number) => void;
323
- type GetContext = (target: Model) => (callback: (got: Context) => void) => void;
324
- type GetAdapter<T> = (refresh: RequestRefresh, context: Context) => {
325
- mount: () => (() => void) | void;
326
- render: () => T;
327
- } | void;
328
- type GetHook = <T>(adapter: GetAdapter<T>) => T | null;
329
- type UseAdapter<T extends Model, R> = (refresh: RequestRefresh) => {
330
- instance: T;
331
- mount: () => (() => void) | void;
332
- render: R;
333
- };
334
- type UseHook = <T extends Model, R>(adapter: UseAdapter<T, R>) => R;
335
- }
336
- declare class Control<T extends {} = any> {
337
- static ready: Set<Control.Callback>;
338
- static after: Set<Callback>;
339
- static before: Set<Callback>;
340
- static pending: Set<Callback>;
341
- static get: Control.GetHook;
342
- static use: Control.UseHook;
343
- static has: Control.GetContext;
344
- static for: typeof control;
345
- static apply: typeof apply;
346
- static watch: typeof watch;
347
- id: string | number | false;
348
- subject: T;
349
- state: {
350
- [key: string]: any;
351
- };
352
- latest?: Model.Event<T>[];
353
- frame: Set<string>;
354
- followers: Set<Observer<any>>;
355
- observers: Map<string, Set<Observer<any>>>;
356
- constructor(subject: T, id: string | number | false);
357
- add(key: string): void;
358
- watch(key: string, output: Control.PropertyDescriptor<any>): void;
359
- ref<K extends Model.Key<T>>(key: K, callback?: (this: T, value: T[K]) => boolean | void): (value: any) => void;
360
- update(key: string): void;
361
- addListener(fn: Observer): () => boolean;
362
- clear(): void;
363
- }
364
- declare function control<T extends Model>(subject: T, ready: Control.OnReady<T>): Callback;
365
- declare function control<T extends Model>(subject: T, ready?: boolean): Control<T>;
366
- type Focus<T extends {}> = T & {
367
- is: T;
368
- };
369
- declare function watch<T extends {}>(value: T, cb: Observer): Focus<T>;
370
- declare function apply<T = any>(instruction: Control.Instruction<T>): T;
371
-
372
- declare const STATE = "__state";
373
- declare const UPDATE = "__update";
374
- declare const CONTROL = "__control";
375
- declare const PARENT = "__parent";
376
- declare const Debug: {
377
- /** Use to access snapshot of Model's current state. */
378
- STATE: "__state";
379
- /**
380
- * Use to access snapshot of Model's latest update.
381
- */
382
- UPDATE: "__update";
383
- /** Use to access a Model's controller. */
384
- CONTROL: "__control";
385
- /** Use to access a Model's parent, if exists. */
386
- PARENT: "__parent";
387
- };
388
- /**
389
- * Generic will declare debug properties on a given Model.
390
- *
391
- * These properties always exist at runtime, however are hidden unless you cast your Model as `Debug<T>`.
392
- */
393
- type Debug<T extends Model = Model> = T & {
394
- /** Controller for this instance. */
395
- [CONTROL]?: Control<T>;
396
- /** Current state of this instance. */
397
- [STATE]?: Model.Export<T>;
398
- /**
399
- * Last update causing a refresh to subscribers.
400
- *
401
- * If accessed directly, will contain all keys from last push.
402
- * If within a subscribed function, will contain only keys which explicitly caused a refresh.
403
- */
404
- [UPDATE]?: readonly Model.Event<T>[];
405
- /**
406
- * Parent currently assigned to parent. Usually the model which this one is a property of.
407
- */
408
- [PARENT]?: Model;
409
- };
410
-
411
- export { Context, Control, Debug, Model, Model as default, get, ref, run, set, use };