@expressive/mvc 0.4.7 → 0.6.1

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
@@ -1,25 +1,5 @@
1
1
  import React from 'react';
2
2
 
3
- declare type Async<T = any> = (...args: any[]) => Promise<T>;
4
- /**
5
- * Sets an exotic method with managed ready-state. Property accepts an async function.
6
- *
7
- * When an act-method is invoked, its `active` property to true for duration of call.
8
- * This is emitted as an update to property, both when called and after returns (or throws).
9
- *
10
- * **Note:** Subsequent calls will immediately throw if one is still pending.
11
- *
12
- * @param action - Action to fire when resulting property is invoked.
13
- */
14
- declare function act(action: Async): typeof action & {
15
- active: boolean;
16
- };
17
- declare function act<S>(action: Async<S>): typeof action & {
18
- active: boolean;
19
- };
20
-
21
- declare function array<T = any>(): T[];
22
-
23
3
  type Callback = () => void;
24
4
  type Class = new (...args: any[]) => any;
25
5
  type InstanceOf<T> = T extends { prototype: infer U } ? U : never;
@@ -39,13 +19,12 @@ declare class Subscriber<T extends Stateful = any> {
39
19
  constructor(parent: Controller<T>, onUpdate: Controller.OnEvent);
40
20
  }
41
21
 
42
- declare const CONTROL: unique symbol;
43
22
  declare const WHY: unique symbol;
44
23
  declare const LOCAL: unique symbol;
45
24
  declare const STATE: unique symbol;
46
25
  interface Stateful {
47
26
  /** Controller for this instance. */
48
- [CONTROL]: Controller;
27
+ [CONTROL]?: Controller;
49
28
  /** Current subscriber (if present) while used in a live context (e.g. hook or effect). */
50
29
  [LOCAL]?: Subscriber;
51
30
  /** Current state of this instance. */
@@ -107,14 +86,6 @@ declare namespace Model {
107
86
  };
108
87
  }
109
88
  interface Model extends Stateful {
110
- /**
111
- * Circular reference to `this` controller.
112
- *
113
- * Useful to obtain full reference where one has already destructured.
114
- *
115
- * @deprecated will be removed in favor of `is`
116
- */
117
- get: this;
118
89
  /**
119
90
  * Circular reference to `this` controller.
120
91
  *
@@ -130,7 +101,6 @@ interface Model extends Stateful {
130
101
  set: Model.Refs<this>;
131
102
  }
132
103
  declare class Model {
133
- static [CONTROL]: Controller;
134
104
  static [WHY]: readonly string[];
135
105
  constructor();
136
106
  get [STATE](): Model.Values<this, Exclude<keyof this & string, keyof Model>>;
@@ -184,6 +154,7 @@ declare class Model {
184
154
  static isTypeof<T extends new () => Model>(this: T, maybe: any): maybe is T;
185
155
  }
186
156
 
157
+ declare const CONTROL: unique symbol;
187
158
  declare namespace Controller {
188
159
  type OnValue<T = any> = (this: T, value: any) => boolean | void;
189
160
  type OnEvent<T = any> = (key: Model.Event<T> | null, source: Controller) => Callback | void;
@@ -239,10 +210,27 @@ declare namespace Instruction {
239
210
  */
240
211
  declare function apply<T = any>(instruction: Instruction<T>, name?: string): T;
241
212
 
213
+ declare type Async<T = any> = (...args: any[]) => Promise<T>;
214
+ /**
215
+ * Sets an exotic method with managed ready-state. Property accepts an async function.
216
+ *
217
+ * When an act-method is invoked, its `active` property to true for duration of call.
218
+ * This is emitted as an update to property, both when called and after returns (or throws).
219
+ *
220
+ * **Note:** Subsequent calls will immediately throw if one is still pending.
221
+ *
222
+ * @param action - Action to fire when resulting property is invoked.
223
+ */
224
+ declare function act(action: Async): typeof action & {
225
+ active: boolean;
226
+ };
227
+ declare function act<S>(action: Async<S>): typeof action & {
228
+ active: boolean;
229
+ };
230
+
242
231
  declare namespace get {
243
- type Function<T, O = any> = (this: O, on: O) => T;
244
- type Factory<T, O = any> = (key: string) => Function<T, O>;
245
- type Getter = (controller: Controller, key: string) => any;
232
+ type Function<T, S = any> = (this: S, on: S) => T;
233
+ type Factory<T, S = unknown> = (this: S, key: string, thisArg: S) => T;
246
234
  }
247
235
  /**
248
236
  * Implement a computed value; output will be generated by provided function.
@@ -259,35 +247,8 @@ declare function get<R, T>(source: T, compute: (this: T, on: T) => R, suspend?:
259
247
  * @param compute - Factory function to generate a getter to subscribe dependancies.
260
248
  * @param suspend - Value will throw suspense when evaulating to undefined.
261
249
  */
262
- declare function get<R, T>(compute: (property: string) => (this: T, state: T) => R, suspend: true): Exclude<R, undefined>;
263
- declare function get<R, T>(compute: (property: string) => (this: T, state: T) => R, suspend?: boolean): R;
264
-
265
- declare namespace ref {
266
- type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<void> | void | boolean;
267
- interface Object<T = any> {
268
- (next: T): void;
269
- current: T | null;
270
- }
271
- /** Object with references to all managed values of `T`. */
272
- type Proxy<T extends Model> = {
273
- [P in Model.Field<T>]: Model.Ref<T[P]>;
274
- };
275
- }
276
- /**
277
- * Creates a ref-compatible property.
278
- * Will persist value, and updates to this are made part of controller event-stream.
279
- *
280
- * *Output is simultaneously a ref-function and ref-object, use as needed.*
281
- *
282
- * @param callback - Optional callback to synchronously fire when reference is first set or does update.
283
- */
284
- declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>;
285
- declare function ref<T, S>(callback?: ref.Callback<T, S>): ref.Object<T>;
286
-
287
- declare namespace set$1 {
288
- type Factory<T, S = unknown> = (this: S, key: string, subject: S) => T;
289
- type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<any> | void | boolean;
290
- }
250
+ declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R, suspend: true): Exclude<R, undefined>;
251
+ declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R, suspend?: boolean): R;
291
252
  /**
292
253
  * Set property with an async function.
293
254
  *
@@ -303,12 +264,10 @@ declare namespace set$1 {
303
264
  * @param factory - Callback run to derrive property value.
304
265
  * @param required - (default: true) Run factory immediately on creation, otherwise on access.
305
266
  */
