@expressive/mvc 1.0.0-beta.2 → 1.0.0-beta.23

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/types/model.d.ts CHANGED
@@ -1,13 +1,40 @@
1
- import React from 'react';
1
+ import { Key } from 'react';
2
2
 
3
- import Controller from './controller';
4
3
  import Lifecycle from './lifecycle';
5
- import { Selector } from './selector';
6
- import { Class, InstanceOf, Key } from './types';
4
+ import { Select, Selector } from './selector';
5
+ import { BunchOf, Callback, Class, InstanceOf, Query, RequestCallback, UpdateCallback } from './types';
6
+
7
+ interface PropertyDescriptor<T> {
8
+ configurable?: boolean;
9
+ enumerable?: boolean;
10
+ value?: T;
11
+ writable?: boolean;
12
+ get?(): T;
13
+ set?(v: T): void;
14
+ }
15
+
16
+ type Argument<T> = T extends (arg: infer U) => any ? U : never;
7
17
 
8
- type RefFunction = (e: HTMLElement | null) => void;
18
+ type Thenable<T> = {
19
+ then(onFulfilled: (arg: T) => void): void;
20
+ }
9
21
 
10
22
  export namespace Model {
23
+ /** Exotic value, actual value is contained. */
24
+ interface Ref<T = any> {
25
+ (next: T): void;
26
+ current: T | null;
27
+ }
28
+
29
+ type GetFunction<T> = (within?: Subscriber) => T;
30
+
31
+ /**
32
+ * Property initializer, will run upon instance creation.
33
+ * Optional returned callback will run when once upon first access.
34
+ */
35
+ type Instruction<T> = (this: Controller, key: string, thisArg: Controller) =>
36
+ void | GetFunction<T> | PropertyDescriptor<T>;
37
+
11
38
  /** Shallow replacement given all entries of Model */
12
39
  type Overlay<T, R> = { [K in keyof Entries<T>]: R };
13
40
 
@@ -17,6 +44,14 @@ export namespace Model {
17
44
  /** Object containing data found in T. */
18
45
  type Entries<T, E = Model> = Pick<T, Fields<T, E>>;
19
46
 
47
+ /** Actual value stored in state. */
48
+ type Value<R> = R extends Ref<infer T> ? T : R;
49
+
50
+ /** Values from current state of given controller. */
51
+ type State<T, K extends keyof T = Fields<T, Model>> = {
52
+ [P in K]: Value<T[P]>;
53
+ }
54
+
20
55
  /** Object comperable to data which may be found in T. */
21
56
  type Data<T, E = Model> = Partial<Entries<T, E>>;
22
57
 
@@ -67,11 +102,68 @@ export namespace Model {
67
102
  type SelectFields<T> = Selector.Function<Omit<T, keyof Model>>;
68
103
 
69
104
  type SelectField<T> = (arg: Omit<T, keyof Model>) => any;
105
+
106
+ type SelectFieldKey<T> = Select.Key<Fields<T>>;
107
+
108
+ type Typeof<T, ST, X extends keyof T = Exclude<keyof T, keyof Model>> = {
109
+ [Key in X]: T[Key] extends ST ? Key : never;
110
+ }[X];
111
+
112
+ type SelectTypeof<T, ST> = Select.Key<Typeof<T, ST>>;
113
+
114
+ type HandleValue = (value: any) => boolean | void;
115
+
116
+ export class Controller {
117
+ state: BunchOf<any>;
118
+ subject: {};
119
+
120
+ /** Add property to managed state. */
121
+ manage(key: string, initial: any, effect?: HandleValue): void;
122
+
123
+ select(using?: Query): string[];
124
+
125
+ setter(key: string, effect?: HandleValue): (value: any) => boolean | void;
126
+
127
+ addListener(batch: BunchOf<RequestCallback>): Callback;
128
+
129
+ update(key: string, value?: any): void;
130
+
131
+ include(cb: RequestCallback): void;
132
+ }
133
+
134
+ export class Subscriber {
135
+ proxy: any;
136
+ source: any;
137
+ parent: Controller;
138
+ active: boolean;
139
+ follows: BunchOf<Callback>;
140
+ dependant: Set<{
141
+ commit(): void;
142
+ release(): void;
143
+ }>;
144
+
145
+ follow(key: string, cb?: Callback | undefined): void;
146
+ commit(): Callback;
147
+ release(): Callback;
148
+ onUpdate(): void;
149
+ }
70
150
  }
71
151
 
72
- export interface Model extends Controller, Lifecycle {}
152
+ declare const CONTROL: unique symbol;
153
+ declare const LOCAL: unique symbol;
154
+ declare const STATE: unique symbol;
73
155
 
156
+ export interface Model extends Lifecycle {}
74
157
  export abstract class Model {
158
+ /** Controller for this instance. */
159
+ [CONTROL]: Model.Controller;
160
+
161
+ /** Current state of this instance. */
162
+ [STATE]?: Model.State<this>;
163
+
164
+ /** Current subscriber (if present) while used in a live context (e.g. hook or effect). */
165
+ [LOCAL]?: Model.Subscriber;
166
+
75
167
  /**
76
168
  * Circular reference to `this` controller.
77
169
  *
@@ -131,15 +223,35 @@ export abstract class Model {
131
223
  */
132
224
  set: this;
133
225
 
134
- /**
135
- * Plug-n-Play references for properties of this controller.
136
- *
137
- * Matched ref-functions automatically bind between receiving element and live value of field.
138
- *
139
- * For `<input type="text" />` this is a two-way binding.
140
- * User input is captured and part of controller's state/event stream.
141
- */
142
- bind: Model.Overlay<this, RefFunction>;
226
+ import <O extends Model.Data<this>> (via: O, select?: string[] | Model.SelectFields<this>): void;
227
+
228
+ export(): Model.State<this>;
229
+ export <P extends Model.Fields<this>> (select: P[]): Model.State<this, P>;
230
+ export <S extends Model.SelectFields<this>> (select: S): Model.State<this, Selector.From<S>>;
231
+
232
+ update(): PromiseLike<string[] | false>;
233
+ update(strict: true): Promise<string[]>;
234
+ update(strict: false): Promise<false>;
235
+ update(strict: boolean): Promise<string[] | false>;
236
+
237
+ update(keys: Model.Fields<this>): Thenable<string[]>;
238
+ update(keys: Model.SelectFieldKey<this>): Thenable<string[]>;
239
+
240
+ update(keys: Model.Fields<this>, callMethod: boolean): PromiseLike<string[]>;
241
+ update(keys: Model.SelectFields<this>, callMethod: boolean): PromiseLike<string[]>;
242
+
243
+ update<T>(keys: Model.Fields<this>, argument: T): PromiseLike<string[]>;
244
+ update<T>(keys: Model.SelectFields<this>, argument: T): PromiseLike<string[]>;
245
+
246
+ /*
247
+ Issue with self-reference, using fallback.
248
+
249
+ update(keys: Model.Typeof<this, () => void>, callMethod: boolean): Thenable<string[]>;
250
+ update(keys: Model.SelectTypeof<this, () => void>, callMethod: boolean): Thenable<string[]>;
251
+
252
+ update<T>(keys: Model.Typeof<this, (arg: T) => void>, argument: T): Thenable<string[]>;
253
+ update<T>(keys: Model.SelectTypeof<this, (arg: T) => void>, argument: T): Thenable<string[]>;
254
+ */
143
255
 
144
256
  /**
145
257
  * Mark this instance for garbage-collection and send `willDestroy` event to all listeners.
@@ -169,6 +281,32 @@ export abstract class Model {
169
281
 
170
282
  tap <K extends Model.SelectField<this>> (key: K, expect?: boolean): ReturnType<K>;
171
283
  tap <K extends Model.SelectField<this>> (key: K, expect: true): Exclude<ReturnType<K>, undefined>;
284
+
285
+ // Keyed
286
+ on <S extends Model.SelectEvents<this>> (via: S, cb: Selector.Callback<S, this>, squash?: false, once?: boolean): Callback;
287
+ on <P extends Model.EventsCompat<this>> (key: P | P[], listener: UpdateCallback<this, P>, squash?: false, once?: boolean): Callback;
288
+ // Squash
289
+ on <S extends Model.SelectEvents<this>> (via: S, cb: (keys: Selector.From<S>[]) => void, squash: true, once?: boolean): Callback;
290
+ on <P extends Model.EventsCompat<this>> (key: P | P[], listener: (keys: P[]) => void, squash: true, once?: boolean): Callback;
291
+ // Unknown
292
+ on <S extends Model.SelectEvents<this>> (via: S, cb: unknown, squash: boolean, once?: boolean): Callback;
293
+ on <P extends Model.EventsCompat<this>> (key: P | P[], listener: unknown, squash: boolean, once?: boolean): Callback;
294
+
295
+ // Keyed
296
+ once <S extends Model.SelectEvents<this>> (via: S, cb: Selector.Callback<S, this>, squash?: false): Callback;
297
+ once <P extends Model.EventsCompat<this>> (key: P | P[], listener: UpdateCallback<this, P>, squash?: false): Callback;
298
+ // Squash
299
+ once <S extends Model.SelectEvents<this>> (via: S, cb: (keys: Selector.From<S>[]) => void, squash: true): Callback;
300
+ once <P extends Model.EventsCompat<this>> (key: P | P[], listener: (keys: P[]) => void, squash: true): Callback;
301
+ // Promise
302
+ once <S extends Model.SelectEvents<this>> (via: S): Promise<Selector.From<S>[]>;
303
+ once <P extends Model.EventsCompat<this>> (key: P | P[]): Promise<P[]>;
304
+ // Unknown
305
+ once <S extends Model.SelectEvents<this>> (via: S, cb: unknown, squash: boolean): Callback;
306
+ once <P extends Model.EventsCompat<this>> (key: P | P[], listener: unknown, squash: boolean): Callback;
307
+
308
+ effect(callback: (this: this, state: this) => void, select?: Model.SelectFields<this>): Callback;
309
+ effect(callback: (this: this, state: this) => void, select?: (keyof this)[]): Callback;
172
310
 
173
311
  /**
174
312
  * **React Hook** - Attach to instance of this controller within ambient component.
@@ -177,7 +315,7 @@ export abstract class Model {
177
315
  *
178
316
  * @param id - Argument passed to controller-lifecycle methods. Use to identify the consumer.
179
317
  */
180
- tag(id?: Key): this;
318
+ tag(id?: any): this;
181
319
 
182
320
  /**
183
321
  * **React Hook** - Attach to instance of this controller within ambient component.
@@ -186,7 +324,16 @@ export abstract class Model {
186
324
  *
187
325
  * @param idFactory - Will be invoked with fetched instance. Use this to register a tag as-needed.
188
326
  */
189
- tag(idFactory: (idFactory: this) => Key | void): this;
327
+ tag(idFactory: (idFactory: this) => any): this;
328
+
329
+ /** Use symbol to access controller of a model. */
330
+ static CONTROL: typeof CONTROL;
331
+
332
+ /** Use symbol to access current state of a model. */
333
+ static STATE: typeof STATE;
334
+
335
+ /** Use symbol to access current subscriber of a model in a live context (e.g. hook or effect). */
336
+ static LOCAL: typeof LOCAL;
190
337
 
191
338
  /**
192
339
  * Creates a new instance of this controller.
@@ -202,8 +349,11 @@ export abstract class Model {
202
349
  *
203
350
  * Differs from `use()` in lacking subscription and lifecycle events.
204
351
  * Much more efficient if you don't need hook-based features.
352
+ *
353
+ * @param args - Arguments passed to constructor of `this`
354
+ * @param callback - Run after creation of instance.
205
355
  */
206
- static new <T extends Class> (this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
356
+ static new <T extends Class> (this: T, args?: ConstructorParameters<T>, callback?: (instance: InstanceOf<T>) => void): InstanceOf<T>;
207
357
 
208
358
  /**
209
359
  * **React Hook** - Create and attach an instance of this controller a react component.
@@ -211,8 +361,9 @@ export abstract class Model {
211
361
  * Note: Model will be destroyed when ambient component unmounts!
212
362
  *
213
363
  * @param args - Arguments passed to constructor of `this`
364
+ * @param callback - Run after creation of instance.
214
365
  */
215
- static use <T extends Class> (this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
366
+ static use <T extends Class> (this: T, args?: ConstructorParameters<T>, callback?: (instance: InstanceOf<T>) => void): InstanceOf<T>;
216
367
 
217
368
  /**
218
369
  * **React Hook** - Similar to `use`, will instanciate a controller bound to ambient component.
@@ -220,7 +371,7 @@ export abstract class Model {
220
371
  *
221
372
  * @param data - Data to be applied to controller upon creation.
222
373
  */
223
- static uses <T extends Class, I extends InstanceOf<T>, D extends Partial<I>> (this: T, data: D): I;
374
+ static uses <T extends Class, I extends InstanceOf<T>, D extends Partial<I>> (this: T, data: D, only?: (keyof D)[]): I;
224
375
 
225
376
  /**
226
377
  * **React Hook** - Similar to `uses`, will instanciate a controller includive of given data.
@@ -229,7 +380,13 @@ export abstract class Model {
229
380
  *
230
381
  * @param data - Data to be observed by controller.
231
382
  */
232
- static using <T extends Class, I extends InstanceOf<T>, D extends Partial<I>> (this: T, data: D): I;
383
+ static using <T extends Class, I extends InstanceOf<T>, D extends Partial<I>> (this: T, data: D, only?: (keyof D)[]): I;
384
+
385
+ /**
386
+ * **React Hook** - Locate most relevant instance of this type in context.
387
+ */
388
+ static find <T extends Class>(this: T, strict: true): InstanceOf<T>;
389
+ static find <T extends Class>(this: T, strict?: boolean): InstanceOf<T> | undefined;
233
390
 
234
391
  /**
235
392
  * **React Hook** - Fetch most instance of this controller from context, if it exists.
@@ -300,7 +457,7 @@ export abstract class Model {
300
457
  *
301
458
  * Documentation TBD.
302
459
  */
303
- static meta <T extends Class>(this: T): T & Controller;
460
+ static meta <T extends Class>(this: T): T;
304
461
 
305
462
  /**
306
463
  * **React Hook** - Fetch and subscribe to value defined on class itself using selectors.
@@ -309,25 +466,27 @@ export abstract class Model {
309
466
  */
310
467
  static meta <T extends Class, K extends Model.SelectField<T>> (this: T, key?: K): ReturnType<K>;
311
468
 
312
- /**
313
- * **React Hook** - Locate most relevant instance of this type in context.
314
- */
315
- static find <T extends Class>(this: T): InstanceOf<T>;
316
-
317
469
  /**
318
470
  * Static equivalent of `x instanceof this`.
319
471
  *
320
472
  * Will determine if provided class is a subtype of this one.
321
473
  */
322
474
  static isTypeof <T extends Class>(this: T, subject: any): subject is T;
323
-
324
- /**
325
- * Retreives parent inherited by this class, unless it is base `Model`.
326
- */
327
- static inherits: typeof Model | undefined;
328
475
  }
329
476
 
330
477
  export class Singleton extends Model {
331
- /** Current instance of this controller accessable anywhere. */
332
- static current?: Singleton;
478
+ /** Get current instance of this singleton. Will throw if one does not exist. */
479
+ static is<T extends Class>(this: T): InstanceOf<T>;
480
+
481
+ /**
482
+ * Update the active instance of this class.
483
+ * Returns a thenable; resolves after successful update.
484
+ * If instance does not already exist, one will be created.
485
+ **/
486
+ static update<T extends Class>(
487
+ this: T, updates: Model.Data<InstanceOf<T>>
488
+ ): PromiseLike<string[] | false>;
489
+
490
+ /** Destroy current instance of Singleton, if it exists. */
491
+ static reset(): void;
333
492
  }
package/types/selector.ts CHANGED
@@ -1,3 +1,7 @@
1
+ export type KeysOfType<T, ST> = {
2
+ [Key in keyof T]: T[Key] extends ST ? Key : never
3
+ }[keyof T];
4
+
1
5
  export namespace Selector {
2
6
  /**
3
7
  * Shallow-recursive clone of source object.
@@ -26,4 +30,18 @@ export namespace Selector {
26
30
 
27
31
  export type Gets<F extends Function<any>> =
28
32
  F extends (spy: any) => infer R ? Values<R> : never;
33
+
34
+ export type Callback<S extends Function<any>, T> =
35
+ (this: T, value: Gets<S>, key: From<S>) => void;
36
+ }
37
+
38
+ export namespace Select {
39
+ export type One<T> =
40
+ (arg: { [K in keyof T]: K }) => string & keyof T;
41
+
42
+ export type Key<T extends string | number | symbol> =
43
+ (arg: { [K in T]: K }) => string & T;
44
+
45
+ export type From<T extends One<any>> =
46
+ T extends (from: any) => infer R ? R : never;
29
47
  }
package/types/types.d.ts CHANGED
@@ -9,4 +9,10 @@ export type IfApplicable<T extends {}, K> = K extends keyof T ? T[K] : undefined
9
9
  export type Callback = () => void;
10
10
  export type RequestCallback = (keys?: string[]) => void;
11
11
  export type EffectCallback<T, A = T> = (this: T, argument: A) => Callback | Promise<any> | void;
12
- export type UpdateCallback<T, P> = (this: T, value: IfApplicable<T, P>, changed: P) => void;
12
+ export type UpdateCallback<T, P> = (this: T, value: IfApplicable<T, P>, changed: P) => void;
13
+ export type InterceptCallback<T> = (argument: T) => ((next: T) => void) | Promise<any> | void | boolean;
14
+
15
+ export type RefFunction = (e: HTMLElement | null) => void;
16
+
17
+ export type Recursive<T> = { [K in keyof T]: Recursive<Omit<T, K>> };
18
+ export type Query = (select: Recursive<{}>) => void;
@@ -1,56 +0,0 @@
1
- import { Model } from './model';
2
- import { Selector } from './selector';
3
- import { Callback, EffectCallback, UpdateCallback } from './types';
4
-
5
- type CallbackFor<S extends Selector.Function<any>, T> =
6
- (this: T, value: Selector.Gets<S>, key: Selector.From<S>) => void;
7
-
8
- /**
9
- * Observable Instance
10
- *
11
- * Implements internal value tracking.
12
- * Able to be subscribed to, per-value to know when updated.
13
- */
14
- interface Dispatch {
15
- // Explicit all
16
- on <S extends Model.SelectEvents<this>> (via: S, cb: CallbackFor<S, this>, squash?: false, once?: boolean): Callback;
17
- on <P extends Model.EventsCompat<this>> (key: P | P[], listener: UpdateCallback<this, P>, squash?: false, once?: boolean): Callback;
18
-
19
- once <S extends Model.SelectEvents<this>> (via: S, cb: CallbackFor<S, this>, squash?: false): Callback;
20
- once <P extends Model.EventsCompat<this>> (key: P | P[], listener: UpdateCallback<this, P>, squash?: false): Callback;
21
-
22
- // Explicit squash
23
- on <S extends Model.SelectEvents<this>> (via: S, cb: (keys: Selector.From<S>[]) => void, squash: true, once?: boolean): Callback;
24
- on <P extends Model.EventsCompat<this>> (key: P | P[], listener: (keys: P[]) => void, squash: true, once?: boolean): Callback;
25
-
26
- once <S extends Model.SelectEvents<this>> (via: S, cb: (keys: Selector.From<S>[]) => void, squash: true): Callback;
27
- once <P extends Model.EventsCompat<this>> (key: P | P[], listener: (keys: P[]) => void, squash: true): Callback;
28
-
29
- // Implicit squash
30
- once <S extends Model.SelectEvents<this>> (via: S): Promise<Selector.From<S>[]>;
31
- once <P extends Model.EventsCompat<this>> (key: P | P[]): Promise<P[]>;
32
-
33
- // Unknown squash
34
- on <S extends Model.SelectEvents<this>> (via: S, cb: unknown, squash: boolean, once?: boolean): Callback;
35
- on <P extends Model.EventsCompat<this>> (key: P | P[], listener: unknown, squash: boolean, once?: boolean): Callback;
36
-
37
- once <S extends Model.SelectEvents<this>> (via: S, cb: unknown, squash: boolean): Callback;
38
- once <P extends Model.EventsCompat<this>> (key: P | P[], listener: unknown, squash: boolean): Callback;
39
-
40
- effect(callback: EffectCallback<this>, select?: Model.SelectFields<this>): Callback;
41
- effect(callback: EffectCallback<this>, select?: (keyof this)[]): Callback;
42
-
43
- import <O extends Model.Data<this>> (via: O, select?: string[] | Model.SelectFields<this>): void;
44
-
45
- export(): Model.Entries<this>;
46
- export <P extends Model.Fields<this>> (select: P[]): Pick<this, P>;
47
- export <S extends Model.SelectFields<this>> (select: S): Pick<this, Selector.From<S>>;
48
-
49
- update(keys: Model.SelectFields<this>): void;
50
- update(keys: Model.Fields<this>[]): void;
51
-
52
- requestUpdate(strict?: boolean): Promise<string[] | false>;
53
- requestUpdate(cb: (keys: string[]) => void): void;
54
- }
55
-
56
- export = Dispatch;
package/types/react.d.ts DELETED
@@ -1,46 +0,0 @@
1
- import { ReactElement } from "react";
2
- import { Model } from "./model"
3
- import { Class } from "./types";
4
-
5
- type ComponentOutput = ReactElement<any, any> | null;
6
-
7
- type ProviderForProps<E> = E extends Class
8
- ? ({ of: E, children?: React.ReactNode } & Model.Data<InstanceType<E>>)
9
- | ({ of: E, children?: (instance: InstanceType<E>) => React.ReactNode } & Model.Data<InstanceType<E>>)
10
- : ({ of: E, children?: React.ReactNode } & Model.Data<E>);
11
-
12
- type ConsumerProps<E extends Class> =
13
- | {
14
- /** Type of controller to fetch from context. */
15
- of: E;
16
- /**
17
- * Getter function. Is called on every natural render of this component.
18
- * Will throw if usable instance cannot be found in context.
19
- */
20
- has: (value: InstanceType<E>) => void;
21
- }
22
- | {
23
- /** Type of controller to fetch from context. */
24
- of: E;
25
- /** Getter function. Is called on every natural render of this component. */
26
- get: (value: InstanceType<E> | undefined) => void;
27
- }
28
- | {
29
- /** Type of controller to fetch from context. */
30
- of: E;
31
- /**
32
- * Render function, will receive instance of desired controller.
33
- *
34
- * Similar to `tap()`, updates to properties accessed in
35
- * this function will cause a refresh when they change.
36
- */
37
- children: (value: InstanceType<E>) => ComponentOutput;
38
- }
39
-
40
- // type ProvideCollection = Array<Model | typeof Model> | BunchOf<Model | typeof Model>;
41
- // declare function Provider(props: ProviderOfProps): ComponentOutput;
42
-
43
- declare function Provider<T>(props: ProviderForProps<T>): ComponentOutput;
44
- declare function Consumer<T extends Class>(props: ConsumerProps<T>): ComponentOutput;
45
-
46
- export { Consumer, Provider };