@expressive/mvc 0.9.5 → 0.10.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
@@ -22,7 +22,7 @@ declare class Subscriber<T extends {} = any> {
22
22
  dependant: Set<Listener>;
23
23
  watch: Map<any, boolean | (() => true | void)>;
24
24
  constructor(parent: Control<T>, onUpdate: Subscriber.OnEvent);
25
- get using(): Model.Values<T>[];
25
+ get using(): Model.Field<T>[];
26
26
  assign(apply: Model.Compat<T>, keys?: any): void;
27
27
  add(key: any, value?: boolean | Callback): void;
28
28
  commit: () => () => void;
@@ -45,7 +45,7 @@ declare type Debug<T extends {}> = T & {
45
45
  /** Current subscriber (if present) while used in a live context (e.g. hook or effect). */
46
46
  [LOCAL]?: Subscriber<T>;
47
47
  /** Current state of this instance. */
48
- [STATE]?: Model.Values<T>;
48
+ [STATE]?: Model.Export<T>;
49
49
  /**
50
50
  * Last update causing a refresh to subscribers.
51
51
  *
@@ -78,18 +78,18 @@ declare namespace Model {
78
78
  *
79
79
  * TODO: Should exclude methods
80
80
  **/
81
- export type Field<T, U extends Model = Model> = Exclude<keyof T, keyof U | keyof Debug<{}>> & string;
81
+ export type Field<T, E extends Model = Model> = Exclude<keyof T, keyof E | keyof Debug<{}>> & string;
82
82
  /**
83
83
  * Including but not limited to `keyof T` which are not methods or defined by base Model.
84
84
  **/
85
- export type Event<T, U extends Model = Model> = Extends<Field<T, U>>;
85
+ export type Event<T, E extends Model = Model> = Extends<Field<T, E>>;
86
86
  /** Object containing managed entries found in T. */
87
- export type Entries<T, U extends Model = Model> = {
88
- [K in Field<T, U>]: T[K];
87
+ export type Entries<T, E extends Model = Model> = {
88
+ [K in Field<T, E>]: T[K];
89
89
  };
90
90
  /** Object comperable to data found in T. */
91
- export type Compat<T, U extends Model = Model> = {
92
- [K in Field<T, U>]?: T[K];
91
+ export type Compat<T, E extends Model = Model> = {
92
+ [K in Field<T, E>]?: T[K];
93
93
  };
94
94
  /** Actual value stored in state. */
95
95
  export type Value<R> = R extends Ref<infer T> ? T : R;
@@ -100,9 +100,12 @@ declare namespace Model {
100
100
  *
101
101
  * Differs from `Entries` as values here will drill into "real" values held by exotics like ref.
102
102
  */
103
- export type Values<T, K extends Field<T> = Field<T>> = {
103
+ export type Get<T, K extends Field<T> = Field<T>> = {
104
104
  [P in K]: Value<T[P]>;
105
105
  };
106
+ export type Export<T, E extends Model = Model> = {
107
+ [P in Field<T, E>]: Value<T[P]>;
108
+ };
106
109
  }
107
110
  declare class Model {
108
111
  /**
@@ -112,11 +115,8 @@ declare class Model {
112
115
  */
113
116
  is: this;
114
117
  constructor();
115
- on<P extends Model.Event<this>>(key: P, timeout?: number): Promise<Model.ValueOf<this, P>>;
116
- on<P extends Model.Field<this>>(key: P, listener: (this: this, value: this[P], key: P) => void, once?: boolean): Callback;
117
- on<P extends Model.Event<this>>(key: P, listener: (this: this, value: undefined, key: P) => void, once?: boolean): Callback;
118
- on<P extends Model.Event<this>>(keys: P[], timeout?: number): Promise<P[]>;
119
- on<P extends Model.Event<this>>(keys: P[], listener: (keys: P[]) => void, once?: boolean): Callback;
118
+ on<P extends Model.Event<this>>(keys?: P | Iterable<P>, timeout?: number): Promise<P[]>;
119
+ on<P extends Model.Event<this>>(keys: P | Iterable<P>, listener: (this: this, keys: Model.Event<this>[]) => void, once?: boolean): Callback;
120
120
  on(effect: Model.Effect<this>): Callback;
121
121
  on(effect: Model.Effect<this>, watch?: []): Callback;
122
122
  on(effect: Model.Effect<this>, watch?: Model.Event<this>[]): Callback;
@@ -124,17 +124,25 @@ declare class Model {
124
124
  on(current?: boolean): Promise<Model.Event<this>[] | null>;
125
125
  on(current: true): Promise<Model.Event<this>[]>;
126
126
  on(current: null): Promise<null>;
127
- export(): Model.Values<this>;
128
- export<P extends Model.Field<this>>(select: Iterable<P>): Model.Values<this, P>;
129
- update(key: Model.Event<this>): PromiseLike<readonly Model.Event<this>[]>;
130
- update(key: Model.Event<this>, callMethod: boolean): PromiseLike<readonly Model.Event<this>[]>;
131
- update<T>(key: Model.Event<this>, argument: T): PromiseLike<readonly Model.Event<this>[]>;
132
- update<T extends Model.Compat<this>>(source: T, select: (keyof T)[]): PromiseLike<(keyof T)[]>;
133
- update<T extends Model.Compat<this>>(source: T, force?: boolean): PromiseLike<(keyof T)[]>;
127
+ get(): Model.Export<this>;
128
+ get<P extends Model.Field<this>>(property: P): this[P];
129
+ get<P extends Model.Field<this>>(property: P, timeout: number): Promise<this[P]>;
130
+ get<P extends Model.Field<this>>(property: P, onChange: true): Promise<this[P]>;
131
+ get<P extends Model.Field<this>>(property: P, onChange: boolean): this[P] | Promise<this[P]>;
132
+ get<P extends Model.Field<this>>(property: P, listener: (this: this, value: this[P], key: P[]) => void): Callback;
133
+ get<P extends Model.Field<this>>(select: Iterable<P>): Model.Get<this, P>;
134
+ get<P extends Model.Field<this>>(select: Iterable<P>, timeout: number): Promise<Model.Get<this, P>>;
135
+ get<P extends Model.Field<this>>(select: Iterable<P>, onChange: true): Promise<Model.Get<this, P>>;
136
+ get<P extends Model.Field<this>>(select: Iterable<P>, onChange: boolean): Model.Get<this, P> | Promise<Model.Get<this, P>>;
137
+ get<P extends Model.Field<this>>(select: Iterable<P>, listener: (this: this, value: Model.Get<this, P>, key: P[]) => void): Callback;
138
+ set(key: Model.Event<this>): PromiseLike<readonly Model.Event<this>[]>;
139
+ set<K extends Model.Event<this>>(key: Model.Event<this>, value: Model.ValueOf<this, K>): PromiseLike<readonly Model.Event<this>[]>;
140
+ set<T extends Model.Compat<this>>(source: T, select: (keyof T)[]): PromiseLike<(keyof T)[]>;
141
+ set<T extends Model.Compat<this>>(source: T, force?: boolean): PromiseLike<(keyof T)[]>;
134
142
  /**
135
143
  * Clean up side effects and mark this instance for garbage-collection.
136
144
  */
137
- destroy(): void;
145
+ end(): void;
138
146
  /**
139
147
  * Creates a new instance of this controller.
140
148
  *
@@ -413,30 +421,42 @@ declare function use<T extends {}>(peer: T, callback?: (i: T) => void): T;
413
421
  declare function has<T extends Model>(Type: Model.Type<T>, required: false): T | undefined;
414
422
  declare function has<T extends Model>(Type: Model.Type<T>, required?: true): T;
415
423
 
424
+ declare namespace MVC {
425
+ type EffectCallback<T = MVC> = (found: T) => Callback | void;
426
+ }
416
427
  declare class MVC extends Model {
428
+ end(force?: boolean): boolean | void;
429
+ static global?: boolean;
430
+ static keepAlive?: boolean;
431
+ /**
432
+ * Create a new instance of this model and activate its managed state.
433
+ *
434
+ * @param args - arguments sent to constructor
435
+ */
436
+ static new<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
417
437
  /**
418
438
  * **React Hook** - Fetch most instance of this controller from context, if it exists.
419
439
  *
420
440
  * @param required - Unless false, will throw where instance cannot be found.
421
441
  */
422
- static get<T extends MVC>(this: Model.Type<T>, required?: boolean): T;
442
+ static get<T extends MVC>(this: Model.Type<T>, required?: boolean, effectCallback?: MVC.EffectCallback<T>): T;
423
443
  /**
424
444
  * **React Hook** - Fetch most instance of this controller from context.
425
445
  *
426
446
  * @param required - If false, may return undefined.
427
447
  */
428
- static get<T extends MVC>(this: Model.Type<T>, required: false): T | undefined;
429
- /**
430
- * **React Hook** - Fetch specific value from instance of this controller in context.
431
- */
432
- static get<I extends MVC, K extends Model.Field<I>>(this: Model.Type<I>, key: K): I[K];
448
+ static get<T extends MVC>(this: Model.Type<T>, required: false, effectCallback?: MVC.EffectCallback<T | undefined>): T | undefined;
433
449
  /**
434
450
  * **React Hook** - Fetch instance.
435
451
  *
436
452
  * Effect callback will run once if found, throw if not found.
437
453
  * Returned function is called on unmount.
438
454
  */
439
- static get<I extends MVC>(this: Model.Type<I>, effect: (found: I) => Callback | void): I;
455
+ static get<I extends MVC>(this: Model.Type<I>, effectCallback: MVC.EffectCallback<I>): I | undefined;
456
+ /**
457
+ * **React Hook** - Fetch specific value from instance of this controller in context.
458
+ */
459
+ static get<I extends MVC, K extends Model.Field<I>>(this: Model.Type<I>, key: K): I[K];
440
460
  /**
441
461
  * **React Hook** - Fetch and subscribe to instance of this controller within ambient component.
442
462
  */
@@ -460,26 +480,21 @@ declare class MVC extends Model {
460
480
  static meta<T, M extends Class>(this: M, from: (this: M, state: M) => T, expect?: boolean): T;
461
481
  }
462
482
 
463
- declare function useValue<T extends {}, R>(source: (() => T) | T, compute: (this: T, from: T) => R, expect: true): Exclude<R, undefined>;
464
- declare function useValue<T extends {}, R>(source: (() => T) | T, compute: (this: T, from: T) => R, expect?: boolean): R;
465
-
466
483
  declare class Lookup {
467
484
  private table;
468
485
  get local(): Model[];
469
486
  private key;
470
487
  get(T: Model.Type): any;
488
+ add(T: Model.Type, I: Model, writable?: boolean): void;
471
489
  push(): this;
472
- inject(T: Model.Type, I: Model, writable?: boolean): void;
473
490
  pop(): void;
474
491
  }
475
492
  declare function useContext<T extends Model>(Type: Model.Type<T>, required: false): T | undefined;
476
- declare function useContext<T extends Model>(Type: Model.Type<T>, arg?: boolean): T;
477
- declare function useContext<T extends Model>(Type: Model.Type<T>, effect: (found: T) => Callback | void): T;
478
- declare function useContext<T extends Model, K extends Model.Field<T>>(Type: Model.Type<T>, key: K): T[K];
493
+ declare function useContext<T extends Model>(Type: Model.Type<T>, required?: boolean): T;
479
494
 
480
- declare function useModel<T extends Model>(source: Model.Type<T> | (() => T), callback?: (instance: T) => void): T;
481
- declare function useModel<T extends Model>(source: Model.Type<T> | (() => T), watch: Model.Field<T>[], callback?: (instance: T) => void): T;
482
- declare function useModel<T extends Model>(source: Model.Type<T> | (() => T), apply: Model.Compat<T>, keys?: Model.Event<T>[]): T;
495
+ declare function useNew<T extends Model>(source: Model.Type<T> | (() => T), callback?: (instance: T) => void): T;
496
+ declare function useNew<T extends Model>(source: Model.Type<T> | (() => T), watch: Model.Field<T>[], callback?: (instance: T) => void): T;
497
+ declare function useNew<T extends Model>(source: Model.Type<T> | (() => T), apply: Model.Compat<T>, keys?: Model.Event<T>[]): T;
483
498
 
484
499
  declare namespace useTap {
485
500
  type Source<T extends Model> = T | Model.Type<T> | (() => T | Model.Type<T>);
@@ -535,48 +550,24 @@ declare namespace Provider {
535
550
  type Multiple<T extends Item = Item> = T[] | {
536
551
  [key: string]: T;
537
552
  };
538
- type Existent<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
539
- type NormalProps<E, I = Existent<E>> = {
553
+ type Instance<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
554
+ type NormalProps<E, I = Instance<E>> = {
540
555
  for: E;
541
556
  children?: React.ReactNode | ((instance: I) => React.ReactNode);
542
- } & Model.Compat<I>;
557
+ and?: Model.Compat<I>;
558
+ };
543
559
  type MultipleProps<T extends Item> = {
544
560
  for: Multiple<T>;
545
561
  children?: React.ReactNode | (() => React.ReactNode);
546
- } & Model.Compat<Existent<T>>;
562
+ and?: Model.Compat<Instance<T>>;
563
+ };
547
564
  type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
548
565
  }
549
566
  declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): React.FunctionComponentElement<React.ProviderProps<Lookup>>;
550
567
 
551
568
  declare class Global extends MVC {
552
- constructor();
553
- destroy(force?: boolean): boolean;
569
+ static global: boolean;
554
570
  static keepAlive: boolean;
555
- static new<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
556
- /**
557
- * Update the active instance of this class.
558
- * Returns a thenable; resolves after successful update.
559
- * If instance does not already exist, one will be created.
560
- */
561
- static set<T extends typeof Global>(this: T, values: Model.Compat<InstanceOf<T>, Global>): PromiseLike<Model.Field<InstanceOf<T>, Global>[]>;
562
- /**
563
- * **React Hook** - Fetch most instance of this controller from context, if it exists.
564
- *
565
- * @param required - If false, may return undefined.
566
- */
567
- static get<T extends Global>(this: Model.Type<T>, required: false): T | undefined;
568
- /**
569
- * **React Hook** - Fetch most instance of this controller from context.
570
- *
571
- * @param required - Unless false, will throw where instance cannot be found.
572
- */
573
- static get<T extends Global>(this: Model.Type<T>, required?: boolean): T;
574
- /**
575
- * **React Hook** - Fetch specific value from instance of this controller in context.
576
- */
577
- static get<T extends Global, K extends Model.Field<T, Global>>(this: Model.Type<T>, key: K): T[K];
578
- /** Destroy current instance of Global, if it exists. */
579
- static reset(): void;
580
571
  }
581
572
 
582
573
  /**
@@ -604,4 +595,4 @@ declare function tap<T extends Model>(Type: Model.Type<T>, callback?: (instance?
604
595
  declare function tap<T extends Model>(Type: Model.Type<T>, required: true): T;
605
596
  declare function tap<T extends Model>(Type: Model.Type<T>, required?: boolean): T | undefined;
606
597
 
607
- export { Consumer, Debug, Global, MVC, MVC as Model, Provider, add, MVC as default, get, has, ref, run, set, tap, use, useContext, useModel, useTap, useValue };
598
+ export { Consumer, Debug, Global, MVC, MVC as Model, Provider, add, MVC as default, get, has, ref, run, set, tap, use, useContext, useNew, useTap };