@expressive/mvc 0.49.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 +31 -31
- package/dist/index.js +287 -249
- package/dist/index.mjs +284 -246
- 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
|
}
|
|
@@ -306,6 +305,7 @@ var Model = class _Model {
|
|
|
306
305
|
* @param args - arguments sent to constructor
|
|
307
306
|
*/
|
|
308
307
|
static new(...args) {
|
|
308
|
+
_Model.is(this);
|
|
309
309
|
const instance = new this(...args);
|
|
310
310
|
emit(instance, true);
|
|
311
311
|
return instance;
|
|
@@ -317,6 +317,8 @@ var Model = class _Model {
|
|
|
317
317
|
* methods and properties of this class.
|
|
318
318
|
*/
|
|
319
319
|
static is(maybe) {
|
|
320
|
+
if (maybe === _Model)
|
|
321
|
+
throw new Error("Model is abstract and not a valid type.");
|
|
320
322
|
return typeof maybe == "function" && maybe.prototype instanceof this;
|
|
321
323
|
}
|
|
322
324
|
/**
|
|
@@ -349,12 +351,12 @@ function fetch(subject, property, required) {
|
|
|
349
351
|
}
|
|
350
352
|
const error = new Error(`${subject}.${property} is not yet available.`);
|
|
351
353
|
const promise = new Promise((resolve, reject) => {
|
|
352
|
-
addListener(subject, (
|
|
353
|
-
if (
|
|
354
|
-
resolve(state[
|
|
354
|
+
addListener(subject, (key2) => {
|
|
355
|
+
if (key2 === property) {
|
|
356
|
+
resolve(state[key2]);
|
|
355
357
|
return null;
|
|
356
358
|
}
|
|
357
|
-
if (
|
|
359
|
+
if (key2 === null)
|
|
358
360
|
reject(new Error(`${subject} is destroyed.`));
|
|
359
361
|
});
|
|
360
362
|
});
|
|
@@ -365,22 +367,29 @@ function fetch(subject, property, required) {
|
|
|
365
367
|
stack: error.stack
|
|
366
368
|
});
|
|
367
369
|
}
|
|
368
|
-
function update(subject,
|
|
370
|
+
function update(subject, key2, value, arg) {
|
|
369
371
|
const state = STATE.get(subject);
|
|
370
372
|
if (Object.isFrozen(state))
|
|
371
|
-
throw new Error(`Tried to update ${String(
|
|
372
|
-
const previous = state[
|
|
373
|
+
throw new Error(`Tried to update ${String(key2)} but ${subject} is destroyed.`);
|
|
374
|
+
const previous = state[key2];
|
|
375
|
+
if (typeof arg == "function") {
|
|
376
|
+
const out = arg.call(subject, value, previous);
|
|
377
|
+
if (out === false)
|
|
378
|
+
return false;
|
|
379
|
+
if (typeof out == "function")
|
|
380
|
+
value = out();
|
|
381
|
+
}
|
|
373
382
|
if (value === previous)
|
|
374
383
|
return true;
|
|
375
384
|
if (value instanceof Model && !PARENT.has(value)) {
|
|
376
385
|
PARENT.set(value, subject);
|
|
377
386
|
emit(value, true);
|
|
378
387
|
}
|
|
379
|
-
state[
|
|
380
|
-
if (
|
|
381
|
-
push(subject,
|
|
388
|
+
state[key2] = value;
|
|
389
|
+
if (arg !== true)
|
|
390
|
+
push(subject, key2);
|
|
382
391
|
}
|
|
383
|
-
function push(subject,
|
|
392
|
+
function push(subject, key2, silent) {
|
|
384
393
|
let pending = PENDING2.get(subject);
|
|
385
394
|
if (!pending) {
|
|
386
395
|
PENDING2.set(subject, pending = /* @__PURE__ */ new Set());
|
|
@@ -390,43 +399,43 @@ function push(subject, key, silent) {
|
|
|
390
399
|
PENDING2.delete(subject);
|
|
391
400
|
});
|
|
392
401
|
}
|
|
393
|
-
pending.add(
|
|
402
|
+
pending.add(key2);
|
|
394
403
|
if (!silent)
|
|
395
|
-
emit(subject,
|
|
404
|
+
emit(subject, key2);
|
|
396
405
|
}
|
|
397
406
|
var METHODS = new WeakMap([[Model, []]]);
|
|
398
407
|
function assign(to, values) {
|
|
399
408
|
const methods = METHODS.get(to.constructor);
|
|
400
409
|
const applicable = new Set(Object.keys(to).concat(methods));
|
|
401
|
-
for (const
|
|
402
|
-
if (applicable.has(
|
|
403
|
-
to[
|
|
410
|
+
for (const key2 in values)
|
|
411
|
+
if (applicable.has(key2))
|
|
412
|
+
to[key2] = values[key2];
|
|
404
413
|
}
|
|
405
414
|
function bindMethods(type) {
|
|
406
|
-
let
|
|
407
|
-
if (!
|
|
408
|
-
METHODS.set(type,
|
|
415
|
+
let keys2 = METHODS.get(type);
|
|
416
|
+
if (!keys2) {
|
|
417
|
+
METHODS.set(type, keys2 = bindMethods(Object.getPrototypeOf(type)).slice());
|
|
409
418
|
const desc = Object.getOwnPropertyDescriptors(type.prototype);
|
|
410
|
-
for (const
|
|
411
|
-
if (
|
|
419
|
+
for (const key2 in desc)
|
|
420
|
+
if (key2 != "constructor" && "value" in desc[key2]) {
|
|
412
421
|
let bind2 = function(method) {
|
|
413
422
|
const value2 = method.bind(this.is);
|
|
414
423
|
METHOD.set(value2, method);
|
|
415
|
-
define(this.is,
|
|
424
|
+
define(this.is, key2, { value: value2, writable: true });
|
|
416
425
|
return value2;
|
|
417
426
|
};
|
|
418
427
|
var bind = bind2;
|
|
419
|
-
let { value } = desc[
|
|
420
|
-
|
|
421
|
-
Object.defineProperty(type.prototype,
|
|
428
|
+
let { value } = desc[key2];
|
|
429
|
+
keys2.push(key2);
|
|
430
|
+
Object.defineProperty(type.prototype, key2, {
|
|
422
431
|
set: bind2,
|
|
423
432
|
get() {
|
|
424
|
-
return this.hasOwnProperty(
|
|
433
|
+
return this.hasOwnProperty(key2) ? value : bind2.call(this, value);
|
|
425
434
|
}
|
|
426
435
|
});
|
|
427
436
|
}
|
|
428
437
|
}
|
|
429
|
-
return
|
|
438
|
+
return keys2;
|
|
430
439
|
}
|
|
431
440
|
function uid() {
|
|
432
441
|
return (Math.random() * 0.722 + 0.278).toString(36).substring(2, 8).toUpperCase();
|
|
@@ -434,62 +443,76 @@ function uid() {
|
|
|
434
443
|
|
|
435
444
|
// src/context.ts
|
|
436
445
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
446
|
+
var Table = /* @__PURE__ */ new Map();
|
|
447
|
+
function key(T, upstream) {
|
|
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;
|
|
461
|
+
}
|
|
437
462
|
var Context = class _Context {
|
|
438
463
|
constructor(inputs) {
|
|
439
|
-
this.downstream = /* @__PURE__ */ new WeakMap();
|
|
440
|
-
this.upstream = /* @__PURE__ */ new WeakMap();
|
|
441
464
|
this.layer = /* @__PURE__ */ new Map();
|
|
465
|
+
this.cleanup = /* @__PURE__ */ new Set();
|
|
442
466
|
if (inputs)
|
|
443
467
|
this.include(inputs);
|
|
444
468
|
}
|
|
445
469
|
static get(from, callback) {
|
|
446
470
|
const waiting = Register.get(from);
|
|
447
|
-
if (
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
else if (waiting)
|
|
452
|
-
waiting.push(callback);
|
|
453
|
-
else
|
|
454
|
-
Register.set(from, [callback]);
|
|
455
|
-
}
|
|
456
|
-
key(T, upstream) {
|
|
457
|
-
const table = upstream ? this.upstream : this.downstream;
|
|
458
|
-
let key = table.get(T);
|
|
459
|
-
if (!key) {
|
|
460
|
-
key = Symbol(T.name + (upstream ? " request" : ""));
|
|
461
|
-
table.set(T, key);
|
|
471
|
+
if (waiting instanceof _Context) {
|
|
472
|
+
if (callback)
|
|
473
|
+
callback(waiting);
|
|
474
|
+
return waiting;
|
|
462
475
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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);
|
|
476
|
+
if (callback)
|
|
477
|
+
if (waiting)
|
|
478
|
+
waiting.push(callback);
|
|
479
|
+
else
|
|
480
|
+
Register.set(from, [callback]);
|
|
474
481
|
}
|
|
475
|
-
get(Type) {
|
|
476
|
-
|
|
482
|
+
get(Type, callback) {
|
|
483
|
+
if (callback)
|
|
484
|
+
Object.defineProperty(this, key(Type, true), { value: callback });
|
|
485
|
+
const result = this[key(Type)];
|
|
477
486
|
if (result === null)
|
|
478
487
|
throw new Error(`Did find ${Type} in context, but multiple were defined.`);
|
|
479
488
|
return result;
|
|
480
489
|
}
|
|
490
|
+
push(inputs) {
|
|
491
|
+
const next = Object.create(this);
|
|
492
|
+
next.layer = /* @__PURE__ */ new Map();
|
|
493
|
+
next.cleanup = /* @__PURE__ */ new Set();
|
|
494
|
+
if (inputs)
|
|
495
|
+
next.include(inputs);
|
|
496
|
+
return next;
|
|
497
|
+
}
|
|
498
|
+
pop() {
|
|
499
|
+
for (const key2 of Object.getOwnPropertySymbols(this))
|
|
500
|
+
delete this[key2];
|
|
501
|
+
this.cleanup.forEach((cb) => cb());
|
|
502
|
+
this.layer.clear();
|
|
503
|
+
}
|
|
481
504
|
include(inputs, forEach) {
|
|
482
505
|
const init = /* @__PURE__ */ new Map();
|
|
483
506
|
Object.entries(inputs).forEach(([K, V]) => {
|
|
484
507
|
if (!(Model.is(V) || V instanceof Model))
|
|
485
508
|
throw new Error(`Context can only include Model or instance but got ${V}${K && " as " + K}.`);
|
|
486
509
|
});
|
|
487
|
-
for (const
|
|
488
|
-
const input = inputs[
|
|
489
|
-
const exists = this.layer.get(
|
|
510
|
+
for (const key2 in inputs) {
|
|
511
|
+
const input = inputs[key2];
|
|
512
|
+
const exists = this.layer.get(key2);
|
|
490
513
|
if (!exists) {
|
|
491
514
|
const instance = this.add(input);
|
|
492
|
-
this.layer.set(
|
|
515
|
+
this.layer.set(key2, input);
|
|
493
516
|
init.set(instance, true);
|
|
494
517
|
} else if (exists !== input) {
|
|
495
518
|
this.pop();
|
|
@@ -508,118 +531,101 @@ var Context = class _Context {
|
|
|
508
531
|
}
|
|
509
532
|
}
|
|
510
533
|
}
|
|
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
534
|
add(input, implicit) {
|
|
525
|
-
let writable = true;
|
|
526
535
|
let T;
|
|
527
536
|
let I;
|
|
528
537
|
if (typeof input == "function") {
|
|
538
|
+
Model.is(input);
|
|
529
539
|
T = input;
|
|
530
540
|
I = new input();
|
|
541
|
+
this.cleanup.add(() => I.set(null));
|
|
531
542
|
} else {
|
|
532
543
|
I = input;
|
|
533
544
|
T = I.constructor;
|
|
534
|
-
writable = false;
|
|
535
545
|
}
|
|
536
|
-
|
|
537
|
-
|
|
546
|
+
keys(T, true).forEach((K) => {
|
|
547
|
+
const result = this[K];
|
|
548
|
+
if (!result)
|
|
549
|
+
return;
|
|
550
|
+
const callback = result(I);
|
|
551
|
+
if (callback)
|
|
552
|
+
this.cleanup.add(callback);
|
|
553
|
+
});
|
|
554
|
+
keys(T).forEach((K) => {
|
|
555
|
+
const value = this.hasOwnProperty(K) ? null : I;
|
|
556
|
+
if (value || this[K] !== I && !implicit)
|
|
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);
|
|
538
566
|
return I;
|
|
539
567
|
}
|
|
540
|
-
push(inputs) {
|
|
541
|
-
const next = Object.create(this);
|
|
542
|
-
next.layer = /* @__PURE__ */ new Map();
|
|
543
|
-
if (inputs)
|
|
544
|
-
next.include(inputs);
|
|
545
|
-
return next;
|
|
546
|
-
}
|
|
547
|
-
pop() {
|
|
548
|
-
const items = /* @__PURE__ */ new Set();
|
|
549
|
-
for (const key of Object.getOwnPropertySymbols(this)) {
|
|
550
|
-
const entry = Object.getOwnPropertyDescriptor(this, key);
|
|
551
|
-
if (entry.writable && entry.value)
|
|
552
|
-
items.add(entry.value);
|
|
553
|
-
delete this[key];
|
|
554
|
-
}
|
|
555
|
-
for (const model of items)
|
|
556
|
-
model.set(null);
|
|
557
|
-
this.layer.clear();
|
|
558
|
-
}
|
|
559
568
|
};
|
|
560
569
|
|
|
561
570
|
// src/instruction/use.ts
|
|
562
571
|
var INSTRUCT = /* @__PURE__ */ new Map();
|
|
563
|
-
function use(
|
|
572
|
+
function use(arg1, arg2) {
|
|
564
573
|
const token = Symbol("instruction");
|
|
565
|
-
if (Model.is(
|
|
566
|
-
const type =
|
|
567
|
-
|
|
568
|
-
|
|
574
|
+
if (Model.is(arg1)) {
|
|
575
|
+
const type = arg1;
|
|
576
|
+
arg1 = (key2, subject) => {
|
|
577
|
+
const desc = { set: set2, get: get2 };
|
|
569
578
|
function set2(next) {
|
|
570
|
-
if (next ? !(next instanceof type) :
|
|
571
|
-
throw new Error(`${subject}.${
|
|
572
|
-
update(subject,
|
|
573
|
-
if (next && typeof
|
|
574
|
-
|
|
579
|
+
if (next ? !(next instanceof type) : arg2 !== false)
|
|
580
|
+
throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
|
|
581
|
+
update(subject, key2, next);
|
|
582
|
+
if (next && typeof arg2 == "function")
|
|
583
|
+
arg2(next);
|
|
575
584
|
return false;
|
|
576
585
|
}
|
|
577
|
-
|
|
578
|
-
|
|
586
|
+
function get2() {
|
|
587
|
+
set2(new type());
|
|
588
|
+
desc.get = arg2 !== false;
|
|
589
|
+
return fetch(subject, key2);
|
|
590
|
+
}
|
|
591
|
+
return desc;
|
|
579
592
|
};
|
|
580
593
|
}
|
|
581
|
-
INSTRUCT.set(token,
|
|
594
|
+
INSTRUCT.set(token, arg1);
|
|
582
595
|
return token;
|
|
583
596
|
}
|
|
584
597
|
Model.on((_, subject) => {
|
|
585
598
|
const state = STATE.get(subject);
|
|
586
599
|
const props = Object.getOwnPropertyDescriptors(subject);
|
|
587
|
-
for (const
|
|
588
|
-
const { value } = props[
|
|
600
|
+
for (const key2 in props) {
|
|
601
|
+
const { value } = props[key2];
|
|
589
602
|
const instruction = INSTRUCT.get(value);
|
|
590
|
-
if (!instruction)
|
|
603
|
+
if (!instruction) {
|
|
604
|
+
if (value instanceof Model && key2 !== "is")
|
|
605
|
+
update(subject, key2, value);
|
|
591
606
|
continue;
|
|
607
|
+
}
|
|
592
608
|
INSTRUCT.delete(value);
|
|
593
|
-
delete subject[
|
|
594
|
-
const output = instruction.call(subject,
|
|
609
|
+
delete subject[key2];
|
|
610
|
+
const output = instruction.call(subject, key2, subject, state);
|
|
595
611
|
if (!output)
|
|
596
612
|
continue;
|
|
597
613
|
const desc = typeof output == "object" ? output : { get: output };
|
|
598
|
-
const { enumerable = true } = desc;
|
|
599
614
|
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
|
-
},
|
|
615
|
+
state[key2] = desc.value;
|
|
616
|
+
Object.defineProperty(subject, key2, {
|
|
617
|
+
enumerable: desc.enumerable !== false,
|
|
617
618
|
get() {
|
|
618
619
|
return watch(
|
|
619
620
|
this,
|
|
620
|
-
|
|
621
|
-
typeof desc.get == "function" ? desc.get(this) : fetch(subject,
|
|
621
|
+
key2,
|
|
622
|
+
typeof desc.get == "function" ? desc.get(this) : fetch(subject, key2, desc.get)
|
|
622
623
|
);
|
|
624
|
+
},
|
|
625
|
+
set(next) {
|
|
626
|
+
if (desc.set === false)
|
|
627
|
+
throw new Error(`${subject}.${key2} is read-only.`);
|
|
628
|
+
update(subject, key2, next, desc.set);
|
|
623
629
|
}
|
|
624
630
|
});
|
|
625
631
|
}
|
|
@@ -629,10 +635,10 @@ Model.on((_, subject) => {
|
|
|
629
635
|
// src/instruction/get.ts
|
|
630
636
|
var STALE = /* @__PURE__ */ new WeakSet();
|
|
631
637
|
function get(arg0, arg1) {
|
|
632
|
-
return use((
|
|
638
|
+
return use((key2, subject, state) => {
|
|
633
639
|
let from = subject;
|
|
634
640
|
if (typeof arg0 == "symbol")
|
|
635
|
-
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${from}.${
|
|
641
|
+
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${from}.${key2}. This is not allowed.`);
|
|
636
642
|
let source = (resolve) => resolve(from);
|
|
637
643
|
if (arg0 instanceof Model) {
|
|
638
644
|
from = arg0;
|
|
@@ -662,26 +668,26 @@ function get(arg0, arg1) {
|
|
|
662
668
|
let connect2 = function(model) {
|
|
663
669
|
reset = effect(model, (current) => {
|
|
664
670
|
proxy = current;
|
|
665
|
-
if (!(
|
|
671
|
+
if (!(key2 in state) || STALE.delete(compute2))
|
|
666
672
|
compute2(!reset);
|
|
667
673
|
return (didUpdate) => {
|
|
668
674
|
if (didUpdate) {
|
|
669
675
|
STALE.add(compute2);
|
|
670
|
-
push(subject,
|
|
676
|
+
push(subject, key2, true);
|
|
671
677
|
}
|
|
672
678
|
};
|
|
673
679
|
});
|
|
674
680
|
}, compute2 = function(initial) {
|
|
675
681
|
let next;
|
|
676
682
|
try {
|
|
677
|
-
next = getter.call(proxy, proxy,
|
|
683
|
+
next = getter.call(proxy, proxy, key2);
|
|
678
684
|
} catch (err) {
|
|
679
|
-
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${
|
|
685
|
+
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${key2}.`);
|
|
680
686
|
if (initial)
|
|
681
687
|
throw err;
|
|
682
688
|
console.error(err);
|
|
683
689
|
}
|
|
684
|
-
update(subject,
|
|
690
|
+
update(subject, key2, next, !isAsync);
|
|
685
691
|
};
|
|
686
692
|
var connect = connect2, compute = compute2;
|
|
687
693
|
const getter = arg1;
|
|
@@ -695,10 +701,10 @@ function get(arg0, arg1) {
|
|
|
695
701
|
}
|
|
696
702
|
if (STALE.delete(compute2))
|
|
697
703
|
compute2();
|
|
698
|
-
return fetch(subject,
|
|
704
|
+
return fetch(subject, key2, !proxy);
|
|
699
705
|
};
|
|
700
706
|
} else
|
|
701
|
-
source((got) => subject.set(
|
|
707
|
+
source((got) => subject.set(key2, got));
|
|
702
708
|
return { get: arg1 };
|
|
703
709
|
});
|
|
704
710
|
}
|
|
@@ -706,35 +712,35 @@ function get(arg0, arg1) {
|
|
|
706
712
|
// src/instruction/ref.ts
|
|
707
713
|
var define2 = Object.defineProperty;
|
|
708
714
|
function ref(arg, arg2) {
|
|
709
|
-
return use((
|
|
715
|
+
return use((key2, subject, state) => {
|
|
710
716
|
let value = {};
|
|
711
717
|
if (arg === subject)
|
|
712
718
|
subject.get(() => {
|
|
713
|
-
for (const
|
|
719
|
+
for (const key3 in state)
|
|
714
720
|
if (typeof arg2 == "function")
|
|
715
|
-
define2(value,
|
|
721
|
+
define2(value, key3, {
|
|
716
722
|
configurable: true,
|
|
717
723
|
get() {
|
|
718
|
-
const out = arg2(
|
|
719
|
-
define2(value,
|
|
724
|
+
const out = arg2(key3);
|
|
725
|
+
define2(value, key3, { value: out });
|
|
720
726
|
return out;
|
|
721
727
|
}
|
|
722
728
|
});
|
|
723
729
|
else {
|
|
724
|
-
const get2 = () => state[
|
|
725
|
-
const set2 = (value2) => subject.set(
|
|
730
|
+
const get2 = () => state[key3];
|
|
731
|
+
const set2 = (value2) => subject.set(key3, value2);
|
|
726
732
|
define2(set2, "current", { get: get2, set: set2 });
|
|
727
733
|
define2(set2, "get", { value: get2 });
|
|
728
|
-
define2(value,
|
|
734
|
+
define2(value, key3, { value: set2 });
|
|
729
735
|
}
|
|
730
736
|
});
|
|
731
737
|
else if (typeof arg == "object")
|
|
732
738
|
throw new Error("ref instruction does not support object which is not 'this'");
|
|
733
739
|
else {
|
|
734
740
|
let unSet;
|
|
735
|
-
const get2 = () => state[
|
|
741
|
+
const get2 = () => state[key2];
|
|
736
742
|
const set2 = (value2) => {
|
|
737
|
-
if (!subject.set(
|
|
743
|
+
if (!subject.set(key2, value2) || !arg)
|
|
738
744
|
return;
|
|
739
745
|
if (unSet)
|
|
740
746
|
unSet = void unSet(value2);
|
|
@@ -744,44 +750,44 @@ function ref(arg, arg2) {
|
|
|
744
750
|
unSet = out;
|
|
745
751
|
}
|
|
746
752
|
};
|
|
747
|
-
state[
|
|
753
|
+
state[key2] = null;
|
|
748
754
|
value = Object.defineProperties(set2, {
|
|
749
755
|
current: { get: get2, set: set2 },
|
|
750
756
|
get: { value: get2 }
|
|
751
757
|
});
|
|
752
758
|
}
|
|
753
|
-
define2(subject,
|
|
759
|
+
define2(subject, key2, { value });
|
|
754
760
|
});
|
|
755
761
|
}
|
|
756
762
|
|
|
757
763
|
// src/instruction/set.ts
|
|
758
764
|
function set(value, argument) {
|
|
759
|
-
return use((
|
|
765
|
+
return use((key2, subject) => {
|
|
760
766
|
const output = {};
|
|
761
767
|
if (typeof value == "function" || value instanceof Promise) {
|
|
762
768
|
let init2 = function() {
|
|
763
769
|
if (typeof value == "function")
|
|
764
770
|
try {
|
|
765
|
-
value = attempt(value.bind(subject,
|
|
771
|
+
value = attempt(value.bind(subject, key2));
|
|
766
772
|
} catch (err) {
|
|
767
|
-
console.warn(`Generating initial value for ${subject}.${
|
|
773
|
+
console.warn(`Generating initial value for ${subject}.${key2} failed.`);
|
|
768
774
|
throw err;
|
|
769
775
|
}
|
|
770
|
-
output.get =
|
|
776
|
+
output.get = argument === true;
|
|
771
777
|
if (value instanceof Promise) {
|
|
772
778
|
value.then((value2) => {
|
|
773
|
-
subject.set(
|
|
779
|
+
subject.set(key2, value2);
|
|
774
780
|
return value2;
|
|
775
781
|
}).catch((err) => {
|
|
776
|
-
subject.set(
|
|
782
|
+
subject.set(key2);
|
|
777
783
|
output.get = () => {
|
|
778
784
|
throw err;
|
|
779
785
|
};
|
|
780
786
|
});
|
|
781
787
|
} else
|
|
782
|
-
subject.set(
|
|
788
|
+
subject.set(key2, value);
|
|
783
789
|
if (0 in arguments)
|
|
784
|
-
return subject[
|
|
790
|
+
return subject[key2];
|
|
785
791
|
};
|
|
786
792
|
var init = init2;
|
|
787
793
|
if (argument)
|
|
@@ -804,7 +810,7 @@ function set(value, argument) {
|
|
|
804
810
|
} else
|
|
805
811
|
output.set = (value2) => {
|
|
806
812
|
output.get = void 0;
|
|
807
|
-
subject.set(
|
|
813
|
+
subject.set(key2, value2);
|
|
808
814
|
};
|
|
809
815
|
return output;
|
|
810
816
|
});
|
|
@@ -828,41 +834,73 @@ function attempt(fn) {
|
|
|
828
834
|
}
|
|
829
835
|
|
|
830
836
|
// src/instruction/has.ts
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
837
|
+
var APPLY = /* @__PURE__ */ new WeakMap();
|
|
838
|
+
function has(arg1, arg2) {
|
|
839
|
+
return use((key2, subject) => {
|
|
840
|
+
const applied = /* @__PURE__ */ new Set();
|
|
841
|
+
const update2 = () => {
|
|
842
|
+
subject.set(key2, Object.freeze(Array.from(applied)));
|
|
843
|
+
};
|
|
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 {
|
|
884
|
+
if (APPLY.has(subject))
|
|
885
|
+
throw new Error(`'has' callback can only be used once per model.`);
|
|
886
|
+
APPLY.set(subject, (recipient) => {
|
|
887
|
+
let remove;
|
|
888
|
+
if (arg1) {
|
|
889
|
+
remove = arg1(recipient, subject);
|
|
890
|
+
if (remove === false)
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
applied.add(recipient);
|
|
894
|
+
update2();
|
|
895
|
+
return () => {
|
|
896
|
+
applied.delete(recipient);
|
|
897
|
+
update2();
|
|
898
|
+
if (typeof remove == "function")
|
|
899
|
+
remove();
|
|
840
900
|
};
|
|
841
|
-
model.get(null, remove);
|
|
842
|
-
return remove;
|
|
843
901
|
});
|
|
844
|
-
return { get: argument };
|
|
845
902
|
}
|
|
846
|
-
|
|
847
|
-
expect((model) => {
|
|
848
|
-
if (value.has(model))
|
|
849
|
-
return;
|
|
850
|
-
const remove = typeof argument == "function" ? argument(model) : void 0;
|
|
851
|
-
if (remove === false)
|
|
852
|
-
return;
|
|
853
|
-
value.add(model);
|
|
854
|
-
subject.set(key);
|
|
855
|
-
const done = () => {
|
|
856
|
-
drop();
|
|
857
|
-
if (typeof remove == "function")
|
|
858
|
-
remove();
|
|
859
|
-
value.delete(model);
|
|
860
|
-
subject.set(key);
|
|
861
|
-
};
|
|
862
|
-
const drop = model.get(null, done);
|
|
863
|
-
return done;
|
|
864
|
-
});
|
|
865
|
-
return { value };
|
|
903
|
+
return { value: [] };
|
|
866
904
|
});
|
|
867
905
|
}
|
|
868
906
|
// Annotate the CommonJS export names for ESM import in node:
|