@expressive/mvc 0.9.6 → 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
@@ -100,7 +100,7 @@ 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
106
  export type Export<T, E extends Model = Model> = {
@@ -115,11 +115,8 @@ declare class Model {
115
115
  */
116
116
  is: this;
117
117
  constructor();
118
- on<P extends Model.Event<this>>(key: P, timeout?: number): Promise<Model.ValueOf<this, P>>;
119
- on<P extends Model.Field<this>>(key: P, listener: (this: this, value: this[P], key: P) => void, once?: boolean): Callback;
120
- on<P extends Model.Event<this>>(key: P, listener: (this: this, value: undefined, key: P) => void, once?: boolean): Callback;
121
- on<P extends Model.Event<this>>(keys: P[], timeout?: number): Promise<P[]>;
122
- 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;
123
120
  on(effect: Model.Effect<this>): Callback;
124
121
  on(effect: Model.Effect<this>, watch?: []): Callback;
125
122
  on(effect: Model.Effect<this>, watch?: Model.Event<this>[]): Callback;
@@ -127,17 +124,25 @@ declare class Model {
127
124
  on(current?: boolean): Promise<Model.Event<this>[] | null>;
128
125
  on(current: true): Promise<Model.Event<this>[]>;
129
126
  on(current: null): Promise<null>;
130
- export(): Model.Export<this>;
131
- export<P extends Model.Field<this>>(select: Iterable<P>): Model.Values<this, P>;
132
- update(key: Model.Event<this>): PromiseLike<readonly Model.Event<this>[]>;
133
- update(key: Model.Event<this>, callMethod: boolean): PromiseLike<readonly Model.Event<this>[]>;
134
- update<T>(key: Model.Event<this>, argument: T): PromiseLike<readonly Model.Event<this>[]>;
135
- update<T extends Model.Compat<this>>(source: T, select: (keyof T)[]): PromiseLike<(keyof T)[]>;
136
- 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)[]>;
137
142
  /**
138
143
  * Clean up side effects and mark this instance for garbage-collection.
139
144
  */
140
- destroy(): void;
145
+ end(): void;
141
146
  /**
142
147
  * Creates a new instance of this controller.
143
148
  *
@@ -416,30 +421,42 @@ declare function use<T extends {}>(peer: T, callback?: (i: T) => void): T;
416
421
  declare function has<T extends Model>(Type: Model.Type<T>, required: false): T | undefined;
417
422
  declare function has<T extends Model>(Type: Model.Type<T>, required?: true): T;
418
423
 
424
+ declare namespace MVC {
425
+ type EffectCallback<T = MVC> = (found: T) => Callback | void;
426
+ }
419
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>;
420
437
  /**
421
438
  * **React Hook** - Fetch most instance of this controller from context, if it exists.
422
439
  *
423
440
  * @param required - Unless false, will throw where instance cannot be found.
424
441
  */
425
- 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;
426
443
  /**
427
444
  * **React Hook** - Fetch most instance of this controller from context.
428
445
  *
429
446
  * @param required - If false, may return undefined.
430
447
  */
431
- static get<T extends MVC>(this: Model.Type<T>, required: false): T | undefined;
432
- /**
433
- * **React Hook** - Fetch specific value from instance of this controller in context.
434
- */
435
- 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;
436
449
  /**
437
450
  * **React Hook** - Fetch instance.
438
451
  *
439
452
  * Effect callback will run once if found, throw if not found.
440
453
  * Returned function is called on unmount.
441
454
  */
442
- 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];
443
460
  /**
444
461
  * **React Hook** - Fetch and subscribe to instance of this controller within ambient component.
445
462
  */
@@ -463,26 +480,21 @@ declare class MVC extends Model {
463
480
  static meta<T, M extends Class>(this: M, from: (this: M, state: M) => T, expect?: boolean): T;
464
481
  }
465
482
 
466
- declare function useValue<T extends {}, R>(source: (() => T) | T, compute: (this: T, from: T) => R, expect: true): Exclude<R, undefined>;
467
- declare function useValue<T extends {}, R>(source: (() => T) | T, compute: (this: T, from: T) => R, expect?: boolean): R;
468
-
469
483
  declare class Lookup {
470
484
  private table;
471
485
  get local(): Model[];
472
486
  private key;
473
487
  get(T: Model.Type): any;
488
+ add(T: Model.Type, I: Model, writable?: boolean): void;
474
489
  push(): this;
475
- inject(T: Model.Type, I: Model, writable?: boolean): void;
476
490
  pop(): void;
477
491
  }
478
492
  declare function useContext<T extends Model>(Type: Model.Type<T>, required: false): T | undefined;
479
- declare function useContext<T extends Model>(Type: Model.Type<T>, arg?: boolean): T;
480
- declare function useContext<T extends Model>(Type: Model.Type<T>, effect: (found: T) => Callback | void): T;
481
- 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;
482
494
 
483
- declare function useModel<T extends Model>(source: Model.Type<T> | (() => T), callback?: (instance: T) => void): T;
484
- declare function useModel<T extends Model>(source: Model.Type<T> | (() => T), watch: Model.Field<T>[], callback?: (instance: T) => void): T;
485
- 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;
486
498
 
487
499
  declare namespace useTap {
488
500
  type Source<T extends Model> = T | Model.Type<T> | (() => T | Model.Type<T>);
@@ -538,48 +550,24 @@ declare namespace Provider {
538
550
  type Multiple<T extends Item = Item> = T[] | {
539
551
  [key: string]: T;
540
552
  };
541
- type Existent<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
542
- 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>> = {
543
555
  for: E;
544
556
  children?: React.ReactNode | ((instance: I) => React.ReactNode);
545
- } & Model.Compat<I>;
557
+ and?: Model.Compat<I>;
558
+ };
546
559
  type MultipleProps<T extends Item> = {
547
560
  for: Multiple<T>;
548
561
  children?: React.ReactNode | (() => React.ReactNode);
549
- } & Model.Compat<Existent<T>>;
562
+ and?: Model.Compat<Instance<T>>;
563
+ };
550
564
  type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
551
565
  }
