@expressive/mvc 0.53.3 → 0.53.6
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 +137 -0
- package/dist/esm/context.test.js +211 -0
- package/dist/esm/control.js +182 -0
- package/dist/esm/control.test.js +183 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/instruction/get.js +87 -0
- package/dist/esm/instruction/get.test.js +645 -0
- package/dist/esm/instruction/has.js +73 -0
- package/dist/esm/instruction/has.test.js +354 -0
- package/dist/esm/instruction/ref.js +59 -0
- package/dist/esm/instruction/ref.test.js +281 -0
- package/dist/esm/instruction/set.js +86 -0
- package/dist/esm/instruction/set.test.js +527 -0
- package/dist/esm/instruction/use.js +60 -0
- package/dist/esm/instruction/use.test.js +305 -0
- package/dist/esm/mocks.js +20 -0
- package/dist/esm/model.js +278 -0
- package/dist/esm/model.test.js +1541 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +76 -44
- package/package.json +6 -7
- package/dist/index.mjs +0 -908
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { get } from './instruction/get';
|
|
2
|
+
export { ref } from './instruction/ref';
|
|
3
|
+
export { set } from './instruction/set';
|
|
4
|
+
export { use } from './instruction/use';
|
|
5
|
+
export { has } from './instruction/has';
|
|
6
|
+
export { Model, Model as default } from './model';
|
|
7
|
+
export { createEffect, createProxy, addListener } from './control';
|
|
8
|
+
export { Context } from './context';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Context } from '../context';
|
|
2
|
+
import { createEffect } from '../control';
|
|
3
|
+
import { fetch, METHOD, Model, PARENT, event, update } from '../model';
|
|
4
|
+
import { use } from './use';
|
|
5
|
+
const STALE = new WeakSet();
|
|
6
|
+
function get(arg0, arg1) {
|
|
7
|
+
return use((key, subject, state) => {
|
|
8
|
+
if (typeof arg0 == "symbol")
|
|
9
|
+
throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${subject}.${key}. This is not allowed.`);
|
|
10
|
+
if (Model.is(arg0)) {
|
|
11
|
+
const hasParent = PARENT.get(subject);
|
|
12
|
+
// TODO: notify on discovered context
|
|
13
|
+
function assign(value) {
|
|
14
|
+
if (typeof arg1 == "function")
|
|
15
|
+
value.get(x => arg1(x, key));
|
|
16
|
+
update(subject, key, value);
|
|
17
|
+
}
|
|
18
|
+
if (!hasParent) {
|
|
19
|
+
if (arg1 === true)
|
|
20
|
+
throw new Error(`${subject} may only exist as a child of type ${arg0}.`);
|
|
21
|
+
Context.get(subject, (context) => {
|
|
22
|
+
const model = context.get(arg0);
|
|
23
|
+
if (model)
|
|
24
|
+
assign(model);
|
|
25
|
+
else if (arg1 !== false)
|
|
26
|
+
throw new Error(`Required ${arg0} not found in context for ${subject}.`);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else if (!arg0 || hasParent instanceof arg0)
|
|
30
|
+
assign(hasParent);
|
|
31
|
+
else
|
|
32
|
+
throw new Error(`New ${subject} created as child of ${hasParent}, but must be instanceof ${arg0}.`);
|
|
33
|
+
return {
|
|
34
|
+
get: arg1 !== false
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
let from = subject;
|
|
38
|
+
if (arg0 instanceof Model) {
|
|
39
|
+
from = arg0;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const fn = METHOD.get(arg0) || arg0;
|
|
43
|
+
arg1 = ((p, k) => fn.call(p, k, p));
|
|
44
|
+
}
|
|
45
|
+
const source = resolve => resolve(from);
|
|
46
|
+
const getter = arg1;
|
|
47
|
+
let reset;
|
|
48
|
+
let isAsync;
|
|
49
|
+
let proxy;
|
|
50
|
+
function connect(model) {
|
|
51
|
+
reset = createEffect(model, current => {
|
|
52
|
+
proxy = current;
|
|
53
|
+
if (!(key in state) || STALE.delete(compute))
|
|
54
|
+
compute(!reset);
|
|
55
|
+
return (didUpdate) => {
|
|
56
|
+
if (didUpdate) {
|
|
57
|
+
STALE.add(compute);
|
|
58
|
+
event(subject, key, true);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}, false);
|
|
62
|
+
}
|
|
63
|
+
function compute(initial) {
|
|
64
|
+
let next;
|
|
65
|
+
try {
|
|
66
|
+
next = getter.call(proxy, proxy, key);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${subject}.${key}.`);
|
|
70
|
+
if (initial)
|
|
71
|
+
throw err;
|
|
72
|
+
console.error(err);
|
|
73
|
+
}
|
|
74
|
+
update(subject, key, next, !isAsync);
|
|
75
|
+
}
|
|
76
|
+
return () => {
|
|
77
|
+
if (!proxy) {
|
|
78
|
+
source(connect);
|
|
79
|
+
isAsync = true;
|
|
80
|
+
}
|
|
81
|
+
if (STALE.delete(compute))
|
|
82
|
+
compute();
|
|
83
|
+
return fetch(subject, key, !proxy);
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export { get };
|