@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.mjs
CHANGED
|
@@ -2,59 +2,44 @@
|
|
|
2
2
|
var DISPATCH = /* @__PURE__ */ new Set();
|
|
3
3
|
var OBSERVER = /* @__PURE__ */ new WeakMap();
|
|
4
4
|
var LISTENER = /* @__PURE__ */ new WeakMap();
|
|
5
|
-
var REQUIRED = /* @__PURE__ */ new WeakSet();
|
|
6
5
|
var onReady = () => null;
|
|
7
6
|
function addListener(subject, callback, select) {
|
|
8
7
|
let subs = LISTENER.get(subject);
|
|
9
8
|
if (!subs)
|
|
10
9
|
LISTENER.set(subject, subs = /* @__PURE__ */ new Map([[onReady, false]]));
|
|
11
|
-
const filter =
|
|
10
|
+
const filter = select !== void 0 && /* @__PURE__ */ new Set([select]);
|
|
12
11
|
if (!filter && !subs.has(onReady))
|
|
13
12
|
callback(true, subject);
|
|
14
13
|
subs.set(callback, filter);
|
|
15
14
|
return () => subs.delete(callback);
|
|
16
15
|
}
|
|
17
|
-
function watch(from,
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const listeners = LISTENER.get(from);
|
|
21
|
-
listeners.set(
|
|
22
|
-
observer,
|
|
23
|
-
(listeners.get(observer) || /* @__PURE__ */ new Set()).add(key)
|
|
24
|
-
);
|
|
25
|
-
const nested = LISTENER.get(value);
|
|
26
|
-
if (nested) {
|
|
27
|
-
LISTENER.set(value = Object.create(value), nested);
|
|
28
|
-
OBSERVER.set(value, observer);
|
|
29
|
-
}
|
|
30
|
-
if (value === void 0 && REQUIRED.has(from))
|
|
31
|
-
throw new Error(`${from}.${key} is required in this context.`);
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
16
|
+
function watch(from, key2, value) {
|
|
17
|
+
const access = OBSERVER.get(from);
|
|
18
|
+
return access ? access(from, key2, value) : value;
|
|
34
19
|
}
|
|
35
20
|
var PENDING = /* @__PURE__ */ new WeakMap();
|
|
36
|
-
function emit(source,
|
|
21
|
+
function emit(source, key2) {
|
|
37
22
|
const subs = LISTENER.get(source);
|
|
38
23
|
const ready = !subs.has(onReady);
|
|
39
|
-
if (
|
|
24
|
+
if (key2 === true && ready)
|
|
40
25
|
return;
|
|
41
26
|
let pending = PENDING.get(source);
|
|
42
27
|
if (pending) {
|
|
43
|
-
pending.add(
|
|
28
|
+
pending.add(key2);
|
|
44
29
|
return;
|
|
45
30
|
}
|
|
46
31
|
PENDING.set(source, pending = new Set(
|
|
47
|
-
|
|
32
|
+
key2 !== true && ready ? [key2] : [true, key2]
|
|
48
33
|
));
|
|
49
|
-
pending.forEach((
|
|
50
|
-
pending.delete(
|
|
34
|
+
pending.forEach((key3) => {
|
|
35
|
+
pending.delete(key3);
|
|
51
36
|
subs.forEach((select, callback) => {
|
|
52
37
|
let after;
|
|
53
|
-
if (!select || select.has(
|
|
54
|
-
if (after = callback.call(source,
|
|
38
|
+
if (!select || select.has(key3)) {
|
|
39
|
+
if (after = callback.call(source, key3, source))
|
|
55
40
|
queue(after);
|
|
56
41
|
}
|
|
57
|
-
if (after === null ||
|
|
42
|
+
if (after === null || key3 === null)
|
|
58
43
|
subs.delete(callback);
|
|
59
44
|
});
|
|
60
45
|
});
|
|
@@ -81,8 +66,7 @@ function effect(target, callback, requireValues) {
|
|
|
81
66
|
function invoke() {
|
|
82
67
|
let stale;
|
|
83
68
|
const subscriber = Object.create(target);
|
|
84
|
-
|
|
85
|
-
OBSERVER.set(subscriber, () => {
|
|
69
|
+
function onUpdate() {
|
|
86
70
|
if (stale)
|
|
87
71
|
return null;
|
|
88
72
|
stale = true;
|
|
@@ -91,9 +75,24 @@ function effect(target, callback, requireValues) {
|
|
|
91
75
|
unset = void 0;
|
|
92
76
|
}
|
|
93
77
|
return reset;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
78
|
+
}
|
|
79
|
+
function access(from, key2, value) {
|
|
80
|
+
if (value === void 0 && requireValues)
|
|
81
|
+
throw new Error(`${from}.${key2} is required in this context.`);
|
|
82
|
+
const listeners2 = LISTENER.get(from);
|
|
83
|
+
let listener = listeners2.get(onUpdate);
|
|
84
|
+
if (!listener)
|
|
85
|
+
listeners2.set(onUpdate, listener = /* @__PURE__ */ new Set());
|
|
86
|
+
listener.add(key2);
|
|
87
|
+
const nested = LISTENER.get(value);
|
|
88
|
+
if (nested) {
|
|
89
|
+
LISTENER.set(value = Object.create(value), nested);
|
|
90
|
+
OBSERVER.set(value, access);
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
LISTENER.set(subscriber, listeners);
|
|
95
|
+
OBSERVER.set(subscriber, access);
|
|
97
96
|
try {
|
|
98
97
|
const out = callback.call(subscriber, subscriber);
|
|
99
98
|
unset = typeof out == "function" ? out : void 0;
|
|
@@ -106,12 +105,12 @@ function effect(target, callback, requireValues) {
|
|
|
106
105
|
throw err;
|
|
107
106
|
}
|
|
108
107
|
}
|
|
109
|
-
addListener(target, (
|
|
110
|
-
if (
|
|
108
|
+
addListener(target, (key2) => {
|
|
109
|
+
if (key2 === true)
|
|
111
110
|
invoke();
|
|
112
111
|
else if (!reset)
|
|
113
112
|
return reset;
|
|
114
|
-
if (
|
|
113
|
+
if (key2 === null && unset)
|
|
115
114
|
unset(null);
|
|
116
115
|
});
|
|
117
116
|
return () => {
|
|
@@ -165,15 +164,15 @@ var Model = class _Model {
|
|
|
165
164
|
}
|
|
166
165
|
done = cleanup;
|
|
167
166
|
})();
|
|
168
|
-
for (const
|
|
169
|
-
const desc = Object.getOwnPropertyDescriptor(this,
|
|
167
|
+
for (const key2 in this) {
|
|
168
|
+
const desc = Object.getOwnPropertyDescriptor(this, key2);
|
|
170
169
|
if ("value" in desc) {
|
|
171
|
-
update(this,
|
|
172
|
-
define(this,
|
|
170
|
+
update(this, key2, desc.value, true);
|
|
171
|
+
define(this, key2, {
|
|
173
172
|
configurable: false,
|
|
174
|
-
set: (x) => update(this,
|
|
173
|
+
set: (x) => update(this, key2, x),
|
|
175
174
|
get() {
|
|
176
|
-
return watch(this,
|
|
175
|
+
return watch(this, key2, state[key2]);
|
|
177
176
|
}
|
|
178
177
|
});
|
|
179
178
|
}
|
|
@@ -208,12 +207,12 @@ var Model = class _Model {
|
|
|
208
207
|
if (arg1 === void 0) {
|
|
209
208
|
const values = {};
|
|
210
209
|
const current = !EXPORT && (EXPORT = /* @__PURE__ */ new Map([[self, values]]));
|
|
211
|
-
for (let [
|
|
210
|
+
for (let [key2, value] of self) {
|
|
212
211
|
if (EXPORT.has(value))
|
|
213
212
|
value = EXPORT.get(value);
|
|
214
213
|
else if (value && typeof value.get === "function")
|
|
215
214
|
EXPORT.set(value, value = value.get());
|
|
216
|
-
values[
|
|
215
|
+
values[key2] = value;
|
|
217
216
|
}
|
|
218
217
|
if (current)
|
|
219
218
|
EXPORT = void 0;
|
|
@@ -234,9 +233,9 @@ var Model = class _Model {
|
|
|
234
233
|
set(arg1, arg2, arg3) {
|
|
235
234
|
const self = this.is;
|
|
236
235
|
if (typeof arg1 == "function")
|
|
237
|
-
return addListener(self, (
|
|
238
|
-
if (typeof
|
|
239
|
-
return arg1.call(self,
|
|
236
|
+
return addListener(self, (key2) => {
|
|
237
|
+
if (typeof key2 == "string")
|
|
238
|
+
return arg1.call(self, key2, self);
|
|
240
239
|
});
|
|
241
240
|
if (arg1 === null)
|
|
242
241
|
return emit(this, null);
|
|
@@ -251,8 +250,8 @@ var Model = class _Model {
|
|
|
251
250
|
if (PENDING2.has(self))
|
|
252
251
|
return {
|
|
253
252
|
then: (res) => new Promise((res2) => {
|
|
254
|
-
const remove = addListener(self, (
|
|
255
|
-
if (
|
|
253
|
+
const remove = addListener(self, (key2) => {
|
|
254
|
+
if (key2 !== true) {
|
|
256
255
|
remove();
|
|
257
256
|
return res2.bind(null, Array.from(PENDING2.get(self)));
|
|
258
257
|
}
|
|
@@ -272,6 +271,7 @@ var Model = class _Model {
|
|
|
272
271
|
* @param args - arguments sent to constructor
|
|
273
272
|
*/
|
|
274
273
|
static new(...args) {
|
|
274
|
+
_Model.is(this);
|
|
275
275
|
const instance = new this(...args);
|
|
276
276
|
emit(instance, true);
|
|
277
277
|
return instance;
|
|
@@ -283,6 +283,8 @@ var Model = class _Model {
|
|
|
283
283
|
* methods and properties of this class.
|
|
284
284
|
*/
|
|
285
285
|
static is(maybe) {
|
|
286
|
+
if (maybe === _Model)
|
|
287
|
+
throw new Error("Model is abstract and not a valid type.");
|
|
286
288
|
return typeof maybe == "function" && maybe.prototype instanceof this;
|
|
287
289
|
}
|
|
288
290
|
/**
|
|
@@ -315,12 +317,12 @@ function fetch(subject, property, required) {
|
|
|
315
317
|
}
|
|
316
318
|
const error = new Error(`${subject}.${property} is not yet available.`);
|
|
317
319
|
const promise = new Promise((resolve, reject) => {
|
|
318
|
-
addListener(subject, (
|
|
319
|
-
if (
|
|
320
|
-
resolve(state[
|
|
320
|
+
addListener(subject, (key2) => {
|
|
321
|
+
if (key2 === property) {
|
|
322
|
+
resolve(state[key2]);
|
|
321
323
|
return null;
|
|
322
324
|
}
|
|
323
|
-
if (
|
|
325
|
+
if (key2 === null)
|
|
324
326
|
reject(new Error(`${subject} is destroyed.`));
|
|
325
327
|
});
|
|
326
328
|
});
|
|
@@ -331,22 +333,29 @@ function fetch(subject, property, required) {
|
|
|
331
333
|
stack: error.stack
|
|
332
334
|
});
|
|
333
335
|
}
|
|
334
|
-
function update(subject,
|
|
336
|
+
function update(subject, key2, value, arg) {
|
|
335
337
|
const state = STATE.get(subject);
|
|
336
338
|
if (Object.isFrozen(state))
|
|
337
|
-
throw new Error(`Tried to update ${String(
|
|
338
|
-
const previous = state[
|
|
339
|
+
throw new Error(`Tried to update ${String(key2)} but ${subject} is destroyed.`);
|
|
340
|
+
const previous = state[key2];
|
|
341
|
+
if (typeof arg == "function") {
|
|
342
|
+
const out = arg.call(subject, value, previous);
|
|
343
|
+
if (out === false)
|
|
344
|
+
return false;
|
|
345
|
+
if (typeof out == "function")
|
|
346
|
+
value = out();
|
|
347
|
+
}
|
|
339
348
|
if (value === previous)
|
|
340
349
|
return true;
|
|
341
350
|
if (value instanceof Model && !PARENT.has(value)) {
|
|
342
351
|
PARENT.set(value, subject);
|
|
343
352
|
emit(value, true);
|
|
344
353
|
}
|
|
345
|
-
state[
|
|
346
|
-
if (
|
|
347
|
-
push(subject,
|
|
354
|
+
state[key2] = value;
|
|
355
|
+
if (arg !== true)
|
|
356
|
+
push(subject, key2);
|
|
348
357
|
}
|
|
349
|
-
function push(subject,
|
|
358
|
+
function push(subject, key2, silent) {
|
|
350
359
|
let pending = PENDING2.get(subject);
|
|
351
360
|
if (!pending) {
|
|
352
361
|
PENDING2.set(subject, pending = /* @__PURE__ */ new Set());
|
|
@@ -356,43 +365,43 @@ function push(subject, key, silent) {
|
|
|
356
365
|
PENDING2.delete(subject);
|
|
357
366
|
});
|
|
358
367
|
}
|
|
359
|
-
pending.add(
|
|
368
|
+
pending.add(key2);
|
|
360
369
|
if (!silent)
|
|
361
|
-
emit(subject,
|
|
370
|
+
emit(subject, key2);
|
|
362
371
|
}
|
|
363
372
|
var METHODS = new WeakMap([[Model, []]]);
|
|
364
373
|
function assign(to, values) {
|
|
365
374
|
const methods = METHODS.get(to.constructor);
|
|
366
375
|
const applicable = new Set(Object.keys(to).concat(methods));
|
|
367
|
-
for (const
|
|
368
|
-
if (applicable.has(
|
|
369
|
-
to[
|
|
376
|
+
for (const key2 in values)
|
|
377
|
+
if (applicable.has(key2))
|
|
378
|
+
to[key2] = values[key2];
|
|
370
379
|
}
|
|
371
380
|
function bindMethods(type) {
|
|
372
|
-
let
|
|
373
|
-
if (!
|
|
374
|
-
METHODS.set(type,
|
|
381
|
+
let keys2 = METHODS.get(type);
|
|
382
|
+
if (!keys2) {
|
|
383
|
+
METHODS.set(type, keys2 = bindMethods(Object.getPrototypeOf(type)).slice());
|
|
375
384
|
const desc = Object.getOwnPropertyDescriptors(type.prototype);
|
|
376
|
-
for (const
|
|
377
|
-
if (
|
|
385
|
+
for (const key2 in desc)
|
|
386
|
+
if (key2 != "constructor" && "value" in desc[key2]) {
|
|
378
387
|
let bind2 = function(method) {
|
|
379
388
|
const value2 = method.bind(this.is);
|
|
380
389
|
METHOD.set(value2, method);
|
|
381
|
-
define(this.is,
|
|
390
|
+
define(this.is, key2, { value: value2, writable: true });
|
|
382
391
|
return value2;
|
|
383
392
|
};
|
|
384
393
|
var bind = bind2;
|
|
385
|
-
let { value } = desc[
|
|
386
|
-
|
|
387
|
-
Object.defineProperty(type.prototype,
|
|
394
|
+
let { value } = desc[key2];
|
|
395
|
+
keys2.push(key2);
|
|
396
|
+
Object.defineProperty(type.prototype, key2, {
|
|
388
397
|
set: bind2,
|
|
389
398
|
get() {
|
|
390
|
-
return this.hasOwnProperty(
|
|
399
|
+
return this.hasOwnProperty(key2) ? value : bind2.call(this, value);
|
|
391
400
|
}
|
|
392
401
|
});
|
|
393
402
|
}
|
|
394
403
|
}
|
|
395
|
-
return
|
|
404
|
+
return keys2;
|
|
396
405
|
}
|
|
397
406
|
function uid() {
|
|
398
407
|
return (Math.random() * 0.722 + 0.278).toString(36).substring(2, 8).toUpperCase();
|
|
@@ -400,62 +409,76 @@ function uid() {
|
|
|
400
409
|
|
|
401
410
|
// src/context.ts
|
|
402
411
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
412
|
+
var Table = /* @__PURE__ */ new Map();
|
|
413
|
+
function key(T, upstream) {
|
|
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;
|
|
427
|
+
}
|
|
403
428
|
var Context = class _Context {
|
|
404
429
|
constructor(inputs) {
|
|
405
|
-
this.downstream = /* @__PURE__ */ new WeakMap();
|
|
406
|
-
this.upstream = /* @__PURE__ */ new WeakMap();
|
|
407
430
|
this.layer = /* @__PURE__ */ new Map();
|
|
431
|
+
this.cleanup = /* @__PURE__ */ new Set();
|
|
408
432
|
if (inputs)
|
|
409
433
|
this.include(inputs);
|
|
410
434
|
}
|
|
411
435
|
static get(from, callback) {
|
|
412
436
|
const waiting = Register.get(from);
|
|
413
|
-
if (
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
else if (waiting)
|
|
418
|
-
waiting.push(callback);
|
|
419
|
-
else
|
|
420
|
-
Register.set(from, [callback]);
|
|
421
|
-
}
|
|
422
|
-
key(T, upstream) {
|
|
423
|
-
const table = upstream ? this.upstream : this.downstream;
|
|
424
|
-
let key = table.get(T);
|
|
425
|
-
if (!key) {
|
|
426
|
-
key = Symbol(T.name + (upstream ? " request" : ""));
|
|
427
|
-
table.set(T, key);
|
|
437
|
+
if (waiting instanceof _Context) {
|
|
438
|
+
if (callback)
|
|
439
|
+
callback(waiting);
|
|
440
|
+
return waiting;
|
|
428
441
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
const waiting = Register.get(model);
|
|
435
|
-
if (waiting instanceof Array)
|
|
436
|
-
waiting.forEach((cb) => cb(this));
|
|
437
|
-
Register.set(model, this);
|
|
438
|
-
if (typeof result == "function")
|
|
439
|
-
return result(model);
|
|
442
|
+
if (callback)
|
|
443
|
+
if (waiting)
|
|
444
|
+
waiting.push(callback);
|
|
445
|
+
else
|
|
446
|
+
Register.set(from, [callback]);
|
|
440
447
|
}
|
|
441
|
-
get(Type) {
|
|
442
|
-
|
|
448
|
+
get(Type, callback) {
|
|
449
|
+
if (callback)
|
|
450
|
+
Object.defineProperty(this, key(Type, true), { value: callback });
|
|
451
|
+
const result = this[key(Type)];
|
|
443
452
|
if (result === null)
|
|
444
453
|
throw new Error(`Did find ${Type} in context, but multiple were defined.`);
|
|
445
454
|
return result;
|
|
446
455
|
}
|
|
456
|
+
push(inputs) {
|
|
457
|
+
const next = Object.create(this);
|
|
458
|
+
next.layer = /* @__PURE__ */ new Map();
|
|
459
|
+
next.cleanup = /* @__PURE__ */ new Set();
|
|
460
|
+
if (inputs)
|
|
461
|
+
next.include(inputs);
|
|
462
|
+
return next;
|
|
463
|
+
}
|
|
464
|
+
pop() {
|
|
465
|
+
for (const key2 of Object.getOwnPropertySymbols(this))
|
|
466
|
+
delete this[key2];
|
|
467
|
+
this.cleanup.forEach((cb) => cb());
|
|
468
|
+
this.layer.clear();
|
|
469
|
+
}
|
|
447
470
|
include(inputs, forEach) {
|
|
448
471
|
const init = /* @__PURE__ */ new Map();
|
|
449
472
|
Object.entries(inputs).forEach(([K, V]) => {
|
|
450
473
|
if (!(Model.is(V) || V instanceof Model))
|
|
451
474
|
throw new Error(`Context can only include Model or instance but got ${V}${K && " as " + K}.`);
|
|
452
475
|
});
|
|
453
|
-
for (const
|
|
454
|
-
const input = inputs[
|
|
455
|
-
const exists = this.layer.get(
|
|
476
|
+
for (const key2 in inputs) {
|
|
477
|
+
const input = inputs[key2];
|
|
478
|
+
const exists = this.layer.get(key2);
|
|
456
479
|
if (!exists) {
|
|
457
480
|
const instance = this.add(input);
|
|
458
|
-
this.layer.set(
|
|
481
|
+
this.layer.set(key2, input);
|
|
459
482
|
init.set(instance, true);
|
|
460
483
|
} else if (exists !== input) {
|
|
461
484
|
this.pop();
|
|
@@ -474,118 +497,101 @@ var Context = class _Context {
|
|
|
474
497
|
}
|
|
475
498
|
}
|
|
476
499
|
}
|
|
477
|
-
put(T, I, implicit, writable) {
|
|
478
|
-
do {
|
|
479
|
-
const key = this.key(T, typeof I == "function");
|
|
480
|
-
const value = this.hasOwnProperty(key) ? null : I;
|
|
481
|
-
if (value || this[key] !== I && !implicit)
|
|
482
|
-
Object.defineProperty(this, key, {
|
|
483
|
-
configurable: true,
|
|
484
|
-
writable,
|
|
485
|
-
value
|
|
486
|
-
});
|
|
487
|
-
T = Object.getPrototypeOf(T);
|
|
488
|
-
} while (T !== Model);
|
|
489
|
-
}
|
|
490
500
|
add(input, implicit) {
|
|
491
|
-
let writable = true;
|
|
492
501
|
let T;
|
|
493
502
|
let I;
|
|
494
503
|
if (typeof input == "function") {
|
|
504
|
+
Model.is(input);
|
|
495
505
|
T = input;
|
|
496
506
|
I = new input();
|
|
507
|
+
this.cleanup.add(() => I.set(null));
|
|
497
508
|
} else {
|
|
498
509
|
I = input;
|
|
499
510
|
T = I.constructor;
|
|
500
|
-
writable = false;
|
|
501
511
|
}
|
|
502
|
-
|
|
503
|
-
|
|
512
|
+
keys(T, true).forEach((K) => {
|
|
513
|
+
const result = this[K];
|
|
514
|
+
if (!result)
|
|
515
|
+
return;
|
|
516
|
+
const callback = result(I);
|
|
517
|
+
if (callback)
|
|
518
|
+
this.cleanup.add(callback);
|
|
519
|
+
});
|
|
520
|
+
keys(T).forEach((K) => {
|
|
521
|
+
const value = this.hasOwnProperty(K) ? null : I;
|
|
522
|
+
if (value || this[K] !== I && !implicit)
|
|
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);
|
|
504
532
|
return I;
|
|
505
533
|
}
|
|
506
|
-
push(inputs) {
|
|
507
|
-
const next = Object.create(this);
|
|
508
|
-
next.layer = /* @__PURE__ */ new Map();
|
|
509
|
-
if (inputs)
|
|
510
|
-
next.include(inputs);
|
|
511
|
-
return next;
|
|
512
|
-
}
|
|
513
|
-
pop() {
|
|
514
|
-
const items = /* @__PURE__ */ new Set();
|
|
515
|
-
for (const key of Object.getOwnPropertySymbols(this)) {
|
|
516
|
-
const entry = Object.getOwnPropertyDescriptor(this, key);
|
|
517
|
-
if (entry.writable && entry.value)
|
|
518
|
-
items.add(entry.value);
|
|
519
|
-
delete this[key];
|
|
520
|
-
}
|
|
521
|
-
for (const model of items)
|
|
522
|
-
model.set(null);
|
|
523
|
-
this.layer.clear();
|
|
524
|
-
}
|
|
525
534
|
};
|
|
526
535
|
|
|
527
536
|
// src/instruction/use.ts
|
|
528
537
|
var INSTRUCT = /* @__PURE__ */ new Map();
|
|
529
|
-
function use(
|
|
538
|
+
function use(arg1, arg2) {
|
|
530
539
|
const token = Symbol("instruction");
|
|
531
|
-
if (Model.is(
|
|
532
|
-
const type =
|
|
533
|
-
|
|
534
|
-
|
|
540
|
+
if (Model.is(arg1)) {
|
|
541
|
+
const type = arg1;
|
|
542
|
+
arg1 = (key2, subject) => {
|
|
543
|
+
const desc = { set: set2, get: get2 };
|
|
535
544
|
function set2(next) {
|
|
536
|
-
if (next ? !(next instanceof type) :
|
|
537
|
-
throw new Error(`${subject}.${
|
|
538
|
-
update(subject,
|
|
539
|
-
if (next && typeof
|
|
540
|
-
|
|
545
|
+
if (next ? !(next instanceof type) : arg2 !== false)
|
|
546
|
+
throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
|
|
547
|
+
update(subject, key2, next);
|
|
548
|
+
if (next && typeof arg2 == "function")
|
|
549
|
+
arg2(next);
|
|
541
550
|
return false;
|
|
542
551
|
}
|
|
543
|
-
|
|
544
|
-
|
|
552
|
+
function get2() {
|
|
553
|
+
set2(new type());
|
|
554
|
+
desc.get = arg2 !== false;
|
|
555
|
+
return fetch(subject, key2);
|
|
556
|
+
}
|
|
557
|
+
return desc;
|
|
545
558
|
};
|
|
546
559
|
}
|
|
547
|
-
INSTRUCT.set(token,
|
|
560
|
+
INSTRUCT.set(token, arg1);
|
|
548
561
|
return token;
|
|
549
562
|
}
|
|
550
563
|
Model.on((_, subject) => {
|
|
551
564
|
const state = STATE.get(subject);
|
|
552
565
|
const props = Object.getOwnPropertyDescriptors(subject);
|
|
553
|
-
for (const
|
|
554
|
-
const { value } = props[
|
|
566
|
+
for (const key2 in props) {
|
|
567
|
+
const { value } = props[key2];
|
|
555
568
|
const instruction = INSTRUCT.get(value);
|
|
556
|
-
if (!instruction)
|
|
569
|
+
if (!instruction) {
|
|
570
|
+
if (value instanceof Model && key2 !== "is")
|
|
571
|
+
update(subject, key2, value);
|
|
557
572
|
continue;
|
|
573
|
+
}
|
|
558
574
|
INSTRUCT.delete(value);
|
|
559
|
-
delete subject[
|
|
560
|
-
const output = instruction.call(subject,
|
|
575
|
+
delete subject[key2];
|
|
576
|
+
const output = instruction.call(subject, key2, subject, state);
|
|
561
577
|
if (!output)
|
|
562
578
|
continue;
|
|
563
579
|
const desc = typeof output == "object" ? output : { get: output };
|
|
564
|
-
const { enumerable = true } = desc;
|
|
565
580
|
if ("value" in desc)
|
|
566
|
-
state[
|
|
567
|
-
Object.defineProperty(subject,
|
|
568
|
-
enumerable,
|
|
569
|
-
set(next) {
|
|
570
|
-
let { set: set2 } = desc;
|
|
571
|
-
if (set2 === false)
|
|
572
|
-
throw new Error(`${subject}.${key} is read-only.`);
|
|
573
|
-
if (typeof set2 == "function") {
|
|
574
|
-
const result = set2.call(subject, next, state[key]);
|
|
575
|
-
if (result === false)
|
|
576
|
-
return;
|
|
577
|
-
if (typeof result == "function")
|
|
578
|
-
next = result();
|
|
579
|
-
set2 = false;
|
|
580
|
-
}
|
|
581
|
-
update(subject, key, next, !!set2);
|
|
582
|
-
},
|
|
581
|
+
state[key2] = desc.value;
|
|
582
|
+
Object.defineProperty(subject, key2, {
|
|
583
|
+
enumerable: desc.enumerable !== false,
|
|
583
584
|
get() {
|
|
584
585
|
return watch(
|
|
585
586
|
this,
|
|
586
|
-
|
|
587
|
-
typeof desc.get == "function" ? desc.get(this) : fetch(subject,
|
|
587
|
+
key2,
|
|
588
|
+
typeof desc.get == "function" ? desc.get(this) : fetch(subject, key2, desc.get)
|
|
588
589
|
);
|
|
590
|
+
},
|
|
591
|
+
set(next) {
|
|
592
|
+
if (desc.set === false)
|
|
593
|
+
throw new Error(`${subject}.${key2} is read-only.`);
|
|
594
|
+
update(subject, key2, next, desc.set);
|
|
589
595
|
}
|
|
590
596
|
});
|
|
591
597
|
}
|
|
@@ -595,10 +601,10 @@ Model.on((_, subject) => {
|
|
|
595
601
|
// src/instruction/get.ts
|
|
596
602
|
var STALE = /* @__PURE__ */ new WeakSet();
|
|
597
603
|
function get(arg0, arg1) {
|
|
598
|
-
return use((
|
|
604
|
+
return use((key2, subject, state) => {
|
|
599
605
|
let from = subject;
|
|
600
606
|
if (typeof arg0 == "symbol")
|
|
601
|
-
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${from}.${
|
|
607
|
+
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${from}.${key2}. This is not allowed.`);
|
|
602
608
|
let source = (resolve) => resolve(from);
|
|
603
609
|
if (arg0 instanceof Model) {
|
|
604
610
|
from = arg0;
|
|
@@ -628,26 +634,26 @@ function get(arg0, arg1) {
|
|
|
628
634
|
let connect2 = function(model) {
|
|
629
635
|
reset = effect(model, (current) => {
|
|
630
636
|
proxy = current;
|
|
631
|
-
if (!(
|
|
637
|
+
if (!(key2 in state) || STALE.delete(compute2))
|
|
632
638
|
compute2(!reset);
|
|
633
639
|
return (didUpdate) => {
|
|
634
640
|
if (didUpdate) {
|
|
635
641
|
STALE.add(compute2);
|
|
636
|
-
push(subject,
|
|
642
|
+
push(subject, key2, true);
|
|
637
643
|
}
|
|
638
644
|
};
|
|
639
645
|
});
|
|
640
646
|
}, compute2 = function(initial) {
|
|
641
647
|
let next;
|
|
642
648
|
try {
|
|
643
|
-
next = getter.call(proxy, proxy,
|
|
649
|
+
next = getter.call(proxy, proxy, key2);
|
|
644
650
|
} catch (err) {
|
|
645
|
-
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${
|
|
651
|
+
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${key2}.`);
|
|
646
652
|
if (initial)
|
|
647
653
|
throw err;
|
|
648
654
|
console.error(err);
|
|
649
655
|
}
|
|
650
|
-
update(subject,
|
|
656
|
+
update(subject, key2, next, !isAsync);
|
|
651
657
|
};
|
|
652
658
|
var connect = connect2, compute = compute2;
|
|
653
659
|
const getter = arg1;
|
|
@@ -661,10 +667,10 @@ function get(arg0, arg1) {
|
|
|
661
667
|
}
|
|
662
668
|
if (STALE.delete(compute2))
|
|
663
669
|
compute2();
|
|
664
|
-
return fetch(subject,
|
|
670
|
+
return fetch(subject, key2, !proxy);
|
|
665
671
|
};
|
|
666
672
|
} else
|
|
667
|
-
source((got) => subject.set(
|
|
673
|
+
source((got) => subject.set(key2, got));
|
|
668
674
|
return { get: arg1 };
|
|
669
675
|
});
|
|
670
676
|
}
|
|
@@ -672,35 +678,35 @@ function get(arg0, arg1) {
|
|
|
672
678
|
// src/instruction/ref.ts
|
|
673
679
|
var define2 = Object.defineProperty;
|
|
674
680
|
function ref(arg, arg2) {
|
|
675
|
-
return use((
|
|
681
|
+
return use((key2, subject, state) => {
|
|
676
682
|
let value = {};
|
|
677
683
|
if (arg === subject)
|
|
678
684
|
subject.get(() => {
|
|
679
|
-
for (const
|
|
685
|
+
for (const key3 in state)
|
|
680
686
|
if (typeof arg2 == "function")
|
|
681
|
-
define2(value,
|
|
687
|
+
define2(value, key3, {
|
|
682
688
|
configurable: true,
|
|
683
689
|
get() {
|
|
684
|
-
const out = arg2(
|
|
685
|
-
define2(value,
|
|
690
|
+
const out = arg2(key3);
|
|
691
|
+
define2(value, key3, { value: out });
|
|
686
692
|
return out;
|
|
687
693
|
}
|
|
688
694
|
});
|
|
689
695
|
else {
|
|
690
|
-
const get2 = () => state[
|
|
691
|
-
const set2 = (value2) => subject.set(
|
|
696
|
+
const get2 = () => state[key3];
|
|
697
|
+
const set2 = (value2) => subject.set(key3, value2);
|
|
692
698
|
define2(set2, "current", { get: get2, set: set2 });
|
|
693
699
|
define2(set2, "get", { value: get2 });
|
|
694
|
-
define2(value,
|
|
700
|
+
define2(value, key3, { value: set2 });
|
|
695
701
|
}
|
|
696
702
|
});
|
|
697
703
|
else if (typeof arg == "object")
|
|
698
704
|
throw new Error("ref instruction does not support object which is not 'this'");
|
|
699
705
|
else {
|
|
700
706
|
let unSet;
|
|
701
|
-
const get2 = () => state[
|
|
707
|
+
const get2 = () => state[key2];
|
|
702
708
|
const set2 = (value2) => {
|
|
703
|
-
if (!subject.set(
|
|
709
|
+
if (!subject.set(key2, value2) || !arg)
|
|
704
710
|
return;
|
|
705
711
|
if (unSet)
|
|
706
712
|
unSet = void unSet(value2);
|
|
@@ -710,44 +716,44 @@ function ref(arg, arg2) {
|
|
|
710
716
|
unSet = out;
|
|
711
717
|
}
|
|
712
718
|
};
|
|
713
|
-
state[
|
|
719
|
+
state[key2] = null;
|
|
714
720
|
value = Object.defineProperties(set2, {
|
|
715
721
|
current: { get: get2, set: set2 },
|
|
716
722
|
get: { value: get2 }
|
|
717
723
|
});
|
|
718
724
|
}
|
|
719
|
-
define2(subject,
|
|
725
|
+
define2(subject, key2, { value });
|
|
720
726
|
});
|
|
721
727
|
}
|
|
722
728
|
|
|
723
729
|
// src/instruction/set.ts
|
|
724
730
|
function set(value, argument) {
|
|
725
|
-
return use((
|
|
731
|
+
return use((key2, subject) => {
|
|
726
732
|
const output = {};
|
|
727
733
|
if (typeof value == "function" || value instanceof Promise) {
|
|
728
734
|
let init2 = function() {
|
|
729
735
|
if (typeof value == "function")
|
|
730
736
|
try {
|
|
731
|
-
value = attempt(value.bind(subject,
|
|
737
|
+
value = attempt(value.bind(subject, key2));
|
|
732
738
|
} catch (err) {
|
|
733
|
-
console.warn(`Generating initial value for ${subject}.${
|
|
739
|
+
console.warn(`Generating initial value for ${subject}.${key2} failed.`);
|
|
734
740
|
throw err;
|
|
735
741
|
}
|
|
736
|
-
output.get =
|
|
742
|
+
output.get = argument === true;
|
|
737
743
|
if (value instanceof Promise) {
|
|
738
744
|
value.then((value2) => {
|
|
739
|
-
subject.set(
|
|
745
|
+
subject.set(key2, value2);
|
|
740
746
|
return value2;
|
|
741
747
|
}).catch((err) => {
|
|
742
|
-
subject.set(
|
|
748
|
+
subject.set(key2);
|
|
743
749
|
output.get = () => {
|
|
744
750
|
throw err;
|
|
745
751
|
};
|
|
746
752
|
});
|
|
747
753
|
} else
|
|
748
|
-
subject.set(
|
|
754
|
+
subject.set(key2, value);
|
|
749
755
|
if (0 in arguments)
|
|
750
|
-
return subject[
|
|
756
|
+
return subject[key2];
|
|
751
757
|
};
|
|
752
758
|
var init = init2;
|
|
753
759
|
if (argument)
|
|
@@ -770,7 +776,7 @@ function set(value, argument) {
|
|
|
770
776
|
} else
|
|
771
777
|
output.set = (value2) => {
|
|
772
778
|
output.get = void 0;
|
|
773
|
-
subject.set(
|
|
779
|
+
subject.set(key2, value2);
|
|
774
780
|
};
|
|
775
781
|
return output;
|
|
776
782
|
});
|
|
@@ -794,41 +800,73 @@ function attempt(fn) {
|
|
|
794
800
|
}
|
|
795
801
|
|
|
796
802
|
// src/instruction/has.ts
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
803
|
+
var APPLY = /* @__PURE__ */ new WeakMap();
|
|
804
|
+
function has(arg1, arg2) {
|
|
805
|
+
return use((key2, subject) => {
|
|
806
|
+
const applied = /* @__PURE__ */ new Set();
|
|
807
|
+
const update2 = () => {
|
|
808
|
+
subject.set(key2, Object.freeze(Array.from(applied)));
|
|
809
|
+
};
|
|
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 {
|
|
850
|
+
if (APPLY.has(subject))
|
|
851
|
+
throw new Error(`'has' callback can only be used once per model.`);
|
|
852
|
+
APPLY.set(subject, (recipient) => {
|
|
853
|
+
let remove;
|
|
854
|
+
if (arg1) {
|
|
855
|
+
remove = arg1(recipient, subject);
|
|
856
|
+
if (remove === false)
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
applied.add(recipient);
|
|
860
|
+
update2();
|
|
861
|
+
return () => {
|
|
862
|
+
applied.delete(recipient);
|
|
863
|
+
update2();
|
|
864
|
+
if (typeof remove == "function")
|
|
865
|
+
remove();
|
|
806
866
|
};
|
|
807
|
-
model.get(null, remove);
|
|
808
|
-
return remove;
|
|
809
867
|
});
|
|
810
|
-
return { get: argument };
|
|
811
868
|
}
|
|
812
|
-
|
|
813
|
-
expect((model) => {
|
|
814
|
-
if (value.has(model))
|
|
815
|
-
return;
|
|
816
|
-
const remove = typeof argument == "function" ? argument(model) : void 0;
|
|
817
|
-
if (remove === false)
|
|
818
|
-
return;
|
|
819
|
-
value.add(model);
|
|
820
|
-
subject.set(key);
|
|
821
|
-
const done = () => {
|
|
822
|
-
drop();
|
|
823
|
-
if (typeof remove == "function")
|
|
824
|
-
remove();
|
|
825
|
-
value.delete(model);
|
|
826
|
-
subject.set(key);
|
|
827
|
-
};
|
|
828
|
-
const drop = model.get(null, done);
|
|
829
|
-
return done;
|
|
830
|
-
});
|
|
831
|
-
return { value };
|
|
869
|
+
return { value: [] };
|
|
832
870
|
});
|
|
833
871
|
}
|
|
834
872
|
export {
|