306
- declare function put<T>(factory: set$1.Factory<Promise<T>>, required: false): T | undefined;
307
- declare function put<T, S>(factory: set$1.Factory<Promise<T>, S>, required: false): T | undefined;
308
- declare function put<T>(factory: set$1.Factory<Promise<T>>, required?: boolean): T;
309
- declare function put<T, S>(factory: set$1.Factory<Promise<T>, S>, required?: boolean): T;
310
- declare function put<T>(value: set$1.Factory<Promise<T>>, onUpdate: set$1.Callback<T>): T;
311
- declare function put<T, S>(value: set$1.Factory<Promise<T>, S>, onUpdate: set$1.Callback<T, S>): T;
267
+ declare function get<T>(factory: get.Factory<Promise<T>>, required: false): T | undefined;
268
+ declare function get<T, S>(factory: get.Factory<Promise<T>, S>, required: false): T | undefined;
269
+ declare function get<T>(factory: get.Factory<Promise<T>>, required?: boolean): T;
270
+ declare function get<T, S>(factory: get.Factory<Promise<T>, S>, required?: boolean): T;
312
271
  /**
313
272
  * Set property with a factory function.
314
273
  *
@@ -321,24 +280,37 @@ declare function put<T, S>(value: set$1.Factory<Promise<T>, S>, onUpdate: set$1.
321
280
  * @param factory - Callback run to derrive property value.
322
281
  * @param required - (default: true) Run factory immediately on creation, otherwise on access.
323
282
  */
324
- declare function put<T>(factory: set$1.Factory<T>, required: false): T | undefined;
325
- declare function put<T, S>(factory: set$1.Factory<T, S>, required: false): T | undefined;
326
- declare function put<T>(factory: set$1.Factory<T>, required?: boolean): T;
327
- declare function put<T, S>(factory: set$1.Factory<T, S>, required?: boolean): T;
328
- declare function put<T>(value: set$1.Factory<T>, onUpdate: set$1.Callback<T>): T;
329
- declare function put<T, S>(value: set$1.Factory<T, S>, onUpdate: set$1.Callback<T, S>): T;
283
+ declare function get<T>(factory: get.Factory<T>, required: false): T | undefined;
284
+ declare function get<T, S>(factory: get.Factory<T, S>, required: false): T | undefined;
285
+ declare function get<T>(factory: get.Factory<T>, required?: boolean): T;
286
+ declare function get<T, S>(factory: get.Factory<T, S>, required?: boolean): T;
330
287
  /**
331
288
  * Assign a property with result of a promise.
332
289
  */
333
- declare function put<T>(factory: Promise<T>, required: false): T | undefined;
334
- declare function put<T>(factory: Promise<T>, required?: boolean): T;
335
- declare function put<T>(value: Promise<T>, onUpdate: set$1.Callback<T>): T;
290
+ declare function get<T>(factory: Promise<T>, required: false): T | undefined;
291
+ declare function get<T>(factory: Promise<T>, required?: boolean): T;
292
+
293
+ declare namespace ref {
294
+ type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<void> | void | boolean;
295
+ interface Object<T = any> {
296
+ (next: T): void;
297
+ current: T | null;
298
+ }
299
+ /** Object with references to all managed values of `T`. */
300
+ type Proxy<T extends Model> = {
301
+ [P in Model.Field<T>]: Model.Ref<T[P]>;
302
+ };
303
+ }
336
304
  /**
337
- * Assign a property.
305
+ * Creates a ref-compatible property.
306
+ * Will persist value, and updates to this are made part of controller event-stream.
307
+ *
308
+ * *Output is simultaneously a ref-function and ref-object, use as needed.*
309
+ *
310
+ * @param callback - Optional callback to synchronously fire when reference is first set or does update.
338
311
  */
