@expressive/mvc 1.0.0-beta.32 → 1.0.0-beta.35

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.
@@ -1,5 +1,5 @@
1
1
  import Model from '.';
2
- import { Async, Class, InstanceOf, InterceptCallback } from './types';
2
+ import { Async, Class, InstanceOf } from './types';
3
3
 
4
4
  /**
5
5
  * Run instruction as controller sets itself up.
@@ -15,57 +15,22 @@ export function use <T extends Model> (): T | undefined;
15
15
  /**
16
16
  * Create a new child instance of model.
17
17
  */
18
- export function use <T extends Class> (Type: T): InstanceOf<T>;
19
-
20
- /**
21
- * Create a new child-instance of specified controller, in readonly mode.
22
- * Will prevent assignment of new value.
23
- *
24
- * **Note:** This is only enforced at runtime. Use
25
- * [`readonly`](https://www.typescriptlang.org/docs/handbook/2/classes.html#readonly), if writing in typescript.
26
- */
27
- export function use <T extends Class> (Type: T, readonly: true): InstanceOf<T>;
28
- export function use <T extends Model> (from: () => T, readonly: true): T;
29
-
30
- /**
31
- * Create a new child-instance of specified controller, in optional mode.
32
- * Will allow assignment of undefined, and likewise should be checked before usage.
33
- */
34
- export function use <T extends Class> (Type: T, required: false): InstanceOf<T> | undefined;
35
- export function use <T extends Model> (from: () => T | undefined, required: false): T | undefined;
36
-
37
- /**
38
- * Create a new child-instance of specified controller.
39
- *
40
- * By default, property is non-nullable. If callback returns `true` the first time, will be in readonly mode.
41
- * If callback returns false, property may also be set to undefined, as needed.
42
- */
43
- export function use <T extends Class> (Type: T, callback: (i: InstanceOf<T>) => true): InstanceOf<T>;
44
- export function use <T extends Class> (Type: T, callback: (i: InstanceOf<T>) => false): InstanceOf<T> | undefined;
45
- export function use <T extends Class> (Type: T, callback: (i: InstanceOf<T>) => void): InstanceOf<T>;
18
+ export function use <T extends Class> (Type: T, callback?: (i: InstanceOf<T>) => void): InstanceOf<T>;
46
19
 
47
20
  /**
48
21
  * Create a new child-instance from factory function.
49
22
  */
50
- export function use <T extends Model> (from: () => T): T;
51
- export function use <T extends Model> (from: () => T, callback: (i: T) => true): T;
52
- export function use <T extends Model> (from: () => T | undefined, callback: (i: T) => false): T | undefined;
53
- export function use <T extends Model> (from: () => T, callback: (i: T) => void): T;
23
+ export function use <T extends Model> (from: () => T, callback?: (i: T) => void): T;
54
24
 
55
25
  /**
56
- * Createa child-instance relationship with provided model.
26
+ * Create child-instance relationship with provided model.
57
27
  *
58
28
  * Note: If `peer` is not already initialized before parent is
59
29
  * (created with `new` as opposed to create method), that model will
60
30
  * attach this via `parent()` instruction. It will not, if
61
31
  * already active.
62
32
  */
63
- export function use <T extends Model> (peer: T): T;
64
- export function use <T extends Model> (peer: T, readonly: true): T;
65
- export function use <T extends Model> (peer: T, required: false): T | undefined;
66
- export function use <T extends Model> (peer: T, callback: (i: T) => true): T;
67
- export function use <T extends Model> (peer: T, callback: (i: T) => false): T | undefined;
68
- export function use <T extends Model> (peer: T, callback: (i: T) => void): T;
33
+ export function use <T extends Model> (peer: T, callback?: (i: T) => void): T;
69
34
 
70
35
  /**
71
36
  * Generate a child controller from specified object. Object's values are be trackable, as would be for a full-model.
@@ -74,12 +39,19 @@ export function use <T extends Model> (peer: T, callback: (i: T) => void): T;
74
39
  */
75
40
  export function use <T extends {}> (object: T): T;
76
41
 
42
+ export namespace child {
43
+ type Instruction<T> = (this: Model.Controller, key: string) => {
44
+ get: () => T | undefined,
45
+ set?: (value: T | undefined) => void
46
+ }
47
+ }
48
+
77
49
  /**
78
50
  * Generic instruction used by `use()` and `tap()` for recursive subscription.
79
51
  *
80
52
  * @param from - Instruction body is run upon parent create. Return function to fetch current value of field.
81
53
  */
