@expressive/mvc 0.3.0 → 0.4.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/lib/index.d.ts +18 -35
- package/lib/index.esm.js +642 -491
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +642 -491
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/lib/index.esm.js
CHANGED
|
@@ -51,7 +51,7 @@ function suspend(source, key) {
|
|
|
51
51
|
const release = source.addListener(forKey => {
|
|
52
52
|
if (forKey == key)
|
|
53
53
|
return () => {
|
|
54
|
-
if (source.state
|
|
54
|
+
if (source.state.get(key) !== undefined) {
|
|
55
55
|
release();
|
|
56
56
|
resolve();
|
|
57
57
|
}
|
|
@@ -132,240 +132,37 @@ function createValueEffect(callback) {
|
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
UPDATE.delete(subject);
|
|
141
|
-
}, 0);
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
function getUpdate(subject) {
|
|
145
|
-
return UPDATE.get(subject);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const CONTROL = Symbol("CONTROL");
|
|
149
|
-
const WHY = Symbol("UPDATE");
|
|
150
|
-
const LOCAL = Symbol("LOCAL");
|
|
151
|
-
const STATE = Symbol("STATE");
|
|
152
|
-
class Model {
|
|
153
|
-
constructor() {
|
|
154
|
-
define(this, CONTROL, new Controller(this));
|
|
155
|
-
define(this, "get", this);
|
|
156
|
-
define(this, "is", this);
|
|
157
|
-
defineLazy(this, "set", () => {
|
|
158
|
-
const controller = control(this);
|
|
159
|
-
const assign = (key, value) => {
|
|
160
|
-
controller.update(key, value);
|
|
161
|
-
};
|
|
162
|
-
for (const key of getOwnPropertyNames(controller.proxy))
|
|
163
|
-
define(assign, key, controller.ref(key));
|
|
164
|
-
return assign;
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
get [STATE]() {
|
|
168
|
-
return this[CONTROL].state;
|
|
169
|
-
}
|
|
170
|
-
get [WHY]() {
|
|
171
|
-
return UPDATE.get(this);
|
|
172
|
-
}
|
|
173
|
-
on(select, handler, squash, once) {
|
|
174
|
-
return control(this, control => {
|
|
175
|
-
if (typeof select == "function")
|
|
176
|
-
return control.addListener(select);
|
|
177
|
-
const selection = typeof select == "string" ? [select] :
|
|
178
|
-
!select.length ? getOwnPropertyNames(control.state) :
|
|
179
|
-
select;
|
|
180
|
-
ensure(control, selection);
|
|
181
|
-
const callback = squash
|
|
182
|
-
? () => {
|
|
183
|
-
handler.call(this, getUpdate(this));
|
|
184
|
-
}
|
|
185
|
-
: () => {
|
|
186
|
-
getUpdate(this)
|
|
187
|
-
.filter(k => selection.includes(k))
|
|
188
|
-
.forEach(k => {
|
|
189
|
-
handler.call(this, control.state[k], k);
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
const onEvent = once
|
|
193
|
-
? () => {
|
|
194
|
-
remove();
|
|
195
|
-
callback();
|
|
196
|
-
}
|
|
197
|
-
: callback;
|
|
198
|
-
const remove = control.addListener(key => {
|
|
199
|
-
if (selection.includes(key))
|
|
200
|
-
return onEvent;
|
|
201
|
-
});
|
|
202
|
-
return remove;
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
once(select, callback, squash) {
|
|
206
|
-
if (callback)
|
|
207
|
-
return this.on(select, callback, squash, true);
|
|
208
|
-
return new Promise(resolve => {
|
|
209
|
-
this.on(select, resolve, true, true);
|
|
210
|
-
});
|
|
135
|
+
function ensure(subject, cb) {
|
|
136
|
+
let control = subject[CONTROL];
|
|
137
|
+
if (!control) {
|
|
138
|
+
control = new Controller(subject);
|
|
139
|
+
define(subject, CONTROL, control);
|
|
211
140
|
}
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
let
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (busy)
|
|
219
|
-
return;
|
|
220
|
-
const output = mayRetry(() => {
|
|
221
|
-
effect.call(inject, inject);
|
|
222
|
-
});
|
|
223
|
-
if (output instanceof Promise) {
|
|
224
|
-
output.finally(() => busy = false);
|
|
225
|
-
busy = true;
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
if (!select) {
|
|
229
|
-
const sub = new Subscriber(control, () => invoke);
|
|
230
|
-
inject = sub.proxy;
|
|
231
|
-
invoke();
|
|
232
|
-
return sub.commit();
|
|
233
|
-
}
|
|
234
|
-
invoke();
|
|
235
|
-
if (!select.length) {
|
|
236
|
-
control.onDestroy.add(invoke);
|
|
237
|
-
return () => {
|
|
238
|
-
control.onDestroy.delete(invoke);
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
return control.addListener(key => {
|
|
242
|
-
if (select.includes(key))
|
|
243
|
-
return invoke;
|
|
141
|
+
if (!control.state) {
|
|
142
|
+
const { waiting } = control;
|
|
143
|
+
if (cb) {
|
|
144
|
+
let done;
|
|
145
|
+
waiting.add(() => {
|
|
146
|
+
done = cb(control);
|
|
244
147
|
});
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
// TODO: account for exotic properties
|
|
248
|
-
import(source, select) {
|
|
249
|
-
const { subject } = control(this);
|
|
250
|
-
if (!select)
|
|
251
|
-
select = getOwnPropertyNames(subject);
|
|
252
|
-
for (const key of select)
|
|
253
|
-
if (key in source)
|
|
254
|
-
this[key] = source[key];
|
|
255
|
-
}
|
|
256
|
-
export(subset) {
|
|
257
|
-
const { state } = control(this);
|
|
258
|
-
const output = {};
|
|
259
|
-
if (!subset)
|
|
260
|
-
subset = getOwnPropertyNames(state);
|
|
261
|
-
for (const key of subset)
|
|
262
|
-
output[key] = state[key];
|
|
263
|
-
return output;
|
|
264
|
-
}
|
|
265
|
-
update(arg, tag) {
|
|
266
|
-
const target = control(this);
|
|
267
|
-
if (typeof arg == "string") {
|
|
268
|
-
target.update(arg);
|
|
269
|
-
if (1 in arguments && arg in this) {
|
|
270
|
-
const method = this[arg];
|
|
271
|
-
if (typeof method == "function")
|
|
272
|
-
if (typeof tag != "boolean")
|
|
273
|
-
method.call(this, tag);
|
|
274
|
-
else if (tag)
|
|
275
|
-
method.call(this);
|
|
276
|
-
}
|
|
277
|
-
arg = undefined;
|
|
148
|
+
return () => done && done();
|
|
278
149
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
toString() {
|
|
288
|
-
return this.constructor.name;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Creates a new instance of this controller.
|
|
292
|
-
*
|
|
293
|
-
* Beyond `new this(...)`, method will activate managed-state.
|
|
294
|
-
*
|
|
295
|
-
* @param args - arguments sent to constructor
|
|
296
|
-
*/
|
|
297
|
-
static create(...args) {
|
|
298
|
-
const instance = new this(...args);
|
|
299
|
-
control(instance);
|
|
300
|
-
return instance;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Static equivalent of `x instanceof this`.
|
|
304
|
-
*
|
|
305
|
-
* Will determine if provided class is a subtype of this one.
|
|
306
|
-
*/
|
|
307
|
-
static isTypeof(maybe) {
|
|
308
|
-
return (typeof maybe == "function" &&
|
|
309
|
-
maybe.prototype instanceof this);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
class Subscriber {
|
|
314
|
-
constructor(target, onUpdate) {
|
|
315
|
-
this.onUpdate = onUpdate;
|
|
316
|
-
this.active = false;
|
|
317
|
-
this.dependant = new Set();
|
|
318
|
-
this.watch = {};
|
|
319
|
-
const parent = target instanceof Controller
|
|
320
|
-
? target : control(target);
|
|
321
|
-
const subject = parent.subject;
|
|
322
|
-
const proxy = create(parent.proxy);
|
|
323
|
-
let reset;
|
|
324
|
-
define(proxy, LOCAL, this);
|
|
325
|
-
defineProperty(this, "proxy", {
|
|
326
|
-
configurable: true,
|
|
327
|
-
get() {
|
|
328
|
-
if (reset)
|
|
329
|
-
setTimeout(reset, 0);
|
|
330
|
-
return proxy;
|
|
331
|
-
}
|
|
332
|
-
});
|
|
333
|
-
const release = parent.addListener(key => {
|
|
334
|
-
const handler = this.watch[key];
|
|
335
|
-
if (!handler || !this.active)
|
|
336
|
-
return;
|
|
337
|
-
if (typeof handler == "function")
|
|
338
|
-
handler();
|
|
339
|
-
const notify = this.onUpdate(key, parent);
|
|
340
|
-
const getWhy = () => {
|
|
341
|
-
const update = getUpdate(subject);
|
|
342
|
-
const applicable = update.filter(k => k in this.watch);
|
|
343
|
-
reset = applyUpdate(proxy, applicable);
|
|
344
|
-
};
|
|
345
|
-
if (notify) {
|
|
346
|
-
parent.requestUpdate(getWhy);
|
|
347
|
-
parent.requestUpdate(notify);
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
this.commit = () => {
|
|
351
|
-
this.active = true;
|
|
352
|
-
this.dependant.forEach(x => x.commit());
|
|
353
|
-
return this.release;
|
|
354
|
-
};
|
|
355
|
-
this.release = () => {
|
|
356
|
-
this.dependant.forEach(x => x.release());
|
|
357
|
-
release();
|
|
358
|
-
};
|
|
150
|
+
control.state = new Map();
|
|
151
|
+
for (const key in subject)
|
|
152
|
+
control.add(key);
|
|
153
|
+
const callback = Array.from(waiting);
|
|
154
|
+
waiting.clear();
|
|
155
|
+
for (const cb of callback)
|
|
156
|
+
cb();
|
|
359
157
|
}
|
|
158
|
+
return cb ? cb(control) : control;
|
|
360
159
|
}
|
|
361
160
|
|
|
362
161
|
const Oops$a = issues({
|
|
363
162
|
BadSource: (model, property, got) => `Bad from-instruction provided to ${model}.${property}. Expects an arrow-function or a Model as source. Got ${got}.`,
|
|
364
163
|
PeerNotAllowed: (model, property) => `Attempted to use an instruction result (probably use or tap) as computed source for ${model}.${property}. This is not possible.`,
|
|
365
|
-
Failed: (parent, property, initial) => `An exception was thrown while ${initial ? "initializing" : "refreshing"} [${parent}.${property}]
|
|
366
|
-
Early: (property) => `Note: Computed values don't run until accessed, except when subscribed to. '${property}' getter may have run earlier than intended.`
|
|
164
|
+
Failed: (parent, property, initial) => `An exception was thrown while ${initial ? "initializing" : "refreshing"} [${parent}.${property}].`
|
|
367
165
|
});
|
|
368
|
-
const INIT = new WeakSet();
|
|
369
166
|
const INFO = new WeakMap();
|
|
370
167
|
const USED = new WeakMap();
|
|
371
168
|
const KEYS = new WeakMap();
|
|
@@ -373,8 +170,8 @@ function from(source, arg1, arg2) {
|
|
|
373
170
|
return apply(function from(key) {
|
|
374
171
|
const parent = this;
|
|
375
172
|
const { subject, state } = this;
|
|
173
|
+
const required = arg2 === true || arg1 === true;
|
|
376
174
|
let getSource;
|
|
377
|
-
let required = arg2 === true || arg1 === true;
|
|
378
175
|
if (typeof arg1 == "boolean")
|
|
379
176
|
arg1 = undefined;
|
|
380
177
|
// Easy mistake, using a peer, will always be unresolved.
|
|
@@ -382,7 +179,7 @@ function from(source, arg1, arg2) {
|
|
|
382
179
|
throw Oops$a.PeerNotAllowed(subject, key);
|
|
383
180
|
// replace source controller in-case it is different
|
|
384
181
|
if (typeof source == "object")
|
|
385
|
-
getSource = () =>
|
|
182
|
+
getSource = () => ensure(source);
|
|
386
183
|
// specifically an arrow function (getter factory)
|
|
387
184
|
else if (!source.prototype) {
|
|
388
185
|
arg1 = source.call(subject, key);
|
|
@@ -391,21 +188,20 @@ function from(source, arg1, arg2) {
|
|
|
391
188
|
// Regular function is too ambiguous so not allowed.
|
|
392
189
|
else
|
|
393
190
|
throw Oops$a.BadSource(subject, key, source);
|
|
394
|
-
let sub;
|
|
395
191
|
const setter = arg1;
|
|
396
192
|
const info = { key, parent, priority: 1 };
|
|
193
|
+
let sub;
|
|
397
194
|
let register = USED.get(parent);
|
|
398
|
-
if (!register)
|
|
399
|
-
register = new Map();
|
|
400
|
-
|
|
401
|
-
}
|
|
195
|
+
if (!register)
|
|
196
|
+
USED.set(parent, register = new Map());
|
|
197
|
+
state.set(key, undefined);
|
|
402
198
|
register.set(key, info);
|
|
403
199
|
function compute(initial) {
|
|
404
200
|
try {
|
|
405
201
|
return setter.call(sub.proxy, sub.proxy);
|
|
406
202
|
}
|
|
407
203
|
catch (err) {
|
|
408
|
-
Oops$a.Failed(subject, key,
|
|
204
|
+
Oops$a.Failed(subject, key, initial).warn();
|
|
409
205
|
throw err;
|
|
410
206
|
}
|
|
411
207
|
}
|
|
@@ -418,16 +214,17 @@ function from(source, arg1, arg2) {
|
|
|
418
214
|
console.error(e);
|
|
419
215
|
}
|
|
420
216
|
finally {
|
|
421
|
-
if (state
|
|
422
|
-
parent.
|
|
217
|
+
if (state.get(key) !== value) {
|
|
218
|
+
parent.state.set(key, value);
|
|
219
|
+
parent.update(key);
|
|
423
220
|
return value;
|
|
424
221
|
}
|
|
425
222
|
}
|
|
426
223
|
}
|
|
427
224
|
function defer(_key, from) {
|
|
428
|
-
let pending = KEYS.get(from
|
|
225
|
+
let pending = KEYS.get(from);
|
|
429
226
|
if (!pending)
|
|
430
|
-
KEYS.set(from
|
|
227
|
+
KEYS.set(from, pending = []);
|
|
431
228
|
if (info.parent !== from)
|
|
432
229
|
update();
|
|
433
230
|
else {
|
|
@@ -435,179 +232,356 @@ function from(source, arg1, arg2) {
|
|
|
435
232
|
pending.splice(after + 1, 0, update);
|
|
436
233
|
}
|
|
437
234
|
}
|
|
438
|
-
function create(
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
value: undefined,
|
|
442
|
-
writable: true
|
|
443
|
-
});
|
|
235
|
+
function create() {
|
|
236
|
+
const control = getSource();
|
|
237
|
+
sub = control.subscribe(defer);
|
|
444
238
|
try {
|
|
445
|
-
|
|
239
|
+
const value = compute(true);
|
|
240
|
+
state.set(key, value);
|
|
241
|
+
return value;
|
|
446
242
|
}
|
|
447
243
|
catch (e) {
|
|
448
|
-
if (early)
|
|
449
|
-
Oops$a.Early(key).warn();
|
|
450
244
|
throw e;
|
|
451
245
|
}
|
|
452
246
|
finally {
|
|
453
247
|
sub.commit();
|
|
454
|
-
for (const key
|
|
248
|
+
for (const key of control.state.keys()) {
|
|
455
249
|
const peer = register.get(key);
|
|
456
250
|
if (peer && peer.priority >= info.priority)
|
|
457
251
|
info.priority = peer.priority + 1;
|
|
458
252
|
}
|
|
459
253
|
}
|
|
460
254
|
}
|
|
461
|
-
INIT.add(create);
|
|
462
255
|
INFO.set(update, info);
|
|
463
|
-
defineProperty(state, key, {
|
|
464
|
-
get: create,
|
|
465
|
-
configurable: true,
|
|
466
|
-
enumerable: true
|
|
467
|
-
});
|
|
468
256
|
return () => {
|
|
469
|
-
const value =
|
|
257
|
+
const value = sub ? state.get(key) : create();
|
|
470
258
|
if (value === undefined && required)
|
|
471
259
|
throw suspend(this, key);
|
|
472
260
|
return value;
|
|
473
261
|
};
|
|
474
262
|
});
|
|
475
263
|
}
|
|
476
|
-
function
|
|
477
|
-
|
|
478
|
-
const desc = getOwnPropertyDescriptor(on.state, key);
|
|
479
|
-
const getter = desc && desc.get;
|
|
480
|
-
if (INIT.has(getter))
|
|
481
|
-
getter(true);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
function flush(frame) {
|
|
485
|
-
const pending = KEYS.get(frame);
|
|
264
|
+
function flush(control) {
|
|
265
|
+
const pending = KEYS.get(control);
|
|
486
266
|
if (!pending)
|
|
487
267
|
return;
|
|
488
268
|
while (pending.length) {
|
|
489
269
|
const compute = pending.shift();
|
|
490
270
|
const { key } = INFO.get(compute);
|
|
491
|
-
if (!frame.has(key))
|
|
271
|
+
if (!control.frame.has(key))
|
|
492
272
|
compute();
|
|
493
273
|
}
|
|
494
|
-
KEYS.delete(
|
|
274
|
+
KEYS.delete(control);
|
|
495
275
|
}
|
|
496
276
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
const
|
|
277
|
+
class Subscriber {
|
|
278
|
+
constructor(parent, onUpdate) {
|
|
279
|
+
this.onUpdate = onUpdate;
|
|
280
|
+
this.active = false;
|
|
281
|
+
this.dependant = new Set();
|
|
282
|
+
const proxy = create(parent.subject);
|
|
283
|
+
const using = new Map();
|
|
284
|
+
define(proxy, LOCAL, this);
|
|
285
|
+
defineProperty(this, "proxy", {
|
|
286
|
+
configurable: true,
|
|
287
|
+
get() {
|
|
288
|
+
if (UPDATE.has(proxy))
|
|
289
|
+
setTimeout(() => UPDATE.delete(proxy), 0);
|
|
290
|
+
return proxy;
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
const release = parent.addListener(key => {
|
|
294
|
+
if (!this.active || !using.has(key))
|
|
295
|
+
return;
|
|
296
|
+
const handler = using.get(key);
|
|
297
|
+
if (typeof handler == "function")
|
|
298
|
+
if (handler() !== true)
|
|
299
|
+
return;
|
|
300
|
+
const notify = this.onUpdate(key, parent);
|
|
301
|
+
const getWhy = () => {
|
|
302
|
+
const update = getUpdate(parent.subject);
|
|
303
|
+
const applicable = update.filter(k => using.has(k));
|
|
304
|
+
UPDATE.set(proxy, applicable);
|
|
305
|
+
};
|
|
306
|
+
if (notify) {
|
|
307
|
+
parent.waiting.add(getWhy);
|
|
308
|
+
parent.waiting.add(notify);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
this.commit = () => {
|
|
312
|
+
this.active = true;
|
|
313
|
+
this.dependant.forEach(x => x.commit());
|
|
314
|
+
return this.release;
|
|
315
|
+
};
|
|
316
|
+
this.release = () => {
|
|
317
|
+
this.dependant.forEach(x => x.release());
|
|
318
|
+
release();
|
|
319
|
+
};
|
|
320
|
+
this.add = (key, value) => {
|
|
321
|
+
if (value)
|
|
322
|
+
using.set(key, value);
|
|
323
|
+
else if (!using.has(key))
|
|
324
|
+
using.set(key, true);
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const UPDATE = new WeakMap();
|
|
330
|
+
function getUpdate(subject) {
|
|
331
|
+
return UPDATE.get(subject);
|
|
332
|
+
}
|
|
503
333
|
class Controller {
|
|
504
334
|
constructor(subject) {
|
|
505
335
|
this.subject = subject;
|
|
506
|
-
this.state = {};
|
|
507
336
|
this.frame = new Set();
|
|
508
|
-
this.onDestroy = new Set();
|
|
509
337
|
this.waiting = new Set();
|
|
510
338
|
this.followers = new Set();
|
|
511
|
-
this.proxy = Object.create(subject);
|
|
512
|
-
}
|
|
513
|
-
start() {
|
|
514
|
-
READY.add(this);
|
|
515
|
-
for (const key in this.subject) {
|
|
516
|
-
const { value } = getOwnPropertyDescriptor(this.subject, key);
|
|
517
|
-
if (typeof value == "function")
|
|
518
|
-
continue;
|
|
519
|
-
if (typeof value == "symbol") {
|
|
520
|
-
const instruction = PENDING.get(value);
|
|
521
|
-
if (instruction)
|
|
522
|
-
instruction.call(this, key, this);
|
|
523
|
-
continue;
|
|
524
|
-
}
|
|
525
|
-
this.manage(key, value);
|
|
526
|
-
}
|
|
527
|
-
this.flush();
|
|
528
339
|
}
|
|
529
|
-
|
|
530
|
-
this
|
|
531
|
-
this.onDestroy.forEach(x => x());
|
|
340
|
+
subscribe(callback) {
|
|
341
|
+
return new Subscriber(this, callback);
|
|
532
342
|
}
|
|
533
|
-
|
|
534
|
-
const { state } = this;
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
get
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
343
|
+
add(key) {
|
|
344
|
+
const { subject, state } = this;
|
|
345
|
+
const { value } = getOwnPropertyDescriptor(subject, key);
|
|
346
|
+
if (typeof value == "function")
|
|
347
|
+
return;
|
|
348
|
+
if (typeof value == "symbol") {
|
|
349
|
+
const instruction = PENDING.get(value);
|
|
350
|
+
if (instruction)
|
|
351
|
+
instruction.call(this, key, this);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
state.set(key, value);
|
|
355
|
+
defineProperty(subject, key, {
|
|
543
356
|
enumerable: false,
|
|
357
|
+
set: this.ref(key),
|
|
544
358
|
get() {
|
|
545
359
|
const local = this[LOCAL];
|
|
546
|
-
if (local
|
|
547
|
-
local.
|
|
548
|
-
return state
|
|
549
|
-
}
|
|
550
|
-
set
|
|
360
|
+
if (local)
|
|
361
|
+
local.add(key);
|
|
362
|
+
return state.get(key);
|
|
363
|
+
}
|
|
551
364
|
});
|
|
552
365
|
}
|
|
366
|
+
addListener(listener) {
|
|
367
|
+
this.followers.add(listener);
|
|
368
|
+
return () => {
|
|
369
|
+
this.followers.delete(listener);
|
|
370
|
+
};
|
|
371
|
+
}
|
|
553
372
|
ref(key, handler) {
|
|
554
|
-
const {
|
|
373
|
+
const { subject, state } = this;
|
|
555
374
|
return (value) => {
|
|
556
|
-
if (state
|
|
375
|
+
if (state.get(key) == value)
|
|
557
376
|
return;
|
|
558
377
|
if (handler)
|
|
559
|
-
switch (handler.call(subject, value
|
|
378
|
+
switch (handler.call(subject, value)) {
|
|
560
379
|
case true:
|
|
561
380
|
this.update(key);
|
|
562
381
|
case false:
|
|
563
382
|
return;
|
|
564
383
|
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
}
|
|
568
|
-
addListener(listener) {
|
|
569
|
-
this.followers.add(listener);
|
|
570
|
-
return () => {
|
|
571
|
-
this.followers.delete(listener);
|
|
384
|
+
state.set(key, value);
|
|
385
|
+
this.update(key);
|
|
572
386
|
};
|
|
573
387
|
}
|
|
574
|
-
update(key
|
|
575
|
-
const { frame } = this;
|
|
576
|
-
if (
|
|
577
|
-
|
|
578
|
-
if (!frame.size)
|
|
388
|
+
update(key) {
|
|
389
|
+
const { followers, frame, subject, waiting } = this;
|
|
390
|
+
if (frame.has(key))
|
|
391
|
+
return;
|
|
392
|
+
else if (!frame.size)
|
|
579
393
|
setTimeout(() => {
|
|
580
|
-
flush(
|
|
581
|
-
|
|
394
|
+
flush(this);
|
|
395
|
+
const callback = Array.from(waiting);
|
|
396
|
+
const keys = Array.from(frame);
|
|
397
|
+
frame.clear();
|
|
398
|
+
waiting.clear();
|
|
399
|
+
UPDATE.set(subject, keys);
|
|
400
|
+
setTimeout(() => {
|
|
401
|
+
UPDATE.delete(subject);
|
|
402
|
+
}, 0);
|
|
403
|
+
for (const cb of callback)
|
|
404
|
+
try {
|
|
405
|
+
cb();
|
|
406
|
+
}
|
|
407
|
+
catch (err) {
|
|
408
|
+
console.error(err);
|
|
409
|
+
}
|
|
582
410
|
}, 0);
|
|
583
|
-
else if (frame.has(key))
|
|
584
|
-
return;
|
|
585
411
|
frame.add(key);
|
|
586
|
-
for (const callback of
|
|
412
|
+
for (const callback of followers) {
|
|
587
413
|
const event = callback(key, this);
|
|
588
414
|
if (typeof event == "function")
|
|
589
|
-
|
|
415
|
+
waiting.add(event);
|
|
590
416
|
}
|
|
591
417
|
}
|
|
592
|
-
|
|
593
|
-
const
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
418
|
+
clear() {
|
|
419
|
+
const listeners = [...this.followers];
|
|
420
|
+
this.followers.clear();
|
|
421
|
+
listeners.forEach(x => x(null, this));
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const Oops$9 = issues({
|
|
426
|
+
Timeout: (keys, timeout) => `No update for [${keys}] within ${timeout}.`,
|
|
427
|
+
StrictUpdate: () => `Strict update() did not find pending updates.`,
|
|
428
|
+
NoChaining: () => `Then called with undefined; update promise will never catch nor supports chaining.`
|
|
429
|
+
});
|
|
430
|
+
const CONTROL = Symbol("CONTROL");
|
|
431
|
+
const WHY = Symbol("UPDATE");
|
|
432
|
+
const LOCAL = Symbol("LOCAL");
|
|
433
|
+
const STATE = Symbol("STATE");
|
|
434
|
+
class Model {
|
|
435
|
+
constructor() {
|
|
436
|
+
define(this, CONTROL, new Controller(this));
|
|
437
|
+
define(this, "get", this);
|
|
438
|
+
define(this, "is", this);
|
|
439
|
+
defineLazy(this, "set", () => {
|
|
440
|
+
const controller = ensure(this);
|
|
441
|
+
const assign = (key, value) => {
|
|
442
|
+
controller.state.set(key, value);
|
|
443
|
+
controller.update(key);
|
|
444
|
+
};
|
|
445
|
+
for (const key of controller.state.keys())
|
|
446
|
+
define(assign, key, controller.ref(key));
|
|
447
|
+
return assign;
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
get [STATE]() {
|
|
451
|
+
return this.export();
|
|
452
|
+
}
|
|
453
|
+
get [WHY]() {
|
|
454
|
+
return UPDATE.get(this);
|
|
455
|
+
}
|
|
456
|
+
on(select, handler, squash, once) {
|
|
457
|
+
return ensure(this, control => {
|
|
458
|
+
if (typeof select == "function")
|
|
459
|
+
return control.addListener(select);
|
|
460
|
+
const keys = typeof select == "string" ? [select] :
|
|
461
|
+
select.length ? select : [...control.state.keys()];
|
|
462
|
+
for (const key of keys)
|
|
463
|
+
try {
|
|
464
|
+
void this[key];
|
|
465
|
+
}
|
|
466
|
+
catch (e) { }
|
|
467
|
+
const callback = squash
|
|
468
|
+
? () => {
|
|
469
|
+
handler.call(this, getUpdate(this));
|
|
470
|
+
}
|
|
471
|
+
: () => {
|
|
472
|
+
getUpdate(this)
|
|
473
|
+
.filter(k => keys.includes(k))
|
|
474
|
+
.forEach(k => {
|
|
475
|
+
handler.call(this, control.state.get(k), k);
|
|
476
|
+
});
|
|
477
|
+
};
|
|
478
|
+
const onEvent = once
|
|
479
|
+
? () => {
|
|
480
|
+
remove();
|
|
481
|
+
callback();
|
|
482
|
+
}
|
|
483
|
+
: callback;
|
|
484
|
+
const remove = control.addListener(key => {
|
|
485
|
+
if (keys.includes(key))
|
|
486
|
+
return onEvent;
|
|
487
|
+
});
|
|
488
|
+
return remove;
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
once(select, argument, squash) {
|
|
492
|
+
if (typeof argument == "function")
|
|
493
|
+
return this.on(select, argument, squash, true);
|
|
494
|
+
if (typeof select == "string")
|
|
495
|
+
select = [select];
|
|
496
|
+
return new Promise((resolve, reject) => {
|
|
497
|
+
const invoke = (key) => {
|
|
498
|
+
if (!key) {
|
|
499
|
+
clear();
|
|
500
|
+
reject(Oops$9.Timeout(select, typeof argument == "number"
|
|
501
|
+
? `${argument}ms`
|
|
502
|
+
: `lifetime of ${this}`));
|
|
503
|
+
}
|
|
504
|
+
else if (select.includes(key)) {
|
|
505
|
+
clear();
|
|
506
|
+
return resolve;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
const clear = ensure(this, x => {
|
|
510
|
+
for (const key of select)
|
|
511
|
+
try {
|
|
512
|
+
void this[key];
|
|
513
|
+
}
|
|
514
|
+
catch (e) { }
|
|
515
|
+
return x.addListener(invoke);
|
|
516
|
+
});
|
|
517
|
+
if (typeof argument == "number")
|
|
518
|
+
setTimeout(invoke, argument);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
effect(callback, select) {
|
|
522
|
+
const effect = createEffect(callback);
|
|
523
|
+
return ensure(this, control => {
|
|
524
|
+
let busy = false;
|
|
525
|
+
let inject = this.is;
|
|
526
|
+
const invoke = () => {
|
|
527
|
+
if (busy)
|
|
528
|
+
return;
|
|
529
|
+
const output = mayRetry(() => {
|
|
530
|
+
effect.call(inject, inject);
|
|
531
|
+
});
|
|
532
|
+
if (output instanceof Promise) {
|
|
533
|
+
output.finally(() => busy = false);
|
|
534
|
+
busy = true;
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
if (!select) {
|
|
538
|
+
const sub = control.subscribe(() => invoke);
|
|
539
|
+
inject = sub.proxy;
|
|
540
|
+
invoke();
|
|
541
|
+
return sub.commit();
|
|
604
542
|
}
|
|
543
|
+
invoke();
|
|
544
|
+
return control.addListener(key => {
|
|
545
|
+
if (key === null) {
|
|
546
|
+
if (!select.length)
|
|
547
|
+
invoke();
|
|
548
|
+
}
|
|
549
|
+
else if (select.includes(key))
|
|
550
|
+
return invoke;
|
|
551
|
+
});
|
|
552
|
+
});
|
|
605
553
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
554
|
+
// TODO: account for exotic properties
|
|
555
|
+
import(source, select) {
|
|
556
|
+
const { subject } = ensure(this);
|
|
557
|
+
if (!select)
|
|
558
|
+
select = getOwnPropertyNames(subject);
|
|
559
|
+
for (const key of select)
|
|
560
|
+
if (key in source)
|
|
561
|
+
this[key] = source[key];
|
|
562
|
+
}
|
|
563
|
+
export(subset) {
|
|
564
|
+
const { state } = ensure(this);
|
|
565
|
+
const output = {};
|
|
566
|
+
const keys = subset || state.keys();
|
|
567
|
+
for (const key of keys)
|
|
568
|
+
output[key] = state.get(key);
|
|
569
|
+
return output;
|
|
570
|
+
}
|
|
571
|
+
update(arg, tag) {
|
|
572
|
+
const target = ensure(this);
|
|
573
|
+
const { frame, waiting } = target;
|
|
574
|
+
if (typeof arg == "string") {
|
|
575
|
+
target.update(arg);
|
|
576
|
+
if (1 in arguments && arg in this) {
|
|
577
|
+
const method = this[arg];
|
|
578
|
+
if (typeof method == "function")
|
|
579
|
+
if (typeof tag != "boolean")
|
|
580
|
+
method.call(this, tag);
|
|
581
|
+
else if (tag)
|
|
582
|
+
method.call(this);
|
|
583
|
+
}
|
|
584
|
+
arg = undefined;
|
|
611
585
|
}
|
|
612
586
|
if (!frame.size && arg === true)
|
|
613
587
|
return Promise.reject(Oops$9.StrictUpdate());
|
|
@@ -617,37 +591,51 @@ class Controller {
|
|
|
617
591
|
throw Oops$9.NoChaining();
|
|
618
592
|
if (frame.size || arg !== false)
|
|
619
593
|
waiting.add(() => {
|
|
620
|
-
callback(getUpdate(
|
|
594
|
+
callback(getUpdate(this));
|
|
621
595
|
});
|
|
622
596
|
else
|
|
623
597
|
callback(false);
|
|
624
598
|
}
|
|
625
599
|
};
|
|
626
600
|
}
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
define(subject, CONTROL, control);
|
|
601
|
+
/**
|
|
602
|
+
* Mark this instance for garbage-collection and send `willDestroy` event to all listeners.
|
|
603
|
+
*/
|
|
604
|
+
destroy() {
|
|
605
|
+
ensure(this).clear();
|
|
633
606
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
607
|
+
toString() {
|
|
608
|
+
return this.constructor.name;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Creates a new instance of this controller.
|
|
612
|
+
*
|
|
613
|
+
* Beyond `new this(...)`, method will activate managed-state.
|
|
614
|
+
*
|
|
615
|
+
* @param args - arguments sent to constructor
|
|
616
|
+
*/
|
|
617
|
+
static create(...args) {
|
|
618
|
+
const instance = new this(...args);
|
|
619
|
+
ensure(instance);
|
|
620
|
+
return instance;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Static equivalent of `x instanceof this`.
|
|
624
|
+
*
|
|
625
|
+
* Will determine if provided class is a subtype of this one.
|
|
626
|
+
*/
|
|
627
|
+
static isTypeof(maybe) {
|
|
628
|
+
return (typeof maybe == "function" &&
|
|
629
|
+
maybe.prototype instanceof this);
|
|
642
630
|
}
|
|
643
|
-
return cb ? cb(control) : control;
|
|
644
631
|
}
|
|
645
632
|
|
|
633
|
+
const PENDING = new Map();
|
|
646
634
|
function apply(fn, label) {
|
|
647
635
|
const name = label || fn.name || "pending";
|
|
648
636
|
const placeholder = Symbol(`${name} instruction`);
|
|
649
637
|
function setup(key) {
|
|
650
|
-
const {
|
|
638
|
+
const { subject, state } = this;
|
|
651
639
|
const control = this;
|
|
652
640
|
PENDING.delete(placeholder);
|
|
653
641
|
delete subject[key];
|
|
@@ -661,67 +649,56 @@ function apply(fn, label) {
|
|
|
661
649
|
return false;
|
|
662
650
|
}
|
|
663
651
|
if ("recursive" in output && output.recursive)
|
|
664
|
-
output = Object.assign(Object.assign({}, output), { get:
|
|
652
|
+
output = Object.assign(Object.assign({}, output), { get: getRecursive(key, this) });
|
|
665
653
|
const desc = output;
|
|
666
654
|
if ("value" in desc)
|
|
667
|
-
state
|
|
655
|
+
state.set(key, desc.value);
|
|
668
656
|
const { get: onGet, set: onSet, enumerable, suspense } = desc;
|
|
669
|
-
|
|
670
|
-
?
|
|
671
|
-
:
|
|
657
|
+
const set = onSet !== false
|
|
658
|
+
? this.ref(key, onSet)
|
|
659
|
+
: undefined;
|
|
672
660
|
function get() {
|
|
673
|
-
if (!(key
|
|
661
|
+
if (!state.has(key) && suspense)
|
|
674
662
|
throw suspend(control, key);
|
|
675
663
|
const local = this[LOCAL];
|
|
676
|
-
if (local
|
|
677
|
-
local.
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
return local
|
|
682
|
-
? onGet(value, local)
|
|
683
|
-
: onGet(value);
|
|
664
|
+
if (local)
|
|
665
|
+
local.add(key);
|
|
666
|
+
return onGet
|
|
667
|
+
? onGet(local)
|
|
668
|
+
: state.get(key);
|
|
684
669
|
}
|
|
685
|
-
|
|
686
|
-
defineProperty(x, key, { enumerable, get, set });
|
|
670
|
+
defineProperty(subject, key, { enumerable, get, set });
|
|
687
671
|
}
|
|
688
672
|
PENDING.set(placeholder, setup);
|
|
689
673
|
return placeholder;
|
|
690
674
|
}
|
|
691
|
-
function
|
|
692
|
-
return forSubscriber((parent, context) => {
|
|
693
|
-
let child;
|
|
694
|
-
function init() {
|
|
695
|
-
if (child) {
|
|
696
|
-
child.release();
|
|
697
|
-
parent.dependant.delete(child);
|
|
698
|
-
context.set(parent, undefined);
|
|
699
|
-
child = undefined;
|
|
700
|
-
}
|
|
701
|
-
const value = source.state[key];
|
|
702
|
-
if (value && CONTROL in value) {
|
|
703
|
-
child = new Subscriber(value, parent.onUpdate);
|
|
704
|
-
if (parent.active)
|
|
705
|
-
child.commit();
|
|
706
|
-
parent.dependant.add(child);
|
|
707
|
-
context.set(parent, child.proxy);
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
init();
|
|
711
|
-
parent.watch[key] = init;
|
|
712
|
-
});
|
|
713
|
-
}
|
|
714
|
-
function forSubscriber(fn) {
|
|
675
|
+
function getRecursive(key, from) {
|
|
715
676
|
const context = new WeakMap();
|
|
716
|
-
|
|
677
|
+
const { state } = from;
|
|
678
|
+
return (local) => {
|
|
717
679
|
if (!local)
|
|
718
|
-
return
|
|
680
|
+
return state.get(key);
|
|
719
681
|
if (!context.has(local)) {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
682
|
+
let child;
|
|
683
|
+
const init = () => {
|
|
684
|
+
if (child) {
|
|
685
|
+
child.release();
|
|
686
|
+
local.dependant.delete(child);
|
|
687
|
+
context.set(local, undefined);
|
|
688
|
+
child = undefined;
|
|
689
|
+
}
|
|
690
|
+
const value = state.get(key);
|
|
691
|
+
if (value && CONTROL in value) {
|
|
692
|
+
child = ensure(value).subscribe(local.onUpdate);
|
|
693
|
+
if (local.active)
|
|
694
|
+
child.commit();
|
|
695
|
+
local.dependant.add(child);
|
|
696
|
+
context.set(local, child.proxy);
|
|
697
|
+
}
|
|
698
|
+
return true;
|
|
699
|
+
};
|
|
700
|
+
init();
|
|
701
|
+
local.add(key, init);
|
|
725
702
|
}
|
|
726
703
|
return context.get(local);
|
|
727
704
|
};
|
|
@@ -746,7 +723,7 @@ function act(task) {
|
|
|
746
723
|
this.update(key);
|
|
747
724
|
}
|
|
748
725
|
});
|
|
749
|
-
this.state
|
|
726
|
+
this.state.set(key, undefined);
|
|
750
727
|
defineProperty(invoke, "active", {
|
|
751
728
|
get: () => pending
|
|
752
729
|
});
|
|
@@ -767,7 +744,7 @@ class Managed extends Array {
|
|
|
767
744
|
};
|
|
768
745
|
}
|
|
769
746
|
push(...items) {
|
|
770
|
-
const length = this
|
|
747
|
+
const { length } = this;
|
|
771
748
|
const end = super.push(...items);
|
|
772
749
|
this.update(length, end);
|
|
773
750
|
return end;
|
|
@@ -778,64 +755,65 @@ class Managed extends Array {
|
|
|
778
755
|
return end;
|
|
779
756
|
}
|
|
780
757
|
splice(start, deleteCount, ...rest) {
|
|
781
|
-
|
|
758
|
+
const end = deleteCount !== rest.length ? Infinity : deleteCount;
|
|
759
|
+
this.update(start, end);
|
|
782
760
|
return super.splice(start, deleteCount, ...rest);
|
|
783
761
|
}
|
|
784
762
|
}
|
|
785
763
|
function array() {
|
|
786
764
|
return apply(function array(key) {
|
|
765
|
+
const context = new WeakMap();
|
|
787
766
|
const array = new Managed(() => this.update(key));
|
|
788
|
-
const
|
|
789
|
-
const
|
|
790
|
-
const update = context.onUpdate(key, this);
|
|
767
|
+
const getLocal = (context) => {
|
|
768
|
+
const proxy = Object.create(array);
|
|
791
769
|
let rangeA = 0;
|
|
792
770
|
let rangeB = 0;
|
|
793
|
-
|
|
771
|
+
const onUpdate = () => {
|
|
794
772
|
const [lastA, lastB] = array.lastUpdate;
|
|
795
773
|
if (rangeA <= lastB && rangeB >= lastA)
|
|
796
|
-
return
|
|
797
|
-
}
|
|
798
|
-
|
|
774
|
+
return true;
|
|
775
|
+
};
|
|
776
|
+
const listen = (eventA, eventB) => {
|
|
799
777
|
if (context.active)
|
|
800
778
|
return;
|
|
801
|
-
context.
|
|
779
|
+
context.add(key, onUpdate);
|
|
802
780
|
rangeA = Math.min(rangeA, eventA);
|
|
803
781
|
rangeB = Math.max(rangeB, eventB);
|
|
804
|
-
}
|
|
805
|
-
|
|
782
|
+
};
|
|
783
|
+
proxy[Symbol.iterator] = () => {
|
|
806
784
|
listen(0, Infinity);
|
|
807
785
|
return array[Symbol.iterator]();
|
|
808
786
|
};
|
|
809
|
-
return
|
|
810
|
-
}
|
|
787
|
+
return proxy;
|
|
788
|
+
};
|
|
811
789
|
return {
|
|
812
|
-
|
|
790
|
+
value: array,
|
|
813
791
|
set: false,
|
|
814
|
-
|
|
792
|
+
get: (local) => {
|
|
793
|
+
if (!local)
|
|
794
|
+
return this.state.get(key);
|
|
795
|
+
if (context.has(local))
|
|
796
|
+
return context.get(local);
|
|
797
|
+
const output = getLocal(local);
|
|
798
|
+
context.set(local, output);
|
|
799
|
+
return output;
|
|
800
|
+
}
|
|
815
801
|
};
|
|
816
802
|
});
|
|
817
803
|
}
|
|
818
804
|
|
|
819
|
-
function ref(
|
|
805
|
+
function ref(callback) {
|
|
820
806
|
return apply(function ref(key) {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
function createRef(key, cb) {
|
|
833
|
-
const refObjectFunction = this.ref(key, cb && createValueEffect(cb));
|
|
834
|
-
defineProperty(refObjectFunction, "current", {
|
|
835
|
-
set: refObjectFunction,
|
|
836
|
-
get: () => this.state[key]
|
|
807
|
+
const value = this.ref(key, callback && createValueEffect(callback));
|
|
808
|
+
defineProperty(value, "current", {
|
|
809
|
+
set: value,
|
|
810
|
+
get: () => this.state.get(key)
|
|
811
|
+
});
|
|
812
|
+
return {
|
|
813
|
+
value,
|
|
814
|
+
explicit: true
|
|
815
|
+
};
|
|
837
816
|
});
|
|
838
|
-
return refObjectFunction;
|
|
839
817
|
}
|
|
840
818
|
|
|
841
819
|
const Oops$7 = issues({
|
|
@@ -858,14 +836,18 @@ function put(factory, argument) {
|
|
|
858
836
|
if (output instanceof Promise) {
|
|
859
837
|
pending = output
|
|
860
838
|
.catch(err => error = err)
|
|
861
|
-
.then(val =>
|
|
839
|
+
.then(val => {
|
|
840
|
+
state.set(key, val);
|
|
841
|
+
return val;
|
|
842
|
+
})
|
|
862
843
|
.finally(() => {
|
|
863
844
|
pending = undefined;
|
|
864
845
|
this.update(key);
|
|
865
846
|
});
|
|
866
847
|
return;
|
|
867
848
|
}
|
|
868
|
-
|
|
849
|
+
state.set(key, output);
|
|
850
|
+
return output;
|
|
869
851
|
};
|
|
870
852
|
const suspend = () => {
|
|
871
853
|
if (required === false)
|
|
@@ -890,8 +872,8 @@ function put(factory, argument) {
|
|
|
890
872
|
return suspend();
|
|
891
873
|
if (error)
|
|
892
874
|
throw error;
|
|
893
|
-
if (key
|
|
894
|
-
return state
|
|
875
|
+
if (state.has(key))
|
|
876
|
+
return state.get(key);
|
|
895
877
|
let output = init();
|
|
896
878
|
return pending
|
|
897
879
|
? suspend()
|
|
@@ -918,7 +900,7 @@ function set(value, argument) {
|
|
|
918
900
|
}
|
|
919
901
|
return apply(function set(key) {
|
|
920
902
|
if (value !== undefined)
|
|
921
|
-
this.state
|
|
903
|
+
this.state.set(key, value);
|
|
922
904
|
return {
|
|
923
905
|
suspense: true,
|
|
924
906
|
set: typeof argument == "function"
|
|
@@ -948,35 +930,212 @@ function parent(Expects, required) {
|
|
|
948
930
|
});
|
|
949
931
|
}
|
|
950
932
|
|
|
933
|
+
const ANY$1 = Symbol("any");
|
|
934
|
+
function managedMap(control, property, initial) {
|
|
935
|
+
const context = new WeakMap();
|
|
936
|
+
const lastUpdate = new Set();
|
|
937
|
+
let managed;
|
|
938
|
+
let value;
|
|
939
|
+
function reset() {
|
|
940
|
+
lastUpdate.clear();
|
|
941
|
+
}
|
|
942
|
+
function emit(key) {
|
|
943
|
+
lastUpdate.add(key);
|
|
944
|
+
control.update(property);
|
|
945
|
+
control.waiting.add(reset);
|
|
946
|
+
}
|
|
947
|
+
function setValue(next, initial) {
|
|
948
|
+
value = next;
|
|
949
|
+
managed = create(next);
|
|
950
|
+
control.state.set(property, next);
|
|
951
|
+
if (!initial)
|
|
952
|
+
emit(ANY$1);
|
|
953
|
+
managed.set = (k, v) => {
|
|
954
|
+
emit(k);
|
|
955
|
+
return value.set(k, v);
|
|
956
|
+
};
|
|
957
|
+
managed.delete = (k) => {
|
|
958
|
+
emit(k);
|
|
959
|
+
return value.delete(k);
|
|
960
|
+
};
|
|
961
|
+
managed.clear = () => {
|
|
962
|
+
emit(ANY$1);
|
|
963
|
+
value.clear();
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
function getValue(local) {
|
|
967
|
+
if (!local)
|
|
968
|
+
return managed;
|
|
969
|
+
if (context.has(local))
|
|
970
|
+
return context.get(local);
|
|
971
|
+
const proxy = Object.create(managed);
|
|
972
|
+
const using = new Set();
|
|
973
|
+
context.set(local, proxy);
|
|
974
|
+
local.add(property, () => {
|
|
975
|
+
if (using.has(ANY$1) || lastUpdate.has(ANY$1) && using.size)
|
|
976
|
+
return true;
|
|
977
|
+
for (const key of lastUpdate)
|
|
978
|
+
if (using.has(key))
|
|
979
|
+
return true;
|
|
980
|
+
});
|
|
981
|
+
const watch = (key) => {
|
|
982
|
+
if (!local.active)
|
|
983
|
+
using.add(key);
|
|
984
|
+
};
|
|
985
|
+
proxy.get = (key) => {
|
|
986
|
+
watch(key);
|
|
987
|
+
return value.get(key);
|
|
988
|
+
};
|
|
989
|
+
proxy.has = (key) => {
|
|
990
|
+
watch(key);
|
|
991
|
+
return value.has(key);
|
|
992
|
+
};
|
|
993
|
+
proxy.values = () => {
|
|
994
|
+
watch(ANY$1);
|
|
995
|
+
return value.values();
|
|
996
|
+
};
|
|
997
|
+
proxy.keys = () => {
|
|
998
|
+
watch(ANY$1);
|
|
999
|
+
return value.keys();
|
|
1000
|
+
};
|
|
1001
|
+
proxy.entries = () => {
|
|
1002
|
+
watch(ANY$1);
|
|
1003
|
+
return value.entries();
|
|
1004
|
+
};
|
|
1005
|
+
proxy[Symbol.iterator] = () => {
|
|
1006
|
+
watch(ANY$1);
|
|
1007
|
+
return value[Symbol.iterator]();
|
|
1008
|
+
};
|
|
1009
|
+
defineProperty(proxy, "size", {
|
|
1010
|
+
get() {
|
|
1011
|
+
watch(ANY$1);
|
|
1012
|
+
return value.size;
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
return proxy;
|
|
1016
|
+
}
|
|
1017
|
+
setValue(initial, true);
|
|
1018
|
+
return {
|
|
1019
|
+
set: setValue,
|
|
1020
|
+
get: getValue
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
const ANY = Symbol("any");
|
|
1025
|
+
function managedSet(control, property, initial) {
|
|
1026
|
+
const context = new WeakMap();
|
|
1027
|
+
const lastUpdate = new Set();
|
|
1028
|
+
let managed;
|
|
1029
|
+
let value;
|
|
1030
|
+
function reset() {
|
|
1031
|
+
lastUpdate.clear();
|
|
1032
|
+
}
|
|
1033
|
+
function emit(key) {
|
|
1034
|
+
lastUpdate.add(key);
|
|
1035
|
+
control.update(property);
|
|
1036
|
+
control.waiting.add(reset);
|
|
1037
|
+
}
|
|
1038
|
+
function setValue(next, initial) {
|
|
1039
|
+
value = next;
|
|
1040
|
+
managed = create(next);
|
|
1041
|
+
control.state.set(property, next);
|
|
1042
|
+
if (!initial)
|
|
1043
|
+
emit(ANY);
|
|
1044
|
+
managed.add = (k) => {
|
|
1045
|
+
emit(k);
|
|
1046
|
+
return value.add(k);
|
|
1047
|
+
};
|
|
1048
|
+
managed.delete = (k) => {
|
|
1049
|
+
emit(k);
|
|
1050
|
+
return value.delete(k);
|
|
1051
|
+
};
|
|
1052
|
+
managed.clear = () => {
|
|
1053
|
+
emit(ANY);
|
|
1054
|
+
value.clear();
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
function getValue(local) {
|
|
1058
|
+
if (!local)
|
|
1059
|
+
return managed;
|
|
1060
|
+
if (context.has(local))
|
|
1061
|
+
return context.get(local);
|
|
1062
|
+
const proxy = Object.create(managed);
|
|
1063
|
+
const using = new Set();
|
|
1064
|
+
context.set(local, proxy);
|
|
1065
|
+
local.add(property, () => {
|
|
1066
|
+
if (using.has(ANY) || lastUpdate.has(ANY) && using.size)
|
|
1067
|
+
return true;
|
|
1068
|
+
for (const key of lastUpdate)
|
|
1069
|
+
if (using.has(key))
|
|
1070
|
+
return true;
|
|
1071
|
+
});
|
|
1072
|
+
const watch = (key) => {
|
|
1073
|
+
if (!local.active)
|
|
1074
|
+
using.add(key);
|
|
1075
|
+
};
|
|
1076
|
+
proxy.has = (key) => {
|
|
1077
|
+
watch(key);
|
|
1078
|
+
return value.has(key);
|
|
1079
|
+
};
|
|
1080
|
+
proxy.values = () => {
|
|
1081
|
+
watch(ANY);
|
|
1082
|
+
return value.values();
|
|
1083
|
+
};
|
|
1084
|
+
proxy.keys = () => {
|
|
1085
|
+
watch(ANY);
|
|
1086
|
+
return value.keys();
|
|
1087
|
+
};
|
|
1088
|
+
proxy.entries = () => {
|
|
1089
|
+
watch(ANY);
|
|
1090
|
+
return value.entries();
|
|
1091
|
+
};
|
|
1092
|
+
proxy[Symbol.iterator] = () => {
|
|
1093
|
+
watch(ANY);
|
|
1094
|
+
return value[Symbol.iterator]();
|
|
1095
|
+
};
|
|
1096
|
+
defineProperty(proxy, "size", {
|
|
1097
|
+
get() {
|
|
1098
|
+
watch(ANY);
|
|
1099
|
+
return value.size;
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
return proxy;
|
|
1103
|
+
}
|
|
1104
|
+
setValue(initial, true);
|
|
1105
|
+
return {
|
|
1106
|
+
set: setValue,
|
|
1107
|
+
get: getValue
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
|
|
951
1111
|
const Oops$5 = issues({
|
|
952
1112
|
BadArgument: (type) => `Instruction \`use\` cannot accept argument type of ${type}.`,
|
|
953
1113
|
});
|
|
954
1114
|
function use$1(input, argument) {
|
|
955
1115
|
return apply(function use(key) {
|
|
956
|
-
const { subject } = this;
|
|
957
|
-
|
|
958
|
-
|
|
1116
|
+
const { state, subject } = this;
|
|
1117
|
+
if (typeof input === "function")
|
|
1118
|
+
input = Model.isTypeof(input)
|
|
1119
|
+
? new input()
|
|
1120
|
+
: input();
|
|
1121
|
+
else if (input && typeof input !== "object")
|
|
1122
|
+
throw Oops$5.BadArgument(typeof input);
|
|
1123
|
+
if (input instanceof Map)
|
|
1124
|
+
return managedMap(this, key, input);
|
|
1125
|
+
if (input instanceof Set)
|
|
1126
|
+
return managedSet(this, key, input);
|
|
1127
|
+
function onUpdate(next) {
|
|
1128
|
+
state.set(key, next);
|
|
959
1129
|
if (next) {
|
|
960
1130
|
Parent.set(next, subject);
|
|
961
|
-
|
|
1131
|
+
ensure(next);
|
|
962
1132
|
}
|
|
963
1133
|
if (typeof argument == "function")
|
|
964
1134
|
argument(next);
|
|
965
1135
|
return true;
|
|
966
|
-
};
|
|
967
|
-
if (input) {
|
|
968
|
-
const value = Model.isTypeof(input)
|
|
969
|
-
? new input() :
|
|
970
|
-
typeof input === "object"
|
|
971
|
-
? input :
|
|
972
|
-
typeof input === "function"
|
|
973
|
-
? input()
|
|
974
|
-
: null;
|
|
975
|
-
if (value === null)
|
|
976
|
-
throw Oops$5.BadArgument(typeof input);
|
|
977
|
-
if (value)
|
|
978
|
-
onUpdate(value);
|
|
979
1136
|
}
|
|
1137
|
+
if (input)
|
|
1138
|
+
onUpdate(input);
|
|
980
1139
|
return {
|
|
981
1140
|
set: onUpdate,
|
|
982
1141
|
recursive: true
|
|
@@ -1088,7 +1247,8 @@ function tap(type, argument) {
|
|
|
1088
1247
|
}
|
|
1089
1248
|
else if (!instance && argument)
|
|
1090
1249
|
throw Oops$3.AmbientRequired(type.name, subject, key);
|
|
1091
|
-
this.
|
|
1250
|
+
this.state.set(key, instance);
|
|
1251
|
+
this.update(key);
|
|
1092
1252
|
});
|
|
1093
1253
|
return {
|
|
1094
1254
|
recursive: true,
|
|
@@ -1133,7 +1293,7 @@ function useModel(source, arg, arg2) {
|
|
|
1133
1293
|
new source() :
|
|
1134
1294
|
source() :
|
|
1135
1295
|
source;
|
|
1136
|
-
|
|
1296
|
+
ensure(instance);
|
|
1137
1297
|
if (typeof cb == "function")
|
|
1138
1298
|
cb(instance);
|
|
1139
1299
|
return instance;
|
|
@@ -1151,7 +1311,7 @@ function useModel(source, arg, arg2) {
|
|
|
1151
1311
|
return instance;
|
|
1152
1312
|
}
|
|
1153
1313
|
else {
|
|
1154
|
-
const local = use(refresh => (
|
|
1314
|
+
const local = use(refresh => (ensure(instance).subscribe(() => refresh)));
|
|
1155
1315
|
if (typeof arg == "object") {
|
|
1156
1316
|
local.active = false;
|
|
1157
1317
|
if (typeof arg2 !== "object")
|
|
@@ -1177,7 +1337,7 @@ function useModel(source, arg, arg2) {
|
|
|
1177
1337
|
|
|
1178
1338
|
function useFrom(target, compute, suspend) {
|
|
1179
1339
|
const local = use(refresh => {
|
|
1180
|
-
const sub =
|
|
1340
|
+
const sub = ensure(target).subscribe(() => update);
|
|
1181
1341
|
const spy = sub.proxy;
|
|
1182
1342
|
let value = compute.call(spy, spy);
|
|
1183
1343
|
let update;
|
|
@@ -1229,23 +1389,14 @@ function useTap(source, path, expect) {
|
|
|
1229
1389
|
}, [])();
|
|
1230
1390
|
if (typeof path == "function")
|
|
1231
1391
|
return useFrom(instance, path, expect);
|
|
1232
|
-
const local = use(refresh =>
|
|
1233
|
-
const parent = control(instance);
|
|
1234
|
-
const sub = new Subscriber(parent, () => refresh);
|
|
1235
|
-
if (typeof path == "string") {
|
|
1236
|
-
const source = sub.proxy;
|
|
1237
|
-
defineProperty(sub, "proxy", {
|
|
1238
|
-
get() {
|
|
1239
|
-
const value = source[path];
|
|
1240
|
-
if (value === undefined && expect)
|
|
1241
|
-
throw suspend(parent, path);
|
|
1242
|
-
return value;
|
|
1243
|
-
}
|
|
1244
|
-
});
|
|
1245
|
-
}
|
|
1246
|
-
return sub;
|
|
1247
|
-
});
|
|
1392
|
+
const local = use(refresh => (ensure(instance).subscribe(() => refresh)));
|
|
1248
1393
|
React.useLayoutEffect(local.commit, []);
|
|
1394
|
+
if (typeof path !== "undefined") {
|
|
1395
|
+
const value = local.proxy[path];
|
|
1396
|
+
if (value === undefined && expect)
|
|
1397
|
+
throw suspend(ensure(instance), path);
|
|
1398
|
+
return value;
|
|
1399
|
+
}
|
|
1249
1400
|
return local.proxy;
|
|
1250
1401
|
}
|
|
1251
1402
|
|
|
@@ -1266,7 +1417,7 @@ class MVC extends Model {
|
|
|
1266
1417
|
static new(arg) {
|
|
1267
1418
|
const instance = React.useMemo(() => {
|
|
1268
1419
|
const instance = new this();
|
|
1269
|
-
|
|
1420
|
+
ensure(instance);
|
|
1270
1421
|
if (typeof arg == "function")
|
|
1271
1422
|
arg(instance);
|
|
1272
1423
|
else if (arg)
|
|
@@ -1351,7 +1502,7 @@ function useAppliedProps(within, props) {
|
|
|
1351
1502
|
const targets = within.local;
|
|
1352
1503
|
return function integrate(props) {
|
|
1353
1504
|
for (const [key, value] of entries(props))
|
|
1354
|
-
if (["of", "for", "children"].
|
|
1505
|
+
if (!["of", "for", "children"].includes(key))
|
|
1355
1506
|
for (const into of targets)
|
|
1356
1507
|
if (into && key in into)
|
|
1357
1508
|
into[key] = value;
|
|
@@ -1363,7 +1514,7 @@ function RenderFunction(props) {
|
|
|
1363
1514
|
const hook = use(refresh => {
|
|
1364
1515
|
const targets = props.context.local;
|
|
1365
1516
|
if (targets.length == 1)
|
|
1366
|
-
return
|
|
1517
|
+
return ensure(targets[0]).subscribe(() => refresh);
|
|
1367
1518
|
return {};
|
|
1368
1519
|
});
|
|
1369
1520
|
if (hook.commit)
|