339
- declare function put<T>(factory: T, required: false): T | undefined;
340
- declare function put<T>(factory: T, required?: boolean): T;
341
- declare function put<T>(value: T, onUpdate: set$1.Callback<T>): T;
312
+ declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>;
313
+ declare function ref<T, S>(callback?: ref.Callback<T, S>): ref.Object<T>;
342
314
 
343
315
  declare namespace set {
344
316
  type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<any> | void | boolean;
@@ -357,16 +329,26 @@ declare function set<T, S>(value: undefined, onUpdate: set.Callback<T, S>): T;
357
329
  declare function set<T>(value: T, onUpdate: set.Callback<T>): T;
358
330
  declare function set<T, S>(value: T, onUpdate: set.Callback<T, S>): T;
359
331
 
360
- declare function use<K = any>(initial: SetConstructor): Set<K>;
361
- declare function use<K = any, V = any>(initial: MapConstructor): Map<K, V>;
362
- declare function use(initial: Set<unknown>): Set<any>;
363
- declare function use(initial: Map<unknown, unknown>): Map<any, any>;
364
- declare function use(from: () => Set<unknown>): Set<any>;
365
- declare function use(from: () => Map<unknown, unknown>): Map<any, any>;
332
+ 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;
333
+ declare type Managed<T> = Exclude<T, "forEach"> & {
334
+ from?: T;
335
+ forEach<R>(mapFunction: MapFunction<T, R>, thisArg?: any): Exclude<R, undefined>[] | void;
336
+ };
337
+
338
+ declare function use<K = any>(initial: SetConstructor): Managed<Set<K>>;
339
+ declare function use<K = any, V = any>(initial: MapConstructor): Managed<Map<K, V>>;
340
+ declare function use(initial: Set<unknown>): Managed<Set<any>>;
341
+ declare function use(initial: Map<unknown, unknown>): Managed<Map<any, any>>;
342
+ declare function use(from: () => Set<unknown>): Managed<Set<any>>;
343
+ declare function use(from: () => Map<unknown, unknown>): Managed<Map<any, any>>;
344
+ declare function use<T extends Set<any> | Map<any, any>>(initial: T): Managed<T>;
345
+ declare function use<T extends Set<any> | Map<any, any>>(from: () => T): Managed<T>;
366
346
  /**
367
347
  * Create a placeholder for specified Model type.
368
348
  */
369
349
  declare function use<T extends Model>(): T | undefined;
350
+ declare function use<T extends Model>(from: () => T, required: false): T | undefined;
351
+ declare function use<T extends Model>(from: () => T, required: boolean): T;
370
352
  /**
371
353
  * Create a new child instance of model.
372
354
  */
@@ -374,6 +356,9 @@ declare function use<T extends Class>(Type: T, callback?: (i: InstanceOf<T>) =>
374
356
  /**
375
357
  * Create a managed child from factory function.
376
358
  */
359
+ declare function use<T extends {}>(from: () => Promise<T>, required: false): T | undefined;
360
+ declare function use<T extends {}>(from: () => Promise<T>, required: boolean): T;
361
+ declare function use<T extends {}>(from: () => Promise<T>, callback?: (i: T) => void): T;
377
362
  declare function use<T extends {}>(from: () => T, callback?: (i: T) => void): T;
378
363
  /**
379
364
  * Create child-instance relationship with provided model.
@@ -393,8 +378,8 @@ declare function use<T extends {}>(peer: T, callback?: (i: T) => void): T;
393
378
  * @param Expects - Type of controller compatible with this class.
394
379
  * @param required - Throw if controller is created independantly.
395
380
  */
396
- declare function parent<T extends typeof Model>(Expects: T, required: false): InstanceOf<T> | undefined;
397
- declare function parent<T extends typeof Model>(Expects: T, required?: true): InstanceOf<T>;
381
+ declare function has<T extends typeof Model>(Expects: T, required: false): InstanceOf<T> | undefined;
382
+ declare function has<T extends typeof Model>(Expects: T, required?: true): InstanceOf<T>;
398
383
 
399
384
  declare class MVC extends Model {
400
385
  static [CONTROL]: Controller;
@@ -629,4 +614,4 @@ declare function useTap<T extends Stateful, K extends Model.Field<T>>(source: T
629
614
  declare function useTap<T extends Stateful, R>(source: T | (() => T) | useTap.Tappable, compute: (this: T, from: T) => R, expect: true): Exclude<R, undefined>;
630
615
  declare function useTap<T extends Stateful, R>(source: T | (() => T) | useTap.Tappable, compute: (this: T, from: T) => R, expect?: boolean): R;
631
616
 
632
- export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, Stateful, WHY, act, apply, array, MVC as default, get as from, get, parent, put, ref, set, tap, use, useFrom, useLocal, useModel, useTap };
617
+ export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, Stateful, WHY, act, apply, MVC as default, get, has, ref, set, tap, use, useFrom, useLocal, useModel, useTap };