@expressive/mvc 0.71.0 → 0.72.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/esm/context.js +111 -90
- package/dist/esm/control.js +31 -42
- package/dist/esm/index.js +10 -11
- package/dist/esm/instruction/get.js +64 -73
- package/dist/esm/instruction/ref.js +40 -37
- package/dist/esm/instruction/set.js +78 -27
- package/dist/esm/instruction/use.js +19 -21
- package/dist/esm/state.js +324 -0
- package/dist/index.d.ts +225 -190
- package/dist/index.js +502 -531
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/esm/instruction/has.js +0 -86
- package/dist/esm/model.js +0 -318
package/dist/index.js
CHANGED
|
@@ -22,17 +22,16 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
Context: () => Context,
|
|
24
24
|
METHOD: () => METHOD,
|
|
25
|
-
Model: () => Model,
|
|
26
25
|
Observable: () => Observable,
|
|
26
|
+
State: () => State,
|
|
27
27
|
addListener: () => addListener,
|
|
28
|
-
|
|
29
|
-
default: () => Model,
|
|
28
|
+
default: () => State,
|
|
30
29
|
event: () => event,
|
|
31
30
|
get: () => get,
|
|
32
|
-
has: () => has,
|
|
33
31
|
ref: () => ref,
|
|
34
32
|
set: () => set,
|
|
35
|
-
use: () => use
|
|
33
|
+
use: () => use,
|
|
34
|
+
watch: () => watch
|
|
36
35
|
});
|
|
37
36
|
module.exports = __toCommonJS(index_exports);
|
|
38
37
|
|
|
@@ -49,40 +48,38 @@ function addListener(subject, callback, select) {
|
|
|
49
48
|
LISTENERS.set(subject, listeners = /* @__PURE__ */ new Map([[onReady, void 0]]));
|
|
50
49
|
if (select !== void 0 && !(select instanceof Set))
|
|
51
50
|
select = /* @__PURE__ */ new Set([select]);
|
|
52
|
-
if (!listeners.has(onReady) && !select)
|
|
51
|
+
if (!listeners.has(onReady) && !select) {
|
|
53
52
|
callback.call(subject, true, subject);
|
|
53
|
+
}
|
|
54
54
|
listeners.set(callback, select);
|
|
55
55
|
return () => listeners.delete(callback);
|
|
56
56
|
}
|
|
57
|
-
function emit(
|
|
58
|
-
const listeners = LISTENERS.get(
|
|
57
|
+
function emit(state, key2) {
|
|
58
|
+
const listeners = LISTENERS.get(state);
|
|
59
59
|
const notReady = listeners.has(onReady);
|
|
60
|
-
if (key2 === true && !notReady)
|
|
61
|
-
|
|
62
|
-
let pending = PENDING.get(model);
|
|
60
|
+
if (key2 === true && !notReady) return;
|
|
61
|
+
let pending = PENDING.get(state);
|
|
63
62
|
if (pending) {
|
|
64
63
|
pending.add(key2);
|
|
65
64
|
return;
|
|
66
65
|
}
|
|
67
|
-
PENDING.set(
|
|
66
|
+
PENDING.set(state, pending = new Set(notReady ? [true, key2] : [key2]));
|
|
68
67
|
for (const key3 of pending)
|
|
69
68
|
for (const [callback, filter] of listeners)
|
|
70
69
|
if (!filter || filter.has(key3)) {
|
|
71
|
-
const after = callback.call(
|
|
72
|
-
if (after)
|
|
70
|
+
const after = callback.call(state, key3, state);
|
|
71
|
+
if (after) {
|
|
73
72
|
enqueue(after);
|
|
74
|
-
else if (after === null)
|
|
73
|
+
} else if (after === null) {
|
|
75
74
|
listeners.delete(callback);
|
|
75
|
+
}
|
|
76
76
|
}
|
|
77
|
-
if (key2 === null)
|
|
78
|
-
|
|
79
|
-
PENDING.delete(model);
|
|
77
|
+
if (key2 === null) listeners.clear();
|
|
78
|
+
PENDING.delete(state);
|
|
80
79
|
}
|
|
81
80
|
function event(subject, key2, silent) {
|
|
82
|
-
if (key2 === null)
|
|
83
|
-
|
|
84
|
-
if (!key2)
|
|
85
|
-
return emit(subject, true);
|
|
81
|
+
if (key2 === null) return emit(subject, key2);
|
|
82
|
+
if (!key2) return emit(subject, true);
|
|
86
83
|
let pending = PENDING_KEYS.get(subject);
|
|
87
84
|
if (!pending) {
|
|
88
85
|
PENDING_KEYS.set(subject, pending = /* @__PURE__ */ new Set());
|
|
@@ -93,8 +90,7 @@ function event(subject, key2, silent) {
|
|
|
93
90
|
});
|
|
94
91
|
}
|
|
95
92
|
pending.add(key2);
|
|
96
|
-
if (!silent)
|
|
97
|
-
emit(subject, key2);
|
|
93
|
+
if (!silent) emit(subject, key2);
|
|
98
94
|
}
|
|
99
95
|
function enqueue(eventHandler) {
|
|
100
96
|
if (!DISPATCH.size)
|
|
@@ -110,14 +106,13 @@ function enqueue(eventHandler) {
|
|
|
110
106
|
}, 0);
|
|
111
107
|
DISPATCH.add(eventHandler);
|
|
112
108
|
}
|
|
113
|
-
function
|
|
109
|
+
function watch(target, callback, argument) {
|
|
114
110
|
let unset;
|
|
115
111
|
let reset;
|
|
116
112
|
function invoke() {
|
|
117
113
|
let ignore = true;
|
|
118
114
|
function onUpdate() {
|
|
119
|
-
if (ignore || reset === null)
|
|
120
|
-
return;
|
|
115
|
+
if (ignore || reset === null) return;
|
|
121
116
|
ignore = true;
|
|
122
117
|
if (reset && unset) {
|
|
123
118
|
unset(true);
|
|
@@ -133,41 +128,34 @@ function createEffect(target, callback, argument) {
|
|
|
133
128
|
ignore = false;
|
|
134
129
|
reset = output === null ? null : invoke;
|
|
135
130
|
unset = (key2) => {
|
|
136
|
-
if (typeof output == "function")
|
|
137
|
-
output(key2);
|
|
131
|
+
if (typeof output == "function") output(key2);
|
|
138
132
|
flush();
|
|
139
133
|
};
|
|
140
134
|
} catch (err) {
|
|
141
135
|
if (err instanceof Promise) {
|
|
142
136
|
reset = void 0;
|
|
143
137
|
err.then(invoke);
|
|
144
|
-
} else
|
|
138
|
+
} else {
|
|
145
139
|
throw err;
|
|
140
|
+
}
|
|
146
141
|
}
|
|
147
142
|
}
|
|
148
143
|
function cleanup() {
|
|
149
|
-
if (unset)
|
|
150
|
-
unset(false);
|
|
144
|
+
if (unset) unset(false);
|
|
151
145
|
reset = null;
|
|
152
146
|
}
|
|
153
|
-
;
|
|
154
|
-
if (EffectContext && argument !== false)
|
|
155
|
-
EffectContext.add(cleanup);
|
|
147
|
+
if (EffectContext && argument !== false) EffectContext.add(cleanup);
|
|
156
148
|
addListener(target, (key2) => {
|
|
157
|
-
if (key2 === true)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return reset;
|
|
161
|
-
if (key2 === null && unset)
|
|
162
|
-
unset(null);
|
|
149
|
+
if (key2 === true) invoke();
|
|
150
|
+
else if (!reset) return reset;
|
|
151
|
+
if (key2 === null && unset) unset(null);
|
|
163
152
|
});
|
|
164
153
|
return cleanup;
|
|
165
154
|
}
|
|
166
155
|
var EffectContext;
|
|
167
156
|
function enter(ignore) {
|
|
168
|
-
if (ignore)
|
|
169
|
-
|
|
170
|
-
};
|
|
157
|
+
if (ignore) return () => () => {
|
|
158
|
+
};
|
|
171
159
|
const last = EffectContext;
|
|
172
160
|
const context = EffectContext = /* @__PURE__ */ new Set();
|
|
173
161
|
return () => {
|
|
@@ -176,7 +164,7 @@ function enter(ignore) {
|
|
|
176
164
|
};
|
|
177
165
|
}
|
|
178
166
|
|
|
179
|
-
// src/
|
|
167
|
+
// src/state.ts
|
|
180
168
|
var define = Object.defineProperty;
|
|
181
169
|
var ID = /* @__PURE__ */ new WeakMap();
|
|
182
170
|
var STATE = /* @__PURE__ */ new WeakMap();
|
|
@@ -184,7 +172,8 @@ var NOTIFY = /* @__PURE__ */ new WeakMap();
|
|
|
184
172
|
var PARENT = /* @__PURE__ */ new WeakMap();
|
|
185
173
|
var METHODS = /* @__PURE__ */ new WeakMap();
|
|
186
174
|
var METHOD = /* @__PURE__ */ new WeakMap();
|
|
187
|
-
var
|
|
175
|
+
var EXPORT;
|
|
176
|
+
var State = class {
|
|
188
177
|
constructor(...args) {
|
|
189
178
|
prepare(this);
|
|
190
179
|
define(this, "is", { value: this });
|
|
@@ -194,8 +183,7 @@ var Model = class {
|
|
|
194
183
|
const watch2 = /* @__PURE__ */ new Set();
|
|
195
184
|
const proxy = Object.create(this);
|
|
196
185
|
addListener(this, (key2) => {
|
|
197
|
-
if (watch2.has(key2))
|
|
198
|
-
callback();
|
|
186
|
+
if (watch2.has(key2)) callback();
|
|
199
187
|
});
|
|
200
188
|
OBSERVER.set(proxy, (key2, value) => {
|
|
201
189
|
if (value === void 0 && required)
|
|
@@ -209,19 +197,11 @@ var Model = class {
|
|
|
209
197
|
}
|
|
210
198
|
get(arg1, arg2) {
|
|
211
199
|
const self = this.is;
|
|
212
|
-
if (
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
return cb === null ? null : (update2) => {
|
|
218
|
-
pending = PENDING_KEYS.get(self);
|
|
219
|
-
if (typeof cb == "function")
|
|
220
|
-
cb(update2);
|
|
221
|
-
};
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
return arg1 === void 0 ? snapshot(self) : typeof arg2 == "function" ? addListener(self, arg2, arg1) : arg1 === null ? Object.isFrozen(STATE.get(self)) : fetch(self, arg1, arg2);
|
|
200
|
+
if (arg1 === void 0) return values(self);
|
|
201
|
+
if (typeof arg1 == "function") return effect(self, arg1);
|
|
202
|
+
if (typeof arg2 == "function") return addListener(self, arg2, arg1);
|
|
203
|
+
if (arg1 === null) return Object.isFrozen(STATE.get(self));
|
|
204
|
+
return access(self, arg1, arg2);
|
|
225
205
|
}
|
|
226
206
|
set(arg1, arg2, arg3) {
|
|
227
207
|
const self = this.is;
|
|
@@ -230,14 +210,15 @@ var Model = class {
|
|
|
230
210
|
if (arg2 === key2 || arg2 === void 0 && typeof key2 == "string")
|
|
231
211
|
return arg1.call(self, key2, self);
|
|
232
212
|
});
|
|
233
|
-
if (arg1 && typeof arg1 == "object")
|
|
213
|
+
if (arg1 && typeof arg1 == "object") {
|
|
234
214
|
assign(self, arg1, arg2 === true);
|
|
235
|
-
else if (!arg2)
|
|
215
|
+
} else if (!arg2) {
|
|
236
216
|
event(self, arg1);
|
|
237
|
-
else if (arg3)
|
|
217
|
+
} else if (arg3) {
|
|
238
218
|
manage(self, arg1, arg2);
|
|
239
|
-
else
|
|
219
|
+
} else {
|
|
240
220
|
update(self, arg1, arg2);
|
|
221
|
+
}
|
|
241
222
|
const pending = PENDING_KEYS.get(this);
|
|
242
223
|
if (pending)
|
|
243
224
|
return {
|
|
@@ -252,21 +233,24 @@ var Model = class {
|
|
|
252
233
|
};
|
|
253
234
|
}
|
|
254
235
|
/**
|
|
255
|
-
* Iterate over managed properties in this instance of
|
|
236
|
+
* Iterate over managed properties in this instance of State.
|
|
256
237
|
* Yeilds the key and current value for each property.
|
|
257
238
|
*/
|
|
258
239
|
[Symbol.iterator]() {
|
|
259
240
|
return Object.entries(STATE.get(this.is))[Symbol.iterator]();
|
|
260
241
|
}
|
|
261
242
|
/**
|
|
262
|
-
* Create and activate a new instance of this
|
|
263
|
-
*
|
|
243
|
+
* Create and activate a new instance of this state.
|
|
244
|
+
*
|
|
264
245
|
* **Important** - Unlike `new this(...)` - this method also activates state.
|
|
265
|
-
*
|
|
246
|
+
*
|
|
266
247
|
* @param args - arguments sent to constructor
|
|
267
248
|
*/
|
|
268
249
|
static new(...args) {
|
|
269
|
-
const instance = new this(...args)
|
|
250
|
+
const instance = new this(...args, (x) => {
|
|
251
|
+
const cb = x.new && x.new();
|
|
252
|
+
if (cb) x.set(cb, null);
|
|
253
|
+
});
|
|
270
254
|
event(instance);
|
|
271
255
|
return instance;
|
|
272
256
|
}
|
|
@@ -280,22 +264,21 @@ var Model = class {
|
|
|
280
264
|
return maybe === this || typeof maybe == "function" && maybe.prototype instanceof this;
|
|
281
265
|
}
|
|
282
266
|
/**
|
|
283
|
-
* Register a callback to run when any instance of this
|
|
267
|
+
* Register a callback to run when any instance of this State is updated.
|
|
284
268
|
*/
|
|
285
269
|
static on(listener) {
|
|
286
270
|
let notify = NOTIFY.get(this);
|
|
287
|
-
if (!notify)
|
|
288
|
-
NOTIFY.set(this, notify = /* @__PURE__ */ new Set());
|
|
271
|
+
if (!notify) NOTIFY.set(this, notify = /* @__PURE__ */ new Set());
|
|
289
272
|
notify.add(listener);
|
|
290
273
|
return () => notify.delete(listener);
|
|
291
274
|
}
|
|
292
275
|
};
|
|
293
|
-
define(
|
|
276
|
+
define(State.prototype, "toString", {
|
|
294
277
|
value() {
|
|
295
278
|
return ID.get(this.is);
|
|
296
279
|
}
|
|
297
280
|
});
|
|
298
|
-
define(
|
|
281
|
+
define(State, "toString", {
|
|
299
282
|
value() {
|
|
300
283
|
return this.name;
|
|
301
284
|
}
|
|
@@ -304,29 +287,29 @@ function assign(subject, data, silent) {
|
|
|
304
287
|
const methods = METHODS.get(subject.constructor);
|
|
305
288
|
for (const key2 in data) {
|
|
306
289
|
const bind = methods.get(key2);
|
|
307
|
-
if (bind)
|
|
308
|
-
bind.call(subject, data[key2]);
|
|
290
|
+
if (bind) bind.call(subject, data[key2]);
|
|
309
291
|
else if (key2 in subject && key2 !== "is") {
|
|
310
292
|
const desc = Object.getOwnPropertyDescriptor(subject, key2);
|
|
311
293
|
const set2 = desc && desc.set;
|
|
312
|
-
if (set2)
|
|
294
|
+
if (set2) {
|
|
313
295
|
set2.call(subject, data[key2], silent);
|
|
314
|
-
else
|
|
296
|
+
} else {
|
|
315
297
|
subject[key2] = data[key2];
|
|
298
|
+
}
|
|
316
299
|
}
|
|
317
300
|
}
|
|
318
301
|
}
|
|
319
|
-
function prepare(
|
|
320
|
-
let T =
|
|
321
|
-
if (T ===
|
|
322
|
-
throw new Error("Cannot create base Model.");
|
|
302
|
+
function prepare(state) {
|
|
303
|
+
let T = state.constructor;
|
|
304
|
+
if (T === State) throw new Error("Cannot create base State.");
|
|
323
305
|
const chain = [];
|
|
324
306
|
let keys2 = /* @__PURE__ */ new Map();
|
|
325
|
-
ID.set(
|
|
307
|
+
ID.set(state, `${T}-${uid()}`);
|
|
326
308
|
while (T.name) {
|
|
327
|
-
for (const cb of NOTIFY.get(T) || [])
|
|
328
|
-
addListener(
|
|
329
|
-
|
|
309
|
+
for (const cb of NOTIFY.get(T) || []) {
|
|
310
|
+
addListener(state, cb);
|
|
311
|
+
}
|
|
312
|
+
if (T === State) break;
|
|
330
313
|
else chain.unshift(T);
|
|
331
314
|
T = Object.getPrototypeOf(T);
|
|
332
315
|
}
|
|
@@ -341,8 +324,7 @@ function prepare(model) {
|
|
|
341
324
|
)) {
|
|
342
325
|
let bind2 = function(original) {
|
|
343
326
|
const { is } = this;
|
|
344
|
-
if (is.hasOwnProperty(key2) && !original)
|
|
345
|
-
return value;
|
|
327
|
+
if (is.hasOwnProperty(key2) && !original) return value;
|
|
346
328
|
const fn = original || value;
|
|
347
329
|
const bound = fn.bind(is);
|
|
348
330
|
METHOD.set(bound, fn);
|
|
@@ -350,58 +332,85 @@ function prepare(model) {
|
|
|
350
332
|
return bound;
|
|
351
333
|
};
|
|
352
334
|
var bind = bind2;
|
|
353
|
-
if (!value || key2 == "constructor")
|
|
354
|
-
continue;
|
|
335
|
+
if (!value || key2 == "constructor") continue;
|
|
355
336
|
keys2.set(key2, bind2);
|
|
356
337
|
define(type.prototype, key2, { get: bind2, set: bind2 });
|
|
357
338
|
}
|
|
358
339
|
}
|
|
359
340
|
}
|
|
360
|
-
function init(
|
|
341
|
+
function init(self, args) {
|
|
361
342
|
const state = {};
|
|
362
|
-
STATE.set(
|
|
343
|
+
STATE.set(self, state);
|
|
363
344
|
args = args.flat().filter((arg) => {
|
|
364
|
-
if (typeof arg
|
|
365
|
-
|
|
366
|
-
ID.set(model, arg);
|
|
345
|
+
if (typeof arg == "string") ID.set(self, arg);
|
|
346
|
+
else return true;
|
|
367
347
|
});
|
|
368
|
-
addListener(
|
|
369
|
-
if (!PARENT.has(
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
if ("value" in desc)
|
|
374
|
-
manage(model, key2, desc.value, true);
|
|
348
|
+
addListener(self, () => {
|
|
349
|
+
if (!PARENT.has(self)) PARENT.set(self, null);
|
|
350
|
+
for (const key2 in self) {
|
|
351
|
+
const desc = Object.getOwnPropertyDescriptor(self, key2);
|
|
352
|
+
if ("value" in desc) manage(self, key2, desc.value, true);
|
|
375
353
|
}
|
|
376
354
|
for (const arg of args) {
|
|
377
|
-
const use2 = typeof arg == "function" ? arg.call(
|
|
355
|
+
const use2 = typeof arg == "function" ? arg.call(self, self) : arg;
|
|
378
356
|
if (use2 instanceof Promise)
|
|
379
357
|
use2.catch((err) => {
|
|
380
|
-
console.error(`Async error in constructor for ${
|
|
358
|
+
console.error(`Async error in constructor for ${self}:`);
|
|
381
359
|
console.error(err);
|
|
382
360
|
});
|
|
383
|
-
else if (
|
|
384
|
-
|
|
385
|
-
else if (typeof use2 == "object")
|
|
386
|
-
assign(model, use2, true);
|
|
361
|
+
else if (Array.isArray(use2)) args.push(...use2);
|
|
362
|
+
else if (typeof use2 == "function") addListener(self, use2, null);
|
|
363
|
+
else if (typeof use2 == "object") assign(self, use2, true);
|
|
387
364
|
}
|
|
388
|
-
addListener(
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
365
|
+
addListener(
|
|
366
|
+
self,
|
|
367
|
+
() => {
|
|
368
|
+
for (const [_, value] of self)
|
|
369
|
+
if (value instanceof State && PARENT.get(value) === self)
|
|
370
|
+
value.set(null);
|
|
371
|
+
Object.freeze(state);
|
|
372
|
+
},
|
|
373
|
+
null
|
|
374
|
+
);
|
|
394
375
|
return null;
|
|
395
376
|
});
|
|
396
377
|
}
|
|
378
|
+
function effect(target, fn) {
|
|
379
|
+
const effect2 = METHOD.get(fn) || fn;
|
|
380
|
+
let pending = /* @__PURE__ */ new Set();
|
|
381
|
+
return watch(target, (proxy) => {
|
|
382
|
+
const cb = effect2.call(proxy, proxy, pending);
|
|
383
|
+
if (cb === null) return cb;
|
|
384
|
+
return (update2) => {
|
|
385
|
+
pending = PENDING_KEYS.get(target);
|
|
386
|
+
if (typeof cb == "function") cb(update2);
|
|
387
|
+
};
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
function values(target) {
|
|
391
|
+
const values2 = {};
|
|
392
|
+
let isNotRecursive;
|
|
393
|
+
if (!EXPORT) {
|
|
394
|
+
isNotRecursive = true;
|
|
395
|
+
EXPORT = /* @__PURE__ */ new Map([[target, values2]]);
|
|
396
|
+
}
|
|
397
|
+
for (let [key2, value] of target) {
|
|
398
|
+
if (EXPORT.has(value)) value = EXPORT.get(value);
|
|
399
|
+
else if (value && typeof value == "object" && "get" in value && typeof value.get === "function")
|
|
400
|
+
EXPORT.set(value, value = value.get());
|
|
401
|
+
values2[key2] = value;
|
|
402
|
+
}
|
|
403
|
+
if (isNotRecursive) EXPORT = void 0;
|
|
404
|
+
return Object.freeze(values2);
|
|
405
|
+
}
|
|
397
406
|
function manage(target, key2, value, silent) {
|
|
398
407
|
const state = STATE.get(target);
|
|
399
408
|
function get2() {
|
|
400
|
-
return
|
|
409
|
+
return follow(this, key2, state[key2]);
|
|
401
410
|
}
|
|
402
411
|
function set2(value2, silent2) {
|
|
403
412
|
update(target, key2, value2, silent2);
|
|
404
|
-
if (value2 instanceof
|
|
413
|
+
if (value2 instanceof State && !PARENT.has(value2)) {
|
|
405
414
|
PARENT.set(value2, target);
|
|
406
415
|
event(value2);
|
|
407
416
|
}
|
|
@@ -410,16 +419,15 @@ function manage(target, key2, value, silent) {
|
|
|
410
419
|
set2(value, silent);
|
|
411
420
|
}
|
|
412
421
|
var OBSERVER = /* @__PURE__ */ new WeakMap();
|
|
413
|
-
function
|
|
414
|
-
const
|
|
415
|
-
return
|
|
422
|
+
function follow(from, key2, value) {
|
|
423
|
+
const observe = OBSERVER.get(from);
|
|
424
|
+
return observe ? observe(key2, value) : value;
|
|
416
425
|
}
|
|
417
|
-
function
|
|
426
|
+
function access(subject, property, required) {
|
|
418
427
|
const state = STATE.get(subject);
|
|
419
428
|
if (property in state || required === false) {
|
|
420
429
|
const value = state[property];
|
|
421
|
-
if (value !== void 0 || !required)
|
|
422
|
-
return value;
|
|
430
|
+
if (value !== void 0 || !required) return value;
|
|
423
431
|
}
|
|
424
432
|
if (METHODS.get(subject.constructor).has(property))
|
|
425
433
|
return METHOD.get(subject[property]);
|
|
@@ -430,8 +438,9 @@ function fetch(subject, property, required) {
|
|
|
430
438
|
resolve(state[key2]);
|
|
431
439
|
return null;
|
|
432
440
|
}
|
|
433
|
-
if (key2 === null)
|
|
441
|
+
if (key2 === null) {
|
|
434
442
|
reject(new Error(`${subject} is destroyed.`));
|
|
443
|
+
}
|
|
435
444
|
});
|
|
436
445
|
});
|
|
437
446
|
throw Object.assign(promise, {
|
|
@@ -441,148 +450,145 @@ function fetch(subject, property, required) {
|
|
|
441
450
|
stack: error.stack
|
|
442
451
|
});
|
|
443
452
|
}
|
|
444
|
-
var EXPORT;
|
|
445
|
-
function snapshot(model) {
|
|
446
|
-
const values = {};
|
|
447
|
-
let isNotRecursive;
|
|
448
|
-
if (!EXPORT) {
|
|
449
|
-
isNotRecursive = true;
|
|
450
|
-
EXPORT = /* @__PURE__ */ new Map([[model, values]]);
|
|
451
|
-
}
|
|
452
|
-
for (let [key2, value] of model) {
|
|
453
|
-
if (EXPORT.has(value))
|
|
454
|
-
value = EXPORT.get(value);
|
|
455
|
-
else if (value && typeof value == "object" && "get" in value && typeof value.get === "function")
|
|
456
|
-
EXPORT.set(value, value = value.get());
|
|
457
|
-
values[key2] = value;
|
|
458
|
-
}
|
|
459
|
-
if (isNotRecursive)
|
|
460
|
-
EXPORT = void 0;
|
|
461
|
-
return Object.freeze(values);
|
|
462
|
-
}
|
|
463
453
|
function update(subject, key2, value, arg) {
|
|
464
454
|
const state = STATE.get(subject);
|
|
465
455
|
if (Object.isFrozen(state))
|
|
466
|
-
throw new Error(
|
|
456
|
+
throw new Error(
|
|
457
|
+
`Tried to update ${subject}.${String(key2)} but state is destroyed.`
|
|
458
|
+
);
|
|
467
459
|
const previous = state[key2];
|
|
468
460
|
if (typeof arg == "function") {
|
|
469
461
|
const out = arg.call(subject, value, previous);
|
|
470
|
-
if (out === false)
|
|
471
|
-
|
|
472
|
-
if (typeof out == "function")
|
|
473
|
-
value = out();
|
|
462
|
+
if (out === false) return false;
|
|
463
|
+
if (typeof out == "function") value = out();
|
|
474
464
|
}
|
|
475
|
-
if (value === previous && key2 in state)
|
|
476
|
-
return;
|
|
465
|
+
if (value === previous && key2 in state) return;
|
|
477
466
|
state[key2] = value;
|
|
478
|
-
if (arg !== true)
|
|
479
|
-
event(subject, key2);
|
|
467
|
+
if (arg !== true) event(subject, key2);
|
|
480
468
|
return true;
|
|
481
469
|
}
|
|
482
470
|
function uid() {
|
|
483
471
|
return (0.278 + Math.random() * 0.722).toString(36).substring(2, 8).toUpperCase();
|
|
484
472
|
}
|
|
485
473
|
|
|
474
|
+
// src/instruction/use.ts
|
|
475
|
+
var INSTRUCTION = /* @__PURE__ */ new Map();
|
|
476
|
+
function use(arg1, arg2) {
|
|
477
|
+
if (State.is(arg1)) arg1 = childInstruction(arg1, arg2);
|
|
478
|
+
const token = Symbol("instruction-" + uid());
|
|
479
|
+
INSTRUCTION.set(token, arg1);
|
|
480
|
+
return token;
|
|
481
|
+
}
|
|
482
|
+
function childInstruction(type, arg2) {
|
|
483
|
+
return (key2, subject) => {
|
|
484
|
+
function set2(next) {
|
|
485
|
+
if (next ? !(next instanceof type) : arg2 !== false)
|
|
486
|
+
throw new Error(
|
|
487
|
+
`${subject}.${key2} expected State of type ${type} but got ${next && next.constructor}.`
|
|
488
|
+
);
|
|
489
|
+
update(subject, key2, next);
|
|
490
|
+
if (next && typeof arg2 == "function") arg2(next);
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
const value = new type();
|
|
494
|
+
set2(value);
|
|
495
|
+
PARENT.set(value, subject);
|
|
496
|
+
value.set();
|
|
497
|
+
return { set: set2 };
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
function init2() {
|
|
501
|
+
const state = STATE.get(this);
|
|
502
|
+
for (const key2 in this) {
|
|
503
|
+
const { value } = Object.getOwnPropertyDescriptor(this, key2);
|
|
504
|
+
const instruction = INSTRUCTION.get(value);
|
|
505
|
+
if (!instruction) continue;
|
|
506
|
+
INSTRUCTION.delete(value);
|
|
507
|
+
delete this[key2];
|
|
508
|
+
const output = instruction.call(this, key2, this, state);
|
|
509
|
+
if (!output) continue;
|
|
510
|
+
const desc = typeof output == "object" ? output : { get: output };
|
|
511
|
+
if ("value" in desc) state[key2] = desc.value;
|
|
512
|
+
const self = this;
|
|
513
|
+
Object.defineProperty(self, key2, {
|
|
514
|
+
enumerable: desc.enumerable !== false,
|
|
515
|
+
get() {
|
|
516
|
+
return follow(
|
|
517
|
+
this,
|
|
518
|
+
key2,
|
|
519
|
+
typeof desc.get == "function" ? desc.get(this) : access(self, key2, desc.get)
|
|
520
|
+
);
|
|
521
|
+
},
|
|
522
|
+
set(next) {
|
|
523
|
+
if (desc.set === false) {
|
|
524
|
+
throw new Error(`${self}.${key2} is read-only.`);
|
|
525
|
+
}
|
|
526
|
+
update(self, key2, next, desc.set);
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
State.on(init2);
|
|
533
|
+
|
|
486
534
|
// src/context.ts
|
|
487
535
|
var LOOKUP = /* @__PURE__ */ new WeakMap();
|
|
488
536
|
var KEYS = /* @__PURE__ */ new Map();
|
|
489
537
|
function key(T, upstream) {
|
|
490
538
|
let K = KEYS.get(T);
|
|
491
|
-
if (!K)
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
539
|
+
if (!K) {
|
|
540
|
+
K = Symbol(typeof T == "symbol" ? "get " + T.description : String(T));
|
|
541
|
+
KEYS.set(T, K);
|
|
542
|
+
}
|
|
495
543
|
return upstream ? key(K) : K;
|
|
496
544
|
}
|
|
497
545
|
function keys(from, upstream) {
|
|
498
546
|
const keys2 = /* @__PURE__ */ new Set();
|
|
499
547
|
do {
|
|
500
548
|
keys2.add(key(from, upstream));
|
|
501
|
-
|
|
549
|
+
from = Object.getPrototypeOf(from);
|
|
550
|
+
} while (from !== State);
|
|
502
551
|
return keys2;
|
|
503
552
|
}
|
|
504
553
|
var Context = class _Context {
|
|
505
554
|
constructor(inputs) {
|
|
506
|
-
this.
|
|
555
|
+
this.id = uid();
|
|
556
|
+
this.inputs = {};
|
|
507
557
|
this.cleanup = /* @__PURE__ */ new Set();
|
|
508
|
-
if (inputs)
|
|
509
|
-
this.include(inputs);
|
|
558
|
+
if (inputs) this.use(inputs);
|
|
510
559
|
}
|
|
511
560
|
static get({ is }, callback) {
|
|
512
|
-
const
|
|
513
|
-
if (
|
|
514
|
-
if (callback)
|
|
515
|
-
|
|
516
|
-
return waiting;
|
|
561
|
+
const context = LOOKUP.get(is);
|
|
562
|
+
if (context instanceof _Context) {
|
|
563
|
+
if (callback) callback(context);
|
|
564
|
+
return context;
|
|
517
565
|
}
|
|
518
566
|
if (callback)
|
|
519
|
-
if (
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
567
|
+
if (context) context.push(callback);
|
|
568
|
+
else LOOKUP.set(is, [callback]);
|
|
569
|
+
}
|
|
570
|
+
get(Type, arg2) {
|
|
571
|
+
if (typeof arg2 == "function") {
|
|
572
|
+
const k = key(Type, true);
|
|
573
|
+
Object.defineProperty(this, k, {
|
|
574
|
+
value: arg2,
|
|
575
|
+
configurable: true
|
|
576
|
+
});
|
|
577
|
+
return () => {
|
|
578
|
+
delete this[k];
|
|
579
|
+
};
|
|
580
|
+
}
|
|
529
581
|
const result = this[key(Type)];
|
|
530
582
|
if (result === null)
|
|
531
|
-
throw new Error(`Did find ${Type} in context, but multiple were defined.`);
|
|
532
|
-
if (result)
|
|
533
|
-
return result;
|
|
534
|
-
if (require2)
|
|
535
|
-
throw new Error(`Could not find ${Type} in context.`);
|
|
536
|
-
}
|
|
537
|
-
push(inputs) {
|
|
538
|
-
const next = Object.create(this);
|
|
539
|
-
this.cleanup = /* @__PURE__ */ new Set([() => next.pop(), ...this.cleanup]);
|
|
540
|
-
next.layer = /* @__PURE__ */ new Map();
|
|
541
|
-
next.cleanup = /* @__PURE__ */ new Set();
|
|
542
|
-
if (inputs)
|
|
543
|
-
next.include(inputs);
|
|
544
|
-
return next;
|
|
545
|
-
}
|
|
546
|
-
pop() {
|
|
547
|
-
for (const key2 of Object.getOwnPropertySymbols(this))
|
|
548
|
-
delete this[key2];
|
|
549
|
-
this.cleanup.forEach((cb) => cb());
|
|
550
|
-
this.cleanup.clear();
|
|
551
|
-
this.layer.clear();
|
|
552
|
-
}
|
|
553
|
-
include(inputs, forEach) {
|
|
554
|
-
const init3 = /* @__PURE__ */ new Map();
|
|
555
|
-
if (typeof inputs == "function" || inputs instanceof Model)
|
|
556
|
-
inputs = { [forEach ? 0 : this.layer.size]: inputs };
|
|
557
|
-
for (const [K, V] of Object.entries(inputs)) {
|
|
558
|
-
if (Model.is(V) || V instanceof Model) {
|
|
559
|
-
const exists = this.layer.get(K);
|
|
560
|
-
if (!exists) {
|
|
561
|
-
const instance = this.add(V);
|
|
562
|
-
this.layer.set(K, V);
|
|
563
|
-
init3.set(instance, true);
|
|
564
|
-
} else if (exists !== V) {
|
|
565
|
-
this.pop();
|
|
566
|
-
this.id = uid();
|
|
567
|
-
this.include(inputs);
|
|
568
|
-
}
|
|
569
|
-
continue;
|
|
570
|
-
}
|
|
571
583
|
throw new Error(
|
|
572
|
-
`
|
|
584
|
+
`Did find ${Type} in context, but multiple were defined.`
|
|
573
585
|
);
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
model.set();
|
|
577
|
-
if (explicit && forEach)
|
|
578
|
-
forEach(model);
|
|
579
|
-
for (const [_key, value] of model)
|
|
580
|
-
if (PARENT.get(value) === model) {
|
|
581
|
-
this.add(value, true);
|
|
582
|
-
init3.set(value, false);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
586
|
+
if (result) return result;
|
|
587
|
+
if (arg2) throw new Error(`Could not find ${Type} in context.`);
|
|
585
588
|
}
|
|
589
|
+
/**
|
|
590
|
+
* Adds a State to this context.
|
|
591
|
+
*/
|
|
586
592
|
add(input, implicit) {
|
|
587
593
|
const cleanup = /* @__PURE__ */ new Set();
|
|
588
594
|
let T;
|
|
@@ -596,16 +602,14 @@ var Context = class _Context {
|
|
|
596
602
|
}
|
|
597
603
|
keys(T, true).forEach((K) => {
|
|
598
604
|
const expects = this[K];
|
|
599
|
-
if (
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
}
|
|
607
|
-
return null;
|
|
608
|
-
});
|
|
605
|
+
if (expects)
|
|
606
|
+
addListener(I, (event2) => {
|
|
607
|
+
if (event2 === true) {
|
|
608
|
+
const cb = expects(I);
|
|
609
|
+
if (cb) cleanup.add(cb);
|
|
610
|
+
}
|
|
611
|
+
return null;
|
|
612
|
+
});
|
|
609
613
|
});
|
|
610
614
|
keys(T).forEach((K) => {
|
|
611
615
|
const value = this.hasOwnProperty(K) ? null : I;
|
|
@@ -617,272 +621,261 @@ var Context = class _Context {
|
|
|
617
621
|
});
|
|
618
622
|
this.cleanup.add(() => {
|
|
619
623
|
cleanup.forEach((cb) => cb());
|
|
620
|
-
if (I !== input)
|
|
621
|
-
event(I, null);
|
|
624
|
+
if (I !== input) event(I, null);
|
|
622
625
|
});
|
|
623
626
|
const waiting = LOOKUP.get(I);
|
|
624
|
-
if (waiting instanceof Array)
|
|
625
|
-
waiting.forEach((cb) => cb(this));
|
|
627
|
+
if (waiting instanceof Array) waiting.forEach((cb) => cb(this));
|
|
626
628
|
LOOKUP.set(I, this);
|
|
627
629
|
return I;
|
|
628
630
|
}
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
update(subject, key2, next);
|
|
646
|
-
if (next && typeof arg2 == "function")
|
|
647
|
-
arg2(next);
|
|
648
|
-
return false;
|
|
649
|
-
}
|
|
650
|
-
const value = new type();
|
|
651
|
-
set2(value);
|
|
652
|
-
PARENT.set(value, subject);
|
|
653
|
-
value.set();
|
|
654
|
-
return { set: set2 };
|
|
655
|
-
};
|
|
656
|
-
}
|
|
657
|
-
function init2() {
|
|
658
|
-
const state = STATE.get(this);
|
|
659
|
-
for (const key2 in this) {
|
|
660
|
-
const { value } = Object.getOwnPropertyDescriptor(this, key2);
|
|
661
|
-
const instruction = INSTRUCTION.get(value);
|
|
662
|
-
if (!instruction)
|
|
663
|
-
continue;
|
|
664
|
-
INSTRUCTION.delete(value);
|
|
665
|
-
delete this[key2];
|
|
666
|
-
const output = instruction.call(this, key2, this, state);
|
|
667
|
-
if (!output)
|
|
668
|
-
continue;
|
|
669
|
-
const desc = typeof output == "object" ? output : { get: output };
|
|
670
|
-
if ("value" in desc)
|
|
671
|
-
state[key2] = desc.value;
|
|
672
|
-
const model = this;
|
|
673
|
-
Object.defineProperty(model, key2, {
|
|
674
|
-
enumerable: desc.enumerable !== false,
|
|
675
|
-
get() {
|
|
676
|
-
return watch(
|
|
677
|
-
this,
|
|
678
|
-
key2,
|
|
679
|
-
typeof desc.get == "function" ? desc.get(this) : fetch(model, key2, desc.get)
|
|
631
|
+
/**
|
|
632
|
+
* Register one or more States to this context.
|
|
633
|
+
*
|
|
634
|
+
* Context will add or remove States as needed to keep with provided input.
|
|
635
|
+
*
|
|
636
|
+
* @param inputs State, State class, or map of States / State classes to register.
|
|
637
|
+
* @param forEach Optional callback to run for each State registered.
|
|
638
|
+
*/
|
|
639
|
+
use(inputs, forEach) {
|
|
640
|
+
const init3 = /* @__PURE__ */ new Map();
|
|
641
|
+
if (typeof inputs == "function" || inputs instanceof State)
|
|
642
|
+
inputs = { [0]: inputs };
|
|
643
|
+
for (const [K, V] of Object.entries(inputs)) {
|
|
644
|
+
if (!(State.is(V) || V instanceof State))
|
|
645
|
+
throw new Error(
|
|
646
|
+
`Context may only include instance or class \`extends State\` but got ${K == "0" || K == String(V) ? V : `${V} (as '${K}')`}.`
|
|
680
647
|
);
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
648
|
+
const exists = this.inputs[K];
|
|
649
|
+
if (!exists) {
|
|
650
|
+
init3.set(this.add(V), true);
|
|
651
|
+
} else if (exists !== V) {
|
|
652
|
+
this.pop();
|
|
653
|
+
this.use(inputs);
|
|
654
|
+
this.id = uid();
|
|
655
|
+
return;
|
|
686
656
|
}
|
|
687
|
-
}
|
|
657
|
+
}
|
|
658
|
+
for (const [state, explicit] of init3) {
|
|
659
|
+
state.set();
|
|
660
|
+
if (explicit && forEach) forEach(state);
|
|
661
|
+
for (const [_key, value] of state)
|
|
662
|
+
if (PARENT.get(value) === state) {
|
|
663
|
+
this.add(value, true);
|
|
664
|
+
init3.set(value, false);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
this.inputs = inputs;
|
|
688
668
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
669
|
+
/**
|
|
670
|
+
* Create a child context, optionally registering one or more States to it.
|
|
671
|
+
*
|
|
672
|
+
* @param inputs State, State class, or map of States / State classes to register.
|
|
673
|
+
*/
|
|
674
|
+
push(inputs) {
|
|
675
|
+
const next = Object.create(this);
|
|
676
|
+
this.cleanup = /* @__PURE__ */ new Set([() => next.pop(), ...this.cleanup]);
|
|
677
|
+
next.inputs = {};
|
|
678
|
+
next.cleanup = /* @__PURE__ */ new Set();
|
|
679
|
+
if (inputs) next.use(inputs);
|
|
680
|
+
return next;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Remove all States from this context.
|
|
684
|
+
*
|
|
685
|
+
* Will also run any cleanup callbacks registered when States were added.
|
|
686
|
+
*/
|
|
687
|
+
pop() {
|
|
688
|
+
for (const key2 of Object.getOwnPropertySymbols(this)) delete this[key2];
|
|
689
|
+
this.cleanup.forEach((cb) => cb());
|
|
690
|
+
this.cleanup.clear();
|
|
691
|
+
this.inputs = {};
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
Object.defineProperty(Context.prototype, "toString", {
|
|
695
|
+
value() {
|
|
696
|
+
return `Context-${this.id}`;
|
|
697
|
+
}
|
|
698
|
+
});
|
|
692
699
|
|
|
693
700
|
// src/instruction/get.ts
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
update(subject, key2, value);
|
|
701
|
+
function get(Type, arg1, arg2) {
|
|
702
|
+
const isDownstream = arg1 === true;
|
|
703
|
+
const isOptional = arg1 === false;
|
|
704
|
+
return use((key2, subject) => {
|
|
705
|
+
if (isDownstream) {
|
|
706
|
+
const callback = typeof arg2 === "function" ? arg2 : void 0;
|
|
707
|
+
const applied = /* @__PURE__ */ new Set();
|
|
708
|
+
const reset = () => {
|
|
709
|
+
update(subject, key2, Object.freeze(Array.from(applied)));
|
|
704
710
|
};
|
|
705
|
-
var assign2 = assign3;
|
|
706
|
-
const hasParent = PARENT.get(subject);
|
|
707
|
-
if (!hasParent && arg1 === true)
|
|
708
|
-
throw new Error(`${subject} may only exist as a child of type ${arg0}.`);
|
|
709
|
-
else if (hasParent) {
|
|
710
|
-
if (hasParent instanceof arg0) {
|
|
711
|
-
assign3(hasParent);
|
|
712
|
-
return {};
|
|
713
|
-
} else if (arg1 === true)
|
|
714
|
-
throw new Error(`New ${subject} created as child of ${hasParent}, but must be instanceof ${arg0}.`);
|
|
715
|
-
}
|
|
716
711
|
Context.get(subject, (context) => {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
712
|
+
context.get(Type, (state) => {
|
|
713
|
+
let remove;
|
|
714
|
+
let flush;
|
|
715
|
+
if (applied.has(state)) return;
|
|
716
|
+
if (callback) {
|
|
717
|
+
const exit = enter();
|
|
718
|
+
try {
|
|
719
|
+
const done2 = callback(state, subject);
|
|
720
|
+
if (done2 === false) return false;
|
|
721
|
+
if (typeof done2 == "function") remove = done2;
|
|
722
|
+
} finally {
|
|
723
|
+
flush = exit();
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
applied.add(state);
|
|
727
|
+
reset();
|
|
728
|
+
const done = () => {
|
|
729
|
+
if (flush) flush();
|
|
730
|
+
ignore();
|
|
731
|
+
applied.delete(state);
|
|
732
|
+
reset();
|
|
733
|
+
if (typeof remove == "function") remove();
|
|
734
|
+
remove = void 0;
|
|
735
|
+
};
|
|
736
|
+
const ignore = state.set(done, null);
|
|
737
|
+
return done;
|
|
738
|
+
});
|
|
722
739
|
});
|
|
723
740
|
return {
|
|
724
|
-
|
|
741
|
+
value: [],
|
|
725
742
|
enumerable: false
|
|
726
743
|
};
|
|
727
744
|
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
const getter = arg1;
|
|
737
|
-
let reset;
|
|
738
|
-
let isAsync;
|
|
739
|
-
let proxy;
|
|
740
|
-
function connect(model) {
|
|
741
|
-
reset = createEffect(model, (current) => {
|
|
742
|
-
proxy = current;
|
|
743
|
-
if (!(key2 in state) || STALE.delete(compute))
|
|
744
|
-
compute(!reset);
|
|
745
|
-
return (didUpdate) => {
|
|
746
|
-
if (didUpdate) {
|
|
747
|
-
STALE.add(compute);
|
|
748
|
-
event(subject, key2, true);
|
|
749
|
-
}
|
|
750
|
-
};
|
|
751
|
-
}, false);
|
|
752
|
-
}
|
|
753
|
-
function compute(initial) {
|
|
754
|
-
let next;
|
|
755
|
-
try {
|
|
756
|
-
next = getter.call(proxy, proxy, key2);
|
|
757
|
-
} catch (err) {
|
|
758
|
-
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${key2}.`);
|
|
759
|
-
if (initial)
|
|
760
|
-
throw err;
|
|
761
|
-
console.error(err);
|
|
745
|
+
const hasParent = PARENT.get(subject);
|
|
746
|
+
function assign2(value) {
|
|
747
|
+
if (typeof arg1 === "function") {
|
|
748
|
+
const callback = arg1;
|
|
749
|
+
const result = callback(value, subject);
|
|
750
|
+
if (typeof result === "function") {
|
|
751
|
+
subject.set(result, null);
|
|
752
|
+
}
|
|
762
753
|
}
|
|
763
|
-
update(subject, key2,
|
|
754
|
+
update(subject, key2, value);
|
|
764
755
|
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
756
|
+
if (hasParent && hasParent instanceof Type) {
|
|
757
|
+
assign2(hasParent);
|
|
758
|
+
return {};
|
|
759
|
+
}
|
|
760
|
+
Context.get(subject, (context) => {
|
|
761
|
+
const self = context.get(Type);
|
|
762
|
+
if (self) assign2(self);
|
|
763
|
+
else if (!isOptional)
|
|
764
|
+
throw new Error(
|
|
765
|
+
`Required ${Type} not found in context for ${subject}.`
|
|
766
|
+
);
|
|
767
|
+
});
|
|
768
|
+
return {
|
|
769
|
+
get: !isOptional,
|
|
770
|
+
enumerable: false
|
|
773
771
|
};
|
|
774
772
|
});
|
|
775
773
|
}
|
|
776
774
|
|
|
777
|
-
// src/instruction/
|
|
778
|
-
var
|
|
779
|
-
function
|
|
775
|
+
// src/instruction/set.ts
|
|
776
|
+
var STALE = /* @__PURE__ */ new WeakSet();
|
|
777
|
+
function set(value, argument) {
|
|
780
778
|
return use((key2, subject, state) => {
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
779
|
+
if (typeof value == "symbol")
|
|
780
|
+
throw new Error(
|
|
781
|
+
`Attempted to use an instruction result (probably use or get) as computed source for ${subject}.${key2}. This is not allowed.`
|
|
782
|
+
);
|
|
783
|
+
const property = {};
|
|
784
|
+
if (value instanceof State || value === true) {
|
|
785
|
+
let connect2 = function(self) {
|
|
786
|
+
reset = watch(
|
|
787
|
+
self,
|
|
788
|
+
(current) => {
|
|
789
|
+
proxy = current;
|
|
790
|
+
if (!(key2 in state) || STALE.delete(compute2)) compute2(!reset);
|
|
791
|
+
return (didUpdate) => {
|
|
792
|
+
if (didUpdate) {
|
|
793
|
+
STALE.add(compute2);
|
|
794
|
+
event(subject, key2, true);
|
|
793
795
|
}
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
796
|
+
};
|
|
797
|
+
},
|
|
798
|
+
false
|
|
799
|
+
);
|
|
800
|
+
}, compute2 = function(initial) {
|
|
801
|
+
let next;
|
|
802
|
+
try {
|
|
803
|
+
next = getter.call(proxy, proxy, key2);
|
|
804
|
+
} catch (err) {
|
|
805
|
+
console.warn(
|
|
806
|
+
`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${key2}.`
|
|
807
|
+
);
|
|
808
|
+
if (initial) throw err;
|
|
809
|
+
console.error(err);
|
|
810
|
+
}
|
|
811
|
+
update(subject, key2, next, !isAsync);
|
|
812
|
+
};
|
|
813
|
+
var connect = connect2, compute = compute2;
|
|
814
|
+
let from = subject;
|
|
815
|
+
let getter;
|
|
816
|
+
if (value instanceof State) {
|
|
817
|
+
from = value;
|
|
818
|
+
getter = argument;
|
|
819
|
+
} else {
|
|
820
|
+
const fn = METHOD.get(argument) || argument;
|
|
821
|
+
getter = (p, k) => fn.call(p, k, p);
|
|
822
|
+
}
|
|
823
|
+
const source = (resolve) => resolve(from);
|
|
824
|
+
let reset;
|
|
825
|
+
let isAsync;
|
|
826
|
+
let proxy;
|
|
827
|
+
return () => {
|
|
828
|
+
if (!proxy) {
|
|
829
|
+
source(connect2);
|
|
830
|
+
isAsync = true;
|
|
831
|
+
}
|
|
832
|
+
if (STALE.delete(compute2)) compute2();
|
|
833
|
+
return access(subject, key2, !proxy);
|
|
828
834
|
};
|
|
829
|
-
state[key2] = null;
|
|
830
|
-
value = defineProperties({ get: get2, key: key2, is: subject }, {
|
|
831
|
-
current: { get: get2, set: set2 }
|
|
832
|
-
});
|
|
833
835
|
}
|
|
834
|
-
|
|
835
|
-
});
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
// src/instruction/set.ts
|
|
839
|
-
function set(value, argument) {
|
|
840
|
-
return use((key2, subject) => {
|
|
841
|
-
const property = {};
|
|
842
|
-
if (typeof value == "function" || isThenable(value)) {
|
|
836
|
+
if (typeof value == "function" || value instanceof Promise) {
|
|
843
837
|
let init4 = function() {
|
|
844
838
|
if (typeof value == "function")
|
|
845
839
|
try {
|
|
846
840
|
value = attempt(value.bind(subject, key2));
|
|
847
841
|
} catch (err) {
|
|
848
|
-
console.warn(
|
|
842
|
+
console.warn(
|
|
843
|
+
`Generating initial value for ${subject}.${key2} failed.`
|
|
844
|
+
);
|
|
849
845
|
throw err;
|
|
850
846
|
}
|
|
851
847
|
property.get = argument !== false;
|
|
852
848
|
const set2 = (value2) => subject[key2] = value2;
|
|
853
|
-
if (
|
|
849
|
+
if (value instanceof Promise)
|
|
854
850
|
value.then(set2, (error) => {
|
|
855
851
|
event(subject, key2);
|
|
856
852
|
property.get = () => {
|
|
857
853
|
throw error;
|
|
858
854
|
};
|
|
859
855
|
});
|
|
860
|
-
else
|
|
861
|
-
|
|
862
|
-
if (argument)
|
|
863
|
-
return null;
|
|
856
|
+
else set2(value);
|
|
857
|
+
if (argument) return null;
|
|
864
858
|
return subject[key2];
|
|
865
859
|
};
|
|
866
860
|
var init3 = init4;
|
|
867
|
-
if (argument)
|
|
861
|
+
if (argument) {
|
|
868
862
|
addListener(subject, init4, true);
|
|
869
|
-
else
|
|
863
|
+
} else {
|
|
870
864
|
property.get = init4;
|
|
871
|
-
|
|
865
|
+
}
|
|
866
|
+
} else if (value !== void 0) {
|
|
872
867
|
property.value = value;
|
|
868
|
+
}
|
|
873
869
|
if (typeof argument == "function") {
|
|
874
870
|
let unset;
|
|
875
871
|
property.set = function(value2, previous) {
|
|
876
872
|
const exit = enter();
|
|
877
873
|
const returns = argument.call(this, value2, previous);
|
|
878
874
|
const flush = exit();
|
|
879
|
-
if (returns === false)
|
|
880
|
-
|
|
881
|
-
if (typeof unset == "function")
|
|
882
|
-
unset(value2);
|
|
875
|
+
if (returns === false) return false;
|
|
876
|
+
if (typeof unset == "function") unset(value2);
|
|
883
877
|
unset = (next) => {
|
|
884
|
-
if (typeof returns == "function")
|
|
885
|
-
returns(next);
|
|
878
|
+
if (typeof returns == "function") returns(next);
|
|
886
879
|
flush();
|
|
887
880
|
};
|
|
888
881
|
};
|
|
@@ -896,10 +889,8 @@ function set(value, argument) {
|
|
|
896
889
|
}
|
|
897
890
|
function attempt(fn) {
|
|
898
891
|
function retry(err) {
|
|
899
|
-
if (err instanceof Promise)
|
|
900
|
-
|
|
901
|
-
else
|
|
902
|
-
throw err;
|
|
892
|
+
if (err instanceof Promise) return err.then(compute);
|
|
893
|
+
else throw err;
|
|
903
894
|
}
|
|
904
895
|
function compute() {
|
|
905
896
|
try {
|
|
@@ -911,102 +902,82 @@ function attempt(fn) {
|
|
|
911
902
|
}
|
|
912
903
|
return compute();
|
|
913
904
|
}
|
|
914
|
-
function isThenable(value) {
|
|
915
|
-
return value && typeof value == "object" && typeof value.then == "function";
|
|
916
|
-
}
|
|
917
905
|
|
|
918
|
-
// src/instruction/
|
|
919
|
-
var
|
|
920
|
-
function
|
|
921
|
-
return use((key2, subject) => {
|
|
922
|
-
|
|
923
|
-
const
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
if (typeof done2 == "function")
|
|
949
|
-
remove = done2;
|
|
906
|
+
// src/instruction/ref.ts
|
|
907
|
+
var { defineProperty, defineProperties } = Object;
|
|
908
|
+
function ref(arg, arg2) {
|
|
909
|
+
return use((key2, subject, state) => {
|
|
910
|
+
let value = {};
|
|
911
|
+
const method = (key3, from) => (callback) => callback ? addListener(subject, () => callback(from[key3]), key3) : from[key3];
|
|
912
|
+
if (arg === subject)
|
|
913
|
+
addListener(
|
|
914
|
+
subject,
|
|
915
|
+
() => {
|
|
916
|
+
for (const key3 in subject)
|
|
917
|
+
if (typeof arg2 == "function")
|
|
918
|
+
defineProperty(value, key3, {
|
|
919
|
+
configurable: true,
|
|
920
|
+
get() {
|
|
921
|
+
const out = arg2(key3);
|
|
922
|
+
defineProperty(value, key3, { value: out });
|
|
923
|
+
return out;
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
else {
|
|
927
|
+
const get2 = method(key3, subject);
|
|
928
|
+
const set2 = (x) => subject[key3] = x;
|
|
929
|
+
defineProperties(set2, {
|
|
930
|
+
current: { get: get2, set: set2 },
|
|
931
|
+
get: { value: get2 },
|
|
932
|
+
is: { value: subject },
|
|
933
|
+
key: { value: key3 }
|
|
934
|
+
});
|
|
935
|
+
defineProperty(value, key3, { value: set2 });
|
|
950
936
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
applied.delete(model);
|
|
960
|
-
reset();
|
|
961
|
-
if (disconnect)
|
|
962
|
-
disconnect();
|
|
963
|
-
if (typeof remove == "function")
|
|
964
|
-
remove();
|
|
965
|
-
remove = void 0;
|
|
966
|
-
};
|
|
967
|
-
const ignore = model.set(done, null);
|
|
968
|
-
return done;
|
|
969
|
-
});
|
|
970
|
-
});
|
|
937
|
+
return null;
|
|
938
|
+
},
|
|
939
|
+
true
|
|
940
|
+
);
|
|
941
|
+
else if (typeof arg == "object")
|
|
942
|
+
throw new Error(
|
|
943
|
+
"ref instruction does not support object which is not 'this'"
|
|
944
|
+
);
|
|
971
945
|
else {
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
if (
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
applied.delete(recipient);
|
|
985
|
-
reset();
|
|
986
|
-
if (typeof remove == "function")
|
|
987
|
-
remove();
|
|
946
|
+
let unset;
|
|
947
|
+
const get2 = method(key2, state);
|
|
948
|
+
const set2 = (value2) => {
|
|
949
|
+
if (!update(subject, key2, value2) || !arg) return;
|
|
950
|
+
if (unset) unset = void unset(value2);
|
|
951
|
+
if (value2 === null && arg2 !== false) return;
|
|
952
|
+
const exit = enter();
|
|
953
|
+
const out = arg.call(subject, value2);
|
|
954
|
+
const flush = exit();
|
|
955
|
+
unset = (key3) => {
|
|
956
|
+
if (typeof out == "function") out(key3);
|
|
957
|
+
flush();
|
|
988
958
|
};
|
|
959
|
+
};
|
|
960
|
+
state[key2] = null;
|
|
961
|
+
value = defineProperty({ get: get2, key: key2, is: subject }, "current", {
|
|
962
|
+
get: get2,
|
|
963
|
+
set: set2
|
|
989
964
|
});
|
|
990
965
|
}
|
|
991
|
-
|
|
992
|
-
value: [],
|
|
993
|
-
enumerable: false
|
|
994
|
-
};
|
|
966
|
+
defineProperty(subject, key2, { value });
|
|
995
967
|
});
|
|
996
968
|
}
|
|
997
969
|
// Annotate the CommonJS export names for ESM import in node:
|
|
998
970
|
0 && (module.exports = {
|
|
999
971
|
Context,
|
|
1000
972
|
METHOD,
|
|
1001
|
-
Model,
|
|
1002
973
|
Observable,
|
|
974
|
+
State,
|
|
1003
975
|
addListener,
|
|
1004
|
-
createEffect,
|
|
1005
976
|
event,
|
|
1006
977
|
get,
|
|
1007
|
-
has,
|
|
1008
978
|
ref,
|
|
1009
979
|
set,
|
|
1010
|
-
use
|
|
980
|
+
use,
|
|
981
|
+
watch
|
|
1011
982
|
});
|
|
1012
983
|
//# sourceMappingURL=index.js.map
|