@expressive/mvc 0.49.0 → 0.50.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 +20 -23
- package/dist/index.js +260 -235
- package/dist/index.mjs +257 -232
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -9,9 +9,9 @@ var __export = (target, all) => {
|
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let
|
|
13
|
-
if (!__hasOwnProp.call(to,
|
|
14
|
-
__defProp(to,
|
|
12
|
+
for (let key2 of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key2) && key2 !== except)
|
|
14
|
+
__defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
@@ -36,59 +36,44 @@ module.exports = __toCommonJS(src_exports);
|
|
|
36
36
|
var DISPATCH = /* @__PURE__ */ new Set();
|
|
37
37
|
var OBSERVER = /* @__PURE__ */ new WeakMap();
|
|
38
38
|
var LISTENER = /* @__PURE__ */ new WeakMap();
|
|
39
|
-
var REQUIRED = /* @__PURE__ */ new WeakSet();
|
|
40
39
|
var onReady = () => null;
|
|
41
40
|
function addListener(subject, callback, select) {
|
|
42
41
|
let subs = LISTENER.get(subject);
|
|
43
42
|
if (!subs)
|
|
44
43
|
LISTENER.set(subject, subs = /* @__PURE__ */ new Map([[onReady, false]]));
|
|
45
|
-
const filter =
|
|
44
|
+
const filter = select !== void 0 && /* @__PURE__ */ new Set([select]);
|
|
46
45
|
if (!filter && !subs.has(onReady))
|
|
47
46
|
callback(true, subject);
|
|
48
47
|
subs.set(callback, filter);
|
|
49
48
|
return () => subs.delete(callback);
|
|
50
49
|
}
|
|
51
|
-
function watch(from,
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
const listeners = LISTENER.get(from);
|
|
55
|
-
listeners.set(
|
|
56
|
-
observer,
|
|
57
|
-
(listeners.get(observer) || /* @__PURE__ */ new Set()).add(key)
|
|
58
|
-
);
|
|
59
|
-
const nested = LISTENER.get(value);
|
|
60
|
-
if (nested) {
|
|
61
|
-
LISTENER.set(value = Object.create(value), nested);
|
|
62
|
-
OBSERVER.set(value, observer);
|
|
63
|
-
}
|
|
64
|
-
if (value === void 0 && REQUIRED.has(from))
|
|
65
|
-
throw new Error(`${from}.${key} is required in this context.`);
|
|
66
|
-
}
|
|
67
|
-
return value;
|
|
50
|
+
function watch(from, key2, value) {
|
|
51
|
+
const access = OBSERVER.get(from);
|
|
52
|
+
return access ? access(from, key2, value) : value;
|
|
68
53
|
}
|
|
69
54
|
var PENDING = /* @__PURE__ */ new WeakMap();
|
|
70
|
-
function emit(source,
|
|
55
|
+
function emit(source, key2) {
|
|
71
56
|
const subs = LISTENER.get(source);
|
|
72
57
|
const ready = !subs.has(onReady);
|
|
73
|
-
if (
|
|
58
|
+
if (key2 === true && ready)
|
|
74
59
|
return;
|
|
75
60
|
let pending = PENDING.get(source);
|
|
76
61
|
if (pending) {
|
|
77
|
-
pending.add(
|
|
62
|
+
pending.add(key2);
|
|
78
63
|
return;
|
|
79
64
|
}
|
|
80
65
|
PENDING.set(source, pending = new Set(
|
|
81
|
-
|
|
66
|
+
key2 !== true && ready ? [key2] : [true, key2]
|
|
82
67
|
));
|
|
83
|
-
pending.forEach((
|
|
84
|
-
pending.delete(
|
|
68
|
+
pending.forEach((key3) => {
|
|
69
|
+
pending.delete(key3);
|
|
85
70
|
subs.forEach((select, callback) => {
|
|
86
71
|
let after;
|
|
87
|
-
if (!select || select.has(
|
|
88
|
-
if (after = callback.call(source,
|
|
72
|
+
if (!select || select.has(key3)) {
|
|
73
|
+
if (after = callback.call(source, key3, source))
|
|
89
74
|
queue(after);
|
|
90
75
|
}
|
|
91
|
-
if (after === null ||
|
|
76
|
+
if (after === null || key3 === null)
|
|
92
77
|
subs.delete(callback);
|
|
93
78
|
});
|
|
94
79
|
});
|
|
@@ -115,8 +100,7 @@ function effect(target, callback, requireValues) {
|
|
|
115
100
|
function invoke() {
|
|
116
101
|
let stale;
|
|
117
102
|
const subscriber = Object.create(target);
|
|
118
|
-
|
|
119
|
-
OBSERVER.set(subscriber, () => {
|
|
103
|
+
function onUpdate() {
|
|
120
104
|
if (stale)
|
|
121
105
|
return null;
|
|
122
106
|
stale = true;
|
|
@@ -125,9 +109,24 @@ function effect(target, callback, requireValues) {
|
|
|
125
109
|
unset = void 0;
|
|
126
110
|
}
|
|
127
111
|
return reset;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
112
|
+
}
|
|
113
|
+
function access(from, key2, value) {
|
|
114
|
+
if (value === void 0 && requireValues)
|
|
115
|
+
throw new Error(`${from}.${key2} is required in this context.`);
|
|
116
|
+
const listeners2 = LISTENER.get(from);
|
|
117
|
+
let listener = listeners2.get(onUpdate);
|
|
118
|
+
if (!listener)
|
|
119
|
+
listeners2.set(onUpdate, listener = /* @__PURE__ */ new Set());
|
|
120
|
+
listener.add(key2);
|
|
121
|
+
const nested = LISTENER.get(value);
|
|
122
|
+
if (nested) {
|
|
123
|
+
LISTENER.set(value = Object.create(value), nested);
|
|
124
|
+
OBSERVER.set(value, access);
|
|
125
|
+
}
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
LISTENER.set(subscriber, listeners);
|
|
129
|
+
OBSERVER.set(subscriber, access);
|
|
131
130
|
try {
|
|
132
131
|
const out = callback.call(subscriber, subscriber);
|
|
133
132
|
unset = typeof out == "function" ? out : void 0;
|
|
@@ -140,12 +139,12 @@ function effect(target, callback, requireValues) {
|
|
|
140
139
|
throw err;
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
|
-
addListener(target, (
|
|
144
|
-
if (
|
|
142
|
+
addListener(target, (key2) => {
|
|
143
|
+
if (key2 === true)
|
|
145
144
|
invoke();
|
|
146
145
|
else if (!reset)
|
|
147
146
|
return reset;
|
|
148
|
-
if (
|
|
147
|
+
if (key2 === null && unset)
|
|
149
148
|
unset(null);
|
|
150
149
|
});
|
|
151
150
|
return () => {
|
|
@@ -199,15 +198,15 @@ var Model = class _Model {
|
|
|
199
198
|
}
|
|
200
199
|
done = cleanup;
|
|
201
200
|
})();
|
|
202
|
-
for (const
|
|
203
|
-
const desc = Object.getOwnPropertyDescriptor(this,
|
|
201
|
+
for (const key2 in this) {
|
|
202
|
+
const desc = Object.getOwnPropertyDescriptor(this, key2);
|
|
204
203
|
if ("value" in desc) {
|
|
205
|
-
update(this,
|
|
206
|
-
define(this,
|
|
204
|
+
update(this, key2, desc.value, true);
|
|
205
|
+
define(this, key2, {
|
|
207
206
|
configurable: false,
|
|
208
|
-
set: (x) => update(this,
|
|
207
|
+
set: (x) => update(this, key2, x),
|
|
209
208
|
get() {
|
|
210
|
-
return watch(this,
|
|
209
|
+
return watch(this, key2, state[key2]);
|
|
211
210
|
}
|
|
212
211
|
});
|
|
213
212
|
}
|
|
@@ -242,12 +241,12 @@ var Model = class _Model {
|
|
|
242
241
|
if (arg1 === void 0) {
|
|
243
242
|
const values = {};
|
|
244
243
|
const current = !EXPORT && (EXPORT = /* @__PURE__ */ new Map([[self, values]]));
|
|
245
|
-
for (let [
|
|
244
|
+
for (let [key2, value] of self) {
|
|
246
245
|
if (EXPORT.has(value))
|
|
247
246
|
value = EXPORT.get(value);
|
|
248
247
|
else if (value && typeof value.get === "function")
|
|
249
248
|
EXPORT.set(value, value = value.get());
|
|
250
|
-
values[
|
|
249
|
+
values[key2] = value;
|
|
251
250
|
}
|
|
252
251
|
if (current)
|
|
253
252
|
EXPORT = void 0;
|
|
@@ -268,9 +267,9 @@ var Model = class _Model {
|
|
|
268
267
|
set(arg1, arg2, arg3) {
|
|
269
268
|
const self = this.is;
|
|
270
269
|
if (typeof arg1 == "function")
|
|
271
|
-
return addListener(self, (
|
|
272
|
-
if (typeof
|
|
273
|
-
return arg1.call(self,
|
|
270
|
+
return addListener(self, (key2) => {
|
|
271
|
+
if (typeof key2 == "string")
|
|
272
|
+
return arg1.call(self, key2, self);
|
|
274
273
|
});
|
|
275
274
|
if (arg1 === null)
|
|
276
275
|
return emit(this, null);
|
|
@@ -285,8 +284,8 @@ var Model = class _Model {
|
|
|
285
284
|
if (PENDING2.has(self))
|
|
286
285
|
return {
|
|
287
286
|
then: (res) => new Promise((res2) => {
|
|
288
|
-
const remove = addListener(self, (
|
|
289
|
-
if (
|
|
287
|
+
const remove = addListener(self, (key2) => {
|
|
288
|
+
if (key2 !== true) {
|
|
290
289
|
remove();
|
|
291
290
|
return res2.bind(null, Array.from(PENDING2.get(self)));
|
|
292
291
|
}
|
|
@@ -349,12 +348,12 @@ function fetch(subject, property, required) {
|
|
|
349
348
|
}
|
|
350
349
|
const error = new Error(`${subject}.${property} is not yet available.`);
|
|
351
350
|
const promise = new Promise((resolve, reject) => {
|
|
352
|
-
addListener(subject, (
|
|
353
|
-
if (
|
|
354
|
-
resolve(state[
|
|
351
|
+
addListener(subject, (key2) => {
|
|
352
|
+
if (key2 === property) {
|
|
353
|
+
resolve(state[key2]);
|
|
355
354
|
return null;
|
|
356
355
|
}
|
|
357
|
-
if (
|
|
356
|
+
if (key2 === null)
|
|
358
357
|
reject(new Error(`${subject} is destroyed.`));
|
|
359
358
|
});
|
|
360
359
|
});
|
|
@@ -365,22 +364,29 @@ function fetch(subject, property, required) {
|
|
|
365
364
|
stack: error.stack
|
|
366
365
|
});
|
|
367
366
|
}
|
|
368
|
-
function update(subject,
|
|
367
|
+
function update(subject, key2, value, arg) {
|
|
369
368
|
const state = STATE.get(subject);
|
|
370
369
|
if (Object.isFrozen(state))
|
|
371
|
-
throw new Error(`Tried to update ${String(
|
|
372
|
-
const previous = state[
|
|
370
|
+
throw new Error(`Tried to update ${String(key2)} but ${subject} is destroyed.`);
|
|
371
|
+
const previous = state[key2];
|
|
372
|
+
if (typeof arg == "function") {
|
|
373
|
+
const out = arg.call(subject, value, previous);
|
|
374
|
+
if (out === false)
|
|
375
|
+
return false;
|
|
376
|
+
if (typeof out == "function")
|
|
377
|
+
value = out();
|
|
378
|
+
}
|
|
373
379
|
if (value === previous)
|
|
374
380
|
return true;
|
|
375
381
|
if (value instanceof Model && !PARENT.has(value)) {
|
|
376
382
|
PARENT.set(value, subject);
|
|
377
383
|
emit(value, true);
|
|
378
384
|
}
|
|
379
|
-
state[
|
|
380
|
-
if (
|
|
381
|
-
push(subject,
|
|
385
|
+
state[key2] = value;
|
|
386
|
+
if (arg !== true)
|
|
387
|
+
push(subject, key2);
|
|
382
388
|
}
|
|
383
|
-
function push(subject,
|
|
389
|
+
function push(subject, key2, silent) {
|
|
384
390
|
let pending = PENDING2.get(subject);
|
|
385
391
|
if (!pending) {
|
|
386
392
|
PENDING2.set(subject, pending = /* @__PURE__ */ new Set());
|
|
@@ -390,38 +396,38 @@ function push(subject, key, silent) {
|
|
|
390
396
|
PENDING2.delete(subject);
|
|
391
397
|
});
|
|
392
398
|
}
|
|
393
|
-
pending.add(
|
|
399
|
+
pending.add(key2);
|
|
394
400
|
if (!silent)
|
|
395
|
-
emit(subject,
|
|
401
|
+
emit(subject, key2);
|
|
396
402
|
}
|
|
397
403
|
var METHODS = new WeakMap([[Model, []]]);
|
|
398
404
|
function assign(to, values) {
|
|
399
405
|
const methods = METHODS.get(to.constructor);
|
|
400
406
|
const applicable = new Set(Object.keys(to).concat(methods));
|
|
401
|
-
for (const
|
|
402
|
-
if (applicable.has(
|
|
403
|
-
to[
|
|
407
|
+
for (const key2 in values)
|
|
408
|
+
if (applicable.has(key2))
|
|
409
|
+
to[key2] = values[key2];
|
|
404
410
|
}
|
|
405
411
|
function bindMethods(type) {
|
|
406
412
|
let keys = METHODS.get(type);
|
|
407
413
|
if (!keys) {
|
|
408
414
|
METHODS.set(type, keys = bindMethods(Object.getPrototypeOf(type)).slice());
|
|
409
415
|
const desc = Object.getOwnPropertyDescriptors(type.prototype);
|
|
410
|
-
for (const
|
|
411
|
-
if (
|
|
416
|
+
for (const key2 in desc)
|
|
417
|
+
if (key2 != "constructor" && "value" in desc[key2]) {
|
|
412
418
|
let bind2 = function(method) {
|
|
413
419
|
const value2 = method.bind(this.is);
|
|
414
420
|
METHOD.set(value2, method);
|
|
415
|
-
define(this.is,
|
|
421
|
+
define(this.is, key2, { value: value2, writable: true });
|
|
416
422
|
return value2;
|
|
417
423
|
};
|
|
418
424
|
var bind = bind2;
|
|
419
|
-
let { value } = desc[
|
|
420
|
-
keys.push(
|
|
421
|
-
Object.defineProperty(type.prototype,
|
|
425
|
+
let { value } = desc[key2];
|
|
426
|
+
keys.push(key2);
|
|
427
|
+
Object.defineProperty(type.prototype, key2, {
|
|
422
428
|
set: bind2,
|
|
423
429
|
get() {
|
|
424
|
-
return this.hasOwnProperty(
|
|
430
|
+
return this.hasOwnProperty(key2) ? value : bind2.call(this, value);
|
|
425
431
|
}
|
|
426
432
|
});
|
|
427
433
|
}
|
|
@@ -434,11 +440,21 @@ function uid() {
|
|
|
434
440
|
|
|
435
441
|
// src/context.ts
|
|
436
442
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
443
|
+
var Upstream = /* @__PURE__ */ new WeakMap();
|
|
444
|
+
var Downstream = /* @__PURE__ */ new WeakMap();
|
|
445
|
+
function key(T, upstream) {
|
|
446
|
+
const table = upstream ? Upstream : Downstream;
|
|
447
|
+
let key2 = table.get(T);
|
|
448
|
+
if (!key2) {
|
|
449
|
+
key2 = Symbol(T.name + (upstream ? " request" : ""));
|
|
450
|
+
table.set(T, key2);
|
|
451
|
+
}
|
|
452
|
+
return key2;
|
|
453
|
+
}
|
|
437
454
|
var Context = class _Context {
|
|
438
455
|
constructor(inputs) {
|
|
439
|
-
this.downstream = /* @__PURE__ */ new WeakMap();
|
|
440
|
-
this.upstream = /* @__PURE__ */ new WeakMap();
|
|
441
456
|
this.layer = /* @__PURE__ */ new Map();
|
|
457
|
+
this.cleanup = /* @__PURE__ */ new Set();
|
|
442
458
|
if (inputs)
|
|
443
459
|
this.include(inputs);
|
|
444
460
|
}
|
|
@@ -453,43 +469,40 @@ var Context = class _Context {
|
|
|
453
469
|
else
|
|
454
470
|
Register.set(from, [callback]);
|
|
455
471
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
key = Symbol(T.name + (upstream ? " request" : ""));
|
|
461
|
-
table.set(T, key);
|
|
462
|
-
}
|
|
463
|
-
return key;
|
|
464
|
-
}
|
|
465
|
-
has(model) {
|
|
466
|
-
const key = this.key(model.constructor, true);
|
|
467
|
-
const result = this[key];
|
|
468
|
-
const waiting = Register.get(model);
|
|
469
|
-
if (waiting instanceof Array)
|
|
470
|
-
waiting.forEach((cb) => cb(this));
|
|
471
|
-
Register.set(model, this);
|
|
472
|
-
if (typeof result == "function")
|
|
473
|
-
return result(model);
|
|
474
|
-
}
|
|
475
|
-
get(Type) {
|
|
476
|
-
const result = this[this.key(Type)];
|
|
472
|
+
get(Type, callback) {
|
|
473
|
+
if (callback)
|
|
474
|
+
return this.put(Type, callback);
|
|
475
|
+
const result = this[key(Type)];
|
|
477
476
|
if (result === null)
|
|
478
477
|
throw new Error(`Did find ${Type} in context, but multiple were defined.`);
|
|
479
478
|
return result;
|
|
480
479
|
}
|
|
480
|
+
push(inputs) {
|
|
481
|
+
const next = Object.create(this);
|
|
482
|
+
next.layer = /* @__PURE__ */ new Map();
|
|
483
|
+
next.cleanup = /* @__PURE__ */ new Set();
|
|
484
|
+
if (inputs)
|
|
485
|
+
next.include(inputs);
|
|
486
|
+
return next;
|
|
487
|
+
}
|
|
488
|
+
pop() {
|
|
489
|
+
for (const key2 of Object.getOwnPropertySymbols(this))
|
|
490
|
+
delete this[key2];
|
|
491
|
+
this.cleanup.forEach((cb) => cb());
|
|
492
|
+
this.layer.clear();
|
|
493
|
+
}
|
|
481
494
|
include(inputs, forEach) {
|
|
482
495
|
const init = /* @__PURE__ */ new Map();
|
|
483
496
|
Object.entries(inputs).forEach(([K, V]) => {
|
|
484
497
|
if (!(Model.is(V) || V instanceof Model))
|
|
485
498
|
throw new Error(`Context can only include Model or instance but got ${V}${K && " as " + K}.`);
|
|
486
499
|
});
|
|
487
|
-
for (const
|
|
488
|
-
const input = inputs[
|
|
489
|
-
const exists = this.layer.get(
|
|
500
|
+
for (const key2 in inputs) {
|
|
501
|
+
const input = inputs[key2];
|
|
502
|
+
const exists = this.layer.get(key2);
|
|
490
503
|
if (!exists) {
|
|
491
504
|
const instance = this.add(input);
|
|
492
|
-
this.layer.set(
|
|
505
|
+
this.layer.set(key2, input);
|
|
493
506
|
init.set(instance, true);
|
|
494
507
|
} else if (exists !== input) {
|
|
495
508
|
this.pop();
|
|
@@ -508,118 +521,101 @@ var Context = class _Context {
|
|
|
508
521
|
}
|
|
509
522
|
}
|
|
510
523
|
}
|
|
511
|
-
put(T, I, implicit, writable) {
|
|
512
|
-
do {
|
|
513
|
-
const key = this.key(T, typeof I == "function");
|
|
514
|
-
const value = this.hasOwnProperty(key) ? null : I;
|
|
515
|
-
if (value || this[key] !== I && !implicit)
|
|
516
|
-
Object.defineProperty(this, key, {
|
|
517
|
-
configurable: true,
|
|
518
|
-
writable,
|
|
519
|
-
value
|
|
520
|
-
});
|
|
521
|
-
T = Object.getPrototypeOf(T);
|
|
522
|
-
} while (T !== Model);
|
|
523
|
-
}
|
|
524
524
|
add(input, implicit) {
|
|
525
|
-
let writable = true;
|
|
526
525
|
let T;
|
|
527
526
|
let I;
|
|
528
527
|
if (typeof input == "function") {
|
|
529
528
|
T = input;
|
|
530
529
|
I = new input();
|
|
530
|
+
this.cleanup.add(() => I.set(null));
|
|
531
531
|
} else {
|
|
532
532
|
I = input;
|
|
533
533
|
T = I.constructor;
|
|
534
|
-
writable = false;
|
|
535
534
|
}
|
|
536
|
-
this.has(I);
|
|
537
|
-
this.put(T, I, implicit
|
|
535
|
+
this.has(T, I);
|
|
536
|
+
this.put(T, I, implicit);
|
|
538
537
|
return I;
|
|
539
538
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
539
|
+
has(type, model) {
|
|
540
|
+
do {
|
|
541
|
+
const K = key(type, true);
|
|
542
|
+
const result = this[K];
|
|
543
|
+
if (!result)
|
|
544
|
+
continue;
|
|
545
|
+
const callback = result(model);
|
|
546
|
+
if (callback)
|
|
547
|
+
this.cleanup.add(callback);
|
|
548
|
+
} while ((type = Object.getPrototypeOf(type)) !== Model);
|
|
549
|
+
const waiting = Register.get(model);
|
|
550
|
+
if (waiting instanceof Array)
|
|
551
|
+
waiting.forEach((cb) => cb(this));
|
|
552
|
+
Register.set(model, this);
|
|
546
553
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
const
|
|
551
|
-
if (
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
}
|
|
555
|
-
for (const model of items)
|
|
556
|
-
model.set(null);
|
|
557
|
-
this.layer.clear();
|
|
554
|
+
put(T, I, implicit) {
|
|
555
|
+
do {
|
|
556
|
+
const K = key(T, typeof I == "function");
|
|
557
|
+
const value = this.hasOwnProperty(K) ? null : I;
|
|
558
|
+
if (value || this[K] !== I && !implicit)
|
|
559
|
+
Object.defineProperty(this, K, { configurable: true, value });
|
|
560
|
+
} while ((T = Object.getPrototypeOf(T)) !== Model);
|
|
558
561
|
}
|
|
559
562
|
};
|
|
560
563
|
|
|
561
564
|
// src/instruction/use.ts
|
|
562
565
|
var INSTRUCT = /* @__PURE__ */ new Map();
|
|
563
|
-
function use(
|
|
566
|
+
function use(arg1, arg2) {
|
|
564
567
|
const token = Symbol("instruction");
|
|
565
|
-
if (Model.is(
|
|
566
|
-
const type =
|
|
567
|
-
const value = new
|
|
568
|
-
|
|
568
|
+
if (Model.is(arg1)) {
|
|
569
|
+
const type = arg1;
|
|
570
|
+
const value = new type();
|
|
571
|
+
arg1 = (key2, subject) => {
|
|
569
572
|
function set2(next) {
|
|
570
|
-
if (next ? !(next instanceof type) :
|
|
571
|
-
throw new Error(`${subject}.${
|
|
572
|
-
update(subject,
|
|
573
|
-
if (next && typeof
|
|
574
|
-
|
|
573
|
+
if (next ? !(next instanceof type) : arg2 !== false)
|
|
574
|
+
throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
|
|
575
|
+
update(subject, key2, next);
|
|
576
|
+
if (next && typeof arg2 == "function")
|
|
577
|
+
arg2(next);
|
|
575
578
|
return false;
|
|
576
579
|
}
|
|
577
580
|
set2(value);
|
|
578
581
|
return { set: set2, value };
|
|
579
582
|
};
|
|
580
583
|
}
|
|
581
|
-
INSTRUCT.set(token,
|
|
584
|
+
INSTRUCT.set(token, arg1);
|
|
582
585
|
return token;
|
|
583
586
|
}
|
|
584
587
|
Model.on((_, subject) => {
|
|
585
588
|
const state = STATE.get(subject);
|
|
586
589
|
const props = Object.getOwnPropertyDescriptors(subject);
|
|
587
|
-
for (const
|
|
588
|
-
const { value } = props[
|
|
590
|
+
for (const key2 in props) {
|
|
591
|
+
const { value } = props[key2];
|
|
589
592
|
const instruction = INSTRUCT.get(value);
|
|
590
|
-
if (!instruction)
|
|
593
|
+
if (!instruction) {
|
|
594
|
+
if (value instanceof Model && key2 !== "is")
|
|
595
|
+
update(subject, key2, value);
|
|
591
596
|
continue;
|
|
597
|
+
}
|
|
592
598
|
INSTRUCT.delete(value);
|
|
593
|
-
delete subject[
|
|
594
|
-
const output = instruction.call(subject,
|
|
599
|
+
delete subject[key2];
|
|
600
|
+
const output = instruction.call(subject, key2, subject, state);
|
|
595
601
|
if (!output)
|
|
596
602
|
continue;
|
|
597
603
|
const desc = typeof output == "object" ? output : { get: output };
|
|
598
|
-
const { enumerable = true } = desc;
|
|
599
604
|
if ("value" in desc)
|
|
600
|
-
state[
|
|
601
|
-
Object.defineProperty(subject,
|
|
602
|
-
enumerable,
|
|
603
|
-
set(next) {
|
|
604
|
-
let { set: set2 } = desc;
|
|
605
|
-
if (set2 === false)
|
|
606
|
-
throw new Error(`${subject}.${key} is read-only.`);
|
|
607
|
-
if (typeof set2 == "function") {
|
|
608
|
-
const result = set2.call(subject, next, state[key]);
|
|
609
|
-
if (result === false)
|
|
610
|
-
return;
|
|
611
|
-
if (typeof result == "function")
|
|
612
|
-
next = result();
|
|
613
|
-
set2 = false;
|
|
614
|
-
}
|
|
615
|
-
update(subject, key, next, !!set2);
|
|
616
|
-
},
|
|
605
|
+
state[key2] = desc.value;
|
|
606
|
+
Object.defineProperty(subject, key2, {
|
|
607
|
+
enumerable: desc.enumerable !== false,
|
|
617
608
|
get() {
|
|
618
609
|
return watch(
|
|
619
610
|
this,
|
|
620
|
-
|
|
621
|
-
typeof desc.get == "function" ? desc.get(this) : fetch(subject,
|
|
611
|
+
key2,
|
|
612
|
+
typeof desc.get == "function" ? desc.get(this) : fetch(subject, key2, desc.get)
|
|
622
613
|
);
|
|
614
|
+
},
|
|
615
|
+
set(next) {
|
|
616
|
+
if (desc.set === false)
|
|
617
|
+
throw new Error(`${subject}.${key2} is read-only.`);
|
|
618
|
+
update(subject, key2, next, desc.set);
|
|
623
619
|
}
|
|
624
620
|
});
|
|
625
621
|
}
|
|
@@ -629,10 +625,10 @@ Model.on((_, subject) => {
|
|
|
629
625
|
// src/instruction/get.ts
|
|
630
626
|
var STALE = /* @__PURE__ */ new WeakSet();
|
|
631
627
|
function get(arg0, arg1) {
|
|
632
|
-
return use((
|
|
628
|
+
return use((key2, subject, state) => {
|
|
633
629
|
let from = subject;
|
|
634
630
|
if (typeof arg0 == "symbol")
|
|
635
|
-
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${from}.${
|
|
631
|
+
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${from}.${key2}. This is not allowed.`);
|
|
636
632
|
let source = (resolve) => resolve(from);
|
|
637
633
|
if (arg0 instanceof Model) {
|
|
638
634
|
from = arg0;
|
|
@@ -662,26 +658,26 @@ function get(arg0, arg1) {
|
|
|
662
658
|
let connect2 = function(model) {
|
|
663
659
|
reset = effect(model, (current) => {
|
|
664
660
|
proxy = current;
|
|
665
|
-
if (!(
|
|
661
|
+
if (!(key2 in state) || STALE.delete(compute2))
|
|
666
662
|
compute2(!reset);
|
|
667
663
|
return (didUpdate) => {
|
|
668
664
|
if (didUpdate) {
|
|
669
665
|
STALE.add(compute2);
|
|
670
|
-
push(subject,
|
|
666
|
+
push(subject, key2, true);
|
|
671
667
|
}
|
|
672
668
|
};
|
|
673
669
|
});
|
|
674
670
|
}, compute2 = function(initial) {
|
|
675
671
|
let next;
|
|
676
672
|
try {
|
|
677
|
-
next = getter.call(proxy, proxy,
|
|
673
|
+
next = getter.call(proxy, proxy, key2);
|
|
678
674
|
} catch (err) {
|
|
679
|
-
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${
|
|
675
|
+
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${key2}.`);
|
|
680
676
|
if (initial)
|
|
681
677
|
throw err;
|
|
682
678
|
console.error(err);
|
|
683
679
|
}
|
|
684
|
-
update(subject,
|
|
680
|
+
update(subject, key2, next, !isAsync);
|
|
685
681
|
};
|
|
686
682
|
var connect = connect2, compute = compute2;
|
|
687
683
|
const getter = arg1;
|
|
@@ -695,10 +691,10 @@ function get(arg0, arg1) {
|
|
|
695
691
|
}
|
|
696
692
|
if (STALE.delete(compute2))
|
|
697
693
|
compute2();
|
|
698
|
-
return fetch(subject,
|
|
694
|
+
return fetch(subject, key2, !proxy);
|
|
699
695
|
};
|
|
700
696
|
} else
|
|
701
|
-
source((got) => subject.set(
|
|
697
|
+
source((got) => subject.set(key2, got));
|
|
702
698
|
return { get: arg1 };
|
|
703
699
|
});
|
|
704
700
|
}
|
|
@@ -706,35 +702,35 @@ function get(arg0, arg1) {
|
|
|
706
702
|
// src/instruction/ref.ts
|
|
707
703
|
var define2 = Object.defineProperty;
|
|
708
704
|
function ref(arg, arg2) {
|
|
709
|
-
return use((
|
|
705
|
+
return use((key2, subject, state) => {
|
|
710
706
|
let value = {};
|
|
711
707
|
if (arg === subject)
|
|
712
708
|
subject.get(() => {
|
|
713
|
-
for (const
|
|
709
|
+
for (const key3 in state)
|
|
714
710
|
if (typeof arg2 == "function")
|
|
715
|
-
define2(value,
|
|
711
|
+
define2(value, key3, {
|
|
716
712
|
configurable: true,
|
|
717
713
|
get() {
|
|
718
|
-
const out = arg2(
|
|
719
|
-
define2(value,
|
|
714
|
+
const out = arg2(key3);
|
|
715
|
+
define2(value, key3, { value: out });
|
|
720
716
|
return out;
|
|
721
717
|
}
|
|
722
718
|
});
|
|
723
719
|
else {
|
|
724
|
-
const get2 = () => state[
|
|
725
|
-
const set2 = (value2) => subject.set(
|
|
720
|
+
const get2 = () => state[key3];
|
|
721
|
+
const set2 = (value2) => subject.set(key3, value2);
|
|
726
722
|
define2(set2, "current", { get: get2, set: set2 });
|
|
727
723
|
define2(set2, "get", { value: get2 });
|
|
728
|
-
define2(value,
|
|
724
|
+
define2(value, key3, { value: set2 });
|
|
729
725
|
}
|
|
730
726
|
});
|
|
731
727
|
else if (typeof arg == "object")
|
|
732
728
|
throw new Error("ref instruction does not support object which is not 'this'");
|
|
733
729
|
else {
|
|
734
730
|
let unSet;
|
|
735
|
-
const get2 = () => state[
|
|
731
|
+
const get2 = () => state[key2];
|
|
736
732
|
const set2 = (value2) => {
|
|
737
|
-
if (!subject.set(
|
|
733
|
+
if (!subject.set(key2, value2) || !arg)
|
|
738
734
|
return;
|
|
739
735
|
if (unSet)
|
|
740
736
|
unSet = void unSet(value2);
|
|
@@ -744,44 +740,44 @@ function ref(arg, arg2) {
|
|
|
744
740
|
unSet = out;
|
|
745
741
|
}
|
|
746
742
|
};
|
|
747
|
-
state[
|
|
743
|
+
state[key2] = null;
|
|
748
744
|
value = Object.defineProperties(set2, {
|
|
749
745
|
current: { get: get2, set: set2 },
|
|
750
746
|
get: { value: get2 }
|
|
751
747
|
});
|
|
752
748
|
}
|
|
753
|
-
define2(subject,
|
|
749
|
+
define2(subject, key2, { value });
|
|
754
750
|
});
|
|
755
751
|
}
|
|
756
752
|
|
|
757
753
|
// src/instruction/set.ts
|
|
758
754
|
function set(value, argument) {
|
|
759
|
-
return use((
|
|
755
|
+
return use((key2, subject) => {
|
|
760
756
|
const output = {};
|
|
761
757
|
if (typeof value == "function" || value instanceof Promise) {
|
|
762
758
|
let init2 = function() {
|
|
763
759
|
if (typeof value == "function")
|
|
764
760
|
try {
|
|
765
|
-
value = attempt(value.bind(subject,
|
|
761
|
+
value = attempt(value.bind(subject, key2));
|
|
766
762
|
} catch (err) {
|
|
767
|
-
console.warn(`Generating initial value for ${subject}.${
|
|
763
|
+
console.warn(`Generating initial value for ${subject}.${key2} failed.`);
|
|
768
764
|
throw err;
|
|
769
765
|
}
|
|
770
|
-
output.get =
|
|
766
|
+
output.get = argument === true;
|
|
771
767
|
if (value instanceof Promise) {
|
|
772
768
|
value.then((value2) => {
|
|
773
|
-
subject.set(
|
|
769
|
+
subject.set(key2, value2);
|
|
774
770
|
return value2;
|
|
775
771
|
}).catch((err) => {
|
|
776
|
-
subject.set(
|
|
772
|
+
subject.set(key2);
|
|
777
773
|
output.get = () => {
|
|
778
774
|
throw err;
|
|
779
775
|
};
|
|
780
776
|
});
|
|
781
777
|
} else
|
|
782
|
-
subject.set(
|
|
778
|
+
subject.set(key2, value);
|
|
783
779
|
if (0 in arguments)
|
|
784
|
-
return subject[
|
|
780
|
+
return subject[key2];
|
|
785
781
|
};
|
|
786
782
|
var init = init2;
|
|
787
783
|
if (argument)
|
|
@@ -804,7 +800,7 @@ function set(value, argument) {
|
|
|
804
800
|
} else
|
|
805
801
|
output.set = (value2) => {
|
|
806
802
|
output.get = void 0;
|
|
807
|
-
subject.set(
|
|
803
|
+
subject.set(key2, value2);
|
|
808
804
|
};
|
|
809
805
|
return output;
|
|
810
806
|
});
|
|
@@ -828,41 +824,70 @@ function attempt(fn) {
|
|
|
828
824
|
}
|
|
829
825
|
|
|
830
826
|
// src/instruction/has.ts
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
827
|
+
var APPLY = /* @__PURE__ */ new WeakMap();
|
|
828
|
+
function has(arg1, arg2) {
|
|
829
|
+
return use((key2, subject) => {
|
|
830
|
+
const applied = /* @__PURE__ */ new Set();
|
|
831
|
+
const update2 = () => {
|
|
832
|
+
subject.set(key2, Object.freeze(Array.from(applied)));
|
|
833
|
+
};
|
|
834
|
+
if (!Model.is(arg1)) {
|
|
835
|
+
if (APPLY.has(subject))
|
|
836
|
+
throw new Error(`'has' callback can only be used once per model.`);
|
|
837
|
+
APPLY.set(subject, (recipient) => {
|
|
838
|
+
let remove;
|
|
839
|
+
if (arg1) {
|
|
840
|
+
remove = arg1(recipient, subject);
|
|
841
|
+
if (remove === false)
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
applied.add(recipient);
|
|
845
|
+
update2();
|
|
846
|
+
return () => {
|
|
847
|
+
applied.delete(recipient);
|
|
848
|
+
update2();
|
|
849
|
+
if (typeof remove == "function")
|
|
850
|
+
remove();
|
|
840
851
|
};
|
|
841
|
-
model.get(null, remove);
|
|
842
|
-
return remove;
|
|
843
852
|
});
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
853
|
+
} else
|
|
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
|
+
}));
|
|
890
|
+
return { value: [] };
|
|
866
891
|
});
|
|
867
892
|
}
|
|
868
893
|
// Annotate the CommonJS export names for ESM import in node:
|