82
- export function child <T extends Model> (from: (this: Model.Controller, key: string) => () => T | undefined): T;
54
+ export function child <T extends Model> (from: child.Instruction<T>): T;
83
55
 
84
56
  /**
85
57
  * Fetches and assigns the controller which spawned this host.
@@ -117,6 +89,11 @@ export function tap <T extends typeof Model> (Type: T, callback?: (instance?: In
117
89
  */
118
90
  export function tap <T extends Class> (Type: T, required?: boolean): InstanceOf<T>;
119
91
 
92
+ export namespace on {
93
+ type Callback<T, S = any> = (this: S, argument: T) =>
94
+ ((next: T) => void) | Promise<any> | void | boolean;
95
+ }
96
+
120
97
  /**
121
98
  * Cause property to synchronously run effect upon update.
122
99
  *
@@ -129,15 +106,22 @@ export function tap <T extends Class> (Type: T, required?: boolean): InstanceOf<
129
106
  *
130
107
  * @param initialValue - Beginning value of host property.
131
108
  * @param onUpdate - Effect-callback fired upon set of host property.
109
+ *
110
+ * @deprecated will be removed in favor of `set`
132
111
  */
133
- export function on <T> (initialValue: undefined, onUpdate: InterceptCallback<T>): T | undefined;
134
- export function on <T> (initialValue: T, onUpdate: InterceptCallback<T>): T;
135
- export function on <T, S> (initialValue: undefined, onUpdate: InterceptCallback<T, S>): T | undefined;
136
- export function on <T, S> (initialValue: T, onUpdate: InterceptCallback<T, S>): T;
112
+ export function on <T> (initialValue: undefined, onUpdate: on.Callback<T>): T | undefined;
113
+ export function on <T> (initialValue: T, onUpdate: on.Callback<T>): T;
114
+ export function on <T, S> (initialValue: undefined, onUpdate: on.Callback<T, S>): T | undefined;
115
+ export function on <T, S> (initialValue: T, onUpdate: on.Callback<T, S>): T;
137
116
 
138
117
  /** Object with references to all managed values of `T`. */
139
118
  export type Refs <T extends Model> = { [P in Model.Fields<T>]: Model.Ref<T[P]> };
140
119
 
120
+ export namespace ref {
121
+ type Callback<T, S = any> = (this: S, argument: T) =>
122
+ ((next: T) => void) | Promise<any> | void | boolean;
123
+ }
124
+
141
125
  /**
142
126
  * Creates an object with references to all managed values.
143
127
  * Each property will set value in state when invoked.
@@ -156,8 +140,8 @@ export function ref <T extends Model> (target: T): Refs<T>;
156
140
  *
157
141
  * @param callback - Optional callback to synchronously fire when reference is first set or does update.
158
142
  */
159
- export function ref <T = HTMLElement> (callback?: InterceptCallback<T>): Model.Ref<T>;
160
- export function ref <T, S> (callback?: InterceptCallback<T, S>): Model.Ref<T>;
143
+ export function ref <T = HTMLElement> (callback?: ref.Callback<T>): Model.Ref<T>;
144
+ export function ref <T, S> (callback?: ref.Callback<T, S>): Model.Ref<T>;
161
145
 
162
146
  /**
163
147
  * Sets an exotic method with managed ready-state. Property accepts an async function.
@@ -177,8 +161,10 @@ export function act <S> (action: Async<S>): typeof action & { active: boolean };
177
161
  *
178
162
  * @param compute - Factory function to generate a getter to subscribe dependancies.
179
163
  */
180
- export function from <R> (compute: (property: string) => <T>(this: T, state: T) => R): R;
181
- export function from <R, T> (compute: (property: string) => (this: T, state: T) => R): R;
164
+ export function from <R> (compute: (property: string) => <T>(this: T, state: T) => R | void, suspense: true): R;
165
+ export function from <R, T> (compute: (property: string) => (this: T, state: T) => R | void, suspense: true): R;
166
+ export function from <R> (compute: (property: string) => <T>(this: T, state: T) => R, suspense?: boolean): R;
167
+ export function from <R, T> (compute: (property: string) => (this: T, state: T) => R, suspense?: boolean): R;
182
168
 
