@expressive/mvc 0.50.0 → 0.50.1
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 +14 -11
- package/dist/index.js +97 -84
- package/dist/index.mjs +97 -84
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ type MaybeAsync<T> = T | Promise<T>;
|
|
|
2
2
|
declare namespace Model {
|
|
3
3
|
/** Any type of Model, using own class constructor as its identifier. */
|
|
4
4
|
type Type<T extends Model = Model> = (abstract new (...args: any[]) => T) & typeof Model;
|
|
5
|
+
/** A Model constructor, but which is not abstract. */
|
|
6
|
+
type Init<T extends Model = Model> = (new (...args: Model.Args<T>) => T) & Omit<typeof Model, never>;
|
|
5
7
|
/**
|
|
6
8
|
* Model constructor callback - is called when Model finishes intializing.
|
|
7
9
|
* Returned function will call when model is destroyed.
|
|
@@ -82,7 +84,7 @@ interface Model {
|
|
|
82
84
|
**/
|
|
83
85
|
is: this;
|
|
84
86
|
}
|
|
85
|
-
declare class Model {
|
|
87
|
+
declare abstract class Model {
|
|
86
88
|
constructor(...args: Model.Args);
|
|
87
89
|
/** Pull current values from state. Flattens all models and exotic values recursively. */
|
|
88
90
|
get(): Model.State<this>;
|
|
@@ -184,14 +186,14 @@ declare class Model {
|
|
|
184
186
|
*
|
|
185
187
|
* @param args - arguments sent to constructor
|
|
186
188
|
*/
|
|
187
|
-
static new<T extends Model>(this: Model.
|
|
189
|
+
static new<T extends Model>(this: Model.Init<T>, ...args: Model.Args<T>): T;
|
|
188
190
|
/**
|
|
189
191
|
* Static equivalent of `x instanceof this`.
|
|
190
192
|
* Determines if provided class is a subtype of this one.
|
|
191
193
|
* If so, language server will make available all static
|
|
192
194
|
* methods and properties of this class.
|
|
193
195
|
*/
|
|
194
|
-
static is<T extends Model.Type>(this: T, maybe:
|
|
196
|
+
static is<T extends Model.Type>(this: T, maybe: unknown): maybe is T;
|
|
195
197
|
/**
|
|
196
198
|
* Register a callback to run when any instance of this Model is updated.
|
|
197
199
|
*/
|
|
@@ -293,7 +295,7 @@ declare function ref<O extends Model, R>(target: O, mapper: (key: Model.Field<O>
|
|
|
293
295
|
*
|
|
294
296
|
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
295
297
|
*/
|
|
296
|
-
declare function ref<T
|
|
298
|
+
declare function ref<T>(callback?: ref.Callback<T>): ref.Object<T>;
|
|
297
299
|
/**
|
|
298
300
|
* Creates a ref-compatible property.
|
|
299
301
|
* Will persist value, and updates to this are made part of controller event-stream.
|
|
@@ -303,7 +305,7 @@ declare function ref<T = HTMLElement>(callback?: ref.Callback<T>): ref.Object<T>
|
|
|
303
305
|
* @param callback - Optional callback to synchronously fire when reference is first set or does update.
|
|
304
306
|
* @param ignoreNull - Default `true`. If `false`, will still callback with `null`.
|
|
305
307
|
*/
|
|
306
|
-
declare function ref<T
|
|
308
|
+
declare function ref<T>(callback: ref.Callback<T | null>, ignoreNull: boolean): ref.Object<T>;
|
|
307
309
|
|
|
308
310
|
declare namespace set {
|
|
309
311
|
type Callback<T, S = any> = (this: S, next: T, previous: T) => ((next: T) => void) | Promise<any> | void | boolean;
|
|
@@ -342,8 +344,8 @@ declare function set<T>(factory: set.Factory<T> | Promise<T>, required?: boolean
|
|
|
342
344
|
declare function set<T>(value: T | undefined, onUpdate: set.Callback<T>): T;
|
|
343
345
|
|
|
344
346
|
declare function use<T = any>(instruction: Model.Instruction): T;
|
|
345
|
-
declare function use<T extends Model>(Type: Model.
|
|
346
|
-
declare function use<T extends Model>(Type: Model.
|
|
347
|
+
declare function use<T extends Model>(Type: Model.Init<T>, required: false): T | undefined;
|
|
348
|
+
declare function use<T extends Model>(Type: Model.Init<T>, ready?: (i: T) => void): T;
|
|
347
349
|
|
|
348
350
|
declare namespace has {
|
|
349
351
|
type Callback<T = any> = (model: T, recipient: Model) => void | boolean | (() => void);
|
|
@@ -364,9 +366,12 @@ declare function effect<T extends {}>(target: T, callback: Effect<Required<T>>,
|
|
|
364
366
|
declare function effect<T extends {}>(target: T, callback: Effect<T>, requireValues?: boolean): () => void;
|
|
365
367
|
|
|
366
368
|
declare namespace Context {
|
|
367
|
-
type Input = Record<string | number, Model | Model.
|
|
369
|
+
type Input = Record<string | number, Model | Model.Init>;
|
|
368
370
|
type Expect = (model: Model) => (() => void) | void;
|
|
369
371
|
}
|
|
372
|
+
interface Context {
|
|
373
|
+
[key: symbol]: Model | Context.Expect | null | undefined;
|
|
374
|
+
}
|
|
370
375
|
declare class Context {
|
|
371
376
|
static get<T extends Model>(on: Model, callback: ((got: Context) => void)): void;
|
|
372
377
|
static get<T extends Model>(on: Model): Context | undefined;
|
|
@@ -379,9 +384,7 @@ declare class Context {
|
|
|
379
384
|
push(inputs?: Context.Input): this;
|
|
380
385
|
pop(): void;
|
|
381
386
|
include(inputs: Context.Input, forEach?: (added: Model, explicit: boolean) => void): void;
|
|
382
|
-
protected add<T extends Model>(input: T | Model.
|
|
383
|
-
protected has<T extends Model>(type: Model.Type<T>, model: T): void;
|
|
384
|
-
protected put<T extends Model>(T: Model.Type<T>, I: T | ((model: T) => void), implicit?: boolean): void;
|
|
387
|
+
protected add<T extends Model>(input: T | Model.Init<T>, implicit?: boolean): T;
|
|
385
388
|
}
|
|
386
389
|
|
|
387
390
|
declare global {
|
package/dist/index.js
CHANGED
|
@@ -305,6 +305,7 @@ var Model = class _Model {
|
|
|
305
305
|
* @param args - arguments sent to constructor
|
|
306
306
|
*/
|
|
307
307
|
static new(...args) {
|
|
308
|
+
_Model.is(this);
|
|
308
309
|
const instance = new this(...args);
|
|
309
310
|
emit(instance, true);
|
|
310
311
|
return instance;
|
|
@@ -316,6 +317,8 @@ var Model = class _Model {
|
|
|
316
317
|
* methods and properties of this class.
|
|
317
318
|
*/
|
|
318
319
|
static is(maybe) {
|
|
320
|
+
if (maybe === _Model)
|
|
321
|
+
throw new Error("Model is abstract and not a valid type.");
|
|
319
322
|
return typeof maybe == "function" && maybe.prototype instanceof this;
|
|
320
323
|
}
|
|
321
324
|
/**
|
|
@@ -409,9 +412,9 @@ function assign(to, values) {
|
|
|
409
412
|
to[key2] = values[key2];
|
|
410
413
|
}
|
|
411
414
|
function bindMethods(type) {
|
|
412
|
-
let
|
|
413
|
-
if (!
|
|
414
|
-
METHODS.set(type,
|
|
415
|
+
let keys2 = METHODS.get(type);
|
|
416
|
+
if (!keys2) {
|
|
417
|
+
METHODS.set(type, keys2 = bindMethods(Object.getPrototypeOf(type)).slice());
|
|
415
418
|
const desc = Object.getOwnPropertyDescriptors(type.prototype);
|
|
416
419
|
for (const key2 in desc)
|
|
417
420
|
if (key2 != "constructor" && "value" in desc[key2]) {
|
|
@@ -423,7 +426,7 @@ function bindMethods(type) {
|
|
|
423
426
|
};
|
|
424
427
|
var bind = bind2;
|
|
425
428
|
let { value } = desc[key2];
|
|
426
|
-
|
|
429
|
+
keys2.push(key2);
|
|
427
430
|
Object.defineProperty(type.prototype, key2, {
|
|
428
431
|
set: bind2,
|
|
429
432
|
get() {
|
|
@@ -432,7 +435,7 @@ function bindMethods(type) {
|
|
|
432
435
|
});
|
|
433
436
|
}
|
|
434
437
|
}
|
|
435
|
-
return
|
|
438
|
+
return keys2;
|
|
436
439
|
}
|
|
437
440
|
function uid() {
|
|
438
441
|
return (Math.random() * 0.722 + 0.278).toString(36).substring(2, 8).toUpperCase();
|
|
@@ -440,16 +443,21 @@ function uid() {
|
|
|
440
443
|
|
|
441
444
|
// src/context.ts
|
|
442
445
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
443
|
-
var
|
|
444
|
-
var Downstream = /* @__PURE__ */ new WeakMap();
|
|
446
|
+
var Table = /* @__PURE__ */ new Map();
|
|
445
447
|
function key(T, upstream) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
448
|
+
let K = Table.get(T);
|
|
449
|
+
if (!K)
|
|
450
|
+
Table.set(T, K = Symbol(
|
|
451
|
+
typeof T == "symbol" ? "get " + T.description : String(T)
|
|
452
|
+
));
|
|
453
|
+
return upstream ? key(K) : K;
|
|
454
|
+
}
|
|
455
|
+
function keys(from, upstream) {
|
|
456
|
+
const keys2 = /* @__PURE__ */ new Set();
|
|
457
|
+
do {
|
|
458
|
+
keys2.add(key(from, upstream));
|
|
459
|
+
} while ((from = Object.getPrototypeOf(from)) !== Model);
|
|
460
|
+
return keys2;
|
|
453
461
|
}
|
|
454
462
|
var Context = class _Context {
|
|
455
463
|
constructor(inputs) {
|
|
@@ -460,18 +468,20 @@ var Context = class _Context {
|
|
|
460
468
|
}
|
|
461
469
|
static get(from, callback) {
|
|
462
470
|
const waiting = Register.get(from);
|
|
463
|
-
if (
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
+
if (waiting instanceof _Context) {
|
|
472
|
+
if (callback)
|
|
473
|
+
callback(waiting);
|
|
474
|
+
return waiting;
|
|
475
|
+
}
|
|
476
|
+
if (callback)
|
|
477
|
+
if (waiting)
|
|
478
|
+
waiting.push(callback);
|
|
479
|
+
else
|
|
480
|
+
Register.set(from, [callback]);
|
|
471
481
|
}
|
|
472
482
|
get(Type, callback) {
|
|
473
483
|
if (callback)
|
|
474
|
-
|
|
484
|
+
Object.defineProperty(this, key(Type, true), { value: callback });
|
|
475
485
|
const result = this[key(Type)];
|
|
476
486
|
if (result === null)
|
|
477
487
|
throw new Error(`Did find ${Type} in context, but multiple were defined.`);
|
|
@@ -525,6 +535,7 @@ var Context = class _Context {
|
|
|
525
535
|
let T;
|
|
526
536
|
let I;
|
|
527
537
|
if (typeof input == "function") {
|
|
538
|
+
Model.is(input);
|
|
528
539
|
T = input;
|
|
529
540
|
I = new input();
|
|
530
541
|
this.cleanup.add(() => I.set(null));
|
|
@@ -532,32 +543,27 @@ var Context = class _Context {
|
|
|
532
543
|
I = input;
|
|
533
544
|
T = I.constructor;
|
|
534
545
|
}
|
|
535
|
-
|
|
536
|
-
this.put(T, I, implicit);
|
|
537
|
-
return I;
|
|
538
|
-
}
|
|
539
|
-
has(type, model) {
|
|
540
|
-
do {
|
|
541
|
-
const K = key(type, true);
|
|
546
|
+
keys(T, true).forEach((K) => {
|
|
542
547
|
const result = this[K];
|
|
543
548
|
if (!result)
|
|
544
|
-
|
|
545
|
-
const callback = result(
|
|
549
|
+
return;
|
|
550
|
+
const callback = result(I);
|
|
546
551
|
if (callback)
|
|
547
552
|
this.cleanup.add(callback);
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if (waiting instanceof Array)
|
|
551
|
-
waiting.forEach((cb) => cb(this));
|
|
552
|
-
Register.set(model, this);
|
|
553
|
-
}
|
|
554
|
-
put(T, I, implicit) {
|
|
555
|
-
do {
|
|
556
|
-
const K = key(T, typeof I == "function");
|
|
553
|
+
});
|
|
554
|
+
keys(T).forEach((K) => {
|
|
557
555
|
const value = this.hasOwnProperty(K) ? null : I;
|
|
558
556
|
if (value || this[K] !== I && !implicit)
|
|
559
|
-
Object.defineProperty(this, K, {
|
|
560
|
-
|
|
557
|
+
Object.defineProperty(this, K, {
|
|
558
|
+
configurable: true,
|
|
559
|
+
value
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
const waiting = Register.get(I);
|
|
563
|
+
if (waiting instanceof Array)
|
|
564
|
+
waiting.forEach((cb) => cb(this));
|
|
565
|
+
Register.set(I, this);
|
|
566
|
+
return I;
|
|
561
567
|
}
|
|
562
568
|
};
|
|
563
569
|
|
|
@@ -567,8 +573,8 @@ function use(arg1, arg2) {
|
|
|
567
573
|
const token = Symbol("instruction");
|
|
568
574
|
if (Model.is(arg1)) {
|
|
569
575
|
const type = arg1;
|
|
570
|
-
const value = new type();
|
|
571
576
|
arg1 = (key2, subject) => {
|
|
577
|
+
const desc = { set: set2, get: get2 };
|
|
572
578
|
function set2(next) {
|
|
573
579
|
if (next ? !(next instanceof type) : arg2 !== false)
|
|
574
580
|
throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
|
|
@@ -577,8 +583,12 @@ function use(arg1, arg2) {
|
|
|
577
583
|
arg2(next);
|
|
578
584
|
return false;
|
|
579
585
|
}
|
|
580
|
-
|
|
581
|
-
|
|
586
|
+
function get2() {
|
|
587
|
+
set2(new type());
|
|
588
|
+
desc.get = arg2 !== false;
|
|
589
|
+
return fetch(subject, key2);
|
|
590
|
+
}
|
|
591
|
+
return desc;
|
|
582
592
|
};
|
|
583
593
|
}
|
|
584
594
|
INSTRUCT.set(token, arg1);
|
|
@@ -831,7 +841,46 @@ function has(arg1, arg2) {
|
|
|
831
841
|
const update2 = () => {
|
|
832
842
|
subject.set(key2, Object.freeze(Array.from(applied)));
|
|
833
843
|
};
|
|
834
|
-
if (
|
|
844
|
+
if (Model.is(arg1))
|
|
845
|
+
Context.get(subject, (context) => {
|
|
846
|
+
context.get(arg1, (got) => {
|
|
847
|
+
let remove;
|
|
848
|
+
let disconnect;
|
|
849
|
+
if (applied.has(got))
|
|
850
|
+
return;
|
|
851
|
+
const callback = APPLY.get(got);
|
|
852
|
+
if (callback) {
|
|
853
|
+
const after = callback(subject);
|
|
854
|
+
if (after === false)
|
|
855
|
+
return;
|
|
856
|
+
if (typeof after == "function")
|
|
857
|
+
disconnect = after;
|
|
858
|
+
}
|
|
859
|
+
if (typeof arg2 == "function") {
|
|
860
|
+
const done2 = arg2(got, subject);
|
|
861
|
+
if (done2 === false)
|
|
862
|
+
return false;
|
|
863
|
+
remove = () => {
|
|
864
|
+
if (typeof done2 == "function")
|
|
865
|
+
done2();
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
applied.add(got);
|
|
869
|
+
update2();
|
|
870
|
+
const done = () => {
|
|
871
|
+
reset();
|
|
872
|
+
applied.delete(got);
|
|
873
|
+
update2();
|
|
874
|
+
if (disconnect)
|
|
875
|
+
disconnect();
|
|
876
|
+
if (typeof remove == "function")
|
|
877
|
+
remove();
|
|
878
|
+
};
|
|
879
|
+
const reset = got.get(null, done);
|
|
880
|
+
return done;
|
|
881
|
+
});
|
|
882
|
+
});
|
|
883
|
+
else {
|
|
835
884
|
if (APPLY.has(subject))
|
|
836
885
|
throw new Error(`'has' callback can only be used once per model.`);
|
|
837
886
|
APPLY.set(subject, (recipient) => {
|
|
@@ -850,43 +899,7 @@ function has(arg1, arg2) {
|
|
|
850
899
|
remove();
|
|
851
900
|
};
|
|
852
901
|
});
|
|
853
|
-
}
|
|
854
|
-
Context.get(subject, (ctx) => ctx.get(arg1, (got) => {
|
|
855
|
-
let remove;
|
|
856
|
-
let disconnect;
|
|
857
|
-
if (applied.has(got))
|
|
858
|
-
return;
|
|
859
|
-
const callback = APPLY.get(got);
|
|
860
|
-
if (callback) {
|
|
861
|
-
const after = callback(subject);
|
|
862
|
-
if (after === false)
|
|
863
|
-
return;
|
|
864
|
-
if (typeof after == "function")
|
|
865
|
-
disconnect = after;
|
|
866
|
-
}
|
|
867
|
-
if (typeof arg2 == "function") {
|
|
868
|
-
const done2 = arg2(got, subject);
|
|
869
|
-
if (done2 === false)
|
|
870
|
-
return false;
|
|
871
|
-
remove = () => {
|
|
872
|
-
if (typeof done2 == "function")
|
|
873
|
-
done2();
|
|
874
|
-
};
|
|
875
|
-
}
|
|
876
|
-
applied.add(got);
|
|
877
|
-
update2();
|
|
878
|
-
const done = () => {
|
|
879
|
-
reset();
|
|
880
|
-
applied.delete(got);
|
|
881
|
-
update2();
|
|
882
|
-
if (disconnect)
|
|
883
|
-
disconnect();
|
|
884
|
-
if (typeof remove == "function")
|
|
885
|
-
remove();
|
|
886
|
-
};
|
|
887
|
-
const reset = got.get(null, done);
|
|
888
|
-
return done;
|
|
889
|
-
}));
|
|
902
|
+
}
|
|
890
903
|
return { value: [] };
|
|
891
904
|
});
|
|
892
905
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -271,6 +271,7 @@ var Model = class _Model {
|
|
|
271
271
|
* @param args - arguments sent to constructor
|
|
272
272
|
*/
|
|
273
273
|
static new(...args) {
|
|
274
|
+
_Model.is(this);
|
|
274
275
|
const instance = new this(...args);
|
|
275
276
|
emit(instance, true);
|
|
276
277
|
return instance;
|
|
@@ -282,6 +283,8 @@ var Model = class _Model {
|
|
|
282
283
|
* methods and properties of this class.
|
|
283
284
|
*/
|
|
284
285
|
static is(maybe) {
|
|
286
|
+
if (maybe === _Model)
|
|
287
|
+
throw new Error("Model is abstract and not a valid type.");
|
|
285
288
|
return typeof maybe == "function" && maybe.prototype instanceof this;
|
|
286
289
|
}
|
|
287
290
|
/**
|
|
@@ -375,9 +378,9 @@ function assign(to, values) {
|
|
|
375
378
|
to[key2] = values[key2];
|
|
376
379
|
}
|
|
377
380
|
function bindMethods(type) {
|
|
378
|
-
let
|
|
379
|
-
if (!
|
|
380
|
-
METHODS.set(type,
|
|
381
|
+
let keys2 = METHODS.get(type);
|
|
382
|
+
if (!keys2) {
|
|
383
|
+
METHODS.set(type, keys2 = bindMethods(Object.getPrototypeOf(type)).slice());
|
|
381
384
|
const desc = Object.getOwnPropertyDescriptors(type.prototype);
|
|
382
385
|
for (const key2 in desc)
|
|
383
386
|
if (key2 != "constructor" && "value" in desc[key2]) {
|
|
@@ -389,7 +392,7 @@ function bindMethods(type) {
|
|
|
389
392
|
};
|
|
390
393
|
var bind = bind2;
|
|
391
394
|
let { value } = desc[key2];
|
|
392
|
-
|
|
395
|
+
keys2.push(key2);
|
|
393
396
|
Object.defineProperty(type.prototype, key2, {
|
|
394
397
|
set: bind2,
|
|
395
398
|
get() {
|
|
@@ -398,7 +401,7 @@ function bindMethods(type) {
|
|
|
398
401
|
});
|
|
399
402
|
}
|
|
400
403
|
}
|
|
401
|
-
return
|
|
404
|
+
return keys2;
|
|
402
405
|
}
|
|
403
406
|
function uid() {
|
|
404
407
|
return (Math.random() * 0.722 + 0.278).toString(36).substring(2, 8).toUpperCase();
|
|
@@ -406,16 +409,21 @@ function uid() {
|
|
|
406
409
|
|
|
407
410
|
// src/context.ts
|
|
408
411
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
409
|
-
var
|
|
410
|
-
var Downstream = /* @__PURE__ */ new WeakMap();
|
|
412
|
+
var Table = /* @__PURE__ */ new Map();
|
|
411
413
|
function key(T, upstream) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
414
|
+
let K = Table.get(T);
|
|
415
|
+
if (!K)
|
|
416
|
+
Table.set(T, K = Symbol(
|
|
417
|
+
typeof T == "symbol" ? "get " + T.description : String(T)
|
|
418
|
+
));
|
|
419
|
+
return upstream ? key(K) : K;
|
|
420
|
+
}
|
|
421
|
+
function keys(from, upstream) {
|
|
422
|
+
const keys2 = /* @__PURE__ */ new Set();
|
|
423
|
+
do {
|
|
424
|
+
keys2.add(key(from, upstream));
|
|
425
|
+
} while ((from = Object.getPrototypeOf(from)) !== Model);
|
|
426
|
+
return keys2;
|
|
419
427
|
}
|
|
420
428
|
var Context = class _Context {
|
|
421
429
|
constructor(inputs) {
|
|
@@ -426,18 +434,20 @@ var Context = class _Context {
|
|
|
426
434
|
}
|
|
427
435
|
static get(from, callback) {
|
|
428
436
|
const waiting = Register.get(from);
|
|
429
|
-
if (
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
+
if (waiting instanceof _Context) {
|
|
438
|
+
if (callback)
|
|
439
|
+
callback(waiting);
|
|
440
|
+
return waiting;
|
|
441
|
+
}
|
|
442
|
+
if (callback)
|
|
443
|
+
if (waiting)
|
|
444
|
+
waiting.push(callback);
|
|
445
|
+
else
|
|
446
|
+
Register.set(from, [callback]);
|
|
437
447
|
}
|
|
438
448
|
get(Type, callback) {
|
|
439
449
|
if (callback)
|
|
440
|
-
|
|
450
|
+
Object.defineProperty(this, key(Type, true), { value: callback });
|
|
441
451
|
const result = this[key(Type)];
|
|
442
452
|
if (result === null)
|
|
443
453
|
throw new Error(`Did find ${Type} in context, but multiple were defined.`);
|
|
@@ -491,6 +501,7 @@ var Context = class _Context {
|
|
|
491
501
|
let T;
|
|
492
502
|
let I;
|
|
493
503
|
if (typeof input == "function") {
|
|
504
|
+
Model.is(input);
|
|
494
505
|
T = input;
|
|
495
506
|
I = new input();
|
|
496
507
|
this.cleanup.add(() => I.set(null));
|
|
@@ -498,32 +509,27 @@ var Context = class _Context {
|
|
|
498
509
|
I = input;
|
|
499
510
|
T = I.constructor;
|
|
500
511
|
}
|
|
501
|
-
|
|
502
|
-
this.put(T, I, implicit);
|
|
503
|
-
return I;
|
|
504
|
-
}
|
|
505
|
-
has(type, model) {
|
|
506
|
-
do {
|
|
507
|
-
const K = key(type, true);
|
|
512
|
+
keys(T, true).forEach((K) => {
|
|
508
513
|
const result = this[K];
|
|
509
514
|
if (!result)
|
|
510
|
-
|
|
511
|
-
const callback = result(
|
|
515
|
+
return;
|
|
516
|
+
const callback = result(I);
|
|
512
517
|
if (callback)
|
|
513
518
|
this.cleanup.add(callback);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
if (waiting instanceof Array)
|
|
517
|
-
waiting.forEach((cb) => cb(this));
|
|
518
|
-
Register.set(model, this);
|
|
519
|
-
}
|
|
520
|
-
put(T, I, implicit) {
|
|
521
|
-
do {
|
|
522
|
-
const K = key(T, typeof I == "function");
|
|
519
|
+
});
|
|
520
|
+
keys(T).forEach((K) => {
|
|
523
521
|
const value = this.hasOwnProperty(K) ? null : I;
|
|
524
522
|
if (value || this[K] !== I && !implicit)
|
|
525
|
-
Object.defineProperty(this, K, {
|
|
526
|
-
|
|
523
|
+
Object.defineProperty(this, K, {
|
|
524
|
+
configurable: true,
|
|
525
|
+
value
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
const waiting = Register.get(I);
|
|
529
|
+
if (waiting instanceof Array)
|
|
530
|
+
waiting.forEach((cb) => cb(this));
|
|
531
|
+
Register.set(I, this);
|
|
532
|
+
return I;
|
|
527
533
|
}
|
|
528
534
|
};
|
|
529
535
|
|
|
@@ -533,8 +539,8 @@ function use(arg1, arg2) {
|
|
|
533
539
|
const token = Symbol("instruction");
|
|
534
540
|
if (Model.is(arg1)) {
|
|
535
541
|
const type = arg1;
|
|
536
|
-
const value = new type();
|
|
537
542
|
arg1 = (key2, subject) => {
|
|
543
|
+
const desc = { set: set2, get: get2 };
|
|
538
544
|
function set2(next) {
|
|
539
545
|
if (next ? !(next instanceof type) : arg2 !== false)
|
|
540
546
|
throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
|
|
@@ -543,8 +549,12 @@ function use(arg1, arg2) {
|
|
|
543
549
|
arg2(next);
|
|
544
550
|
return false;
|
|
545
551
|
}
|
|
546
|
-
|
|
547
|
-
|
|
552
|
+
function get2() {
|
|
553
|
+
set2(new type());
|
|
554
|
+
desc.get = arg2 !== false;
|
|
555
|
+
return fetch(subject, key2);
|
|
556
|
+
}
|
|
557
|
+
return desc;
|
|
548
558
|
};
|
|
549
559
|
}
|
|
550
560
|
INSTRUCT.set(token, arg1);
|
|
@@ -797,7 +807,46 @@ function has(arg1, arg2) {
|
|
|
797
807
|
const update2 = () => {
|
|
798
808
|
subject.set(key2, Object.freeze(Array.from(applied)));
|
|
799
809
|
};
|
|
800
|
-
if (
|
|
810
|
+
if (Model.is(arg1))
|
|
811
|
+
Context.get(subject, (context) => {
|
|
812
|
+
context.get(arg1, (got) => {
|
|
813
|
+
let remove;
|
|
814
|
+
let disconnect;
|
|
815
|
+
if (applied.has(got))
|
|
816
|
+
return;
|
|
817
|
+
const callback = APPLY.get(got);
|
|
818
|
+
if (callback) {
|
|
819
|
+
const after = callback(subject);
|
|
820
|
+
if (after === false)
|
|
821
|
+
return;
|
|
822
|
+
if (typeof after == "function")
|
|
823
|
+
disconnect = after;
|
|
824
|
+
}
|
|
825
|
+
if (typeof arg2 == "function") {
|
|
826
|
+
const done2 = arg2(got, subject);
|
|
827
|
+
if (done2 === false)
|
|
828
|
+
return false;
|
|
829
|
+
remove = () => {
|
|
830
|
+
if (typeof done2 == "function")
|
|
831
|
+
done2();
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
applied.add(got);
|
|
835
|
+
update2();
|
|
836
|
+
const done = () => {
|
|
837
|
+
reset();
|
|
838
|
+
applied.delete(got);
|
|
839
|
+
update2();
|
|
840
|
+
if (disconnect)
|
|
841
|
+
disconnect();
|
|
842
|
+
if (typeof remove == "function")
|
|
843
|
+
remove();
|
|
844
|
+
};
|
|
845
|
+
const reset = got.get(null, done);
|
|
846
|
+
return done;
|
|
847
|
+
});
|
|
848
|
+
});
|
|
849
|
+
else {
|
|
801
850
|
if (APPLY.has(subject))
|
|
802
851
|
throw new Error(`'has' callback can only be used once per model.`);
|
|
803
852
|
APPLY.set(subject, (recipient) => {
|
|
@@ -816,43 +865,7 @@ function has(arg1, arg2) {
|
|
|
816
865
|
remove();
|
|
817
866
|
};
|
|
818
867
|
});
|
|
819
|
-
}
|
|
820
|
-
Context.get(subject, (ctx) => ctx.get(arg1, (got) => {
|
|
821
|
-
let remove;
|
|
822
|
-
let disconnect;
|
|
823
|
-
if (applied.has(got))
|
|
824
|
-
return;
|
|
825
|
-
const callback = APPLY.get(got);
|
|
826
|
-
if (callback) {
|
|
827
|
-
const after = callback(subject);
|
|
828
|
-
if (after === false)
|
|
829
|
-
return;
|
|
830
|
-
if (typeof after == "function")
|
|
831
|
-
disconnect = after;
|
|
832
|
-
}
|
|
833
|
-
if (typeof arg2 == "function") {
|
|
834
|
-
const done2 = arg2(got, subject);
|
|
835
|
-
if (done2 === false)
|
|
836
|
-
return false;
|
|
837
|
-
remove = () => {
|
|
838
|
-
if (typeof done2 == "function")
|
|
839
|
-
done2();
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
applied.add(got);
|
|
843
|
-
update2();
|
|
844
|
-
const done = () => {
|
|
845
|
-
reset();
|
|
846
|
-
applied.delete(got);
|
|
847
|
-
update2();
|
|
848
|
-
if (disconnect)
|
|
849
|
-
disconnect();
|
|
850
|
-
if (typeof remove == "function")
|
|
851
|
-
remove();
|
|
852
|
-
};
|
|
853
|
-
const reset = got.get(null, done);
|
|
854
|
-
return done;
|
|
855
|
-
}));
|
|
868
|
+
}
|
|
856
869
|
return { value: [] };
|
|
857
870
|
});
|
|
858
871
|
}
|
package/package.json
CHANGED