@expressive/mvc 0.33.0 → 0.35.0-alpha.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/dist/index.d.ts +312 -0
- package/dist/index.js +715 -0
- package/dist/index.mjs +681 -0
- package/package.json +12 -10
- package/lib/index.d.ts +0 -411
- package/lib/index.esm.js +0 -1052
- package/lib/index.js +0 -1065
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
type Async<T = any, Y extends any[] = any> = {
|
|
2
|
+
(...args: Y): Promise<T>;
|
|
3
|
+
active: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Sets an exotic method with managed ready-state. Property accepts an async function.
|
|
7
|
+
*
|
|
8
|
+
* When a run-method is invoked, its `active` property to true for duration of call.
|
|
9
|
+
* This is emitted as an update to property, both when called and after returns (or throws).
|
|
10
|
+
*
|
|
11
|
+
* **Note:** Subsequent calls will immediately throw if one is still pending.
|
|
12
|
+
*
|
|
13
|
+
* @param action - Action to fire when resulting property is invoked.
|
|
14
|
+
*/
|
|
15
|
+
declare function run<T, Y extends any[]>(action: (...args: Y) => T | Promise<T>): Async<T, Y>;
|
|
16
|
+
|
|
17
|
+
type Predicate = (key: string) => boolean | void;
|
|
18
|
+
declare namespace Model {
|
|
19
|
+
/** Any type of Model, using own class constructor as its identifier. */
|
|
20
|
+
type Type<T extends Model = Model> = abstract new (...args: any[]) => T;
|
|
21
|
+
/** A type of Model which may be created without constructor arguments. */
|
|
22
|
+
type New<T extends Model = Model> = (new () => T) & typeof Model;
|
|
23
|
+
/** Subset of `keyof T` which are not methods or defined by base Model U. **/
|
|
24
|
+
type Key<T> = Exclude<keyof T, keyof Model> & string;
|
|
25
|
+
type Any<T> = Exclude<keyof T, keyof Model> | (string & {});
|
|
26
|
+
/** Actual value stored in state. */
|
|
27
|
+
type Value<R> = R extends Ref<infer T> ? T : R extends Model ? Export<R> : R;
|
|
28
|
+
type ValueOf<T extends Model, K extends Any<T>> = K extends keyof T ? T[K] extends Ref<infer V> ? V : T[K] : unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Values from current state of given controller.
|
|
31
|
+
* Differs from `Values` as values here will drill
|
|
32
|
+
* into "real" values held by exotics like ref.
|
|
33
|
+
*/
|
|
34
|
+
type Export<T> = {
|
|
35
|
+
[P in Key<T>]: Value<T[P]>;
|
|
36
|
+
};
|
|
37
|
+
/** Object comperable to data found in T. */
|
|
38
|
+
type Values<T> = {
|
|
39
|
+
[P in Key<T>]?: Value<T[P]>;
|
|
40
|
+
};
|
|
41
|
+
/** Exotic value, where actual value is contained within. */
|
|
42
|
+
type Ref<T = any> = {
|
|
43
|
+
(next: T): void;
|
|
44
|
+
current: T | null;
|
|
45
|
+
};
|
|
46
|
+
/** A callback function which is subscribed to parent and updates when values change. */
|
|
47
|
+
type Effect<T> = (this: T, argument: T) => Callback | Promise<void> | null | void;
|
|
48
|
+
type Event = (key: string) => Callback | void;
|
|
49
|
+
namespace Instruction {
|
|
50
|
+
type Getter<T> = (source: Model) => T;
|
|
51
|
+
type Setter<T> = (value: T, previous: T) => boolean | void | (() => T);
|
|
52
|
+
type Descriptor<T = any> = {
|
|
53
|
+
get?: Getter<T> | boolean;
|
|
54
|
+
set?: Setter<T> | false;
|
|
55
|
+
enumerable?: boolean;
|
|
56
|
+
value?: T;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Property initializer, will run upon instance creation.
|
|
61
|
+
* Optional returned callback will run when once upon first access.
|
|
62
|
+
*/
|
|
63
|
+
type Instruction<T = any, M extends Model = any> = (this: M, key: Model.Key<M>, thisArg: M, state: Model.Export<M>) => Instruction.Descriptor<T> | Instruction.Getter<T> | void;
|
|
64
|
+
}
|
|
65
|
+
interface Model {
|
|
66
|
+
/**
|
|
67
|
+
* Loopback to instance of this model. This is useful when in a subscribed context,
|
|
68
|
+
* to keep write access to `this` after a destructure. You can use it to read variables silently as well.
|
|
69
|
+
**/
|
|
70
|
+
is: this;
|
|
71
|
+
}
|
|
72
|
+
declare class Model {
|
|
73
|
+
constructor(id?: string | number);
|
|
74
|
+
/** Pull current values from state. Flattens all models and exotic values amongst properties. */
|
|
75
|
+
get(): Model.Export<this>;
|
|
76
|
+
/** Run a function which will run automatically when accessed values change. */
|
|
77
|
+
get(effect: Model.Effect<this>): Callback;
|
|
78
|
+
get<T extends string>(key: T, required: true): Exclude<Model.ValueOf<this, T>, undefined>;
|
|
79
|
+
get<T extends string>(key: T, required?: boolean): Model.ValueOf<this, T>;
|
|
80
|
+
/**
|
|
81
|
+
* Check if update is in progress.
|
|
82
|
+
* Returns a promise which resolves updated keys. If no update, will resolve `false`.
|
|
83
|
+
**/
|
|
84
|
+
set(): Promise<Model.Values<this> | false>;
|
|
85
|
+
/**
|
|
86
|
+
* Expect an update to state within a set timeout.
|
|
87
|
+
*
|
|
88
|
+
* @param timeout - milliseconds to wait for update
|
|
89
|
+
* @param predicate - optional function to filter for specific updates. If returns true, update is accepted.
|
|
90
|
+
*
|
|
91
|
+
* @returns a promise to resolve when next accepted update has occured.
|
|
92
|
+
*/
|
|
93
|
+
set(timeout: number, predicate?: Predicate): Promise<Model.Values<this>>;
|
|
94
|
+
/**
|
|
95
|
+
* Call a function whenever an update to state has occured.
|
|
96
|
+
*
|
|
97
|
+
* @param callback - Function to call when any update occurs.
|
|
98
|
+
* Note: This will be called for every assignment which changes the value.
|
|
99
|
+
* To run logic on final value only, this callback may return a function.
|
|
100
|
+
* Returning the same function for the same (or multiple for that matter)
|
|
101
|
+
* will ensure it is only called once.
|
|
102
|
+
*
|
|
103
|
+
* @returns a function to remove listener. Will return true if listener did exist.
|
|
104
|
+
*/
|
|
105
|
+
set(callback: Model.Event): () => boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Push an update without changing the value of associated property.
|
|
108
|
+
*
|
|
109
|
+
* This is useful where a property value internally has changed, but the object remains the same.
|
|
110
|
+
* For example: an array which has pushed a new value, or change to nested properties.
|
|
111
|
+
*/
|
|
112
|
+
set(key: string | null): void;
|
|
113
|
+
/**
|
|
114
|
+
* Update a property programatically within state.
|
|
115
|
+
*
|
|
116
|
+
* @param key - property to update
|
|
117
|
+
* @param value - value to update property with (if the same as current, no update will occur)
|
|
118
|
+
* @param silent - if true, will not notify listeners of an update
|
|
119
|
+
*/
|
|
120
|
+
set<K extends string>(key: K, value?: Model.ValueOf<this, K>, silent?: boolean): void;
|
|
121
|
+
/** Iterate over managed properties in this instance of Model. */
|
|
122
|
+
[Symbol.iterator](): Iterator<[string, unknown]>;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new instance of this controller.
|
|
125
|
+
*
|
|
126
|
+
* Beyond `new this(...)`, method will activate managed-state.
|
|
127
|
+
*
|
|
128
|
+
* @param args - arguments sent to constructor
|
|
129
|
+
*/
|
|
130
|
+
static new<T extends typeof Model>(this: T, ...args: ConstructorParameters<T>): InstanceType<T>;
|
|
131
|
+
/**
|
|
132
|
+
* Static equivalent of `x instanceof this`.
|
|
133
|
+
* Determines if provided class is a subtype of this one.
|
|
134
|
+
* If so, language server will make available all static
|
|
135
|
+
* methods and properties of this class.
|
|
136
|
+
*/
|
|
137
|
+
static is<T extends Model.Type>(this: T, maybe: any): maybe is T;
|
|
138
|
+
}
|
|
139
|
+
declare function add<T = any, M extends Model = any>(instruction: Model.Instruction): T;
|
|
140
|
+
|
|
141
|
+
declare namespace get {
|
|
142
|
+
type Function<T, S = any> = (this: S, on: S) => T;
|
|
143
|
+
type Factory<R, T> = (this: T, property: string, on: T) => Function<R, T>;
|
|
144
|
+
type Source<T extends Model = Model> = (resolve: (x: T) => void) => void;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Fetches and assigns the controller which spawned this host.
|
|
148
|
+
* When parent assigns an instance via `use()` directive, it
|
|
149
|
+
* will be made available to child upon this request.
|
|
150
|
+
*
|
|
151
|
+
* @param Type - Type of controller compatible with this class.
|
|
152
|
+
* @param required - If true, will throw if Model is created without a parent.
|
|
153
|
+
*/
|
|
154
|
+
declare function get<T extends Model>(Type: Model.Type<T>, required?: true): T;
|
|
155
|
+
/**
|
|
156
|
+
* Fetches and assigns the controller which spawned this host.
|
|
157
|
+
* When parent assigns an instance via `use()` directive, it
|
|
158
|
+
* will be made available to child upon this request.
|
|
159
|
+
*
|
|
160
|
+
* @param Type - Type of controller compatible with this class.
|
|
161
|
+
* @param required - If false, property may be undefined. Otherwise will throw suspense.
|
|
162
|
+
*/
|
|
163
|
+
declare function get<T extends Model>(Type: Model.Type<T>, required: boolean): T | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* Implement a computed value from a foreign model. Output will be generated by provided function,
|
|
166
|
+
* where the foreign model will be passed to that function.
|
|
167
|
+
*
|
|
168
|
+
* @param source - Type of Model to fetch for computation.
|
|
169
|
+
* @param compute - Compute function. Will update automatically as input values change.
|
|
170
|
+
*/
|
|
171
|
+
declare function get<R, T extends Model>(Type: Model.Type<T>, compute: (this: T, on: T) => R): R;
|
|
172
|
+
/**
|
|
173
|
+
* Implement a computed value; output will be generated by provided function.
|
|
174
|
+
*
|
|
175
|
+
* @param source - Source model from which computed value will be a subscriber.
|
|
176
|
+
* @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value. Will update automatically as input values change.
|
|
177
|
+
*/
|
|
178
|
+
declare function get<R, T extends Model>(source: T, compute: (this: T, on: T) => R): R;
|
|
179
|
+
/**
|
|
180
|
+
* Implement a computed value; output is returned by function from provided factory.
|
|
181
|
+
*
|
|
182
|
+
* @param compute - Factory function to generate a getter to subscribe dependancies.
|
|
183
|
+
*/
|
|
184
|
+
declare function get<R, T>(compute: (property: string, on: T) => (this: T, state: T) => R): R;
|
|
185
|
+
|
|
186
|
+
declare namespace ref {
|
|
187
|
+
type Callback<T> = (argument: T) => ((next: T | null) => void) | Promise<void> | void | boolean;
|
|
188
|
+
interface Object<T = any> {
|
|
189
|
+
(next: T | null): void;
|
|
190
|
+
current: T | null;
|
|
191
|
+
}
|
|
192
|
+
/** Object with references to all managed values of `T`. */
|
|
193
|
+
type Proxy<T extends Model> = {
|
|
194
|
+
[P in Model.Key<T>]-?: Model.Ref<T[P]>;
|
|
195
|
+
};
|
|
196
|
+
type CustomProxy<T extends Model, R> = {
|
|
197
|
+
[P in Model.Key<T>]-?: R;
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Creates an object with references to all managed values.
|
|
202
|
+
* Each property is a function to set value in state when invoked.
|
|
203
|
+
*
|
|
204
|
+
* *Properties are simultaneously a ref-function and ref-object, use as needed.*
|
|
205
|
+
*
|
|
206
|
+
* @param target - Source model from which to reference values.
|
|
207
|
+
*/
|
|
208
|
+
declare function ref<T extends Model>(target: T): ref.Proxy<T>;
|
|
209
|
+
/**
|
|
210
|
+
* Creates an object with values based on managed values.
|
|
211
|
+
* Each property will invoke mapper function on first access supply
|
|
212
|
+
* the its return value going forward.
|
|
213
|
+
*
|
|
214
|
+
* @param target - Source model from which to reference values.
|
|
215
|
+
* @param mapper - Function producing the placeholder value for any given property.
|
|
216
|
+
*/
|
|
217
|
+
declare function ref<O extends Model, R>(target: O, mapper: (key: Model.Key<O>) => R): ref.CustomProxy<O, R>;
|
|
218
|
+
/**
|
|
219
|
+
* Creates a ref-compatible property.
|
|
220
|
+
* Will persist value, and updates to this are made part of controller event-stream.
|
|
221
|
+
*
|
|
222
|
+
* *Output is simultaneously a ref-function and ref-object, use as needed.*
|
|
223
|
+
*
|
|
224
|
+
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
225
|
+
*/
|
|
226
|
+
declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>;
|
|
227
|
+
/**
|
|
228
|
+
* Creates a ref-compatible property.
|
|
229
|
+
* Will persist value, and updates to this are made part of controller event-stream.
|
|
230
|
+
*
|
|
231
|
+
* *Output is simultaneously a ref-function and ref-object, use as needed.*
|
|
232
|
+
*
|
|
233
|
+
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
234
|
+
* @param ignoreNull - Default `true`. If `false`, will still callback with `null`.
|
|
235
|
+
*/
|
|
236
|
+
declare function ref<T = HTMLElement>(callback: ref.Callback<T | null>, ignoreNull: boolean): ref.Object<T>;
|
|
237
|
+
|
|
238
|
+
declare namespace set {
|
|
239
|
+
type Callback<T, S = any> = (this: S, next: T, previous: T) => ((next: T) => void) | Promise<any> | void | boolean;
|
|
240
|
+
type Factory<T, S = any> = (this: S, key: string, thisArg: S) => Promise<T> | T;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Set property with a placeholder.
|
|
244
|
+
*
|
|
245
|
+
* Property cannot be accessed until it is defined. If accessed while undefined, a hybrid
|
|
246
|
+
* `Promise`/`Error` (ala: [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html)) will be thrown.
|
|
247
|
+
*/
|
|
248
|
+
declare function set<T = any>(): T;
|
|
249
|
+
/**
|
|
250
|
+
* Set property with a factory function.
|
|
251
|
+
*
|
|
252
|
+
* Run factory only if/when accessed. Value will be undefined until
|
|
253
|
+
* factory resolves, which will also dispatch an update for the property.
|
|
254
|
+
*/
|
|
255
|
+
declare function set<T>(factory: set.Factory<T> | Promise<T>, required: false): T | undefined;
|
|
256
|
+
/**
|
|
257
|
+
* Set property with a factory function.
|
|
258
|
+
*
|
|
259
|
+
* If async, property cannot be accessed until resolves, yeilding a result.
|
|
260
|
+
* If accessed while still processing, React Suspense will be thrown.
|
|
261
|
+
*
|
|
262
|
+
* @param factory - Callback run to derrive property value.
|
|
263
|
+
* @param required - If true run factory immediately on creation, otherwise on access.
|
|
264
|
+
*/
|
|
265
|
+
declare function set<T>(factory: set.Factory<T> | Promise<T>, required?: boolean): T;
|
|
266
|
+
/**
|
|
267
|
+
* Set a property with empty placeholder and update callback.
|
|
268
|
+
*
|
|
269
|
+
* @param value - Starting value for property. If undefined, suspense will be thrown on access, until value is set and accepted by callback.
|
|
270
|
+
* @param onUpdate - Callback run when property is set. If returns false, update is not accepted and property will keep previous value.
|
|
271
|
+
*/
|
|
272
|
+
declare function set<T>(value: T | undefined, onUpdate: set.Callback<T>): T;
|
|
273
|
+
|
|
274
|
+
declare namespace use {
|
|
275
|
+
type Object<T extends {}> = T;
|
|
276
|
+
}
|
|
277
|
+
/** Create a placeholder for specified Model type. */
|
|
278
|
+
declare function use<T extends Model>(): T | undefined;
|
|
279
|
+
/** Create a new child instance of model. */
|
|
280
|
+
declare function use<T extends Model>(Type: Model.New<T>, ready?: (i: T) => void): T;
|
|
281
|
+
/**
|
|
282
|
+
* Use existing model as a child of model assigned to.
|
|
283
|
+
*
|
|
284
|
+
* Note: If `peer` is not already initialized before parent is
|
|
285
|
+
* (created with `new` as opposed to create method), that model will
|
|
286
|
+
* attach this via `parent()` instruction. It will not, however, if
|
|
287
|
+
* already active.
|
|
288
|
+
**/
|
|
289
|
+
declare function use<T extends Model>(model: T, ready?: (i: T) => void): T;
|
|
290
|
+
/** Create a managed object with observable entries. */
|
|
291
|
+
declare function use<T extends {}, O = use.Object<T>>(data: T, ready?: (object: O) => void): O;
|
|
292
|
+
|
|
293
|
+
declare namespace Context {
|
|
294
|
+
type Inputs = {
|
|
295
|
+
[key: string | number]: Model | Model.New;
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
declare class Context {
|
|
299
|
+
static resolve(from: Model, callback: (context: Context) => void): void;
|
|
300
|
+
constructor(inputs?: Context.Inputs);
|
|
301
|
+
key: string;
|
|
302
|
+
private table;
|
|
303
|
+
private layer;
|
|
304
|
+
private has;
|
|
305
|
+
get<T extends Model>(Type: Model.Type<T>): T | undefined;
|
|
306
|
+
include(inputs: Context.Inputs): Map<Model, boolean>;
|
|
307
|
+
protected add<T extends Model>(input: T | Model.New<T>, implicit?: boolean): T;
|
|
308
|
+
push(): this;
|
|
309
|
+
pop(): void;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { Context, Model, add, Model as default, get, ref, run, set, use };
|