@expressive/react 0.19.2 → 0.20.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 +73 -240
- package/lib/index.js +77 -241
- 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,212 +1,63 @@
|
|
|
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
|
|
|
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
|
+
|
|
200
51
|
const Oops$1 = 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
62
|
throw Oops$1.BadProps();
|
|
212
63
|
return null;
|
|
@@ -216,62 +67,44 @@ const Oops = issues({
|
|
|
216
67
|
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
217
68
|
});
|
|
218
69
|
function Provider(props) {
|
|
219
|
-
|
|
220
|
-
return (React.createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
221
|
-
? props.children
|
|
222
|
-
: (React.createElement(Suspense, { fallback: props.fallback || null }, props.children))));
|
|
223
|
-
}
|
|
224
|
-
function useNewContext(include, assign) {
|
|
70
|
+
let { for: include, use: assign } = props;
|
|
225
71
|
const init = new Set();
|
|
226
72
|
const ambient = useLookup();
|
|
227
|
-
const context =
|
|
228
|
-
if (
|
|
229
|
-
|
|
230
|
-
|
|
73
|
+
const context = useMemo(() => {
|
|
74
|
+
if (include)
|
|
75
|
+
return ambient.push();
|
|
76
|
+
throw Oops.NoType();
|
|
231
77
|
}, []);
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
? input
|
|
237
|
-
: context.get(input);
|
|
238
|
-
else
|
|
239
|
-
instance = context.add(input, key);
|
|
78
|
+
if (typeof include == "function" || include instanceof Model)
|
|
79
|
+
include = { [0]: include };
|
|
80
|
+
for (const key in include) {
|
|
81
|
+
const instance = context.add(include[key], key);
|
|
240
82
|
init.add(instance);
|
|
241
83
|
if (assign)
|
|
242
|
-
|
|
84
|
+
for (const K in assign)
|
|
85
|
+
if (K in instance)
|
|
86
|
+
instance[K] = assign[K];
|
|
243
87
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (Internal.getParent(value) === instance) {
|
|
88
|
+
for (const model of init) {
|
|
89
|
+
const control = Control.for(model, true);
|
|
90
|
+
const pending = Pending.get(model);
|
|
91
|
+
if (pending)
|
|
92
|
+
pending.forEach(cb => cb(context));
|
|
93
|
+
Object.values(control.state).forEach(value => {
|
|
94
|
+
if (value instanceof Model && Control.for(value).parent === model) {
|
|
252
95
|
context.add(value);
|
|
253
96
|
init.add(value);
|
|
254
97
|
}
|
|
255
98
|
});
|
|
256
|
-
for (const apply of getPending(instance))
|
|
257
|
-
apply(context);
|
|
258
|
-
// TODO: add test to validate this.
|
|
259
|
-
init.delete(instance);
|
|
260
99
|
}
|
|
261
|
-
|
|
262
|
-
return context
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
for (const K in from)
|
|
266
|
-
if (K in into)
|
|
267
|
-
into[K] = from[K];
|
|
100
|
+
useLayoutEffect(() => () => context.pop(), []);
|
|
101
|
+
return (React.createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
102
|
+
? props.children
|
|
103
|
+
: (React.createElement(Suspense, { fallback: props.fallback || null }, props.children))));
|
|
268
104
|
}
|
|
269
105
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
this.use = useModel;
|
|
274
|
-
}
|
|
275
|
-
bootstrap.call(Model);
|
|
106
|
+
Control.get = getModel;
|
|
107
|
+
Control.use = useModel;
|
|
108
|
+
Control.has = hasModel;
|
|
276
109
|
|
|
277
|
-
export { Consumer, Provider
|
|
110
|
+
export { Consumer, Provider };
|
package/lib/index.js
CHANGED
|
@@ -10,211 +10,62 @@ 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
|
|
|
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
|
+
|
|
208
59
|
const Oops$1 = 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
70
|
throw Oops$1.BadProps();
|
|
220
71
|
return null;
|
|
@@ -224,64 +75,50 @@ const Oops = mvc.issues({
|
|
|
224
75
|
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
225
76
|
});
|
|
226
77
|
function Provider(props) {
|
|
227
|
-
|
|
228
|
-
return (React__default["default"].createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
229
|
-
? props.children
|
|
230
|
-
: (React__default["default"].createElement(React.Suspense, { fallback: props.fallback || null }, props.children))));
|
|
231
|
-
}
|
|
232
|
-
function useNewContext(include, assign) {
|
|
78
|
+
let { for: include, use: assign } = props;
|
|
233
79
|
const init = new Set();
|
|
234
80
|
const ambient = useLookup();
|
|
235
|
-
const context =
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
|
|
81
|
+
const context = React.useMemo(() => {
|
|
82
|
+
if (include)
|
|
83
|
+
return ambient.push();
|
|
84
|
+
throw Oops.NoType();
|
|
239
85
|
}, []);
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
? input
|
|
245
|
-
: context.get(input);
|
|
246
|
-
else
|
|
247
|
-
instance = context.add(input, key);
|
|
86
|
+
if (typeof include == "function" || include instanceof mvc.Model)
|
|
87
|
+
include = { [0]: include };
|
|
88
|
+
for (const key in include) {
|
|
89
|
+
const instance = context.add(include[key], key);
|
|
248
90
|
init.add(instance);
|
|
249
91
|
if (assign)
|
|
250
|
-
|
|
92
|
+
for (const K in assign)
|
|
93
|
+
if (K in instance)
|
|
94
|
+
instance[K] = assign[K];
|
|
251
95
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if (mvc.Internal.getParent(value) === instance) {
|
|
96
|
+
for (const model of init) {
|
|
97
|
+
const control = mvc.Control.for(model, true);
|
|
98
|
+
const pending = Pending.get(model);
|
|
99
|
+
if (pending)
|
|
100
|
+
pending.forEach(cb => cb(context));
|
|
101
|
+
Object.values(control.state).forEach(value => {
|
|
102
|
+
if (value instanceof mvc.Model && mvc.Control.for(value).parent === model) {
|
|
260
103
|
context.add(value);
|
|
261
104
|
init.add(value);
|
|
262
105
|
}
|
|
263
106
|
});
|
|
264
|
-
for (const apply of getPending(instance))
|
|
265
|
-
apply(context);
|
|
266
|
-
// TODO: add test to validate this.
|
|
267
|
-
init.delete(instance);
|
|
268
107
|
}
|
|
269
|
-
|
|
270
|
-
return context
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
for (const K in from)
|
|
274
|
-
if (K in into)
|
|
275
|
-
into[K] = from[K];
|
|
108
|
+
React.useLayoutEffect(() => () => context.pop(), []);
|
|
109
|
+
return (React__default["default"].createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
110
|
+
? props.children
|
|
111
|
+
: (React__default["default"].createElement(React.Suspense, { fallback: props.fallback || null }, props.children))));
|
|
276
112
|
}
|
|
277
113
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
this.use = useModel;
|
|
282
|
-
}
|
|
283
|
-
bootstrap.call(mvc.Model);
|
|
114
|
+
mvc.Control.get = getModel;
|
|
115
|
+
mvc.Control.use = useModel;
|
|
116
|
+
mvc.Control.has = hasModel;
|
|
284
117
|
|
|
118
|
+
Object.defineProperty(exports, 'Control', {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
get: function () { return mvc.Control; }
|
|
121
|
+
});
|
|
285
122
|
Object.defineProperty(exports, 'Debug', {
|
|
286
123
|
enumerable: true,
|
|
287
124
|
get: function () { return mvc.Debug; }
|
|
@@ -290,14 +127,14 @@ Object.defineProperty(exports, 'Model', {
|
|
|
290
127
|
enumerable: true,
|
|
291
128
|
get: function () { return mvc.Model; }
|
|
292
129
|
});
|
|
293
|
-
Object.defineProperty(exports, 'add', {
|
|
294
|
-
enumerable: true,
|
|
295
|
-
get: function () { return mvc.add; }
|
|
296
|
-
});
|
|
297
130
|
Object.defineProperty(exports, 'default', {
|
|
298
131
|
enumerable: true,
|
|
299
132
|
get: function () { return mvc__default["default"]; }
|
|
300
133
|
});
|
|
134
|
+
Object.defineProperty(exports, 'get', {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
get: function () { return mvc.get; }
|
|
137
|
+
});
|
|
301
138
|
Object.defineProperty(exports, 'ref', {
|
|
302
139
|
enumerable: true,
|
|
303
140
|
get: function () { return mvc.ref; }
|
|
@@ -316,4 +153,3 @@ Object.defineProperty(exports, 'use', {
|
|
|
316
153
|
});
|
|
317
154
|
exports.Consumer = Consumer;
|
|
318
155
|
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.20.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.20.0"
|
|
54
46
|
}
|