@expressive/mvc 1.0.0-beta.3 → 1.0.0-beta.30

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,108 +1,253 @@
1
1
  import Model from '.';
2
- import { Async, Class, EffectCallback, InstanceOf, RefFunction } from './types';
3
-
4
- declare namespace Directives {
5
- /**
6
- * Creates a new child-instance of specified controller.
7
- *
8
- * @param Peer - Type of Model to create and apply to host property.
9
- * @param callback - Fired after controller is created and ready for use.
10
- */
11
- export function setChild <T extends typeof Model> (Peer: T, callback?: (i: InstanceOf<T>) => void): InstanceOf<T>
12
-
13
- /**
14
- * Fetches and assigns the controller which spawned this host.
15
- * When parent assigns an instance via `use()` directive, it
16
- * will be made available to child upon this request.
17
- *
18
- * @param Expects - Type of controller compatible with this class.
19
- * @param required - Throw if controller is created independantly.
20
- */
21
- export function setParent <T extends typeof Model> (Expects: T, required: true): InstanceOf<T>;
22
- export function setParent <T extends typeof Model> (Expects: T, required?: false): InstanceOf<T> | undefined;
23
-
24
- /**
25
- * Find and attach most applicable instance of Model via context.
26
- *
27
- * Expects a `<Provider>` of target controller to exist.
28
- * Host controller will search element-hierarchy relative to where it spawned.
29
- *
30
- * @param Type - Type of controller to attach to property.
31
- * @param required - Throw if instance of Type cannot be found.
32
- */
33
- export function setPeer <T extends Class> (Type: T, required?: boolean): InstanceOf<T>;
34
-
35
- /**
36
- * Sets property to synchronously call an effect upon update (much like a setter).
37
- *
38
- * **Note:** Use generic-type to specifiy property signature.
39
- * Value will always start off as undefined.
40
- *
41
- * @param callback - Effect-callback fired upon update of host property.
42
- */
43
- export function setEffect <T = any> (callback: EffectCallback<T>): T | undefined;
44
-
45
- /**
46
- * Sets property to synchronously call effect upon update.
47
- *
48
- * @param starting - Beginning value of host property.
49
- * @param callback - Effect-callback fired upon set of host property.
50
- */
51
- export function setEffect <T = any> (starting: T, callback: EffectCallback<T>): T;
52
-
53
- /**
54
- * Creates a ref-compatible property for use with components.
55
- * Will persist value, and updates to this are made part of controller event-stream.
56
- *
57
- * *Output is simultaneously a ref-function and ref-object, use as needed.*
58
- *
59
- * @param callback - Optional callback to synchronously fire when reference is first set or does update.
60
- */
61
- export function setReference <T = HTMLElement> (callback?: EffectCallback<T>): ((next: T) => void) & { current: T | null };
62
-
63
- /**
64
- * Ref-functions for all properties of your state. Will bind elements to the current values.
65
- *
66
- * For `<input />` elements, this is two-way binding.
67
- */
68
- export function setBindings <T extends Model> (from: T): Model.Overlay<T, RefFunction>;
69
-
70
- /**
71
- * Sets an exotic method with managed ready-state. Property accepts an async function.
72
- *
73
- * When an act-method is invoked, its `active` property to true for duration of call.
74
- * This is emitted as an update to property, both when called and after returns (or throws).
75
- *
76
- * **Note:** Subsequent calls will immediately throw if one is still pending.
77
- *
78
- * @param action - Action to fire when resulting property is invoked.
79
- */
80
- export function setAction <T extends Async>(action: T): T & { active: boolean };
81
-
82
- /**
83
- * Implement a computed value; output will be generated by provided function.
84
- *
85
- * *Alternative to using native getters; works exactly the same!*
86
- *
87
- * @param fn - Factory function to generate value and subscribe dependancies.
88
- */
89
- export function setComputed <T, R>(fn: (this: T, on: T) => R): R;
90
-
91
- /**
92
- * Memoized value. Computes and stores a value returned by provided factory.
93
- * Equivalent to a getter, sans the automatic updates.
94
- *
95
- * @param compute - Factory for memoized value.
96
- * @param lazy - Wait until accessed to introduce value.
97
- */
98
- export function setMemo <T> (compute: () => T, lazy?: boolean): T;
99
-
100
- /**
101
- * Flag property as not to be tracked. Useful if changes often with no real-time impact.
102
- *
103
- * @param value - starting value of property.
104
- */
105
- export function setIgnored <T> (value?: T): T;
106
- }
107
-
108
- export = Directives;
2
+ import { Async, Class, InstanceOf, InterceptCallback } from './types';
3
+
4
+ /**
5
+ * Run instruction as controller sets itself up.
6
+ * This will specialize the behavior of a given property.
7
+ **/
8
+ export function apply <T = any> (instruction: Model.Instruction<T>, name?: string): T;
9
+
10
+ /**
11
+ * Create a placeholder for specified Model type.
12
+ */
13
+ export function use <T extends Model> (): T | undefined;
14
+
15
+ /**
16
+ * Create a new child instance of model.
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>;
46
+
47
+ /**
48
+ * Create a new child-instance from factory function.
49
+ */
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;
54
+
55
+ /**
56
+ * Createa child-instance relationship with provided model.
57
+ *
58
+ * Note: If `peer` is not already initialized before parent is
59
+ * (created with `new` as opposed to create method), that model will
60
+ * attach this via `parent()` instruction. It will not, if
61
+ * already active.
62
+ */
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;
69
+
70
+ /**
71
+ * Generate a child controller from specified object. Object's values are be trackable, as would be for a full-model.
72
+ *
73
+ * Note: Child will *not* be same object as one provided.
74
+ */
75
+ export function use <T extends {}> (object: T): T;
76
+
77
+ /**
78
+ * Generic instruction used by `use()` and `tap()` for recursive subscription.
79
+ *
80
+ * @param from - Instruction body is run upon parent create. Return function to fetch current value of field.
81
+ */
82
+ export function child <T extends Model> (from: (this: Model.Controller, key: string) => () => T | undefined): T;
83
+
84
+ /**
85
+ * Fetches and assigns the controller which spawned this host.
86
+ * When parent assigns an instance via `use()` directive, it
87
+ * will be made available to child upon this request.
88
+ *
89
+ * @param Expects - Type of controller compatible with this class.
90
+ * @param required - Throw if controller is created independantly.
91
+ */
92
+ export function parent <T extends typeof Model> (Expects: T, required: true): InstanceOf<T>;
93
+ export function parent <T extends typeof Model> (Expects: T, required?: false): InstanceOf<T> | undefined;
94
+
95
+ /**
96
+ * Find and attach most applicable instance of Model via context.
97
+ *
98
+ * Host controller will search element-hierarchy relative to where it spawned.
99
+ *
100
+ * @param Type - Type of model to find from context
101
+ * @param callback -
102
+ * - Invoked after context is scanned, is passed result - either found or undefined.
103
+ * - If argument is inadequate, but required, your implemention should simply throw.
104
+ * - If inadequate and not required, conditionally return false.
105
+ */
106
+ export function tap <T extends typeof Model> (Type: T, callback?: (instance?: InstanceOf<T>) => void | true): InstanceOf<T>;
107
+ export function tap <T extends typeof Model> (Type: T, callback?: (instance?: InstanceOf<T>) => void | false): InstanceOf<T> | undefined;
108
+
109
+ /**
110
+ * Find and attach most applicable instance of Model via context.
111
+ *
112
+ * Expects a `<Provider>` of target controller to exist.
113
+ * Host controller will search element-hierarchy relative to where it spawned.
114
+ *
115
+ * @param Type - Type of controller to attach to property.
116
+ * @param required - Throw if instance of Type cannot be found.
117
+ */
118
+ export function tap <T extends Class> (Type: T, required?: boolean): InstanceOf<T>;
119
+
120
+ /**
121
+ * Cause property to synchronously run effect upon update.
122
+ *
123
+ * Callback may return an effect callback, Promise (ignored), or a boolean.
124
+ *
125
+ * If a boolean is returned explicitly, new value will be dropped. Useful to override.
126
+ *
127
+ * - `false` will prevent update.
128
+ * - `true` will emit update (unchanged).
129
+ *
130
+ * @param initialValue - Beginning value of host property.
131
+ * @param onUpdate - Effect-callback fired upon set of host property.
132
+ */
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;
137
+
138
+ /** Object with references to all managed values of `T`. */
139
+ export type Refs <T extends Model> = { [P in Model.Fields<T>]: Model.Ref<T[P]> };
140
+
141
+ /**
142
+ * Creates an object with references to all managed values.
143
+ * Each property will set value in state when invoked.
144
+ *
145
+ * *Properties are simultaneously a ref-function and ref-object, use as needed.*
146
+ *
147
+ * @param target - Source model from which to reference values.
148
+ */
149
+ export function ref <T extends Model> (target: T): Refs<T>;
150
+
151
+ /**
152
+ * Creates a ref-compatible property.
153
+ * Will persist value, and updates to this are made part of controller event-stream.
154
+ *
155
+ * *Output is simultaneously a ref-function and ref-object, use as needed.*
156
+ *
157
+ * @param callback - Optional callback to synchronously fire when reference is first set or does update.
158
+ */
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>;
161
+
162
+ /**
163
+ * Sets an exotic method with managed ready-state. Property accepts an async function.
164
+ *
165
+ * When an act-method is invoked, its `active` property to true for duration of call.
166
+ * This is emitted as an update to property, both when called and after returns (or throws).
167
+ *
168
+ * **Note:** Subsequent calls will immediately throw if one is still pending.
169
+ *
170
+ * @param action - Action to fire when resulting property is invoked.
171
+ */
172
+ export function act (action: Async): typeof action & { active: boolean };
173
+ export function act <S> (action: Async<S>): typeof action & { active: boolean };
174
+
175
+ /**
176
+ * Implement a computed value; output is returned by function from provided factory.
177
+ *
178
+ * @param compute - Factory function to generate a getter to subscribe dependancies.
179
+ */
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;
182
+
183
+ /**
184
+ * Implement a computed value; output will be generated by provided function.
185
+ *
186
+ * @param source - Source Model to find and subscribe to, for input.
187
+ *
188
+ * @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value.
189
+ * Will update automatically as input values change.
190
+ */
191
+ export function from <T extends Class, R> (source: T, compute: (this: InstanceOf<T>, on: InstanceOf<T>) => R): R;
192
+
193
+ /**
194
+ * Implement a computed value; output will be generated by provided function.
195
+ *
196
+ * @param source - Source model from which computed value will be a subscriber.
197
+ *
198
+ * @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value.
199
+ * Will update automatically as input values change.
200
+ */
201
+ export function from <R, T> (source: T, compute: (this: T, on: T) => R): R;
202
+
203
+ /**
204
+ * Memoized value. Computes and stores a value returned by provided factory.
205
+ * Equivalent to a getter, sans the automatic updates.
206
+ *
207
+ * @param compute - Factory for memoized value.
208
+ * @param lazy - Wait until accessed to introduce value.
209
+ */
210
+ export function memo <R> (compute: () => R, lazy?: boolean): R;
211
+ export function memo <R, T> (compute: (this: T) => R, lazy?: boolean): R;
212
+
213
+ /**
214
+ * Flag property as not to be tracked. Useful if changes often with no real-time impact.
215
+ *
216
+ * @param value - starting value of property.
217
+ */
218
+ export function lazy <T> (value?: T): T;
219
+
220
+ /**
221
+ * Pending value. Will suspend unless value is defined,
222
+ * suspense resolves when a value is aquired.
223
+ */
224
+ export function pending <T = boolean> (): T;
225
+
226
+ /**
227
+ * Suspend during the execution of given function to obtain value.
228
+ *
229
+ * @param waitFor - Async function to suspend for.
230
+ */
231
+ export function pending (waitFor: () => Promise<void>): undefined;
232
+ export function pending <T> (waitFor: (this: T) => Promise<void>): undefined;
233
+ export function pending <R> (waitFor: () => Promise<R>): R;
234
+ export function pending <R, T> (waitFor: (this: T) => Promise<R>): R;
235
+
236
+ /**
237
+ * Suspend for a computed value, returned by syncronous function.
238
+ *
239
+ * If accessed while computing to undefined, will throw a promise. Function is subscribed
240
+ * and will re-evaluate anytime used properties change. Once function returns any value,
241
+ * outstanding promises resolve and are not thrown on subsequent access of this property.
242
+ *
243
+ * Similar to `from`, compute function will only run when inputs change and not upon access.
244
+ *
245
+ * This behavior is recursive. If return value _becomes_ undefined, property
246
+ * access will throw Promises again, awaiting its next value.
247
+ *
248
+ * @param source - Source object, from which computed value will be a subscriber.
249
+ *
250
+ * @param compute - Compute function returns output value. Bound to a subscriber of source.
251
+ * Will re-run automatically as input values change.
252
+ */
253
+ export function pending <R, T> (source: T, compute: (this: T, on: T) => R): R;