552
566
  declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): React.FunctionComponentElement<React.ProviderProps<Lookup>>;
553
567
 
554
568
  declare class Global extends MVC {
555
- constructor();
556
- destroy(force?: boolean): boolean;
569
+ static global: boolean;
557
570
  static keepAlive: boolean;
558
- static new<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
559
- /**
560
- * Update the active instance of this class.
561
- * Returns a thenable; resolves after successful update.
562
- * If instance does not already exist, one will be created.
563
- */
564
- static set<T extends typeof Global>(this: T, values: Model.Compat<InstanceOf<T>, Global>): PromiseLike<Model.Field<InstanceOf<T>, Global>[]>;
565
- /**
566
- * **React Hook** - Fetch most instance of this controller from context, if it exists.
567
- *
568
- * @param required - If false, may return undefined.
569
- */
570
- static get<T extends Global>(this: Model.Type<T>, required: false): T | undefined;
571
- /**
572
- * **React Hook** - Fetch most instance of this controller from context.
573
- *
574
- * @param required - Unless false, will throw where instance cannot be found.
575
- */
576
- static get<T extends Global>(this: Model.Type<T>, required?: boolean): T;
577
- /**
578
- * **React Hook** - Fetch specific value from instance of this controller in context.
579
- */
580
- static get<T extends Global, K extends Model.Field<T, Global>>(this: Model.Type<T>, key: K): T[K];
581
- /** Destroy current instance of Global, if it exists. */
582
- static reset(): void;
583
571
  }
584
572
 
585
573
  /**
@@ -607,4 +595,4 @@ declare function tap<T extends Model>(Type: Model.Type<T>, callback?: (instance?
607
595
  declare function tap<T extends Model>(Type: Model.Type<T>, required: true): T;
608
596
  declare function tap<T extends Model>(Type: Model.Type<T>, required?: boolean): T | undefined;
609
597
 
610
- 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 };