@expressive/react 0.19.2 → 0.21.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 +23 -25
- package/lib/index.esm.js +67 -253
- package/lib/index.js +71 -254
- package/package.json +3 -11
package/lib/index.d.ts
CHANGED
|
@@ -1,62 +1,60 @@
|
|
|
1
|
-
import { Model
|
|
2
|
-
export { Debug, Model,
|
|
3
|
-
import React from 'react';
|
|
1
|
+
import { Model } from '@expressive/mvc';
|
|
2
|
+
export { Control, Debug, Model, default, get, ref, run, set, use } from '@expressive/mvc';
|
|
3
|
+
import * as React$1 from 'react';
|
|
4
|
+
import React__default from 'react';
|
|
4
5
|
|
|
5
|
-
type Class$1 = new () => any;
|
|
6
6
|
declare namespace Consumer {
|
|
7
|
-
type HasProps<
|
|
7
|
+
type HasProps<T extends Model> = {
|
|
8
8
|
/** Type of controller to fetch from context. */
|
|
9
|
-
for:
|
|
9
|
+
for: Model.Type<T>;
|
|
10
10
|
/**
|
|
11
11
|
* Getter function. Is called on every natural render of this component.
|
|
12
12
|
* Will throw if usable instance cannot be found in context.
|
|
13
13
|
*/
|
|
14
|
-
has: (value:
|
|
14
|
+
has: (value: T) => void;
|
|
15
15
|
};
|
|
16
|
-
type GetProps<
|
|
16
|
+
type GetProps<T extends Model> = {
|
|
17
17
|
/** Type of controller to fetch from context. */
|
|
18
|
-
for:
|
|
18
|
+
for: Model.Type<T>;
|
|
19
19
|
/** Getter function. Is called on every natural render of this component. */
|
|
20
|
-
get: (value:
|
|
20
|
+
get: (value: T | undefined) => void;
|
|
21
21
|
};
|
|
22
|
-
type RenderProps<
|
|
22
|
+
type RenderProps<T extends Model> = {
|
|
23
23
|
/** Type of controller to fetch from context. */
|
|
24
|
-
for:
|
|
24
|
+
for: Model.Type<T>;
|
|
25
25
|
/**
|
|
26
26
|
* Render function, will receive instance of desired controller.
|
|
27
27
|
*
|
|
28
28
|
* Similar to `get()`, updates to properties accessed in
|
|
29
29
|
* this function will cause a refresh when they change.
|
|
30
30
|
*/
|
|
31
|
-
children: (value:
|
|
31
|
+
children: (value: T) => React.ReactElement<any, any> | null;
|
|
32
32
|
};
|
|
33
|
-
type Props<T extends
|
|
33
|
+
type Props<T extends Model> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
34
34
|
}
|
|
35
|
-
declare function Consumer<T extends
|
|
35
|
+
declare function Consumer<T extends Model>(props: Consumer.Props<T>): React$1.ReactElement<any, any> | null;
|
|
36
36
|
|
|
37
37
|
type Class = new () => any;
|
|
38
38
|
declare namespace Provider {
|
|
39
39
|
type Item = Model | Model.New;
|
|
40
|
-
type Multiple<T extends Item = Item> =
|
|
41
|
-
[key: string]: T;
|
|
40
|
+
type Multiple<T extends Item = Item> = {
|
|
41
|
+
[key: string | number]: T;
|
|
42
42
|
};
|
|
43
43
|
type Instance<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
|
|
44
|
+
type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
|
|
44
45
|
type NormalProps<E, I = Instance<E>> = {
|
|
45
46
|
for: E;
|
|
46
|
-
fallback?:
|
|
47
|
-
children?:
|
|
47
|
+
fallback?: React__default.ReactNode;
|
|
48
|
+
children?: React__default.ReactNode | ((instance: I) => React__default.ReactNode);
|
|
48
49
|
use?: Model.Compat<I>;
|
|
49
50
|
};
|
|
50
51
|
type MultipleProps<T extends Item> = {
|
|
51
52
|
for: Multiple<T>;
|
|
52
|
-
fallback?:
|
|
53
|
-
children?:
|
|
53
|
+
fallback?: React__default.ReactNode;
|
|
54
|
+
children?: React__default.ReactNode;
|
|
54
55
|
use?: Model.Compat<Instance<T>>;
|
|
55
56
|
};
|
|
56
|
-
type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
|
|
57
57
|
}
|
|
58
58
|
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): JSX.Element;
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
export { Consumer, Provider, instruction as get };
|
|
60
|
+
export { Consumer, Provider };
|
package/lib/index.esm.js
CHANGED
|
@@ -1,277 +1,91 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { Debug, Model,
|
|
3
|
-
import React, { Suspense } from 'react';
|
|
1
|
+
import { Context, issues, Model, Control } from '@expressive/mvc';
|
|
2
|
+
export { Control, Debug, Model, default, get, ref, run, set, use } from '@expressive/mvc';
|
|
3
|
+
import React, { createContext, useContext, useState, useMemo, useLayoutEffect, Suspense } from 'react';
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
MultipleExist: (name) => `Did find ${name} in context, but multiple were defined by same Provider.`
|
|
8
|
-
});
|
|
9
|
-
const Global = new Register();
|
|
10
|
-
const LookupContext = React.createContext(Global);
|
|
11
|
-
function useLookup() {
|
|
12
|
-
return React.useContext(LookupContext);
|
|
13
|
-
}
|
|
14
|
-
function useContext(required) {
|
|
15
|
-
const instance = useLookup().get(this);
|
|
16
|
-
if (instance === null)
|
|
17
|
-
throw Oops$3.MultipleExist(this);
|
|
18
|
-
if (!instance && required !== false)
|
|
19
|
-
throw Oops$3.NotFound(this);
|
|
20
|
-
return instance;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function useGet(arg1, arg2) {
|
|
24
|
-
const instance = this.find(arg1 !== false);
|
|
25
|
-
if (typeof arg1 == "boolean")
|
|
26
|
-
return instance;
|
|
27
|
-
return useSubscriber(instance, arg1, arg2);
|
|
28
|
-
}
|
|
29
|
-
function useSubscriber(source, callback, required) {
|
|
30
|
-
const state = React.useState(0);
|
|
31
|
-
const local = React.useMemo(() => {
|
|
32
|
-
const refresh = state[1].bind(null, x => x + 1);
|
|
33
|
-
if (!callback)
|
|
34
|
-
return new Subscriber(source, () => refresh);
|
|
35
|
-
const sub = new Subscriber(source, () => update);
|
|
36
|
-
const spy = sub.proxy;
|
|
37
|
-
let compute = () => callback.call(spy, spy, forceUpdate);
|
|
38
|
-
function forceUpdate(passthru) {
|
|
39
|
-
if (typeof passthru == "function")
|
|
40
|
-
passthru = passthru();
|
|
41
|
-
if (compute)
|
|
42
|
-
reassign(compute());
|
|
43
|
-
else
|
|
44
|
-
refresh();
|
|
45
|
-
if (passthru)
|
|
46
|
-
return passthru.finally(refresh);
|
|
47
|
-
}
|
|
48
|
-
function reassign(next) {
|
|
49
|
-
value = next;
|
|
50
|
-
if (retry) {
|
|
51
|
-
retry();
|
|
52
|
-
retry = undefined;
|
|
53
|
-
}
|
|
54
|
-
else
|
|
55
|
-
refresh();
|
|
56
|
-
}
|
|
57
|
-
let retry;
|
|
58
|
-
let update;
|
|
59
|
-
let value = compute();
|
|
60
|
-
if (value === null) {
|
|
61
|
-
sub.watch.clear();
|
|
62
|
-
compute = undefined;
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (typeof value == "function") {
|
|
66
|
-
const get = value;
|
|
67
|
-
sub.watch.clear();
|
|
68
|
-
callback = () => get();
|
|
69
|
-
value = get();
|
|
70
|
-
}
|
|
71
|
-
if (value instanceof Promise) {
|
|
72
|
-
value.then(reassign);
|
|
73
|
-
value = undefined;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
sub.commit();
|
|
77
|
-
update = () => {
|
|
78
|
-
const next = compute();
|
|
79
|
-
if (notEqual(value, next))
|
|
80
|
-
reassign(next);
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
Object.defineProperty(sub, "proxy", {
|
|
84
|
-
get() {
|
|
85
|
-
if (value !== undefined)
|
|
86
|
-
return value;
|
|
87
|
-
if (required)
|
|
88
|
-
throw new Promise(res => retry = res);
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
return sub;
|
|
93
|
-
}, [source]);
|
|
94
|
-
if (!local)
|
|
95
|
-
return null;
|
|
96
|
-
React.useLayoutEffect(() => {
|
|
97
|
-
local.commit();
|
|
98
|
-
return () => local.release();
|
|
99
|
-
}, [source]);
|
|
100
|
-
return local.proxy;
|
|
101
|
-
}
|
|
102
|
-
/** Values are not equal for purposes of a refresh. */
|
|
103
|
-
const notEqual = (a, b) => (b !== a && (!Array.isArray(a) ||
|
|
104
|
-
!Array.isArray(b) ||
|
|
105
|
-
a.length !== b.length ||
|
|
106
|
-
a.some((x, i) => x !== b[i])));
|
|
107
|
-
|
|
108
|
-
const Oops$2 = issues({
|
|
109
|
-
AmbientRequired: (requested, requester) => `Attempted to find an instance of ${requested} in context. It is required by ${requester}, but one could not be found.`
|
|
110
|
-
});
|
|
5
|
+
const LookupContext = createContext(new Context());
|
|
6
|
+
const useLookup = () => useContext(LookupContext);
|
|
111
7
|
const Pending = new WeakMap();
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return (callback) => {
|
|
115
|
-
const item = Internal.getParent(from, type);
|
|
116
|
-
if (item)
|
|
117
|
-
callback(item);
|
|
118
|
-
else
|
|
119
|
-
getPending(from).push(context => {
|
|
120
|
-
const got = context.get(type);
|
|
121
|
-
if (got)
|
|
122
|
-
callback(got);
|
|
123
|
-
else if (required)
|
|
124
|
-
throw Oops$2.AmbientRequired(type, from);
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
function usePeerContext(subject) {
|
|
129
|
-
if (Applied.has(subject)) {
|
|
130
|
-
if (Applied.get(subject))
|
|
131
|
-
useLookup();
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const pending = Pending.get(subject);
|
|
135
|
-
if (pending) {
|
|
136
|
-
const local = useLookup();
|
|
137
|
-
for (const init of pending)
|
|
138
|
-
init(local);
|
|
139
|
-
Pending.delete(subject);
|
|
140
|
-
}
|
|
141
|
-
Applied.set(subject, !!pending);
|
|
142
|
-
}
|
|
143
|
-
function getPending(subject) {
|
|
144
|
-
let pending = Pending.get(subject);
|
|
8
|
+
function hasModel(type, relativeTo, callback) {
|
|
9
|
+
let pending = Pending.get(relativeTo);
|
|
145
10
|
if (!pending)
|
|
146
|
-
Pending.set(
|
|
147
|
-
|
|
11
|
+
Pending.set(relativeTo, pending = []);
|
|
12
|
+
pending.push(context => {
|
|
13
|
+
callback(context.get(type));
|
|
14
|
+
});
|
|
148
15
|
}
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const update = React.useState(0)[1];
|
|
163
|
-
React.useLayoutEffect(() => {
|
|
164
|
-
if (arg1.length && instance instanceof Model)
|
|
165
|
-
instance.on(arg1, () => update(x => x + 1));
|
|
166
|
-
return () => {
|
|
167
|
-
instance.gc();
|
|
168
|
-
};
|
|
169
|
-
}, []);
|
|
170
|
-
return instance;
|
|
16
|
+
const Applied = new WeakMap();
|
|
17
|
+
function usePeerContext(instance) {
|
|
18
|
+
const applyPeers = Applied.get(instance);
|
|
19
|
+
if (applyPeers)
|
|
20
|
+
useLookup();
|
|
21
|
+
else if (applyPeers === undefined) {
|
|
22
|
+
const pending = Pending.get(instance);
|
|
23
|
+
if (pending) {
|
|
24
|
+
const local = useLookup();
|
|
25
|
+
pending.forEach(init => init(local));
|
|
26
|
+
Pending.delete(instance);
|
|
27
|
+
Applied.set(instance, true);
|
|
28
|
+
}
|
|
171
29
|
}
|
|
172
|
-
const state = React.useState(0);
|
|
173
|
-
const local = React.useMemo(() => {
|
|
174
|
-
const refresh = state[1].bind(null, x => x + 1);
|
|
175
|
-
return new Subscriber(instance, () => refresh);
|
|
176
|
-
}, []);
|
|
177
|
-
if (typeof arg1 == "object")
|
|
178
|
-
applyValues(local, arg1, arg2);
|
|
179
|
-
React.useLayoutEffect(() => {
|
|
180
|
-
local.commit();
|
|
181
|
-
return () => {
|
|
182
|
-
local.release();
|
|
183
|
-
instance.gc();
|
|
184
|
-
};
|
|
185
|
-
}, []);
|
|
186
|
-
return local.proxy;
|
|
187
|
-
}
|
|
188
|
-
// TODO: move back to MVC pending a generic use-case.
|
|
189
|
-
function applyValues(target, values, keys) {
|
|
190
|
-
const { waiting, subject } = target.parent;
|
|
191
|
-
target.active = false;
|
|
192
|
-
if (!keys)
|
|
193
|
-
keys = Object.getOwnPropertyNames(subject);
|
|
194
|
-
for (const key of keys)
|
|
195
|
-
if (key in values)
|
|
196
|
-
subject[key] = values[key];
|
|
197
|
-
waiting.add(() => target.active = true);
|
|
198
30
|
}
|
|
199
31
|
|
|
200
|
-
const
|
|
32
|
+
const getModel = (type, adapter) => {
|
|
33
|
+
const context = useLookup();
|
|
34
|
+
const state = useState(0);
|
|
35
|
+
const hook = useMemo(() => {
|
|
36
|
+
return adapter(() => state[1](x => x + 1), cb => cb(context.get(type)));
|
|
37
|
+
}, []);
|
|
38
|
+
if (!hook)
|
|
39
|
+
return null;
|
|
40
|
+
useLayoutEffect(hook.mount, []);
|
|
41
|
+
return hook.render();
|
|
42
|
+
};
|
|
43
|
+
const useModel = (adapter) => {
|
|
44
|
+
const state = useState(0);
|
|
45
|
+
const hook = useMemo(() => (adapter(() => state[1](x => x + 1))), []);
|
|
46
|
+
usePeerContext(hook.instance);
|
|
47
|
+
useLayoutEffect(hook.mount, []);
|
|
48
|
+
return hook.render;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const Oops = issues({
|
|
201
52
|
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
202
53
|
});
|
|
203
54
|
function Consumer(props) {
|
|
204
|
-
const {
|
|
55
|
+
const { children, has, get, for: Type } = props;
|
|
205
56
|
if (typeof children == "function")
|
|
206
|
-
return children(
|
|
57
|
+
return children(Type.get());
|
|
207
58
|
const callback = has || get;
|
|
208
59
|
if (typeof callback == "function")
|
|
209
|
-
callback(
|
|
60
|
+
callback(Type.get(!!has));
|
|
210
61
|
else
|
|
211
|
-
throw Oops
|
|
62
|
+
throw Oops.BadProps();
|
|
212
63
|
return null;
|
|
213
64
|
}
|
|
214
65
|
|
|
215
|
-
const Oops = issues({
|
|
216
|
-
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
217
|
-
});
|
|
218
66
|
function Provider(props) {
|
|
219
|
-
|
|
67
|
+
let { for: included, use: assign } = props;
|
|
68
|
+
const ambient = useLookup();
|
|
69
|
+
const context = useMemo(() => ambient.push(), []);
|
|
70
|
+
if (typeof included == "function" || included instanceof Model)
|
|
71
|
+
included = { [0]: included };
|
|
72
|
+
context.include(included).forEach((explicit, model) => {
|
|
73
|
+
if (assign && explicit)
|
|
74
|
+
for (const K in assign)
|
|
75
|
+
if (K in model)
|
|
76
|
+
model[K] = assign[K];
|
|
77
|
+
const pending = Pending.get(model);
|
|
78
|
+
if (pending)
|
|
79
|
+
pending.forEach(cb => cb(context));
|
|
80
|
+
});
|
|
81
|
+
useLayoutEffect(() => () => context.pop(), []);
|
|
220
82
|
return (React.createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
221
83
|
? props.children
|
|
222
84
|
: (React.createElement(Suspense, { fallback: props.fallback || null }, props.children))));
|
|
223
|
-
}
|
|
224
|
-
function useNewContext(include, assign) {
|
|
225
|
-
const init = new Set();
|
|
226
|
-
const ambient = useLookup();
|
|
227
|
-
const context = React.useMemo(() => {
|
|
228
|
-
if (!include)
|
|
229
|
-
throw Oops.NoType();
|
|
230
|
-
return ambient.push();
|
|
231
|
-
}, []);
|
|
232
|
-
function register(key, input) {
|
|
233
|
-
let instance;
|
|
234
|
-
if (context.register.get(key) === input)
|
|
235
|
-
instance = typeof input == "object"
|
|
236
|
-
? input
|
|
237
|
-
: context.get(input);
|
|
238
|
-
else
|
|
239
|
-
instance = context.add(input, key);
|
|
240
|
-
init.add(instance);
|
|
241
|
-
if (assign)
|
|
242
|
-
assignWeak(instance, assign);
|
|
243
|
-
}
|
|
244
|
-
if (typeof include == "function" || include instanceof Model)
|
|
245
|
-
register(0, include);
|
|
246
|
-
else
|
|
247
|
-
Object.entries(include).forEach(e => register(...e));
|
|
248
|
-
for (const instance of init) {
|
|
249
|
-
Control.for(instance).state.forEach(value => {
|
|
250
|
-
// TODO: should this run repeatedly?
|
|
251
|
-
if (Internal.getParent(value) === instance) {
|
|
252
|
-
context.add(value);
|
|
253
|
-
init.add(value);
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
for (const apply of getPending(instance))
|
|
257
|
-
apply(context);
|
|
258
|
-
// TODO: add test to validate this.
|
|
259
|
-
init.delete(instance);
|
|
260
|
-
}
|
|
261
|
-
React.useLayoutEffect(() => () => context.pop(), []);
|
|
262
|
-
return context;
|
|
263
|
-
}
|
|
264
|
-
function assignWeak(into, from) {
|
|
265
|
-
for (const K in from)
|
|
266
|
-
if (K in into)
|
|
267
|
-
into[K] = from[K];
|
|
268
85
|
}
|
|
269
86
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
this.use = useModel;
|
|
274
|
-
}
|
|
275
|
-
bootstrap.call(Model);
|
|
87
|
+
Control.get = getModel;
|
|
88
|
+
Control.use = useModel;
|
|
89
|
+
Control.has = hasModel;
|
|
276
90
|
|
|
277
|
-
export { Consumer, Provider
|
|
91
|
+
export { Consumer, Provider };
|
package/lib/index.js
CHANGED
|
@@ -10,278 +10,96 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
10
|
var mvc__default = /*#__PURE__*/_interopDefaultLegacy(mvc);
|
|
11
11
|
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
MultipleExist: (name) => `Did find ${name} in context, but multiple were defined by same Provider.`
|
|
16
|
-
});
|
|
17
|
-
const Global = new mvc.Register();
|
|
18
|
-
const LookupContext = React__default["default"].createContext(Global);
|
|
19
|
-
function useLookup() {
|
|
20
|
-
return React__default["default"].useContext(LookupContext);
|
|
21
|
-
}
|
|
22
|
-
function useContext(required) {
|
|
23
|
-
const instance = useLookup().get(this);
|
|
24
|
-
if (instance === null)
|
|
25
|
-
throw Oops$3.MultipleExist(this);
|
|
26
|
-
if (!instance && required !== false)
|
|
27
|
-
throw Oops$3.NotFound(this);
|
|
28
|
-
return instance;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function useGet(arg1, arg2) {
|
|
32
|
-
const instance = this.find(arg1 !== false);
|
|
33
|
-
if (typeof arg1 == "boolean")
|
|
34
|
-
return instance;
|
|
35
|
-
return useSubscriber(instance, arg1, arg2);
|
|
36
|
-
}
|
|
37
|
-
function useSubscriber(source, callback, required) {
|
|
38
|
-
const state = React__default["default"].useState(0);
|
|
39
|
-
const local = React__default["default"].useMemo(() => {
|
|
40
|
-
const refresh = state[1].bind(null, x => x + 1);
|
|
41
|
-
if (!callback)
|
|
42
|
-
return new mvc.Subscriber(source, () => refresh);
|
|
43
|
-
const sub = new mvc.Subscriber(source, () => update);
|
|
44
|
-
const spy = sub.proxy;
|
|
45
|
-
let compute = () => callback.call(spy, spy, forceUpdate);
|
|
46
|
-
function forceUpdate(passthru) {
|
|
47
|
-
if (typeof passthru == "function")
|
|
48
|
-
passthru = passthru();
|
|
49
|
-
if (compute)
|
|
50
|
-
reassign(compute());
|
|
51
|
-
else
|
|
52
|
-
refresh();
|
|
53
|
-
if (passthru)
|
|
54
|
-
return passthru.finally(refresh);
|
|
55
|
-
}
|
|
56
|
-
function reassign(next) {
|
|
57
|
-
value = next;
|
|
58
|
-
if (retry) {
|
|
59
|
-
retry();
|
|
60
|
-
retry = undefined;
|
|
61
|
-
}
|
|
62
|
-
else
|
|
63
|
-
refresh();
|
|
64
|
-
}
|
|
65
|
-
let retry;
|
|
66
|
-
let update;
|
|
67
|
-
let value = compute();
|
|
68
|
-
if (value === null) {
|
|
69
|
-
sub.watch.clear();
|
|
70
|
-
compute = undefined;
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
if (typeof value == "function") {
|
|
74
|
-
const get = value;
|
|
75
|
-
sub.watch.clear();
|
|
76
|
-
callback = () => get();
|
|
77
|
-
value = get();
|
|
78
|
-
}
|
|
79
|
-
if (value instanceof Promise) {
|
|
80
|
-
value.then(reassign);
|
|
81
|
-
value = undefined;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
sub.commit();
|
|
85
|
-
update = () => {
|
|
86
|
-
const next = compute();
|
|
87
|
-
if (notEqual(value, next))
|
|
88
|
-
reassign(next);
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
Object.defineProperty(sub, "proxy", {
|
|
92
|
-
get() {
|
|
93
|
-
if (value !== undefined)
|
|
94
|
-
return value;
|
|
95
|
-
if (required)
|
|
96
|
-
throw new Promise(res => retry = res);
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
return sub;
|
|
101
|
-
}, [source]);
|
|
102
|
-
if (!local)
|
|
103
|
-
return null;
|
|
104
|
-
React__default["default"].useLayoutEffect(() => {
|
|
105
|
-
local.commit();
|
|
106
|
-
return () => local.release();
|
|
107
|
-
}, [source]);
|
|
108
|
-
return local.proxy;
|
|
109
|
-
}
|
|
110
|
-
/** Values are not equal for purposes of a refresh. */
|
|
111
|
-
const notEqual = (a, b) => (b !== a && (!Array.isArray(a) ||
|
|
112
|
-
!Array.isArray(b) ||
|
|
113
|
-
a.length !== b.length ||
|
|
114
|
-
a.some((x, i) => x !== b[i])));
|
|
115
|
-
|
|
116
|
-
const Oops$2 = mvc.issues({
|
|
117
|
-
AmbientRequired: (requested, requester) => `Attempted to find an instance of ${requested} in context. It is required by ${requester}, but one could not be found.`
|
|
118
|
-
});
|
|
13
|
+
const LookupContext = React.createContext(new mvc.Context());
|
|
14
|
+
const useLookup = () => React.useContext(LookupContext);
|
|
119
15
|
const Pending = new WeakMap();
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return (callback) => {
|
|
123
|
-
const item = mvc.Internal.getParent(from, type);
|
|
124
|
-
if (item)
|
|
125
|
-
callback(item);
|
|
126
|
-
else
|
|
127
|
-
getPending(from).push(context => {
|
|
128
|
-
const got = context.get(type);
|
|
129
|
-
if (got)
|
|
130
|
-
callback(got);
|
|
131
|
-
else if (required)
|
|
132
|
-
throw Oops$2.AmbientRequired(type, from);
|
|
133
|
-
});
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
function usePeerContext(subject) {
|
|
137
|
-
if (Applied.has(subject)) {
|
|
138
|
-
if (Applied.get(subject))
|
|
139
|
-
useLookup();
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
const pending = Pending.get(subject);
|
|
143
|
-
if (pending) {
|
|
144
|
-
const local = useLookup();
|
|
145
|
-
for (const init of pending)
|
|
146
|
-
init(local);
|
|
147
|
-
Pending.delete(subject);
|
|
148
|
-
}
|
|
149
|
-
Applied.set(subject, !!pending);
|
|
150
|
-
}
|
|
151
|
-
function getPending(subject) {
|
|
152
|
-
let pending = Pending.get(subject);
|
|
16
|
+
function hasModel(type, relativeTo, callback) {
|
|
17
|
+
let pending = Pending.get(relativeTo);
|
|
153
18
|
if (!pending)
|
|
154
|
-
Pending.set(
|
|
155
|
-
|
|
19
|
+
Pending.set(relativeTo, pending = []);
|
|
20
|
+
pending.push(context => {
|
|
21
|
+
callback(context.get(type));
|
|
22
|
+
});
|
|
156
23
|
}
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const update = React__default["default"].useState(0)[1];
|
|
171
|
-
React__default["default"].useLayoutEffect(() => {
|
|
172
|
-
if (arg1.length && instance instanceof mvc.Model)
|
|
173
|
-
instance.on(arg1, () => update(x => x + 1));
|
|
174
|
-
return () => {
|
|
175
|
-
instance.gc();
|
|
176
|
-
};
|
|
177
|
-
}, []);
|
|
178
|
-
return instance;
|
|
24
|
+
const Applied = new WeakMap();
|
|
25
|
+
function usePeerContext(instance) {
|
|
26
|
+
const applyPeers = Applied.get(instance);
|
|
27
|
+
if (applyPeers)
|
|
28
|
+
useLookup();
|
|
29
|
+
else if (applyPeers === undefined) {
|
|
30
|
+
const pending = Pending.get(instance);
|
|
31
|
+
if (pending) {
|
|
32
|
+
const local = useLookup();
|
|
33
|
+
pending.forEach(init => init(local));
|
|
34
|
+
Pending.delete(instance);
|
|
35
|
+
Applied.set(instance, true);
|
|
36
|
+
}
|
|
179
37
|
}
|
|
180
|
-
const state = React__default["default"].useState(0);
|
|
181
|
-
const local = React__default["default"].useMemo(() => {
|
|
182
|
-
const refresh = state[1].bind(null, x => x + 1);
|
|
183
|
-
return new mvc.Subscriber(instance, () => refresh);
|
|
184
|
-
}, []);
|
|
185
|
-
if (typeof arg1 == "object")
|
|
186
|
-
applyValues(local, arg1, arg2);
|
|
187
|
-
React__default["default"].useLayoutEffect(() => {
|
|
188
|
-
local.commit();
|
|
189
|
-
return () => {
|
|
190
|
-
local.release();
|
|
191
|
-
instance.gc();
|
|
192
|
-
};
|
|
193
|
-
}, []);
|
|
194
|
-
return local.proxy;
|
|
195
|
-
}
|
|
196
|
-
// TODO: move back to MVC pending a generic use-case.
|
|
197
|
-
function applyValues(target, values, keys) {
|
|
198
|
-
const { waiting, subject } = target.parent;
|
|
199
|
-
target.active = false;
|
|
200
|
-
if (!keys)
|
|
201
|
-
keys = Object.getOwnPropertyNames(subject);
|
|
202
|
-
for (const key of keys)
|
|
203
|
-
if (key in values)
|
|
204
|
-
subject[key] = values[key];
|
|
205
|
-
waiting.add(() => target.active = true);
|
|
206
38
|
}
|
|
207
39
|
|
|
208
|
-
const
|
|
40
|
+
const getModel = (type, adapter) => {
|
|
41
|
+
const context = useLookup();
|
|
42
|
+
const state = React.useState(0);
|
|
43
|
+
const hook = React.useMemo(() => {
|
|
44
|
+
return adapter(() => state[1](x => x + 1), cb => cb(context.get(type)));
|
|
45
|
+
}, []);
|
|
46
|
+
if (!hook)
|
|
47
|
+
return null;
|
|
48
|
+
React.useLayoutEffect(hook.mount, []);
|
|
49
|
+
return hook.render();
|
|
50
|
+
};
|
|
51
|
+
const useModel = (adapter) => {
|
|
52
|
+
const state = React.useState(0);
|
|
53
|
+
const hook = React.useMemo(() => (adapter(() => state[1](x => x + 1))), []);
|
|
54
|
+
usePeerContext(hook.instance);
|
|
55
|
+
React.useLayoutEffect(hook.mount, []);
|
|
56
|
+
return hook.render;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const Oops = mvc.issues({
|
|
209
60
|
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
210
61
|
});
|
|
211
62
|
function Consumer(props) {
|
|
212
|
-
const {
|
|
63
|
+
const { children, has, get, for: Type } = props;
|
|
213
64
|
if (typeof children == "function")
|
|
214
|
-
return children(
|
|
65
|
+
return children(Type.get());
|
|
215
66
|
const callback = has || get;
|
|
216
67
|
if (typeof callback == "function")
|
|
217
|
-
callback(
|
|
68
|
+
callback(Type.get(!!has));
|
|
218
69
|
else
|
|
219
|
-
throw Oops
|
|
70
|
+
throw Oops.BadProps();
|
|
220
71
|
return null;
|
|
221
72
|
}
|
|
222
73
|
|
|
223
|
-
const Oops = mvc.issues({
|
|
224
|
-
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
225
|
-
});
|
|
226
74
|
function Provider(props) {
|
|
227
|
-
|
|
75
|
+
let { for: included, use: assign } = props;
|
|
76
|
+
const ambient = useLookup();
|
|
77
|
+
const context = React.useMemo(() => ambient.push(), []);
|
|
78
|
+
if (typeof included == "function" || included instanceof mvc.Model)
|
|
79
|
+
included = { [0]: included };
|
|
80
|
+
context.include(included).forEach((explicit, model) => {
|
|
81
|
+
if (assign && explicit)
|
|
82
|
+
for (const K in assign)
|
|
83
|
+
if (K in model)
|
|
84
|
+
model[K] = assign[K];
|
|
85
|
+
const pending = Pending.get(model);
|
|
86
|
+
if (pending)
|
|
87
|
+
pending.forEach(cb => cb(context));
|
|
88
|
+
});
|
|
89
|
+
React.useLayoutEffect(() => () => context.pop(), []);
|
|
228
90
|
return (React__default["default"].createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
229
91
|
? props.children
|
|
230
92
|
: (React__default["default"].createElement(React.Suspense, { fallback: props.fallback || null }, props.children))));
|
|
231
|
-
}
|
|
232
|
-
function useNewContext(include, assign) {
|
|
233
|
-
const init = new Set();
|
|
234
|
-
const ambient = useLookup();
|
|
235
|
-
const context = React__default["default"].useMemo(() => {
|
|
236
|
-
if (!include)
|
|
237
|
-
throw Oops.NoType();
|
|
238
|
-
return ambient.push();
|
|
239
|
-
}, []);
|
|
240
|
-
function register(key, input) {
|
|
241
|
-
let instance;
|
|
242
|
-
if (context.register.get(key) === input)
|
|
243
|
-
instance = typeof input == "object"
|
|
244
|
-
? input
|
|
245
|
-
: context.get(input);
|
|
246
|
-
else
|
|
247
|
-
instance = context.add(input, key);
|
|
248
|
-
init.add(instance);
|
|
249
|
-
if (assign)
|
|
250
|
-
assignWeak(instance, assign);
|
|
251
|
-
}
|
|
252
|
-
if (typeof include == "function" || include instanceof mvc.Model)
|
|
253
|
-
register(0, include);
|
|
254
|
-
else
|
|
255
|
-
Object.entries(include).forEach(e => register(...e));
|
|
256
|
-
for (const instance of init) {
|
|
257
|
-
mvc.Control.for(instance).state.forEach(value => {
|
|
258
|
-
// TODO: should this run repeatedly?
|
|
259
|
-
if (mvc.Internal.getParent(value) === instance) {
|
|
260
|
-
context.add(value);
|
|
261
|
-
init.add(value);
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
for (const apply of getPending(instance))
|
|
265
|
-
apply(context);
|
|
266
|
-
// TODO: add test to validate this.
|
|
267
|
-
init.delete(instance);
|
|
268
|
-
}
|
|
269
|
-
React__default["default"].useLayoutEffect(() => () => context.pop(), []);
|
|
270
|
-
return context;
|
|
271
|
-
}
|
|
272
|
-
function assignWeak(into, from) {
|
|
273
|
-
for (const K in from)
|
|
274
|
-
if (K in into)
|
|
275
|
-
into[K] = from[K];
|
|
276
93
|
}
|
|
277
94
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
this.use = useModel;
|
|
282
|
-
}
|
|
283
|
-
bootstrap.call(mvc.Model);
|
|
95
|
+
mvc.Control.get = getModel;
|
|
96
|
+
mvc.Control.use = useModel;
|
|
97
|
+
mvc.Control.has = hasModel;
|
|
284
98
|
|
|
99
|
+
Object.defineProperty(exports, 'Control', {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
get: function () { return mvc.Control; }
|
|
102
|
+
});
|
|
285
103
|
Object.defineProperty(exports, 'Debug', {
|
|
286
104
|
enumerable: true,
|
|
287
105
|
get: function () { return mvc.Debug; }
|
|
@@ -290,14 +108,14 @@ Object.defineProperty(exports, 'Model', {
|
|
|
290
108
|
enumerable: true,
|
|
291
109
|
get: function () { return mvc.Model; }
|
|
292
110
|
});
|
|
293
|
-
Object.defineProperty(exports, 'add', {
|
|
294
|
-
enumerable: true,
|
|
295
|
-
get: function () { return mvc.add; }
|
|
296
|
-
});
|
|
297
111
|
Object.defineProperty(exports, 'default', {
|
|
298
112
|
enumerable: true,
|
|
299
113
|
get: function () { return mvc__default["default"]; }
|
|
300
114
|
});
|
|
115
|
+
Object.defineProperty(exports, 'get', {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
get: function () { return mvc.get; }
|
|
118
|
+
});
|
|
301
119
|
Object.defineProperty(exports, 'ref', {
|
|
302
120
|
enumerable: true,
|
|
303
121
|
get: function () { return mvc.ref; }
|
|
@@ -316,4 +134,3 @@ Object.defineProperty(exports, 'use', {
|
|
|
316
134
|
});
|
|
317
135
|
exports.Consumer = Consumer;
|
|
318
136
|
exports.Provider = Provider;
|
|
319
|
-
exports.get = instruction;
|
package/package.json
CHANGED
|
@@ -30,25 +30,17 @@
|
|
|
30
30
|
"postversion": "git push & npm publish"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@expressive/mvc": "^0.
|
|
33
|
+
"@expressive/mvc": "^0.21.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"react": ">=16.8.0 <19.0.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@rollup/plugin-commonjs": "^22.0.0",
|
|
40
39
|
"@testing-library/react-hooks": "^8.0.0",
|
|
41
40
|
"@types/react": "^17.0.11",
|
|
42
41
|
"@types/react-test-renderer": "^18.0.0",
|
|
43
|
-
"jest": "^27.5.1",
|
|
44
42
|
"react": ">=17.0.2",
|
|
45
|
-
"react-test-renderer": "^17.0.2"
|
|
46
|
-
"rollup": "^2.61.1",
|
|
47
|
-
"rollup-plugin-dts": "^4.2.1",
|
|
48
|
-
"rollup-plugin-typescript2": "^0.31.1",
|
|
49
|
-
"ts-jest": "^27.0.3",
|
|
50
|
-
"tslib": "^2.3.0",
|
|
51
|
-
"typescript": "^4.9.5"
|
|
43
|
+
"react-test-renderer": "^17.0.2"
|
|
52
44
|
},
|
|
53
|
-
"version": "0.
|
|
45
|
+
"version": "0.21.0"
|
|
54
46
|
}
|