183
169
  /**
184
170
  * Implement a computed value; output will be generated by provided function.
@@ -187,8 +173,11 @@ export function from <R, T> (compute: (property: string) => (this: T, state: T)
187
173
  *
188
174
  * @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value.
189
175
  * Will update automatically as input values change.
176
+ *
177
+ * @param suspense - Compute function will suspend
190
178
  */
191
- export function from <T extends Class, R> (source: T, compute: (this: InstanceOf<T>, on: InstanceOf<T>) => R): R;
179
+ export function from <T extends Class, R> (source: T, compute: (this: InstanceOf<T>, on: InstanceOf<T>) => R | void, suspense: true): R;
180
+ export function from <T extends Class, R> (source: T, compute: (this: InstanceOf<T>, on: InstanceOf<T>) => R, suspense?: boolean): R;
192
181
 
193
182
  /**
194
183
  * Implement a computed value; output will be generated by provided function.
@@ -206,6 +195,8 @@ export function from <R, T> (source: T, compute: (this: T, on: T) => R): R;
206
195
  *
207
196
  * @param compute - Factory for memoized value.
208
197
  * @param lazy - Wait until accessed to introduce value.
198
+ *
199
+ * @deprecated will be removed in favor of `set`
209
200
  */
210
201
  export function memo <R> (compute: () => R, lazy?: boolean): R;
211
202
  export function memo <R, T> (compute: (this: T) => R, lazy?: boolean): R;
@@ -214,12 +205,16 @@ export function memo <R, T> (compute: (this: T) => R, lazy?: boolean): R;
214
205
  * Flag property as not to be tracked. Useful if changes often with no real-time impact.
215
206
  *
216
207
  * @param value - starting value of property.
208
+ *
209
+ * @deprecated will be removed in major version 1.0
217
210
  */
218
211
  export function lazy <T> (value?: T): T;
219
212
 
220
213
  /**
221
- * Pending value. Will suspend unless value is defined,
222
- * suspense resolves when a value is aquired.
214
+ * Pending value. Will suspend unless value is already defined,
215
+ * suspense resolves when a value is then aquired.
216
+ *
217
+ * @deprecated will be removed in favor of `set`
223
218
  */
224
219
  export function pending <T = boolean> (): T;
225
220
 
@@ -227,6 +222,8 @@ export function pending <T = boolean> (): T;
227
222
  * Suspend during the execution of given function to obtain value.
228
223
  *
229
224
  * @param waitFor - Async function to suspend for.
225
+ *
226
+ * @deprecated will be removed in favor of `set`
230
227
  */
231
228
  export function pending (waitFor: () => Promise<void>): undefined;
232
229
  export function pending <T> (waitFor: (this: T) => Promise<void>): undefined;
@@ -250,4 +247,80 @@ export function pending <R, T> (waitFor: (this: T) => Promise<R>): R;
250
247
  * @param compute - Compute function returns output value. Bound to a subscriber of source.
251
248
  * Will re-run automatically as input values change.
252
249
  */
253
- export function pending <R, T> (source: T, compute: (this: T, on: T) => R): R;
250
+ export function pending <R, T> (source: T, compute: (this: T, on: T) => R): R; // ✅
251
+
252
+ export namespace set {
253
+ type Factory<T, S = unknown> = (this: S, key: string, subject: S) => T;
254
+
255
+ type Callback<T, S = any> = (this: S, argument: T) =>
256
+ ((next: T) => void) | Promise<any> | void | boolean;
257
+ }
258
+
259
+ /**
260
+ * Set property with a placeholder.
261
+ *
262
+ * Property cannot be accessed until it is defined. If accessed while undefined, a hybrid
263
+ * `Promise`/`Error` (ala: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.
264
+ */
265
+ export function set <T = any>(): T;
266
+
267
+ /**
268
+ * Set property with starting value `undefined`.
269
+ *
270
+ * If required and accessed while still empty, React Suspense will be thrown.
271
+ * Property will reject any assignment of undefined.
272
+ *
273
+ * @param value - Starting value of host property (undefined).
274
+ * @param required - Property will suspend callee if undefined at time of access.
275
+ */
276
+ export function set <T> (value: undefined, required: false): T | undefined;
277
+ export function set <T> (value: undefined, required?: boolean): T;
278
+
279
+ export function set <T> (value: undefined, onUpdate: set.Callback<T>): T | undefined;
280
+ export function set <T, S> (value: undefined, onUpdate: set.Callback<T, S>): T | undefined;
281
+
282
+ /**
283
+ * Set property with an async function.
284
+ *
285
+ * Property cannot be accessed until factory resolves, yeilding a result.
286
+ * If accessed while processing, React Suspense will be thrown.
287
+ *
288
+ * - `required: true` (default) -
289
+ * Run factory immediately upon creation of model instance.
290
+ * - `required: false` -
291
+ * Run factory only if/when accessed.
292
+ * Value will always throw suspense at least once - use with caution.
293
+ *
294
+ * @param factory - Callback run to derrive property value.
295
+ * @param required - (default: true) Run factory immediately on creation, otherwise on access.
296
+ */
297
+ export function set <T>(factory: set.Factory<Promise<T>>, required: false): T | undefined;
298
+ export function set <T, S>(factory: set.Factory<Promise<T>, S>, required: false): T | undefined;
299
+
300
+ export function set <T>(factory: set.Factory<Promise<T>>, required?: boolean): T;
301
+ export function set <T, S>(factory: set.Factory<Promise<T>, S>, required?: boolean): T;
302
+
303
+ export function set <T> (value: set.Factory<Promise<T>>, onUpdate: set.Callback<T>): T;
304
+ export function set <T, S> (value: set.Factory<Promise<T>, S>, onUpdate: set.Callback<T, S>): T;
305
+
306
+ /**
307
+ * Set property with a factory function.
308
+ *
309
+ * - `required: true` (default) -
310
+ * Run factory immediately upon creation of model instance.
311
+ * - `required: false` -
312
+ * Run factory only if/when accessed.
313
+ * Value will always throw suspense at least once - use with caution.
314
+ *
315
+ * @param factory - Callback run to derrive property value.
316
+ * @param required - (default: true) Run factory immediately on creation, otherwise on access.
317
+ */
318
+ export function set <T>(factory: set.Factory<T>, required: false): T | undefined;
319
+ export function set <T, S>(factory: set.Factory<T, S>, required: false): T | undefined;
320
+
321
+ export function set <T>(factory: set.Factory<T>, required?: boolean): T;
322
+ export function set <T, S>(factory: set.Factory<T, S>, required?: boolean): T;
323
+
324
+ export function set <T> (value: set.Factory<T>, onUpdate: set.Callback<T>): T;
325
+ export function set <T, S> (value: set.Factory<T, S>, onUpdate: set.Callback<T, S>): T;
326
+
@@ -1,4 +1,6 @@
1
- import { Key } from "./types";
1
+ declare namespace Lifecycle {
2
+ type Key = string | symbol | number;
3
+ }
2
4
 
3
5
  /**
4
6
  * Model-Component Lifecycle
@@ -8,17 +10,17 @@ import { Key } from "./types";
8
10
  * call these respectively to that component's lifecycle.
9
11
  */
10
12
  interface Lifecycle {
11
- didMount?(tag?: Key): void;
12
- willRender?(tag?: Key): void;
13
- willUpdate?(tag?: Key): void;
14
- willMount?(tag?: Key): void;
15
- willUnmount?(tag?: Key): void;
13
+ didMount?(tag?: Lifecycle.Key): void;
14
+ willRender?(tag?: Lifecycle.Key): void;
15
+ willUpdate?(tag?: Lifecycle.Key): void;
16
+ willMount?(tag?: Lifecycle.Key): void;
17
+ willUnmount?(tag?: Lifecycle.Key): void;
16
18
 
17
- elementDidMount?(tag: Key): void;
18
- elementWillRender?(tag: Key): void;
19
- elementWillUpdate?(tag: Key): void;
20
- elementWillMount?(tag: Key): void;
21
- elementWillUnmount?(tag: Key): void;
19
+ elementDidMount?(tag: Lifecycle.Key): void;
20
+ elementWillRender?(tag: Lifecycle.Key): void;
21
+ elementWillUpdate?(tag: Lifecycle.Key): void;
22
+ elementWillMount?(tag: Lifecycle.Key): void;
23
+ elementWillUnmount?(tag: Lifecycle.Key): void;
22
24
 
23
25
  componentDidMount?(): void;
24
26
  componentWillRender?(): void;