@expressive/react 0.18.1 → 0.19.1
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 +8 -14
- package/lib/index.esm.js +109 -167
- package/lib/index.js +112 -170
- package/package.json +4 -3
package/lib/index.d.ts
CHANGED
|
@@ -2,12 +2,9 @@ import { Model, get } from '@expressive/mvc';
|
|
|
2
2
|
export { Debug, Model, add, default, ref, run, set, use } from '@expressive/mvc';
|
|
3
3
|
import React from 'react';
|
|
4
4
|
|
|
5
|
-
type Class = new (
|
|
6
|
-
|
|
5
|
+
type Class$1 = new () => any;
|
|
7
6
|
declare namespace Consumer {
|
|
8
|
-
type HasProps<E extends Class> = {
|
|
9
|
-
/** @deprecated - use for instead */
|
|
10
|
-
of?: never;
|
|
7
|
+
type HasProps<E extends Class$1> = {
|
|
11
8
|
/** Type of controller to fetch from context. */
|
|
12
9
|
for: E;
|
|
13
10
|
/**
|
|
@@ -16,31 +13,28 @@ declare namespace Consumer {
|
|
|
16
13
|
*/
|
|
17
14
|
has: (value: InstanceType<E>) => void;
|
|
18
15
|
};
|
|
19
|
-
type GetProps<E extends Class> = {
|
|
20
|
-
/** @deprecated - use for instead */
|
|
21
|
-
of?: never;
|
|
16
|
+
type GetProps<E extends Class$1> = {
|
|
22
17
|
/** Type of controller to fetch from context. */
|
|
23
18
|
for: E;
|
|
24
19
|
/** Getter function. Is called on every natural render of this component. */
|
|
25
20
|
get: (value: InstanceType<E> | undefined) => void;
|
|
26
21
|
};
|
|
27
|
-
type RenderProps<E extends Class> = {
|
|
28
|
-
/** @deprecated - use for instead */
|
|
29
|
-
of?: never;
|
|
22
|
+
type RenderProps<E extends Class$1> = {
|
|
30
23
|
/** Type of controller to fetch from context. */
|
|
31
24
|
for: E;
|
|
32
25
|
/**
|
|
33
26
|
* Render function, will receive instance of desired controller.
|
|
34
27
|
*
|
|
35
|
-
* Similar to `
|
|
28
|
+
* Similar to `get()`, updates to properties accessed in
|
|
36
29
|
* this function will cause a refresh when they change.
|
|
37
30
|
*/
|
|
38
31
|
children: (value: InstanceType<E>) => React.ReactElement<any, any> | null;
|
|
39
32
|
};
|
|
40
|
-
type Props<T extends Class> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
33
|
+
type Props<T extends Class$1> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
41
34
|
}
|
|
42
|
-
declare function Consumer<T extends Class>(props: Consumer.Props<T>): any;
|
|
35
|
+
declare function Consumer<T extends Class$1>(props: Consumer.Props<T>): any;
|
|
43
36
|
|
|
37
|
+
type Class = new () => any;
|
|
44
38
|
declare namespace Provider {
|
|
45
39
|
type Item = Model | Model.New;
|
|
46
40
|
type Multiple<T extends Item = Item> = T[] | {
|
package/lib/index.esm.js
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
import { Register, get, Internal,
|
|
1
|
+
import { issues, Register, Subscriber, get, Internal, Control, Model } from '@expressive/mvc';
|
|
2
2
|
export { Debug, Model, add, default, ref, run, set, use } from '@expressive/mvc';
|
|
3
3
|
import React, { Suspense } from 'react';
|
|
4
4
|
|
|
5
|
-
class Issue extends Error {
|
|
6
|
-
constructor() {
|
|
7
|
-
super(...arguments);
|
|
8
|
-
// drop first line (error factory) in stack trace.
|
|
9
|
-
this.stack = this.stack.replace(/\n.+/, "");
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function issues(register) {
|
|
13
|
-
const Library = {};
|
|
14
|
-
for (const name in register)
|
|
15
|
-
Library[name] = (...args) => new Issue(register[name].apply(null, args));
|
|
16
|
-
return Library;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
5
|
const Oops$3 = issues({
|
|
20
6
|
NotFound: (name) => `Couldn't find ${name} in context; did you forget to use a Provider?`,
|
|
21
7
|
MultipleExist: (name) => `Did find ${name} in context, but multiple were defined by same Provider.`
|
|
@@ -23,22 +9,102 @@ const Oops$3 = issues({
|
|
|
23
9
|
const Global = new Register();
|
|
24
10
|
const LookupContext = React.createContext(Global);
|
|
25
11
|
function useLookup() {
|
|
26
|
-
|
|
27
|
-
return React.useContext(LookupContext);
|
|
28
|
-
}
|
|
29
|
-
catch (err) {
|
|
30
|
-
return Global;
|
|
31
|
-
}
|
|
12
|
+
return React.useContext(LookupContext);
|
|
32
13
|
}
|
|
33
|
-
function useContext(
|
|
34
|
-
const instance = useLookup().get(
|
|
14
|
+
function useContext(required) {
|
|
15
|
+
const instance = useLookup().get(this);
|
|
35
16
|
if (instance === null)
|
|
36
|
-
throw Oops$3.MultipleExist(
|
|
17
|
+
throw Oops$3.MultipleExist(this);
|
|
37
18
|
if (!instance && required !== false)
|
|
38
|
-
throw Oops$3.NotFound(
|
|
19
|
+
throw Oops$3.NotFound(this);
|
|
39
20
|
return instance;
|
|
40
21
|
}
|
|
41
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
|
+
|
|
42
108
|
const Oops$2 = issues({
|
|
43
109
|
AmbientRequired: (requested, requester) => `Attempted to find an instance of ${requested} in context. It is required by ${requester}, but one could not be found.`
|
|
44
110
|
});
|
|
@@ -55,7 +121,7 @@ function getForGetInstruction(type, from, required) {
|
|
|
55
121
|
if (got)
|
|
56
122
|
callback(got);
|
|
57
123
|
else if (required)
|
|
58
|
-
throw Oops$2.AmbientRequired(type
|
|
124
|
+
throw Oops$2.AmbientRequired(type, from);
|
|
59
125
|
});
|
|
60
126
|
};
|
|
61
127
|
}
|
|
@@ -82,12 +148,10 @@ function getPending(subject) {
|
|
|
82
148
|
}
|
|
83
149
|
const instruction = get.using(getForGetInstruction);
|
|
84
150
|
|
|
85
|
-
function useModel(
|
|
151
|
+
function useModel(arg1, arg2) {
|
|
86
152
|
const instance = React.useMemo(() => {
|
|
87
153
|
const callback = arg2 || arg1;
|
|
88
|
-
const instance =
|
|
89
|
-
? new source()
|
|
90
|
-
: source();
|
|
154
|
+
const instance = new this();
|
|
91
155
|
Control.for(instance);
|
|
92
156
|
if (typeof callback == "function")
|
|
93
157
|
callback(instance);
|
|
@@ -100,8 +164,7 @@ function useModel(source, arg1, arg2) {
|
|
|
100
164
|
if (arg1.length && instance instanceof Model)
|
|
101
165
|
instance.on(arg1, () => update(x => x + 1));
|
|
102
166
|
return () => {
|
|
103
|
-
|
|
104
|
-
instance.gc();
|
|
167
|
+
instance.gc();
|
|
105
168
|
};
|
|
106
169
|
}, []);
|
|
107
170
|
return instance;
|
|
@@ -117,8 +180,7 @@ function useModel(source, arg1, arg2) {
|
|
|
117
180
|
local.commit();
|
|
118
181
|
return () => {
|
|
119
182
|
local.release();
|
|
120
|
-
|
|
121
|
-
instance.gc();
|
|
183
|
+
instance.gc();
|
|
122
184
|
};
|
|
123
185
|
}, []);
|
|
124
186
|
return local.proxy;
|
|
@@ -135,133 +197,16 @@ function applyValues(target, values, keys) {
|
|
|
135
197
|
waiting.add(() => target.active = true);
|
|
136
198
|
}
|
|
137
199
|
|
|
138
|
-
const UID_CACHE = new WeakMap();
|
|
139
|
-
function assignWeak(into, from) {
|
|
140
|
-
for (const K in from)
|
|
141
|
-
if (K in into)
|
|
142
|
-
into[K] = from[K];
|
|
143
|
-
}
|
|
144
|
-
function uid(object) {
|
|
145
|
-
let uid = UID_CACHE.get(object);
|
|
146
|
-
if (!uid)
|
|
147
|
-
UID_CACHE.set(object, uid = Math.random());
|
|
148
|
-
return uid;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function useTap(source, arg1, arg2) {
|
|
152
|
-
const instance = typeof source == "object"
|
|
153
|
-
? source
|
|
154
|
-
: "get" in source
|
|
155
|
-
? source.get()
|
|
156
|
-
: source();
|
|
157
|
-
const deps = [uid(instance)];
|
|
158
|
-
const state = React.useState(0);
|
|
159
|
-
const local = React.useMemo(() => {
|
|
160
|
-
const refresh = state[1].bind(null, x => x + 1);
|
|
161
|
-
if (typeof arg1 != "function")
|
|
162
|
-
return new TapSubscriber(instance, () => refresh, arg1);
|
|
163
|
-
const sub = new Subscriber(instance, () => update);
|
|
164
|
-
const spy = sub.proxy;
|
|
165
|
-
let make = () => arg1.call(spy, spy, forceUpdate);
|
|
166
|
-
function forceUpdate(passthru) {
|
|
167
|
-
if (typeof passthru == "function")
|
|
168
|
-
passthru = passthru();
|
|
169
|
-
if (make)
|
|
170
|
-
reassign(make());
|
|
171
|
-
else
|
|
172
|
-
refresh();
|
|
173
|
-
if (passthru)
|
|
174
|
-
return passthru.finally(refresh);
|
|
175
|
-
}
|
|
176
|
-
function reassign(next) {
|
|
177
|
-
value = next;
|
|
178
|
-
if (retry) {
|
|
179
|
-
retry();
|
|
180
|
-
retry = undefined;
|
|
181
|
-
}
|
|
182
|
-
else
|
|
183
|
-
refresh();
|
|
184
|
-
}
|
|
185
|
-
let retry;
|
|
186
|
-
let update;
|
|
187
|
-
let value = make();
|
|
188
|
-
if (value === null) {
|
|
189
|
-
sub.watch.clear();
|
|
190
|
-
make = undefined;
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
if (typeof value == "function") {
|
|
194
|
-
const get = value;
|
|
195
|
-
sub.watch.clear();
|
|
196
|
-
arg1 = () => get();
|
|
197
|
-
value = get();
|
|
198
|
-
}
|
|
199
|
-
if (value instanceof Promise) {
|
|
200
|
-
value.then(reassign);
|
|
201
|
-
value = undefined;
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
sub.commit();
|
|
205
|
-
update = () => {
|
|
206
|
-
const next = make();
|
|
207
|
-
if (notEqual(value, next))
|
|
208
|
-
reassign(next);
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
Object.defineProperty(sub, "proxy", {
|
|
212
|
-
get() {
|
|
213
|
-
if (value !== undefined)
|
|
214
|
-
return value;
|
|
215
|
-
if (arg2)
|
|
216
|
-
throw new Promise(res => retry = res);
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
return sub;
|
|
221
|
-
}, deps);
|
|
222
|
-
if (!local)
|
|
223
|
-
return null;
|
|
224
|
-
React.useLayoutEffect(() => {
|
|
225
|
-
local.commit();
|
|
226
|
-
return () => local.release();
|
|
227
|
-
}, deps);
|
|
228
|
-
return local.proxy;
|
|
229
|
-
}
|
|
230
|
-
class TapSubscriber extends Subscriber {
|
|
231
|
-
constructor(parent, onUpdate, strict) {
|
|
232
|
-
super(parent, onUpdate);
|
|
233
|
-
this.strict = strict;
|
|
234
|
-
}
|
|
235
|
-
get(key, using) {
|
|
236
|
-
try {
|
|
237
|
-
const value = super.get(key, using);
|
|
238
|
-
if (value === undefined && this.strict === true)
|
|
239
|
-
this.parent.waitFor(key);
|
|
240
|
-
return value;
|
|
241
|
-
}
|
|
242
|
-
catch (err) {
|
|
243
|
-
if (err instanceof Promise && this.strict === false)
|
|
244
|
-
return;
|
|
245
|
-
throw err;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
/** Values are not equal for purposes of a refresh. */
|
|
250
|
-
const notEqual = (a, b) => (b !== a && (!Array.isArray(a) ||
|
|
251
|
-
!Array.isArray(b) ||
|
|
252
|
-
a.length !== b.length ||
|
|
253
|
-
a.some((x, i) => x !== b[i])));
|
|
254
|
-
|
|
255
200
|
const Oops$1 = issues({
|
|
256
201
|
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
257
202
|
});
|
|
258
203
|
function Consumer(props) {
|
|
259
204
|
const { get, has, children, for: type } = props;
|
|
260
205
|
if (typeof children == "function")
|
|
261
|
-
return children(
|
|
206
|
+
return children(type.get());
|
|
262
207
|
const callback = has || get;
|
|
263
208
|
if (typeof callback == "function")
|
|
264
|
-
callback(
|
|
209
|
+
callback(type.find(!!has));
|
|
265
210
|
else
|
|
266
211
|
throw Oops$1.BadProps();
|
|
267
212
|
return null;
|
|
@@ -315,21 +260,18 @@ function useNewContext(include, assign) {
|
|
|
315
260
|
}
|
|
316
261
|
React.useLayoutEffect(() => () => context.pop(), []);
|
|
317
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];
|
|
318
268
|
}
|
|
319
269
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
},
|
|
327
|
-
tap(arg1, arg2) {
|
|
328
|
-
return useTap(this, arg1, arg2);
|
|
329
|
-
},
|
|
330
|
-
meta(arg1, arg2) {
|
|
331
|
-
return useTap(() => this, arg1, arg2);
|
|
332
|
-
}
|
|
333
|
-
});
|
|
270
|
+
function bootstrap() {
|
|
271
|
+
this.find = useContext;
|
|
272
|
+
this.get = useGet;
|
|
273
|
+
this.use = useModel;
|
|
274
|
+
}
|
|
275
|
+
bootstrap.call(Model);
|
|
334
276
|
|
|
335
277
|
export { Consumer, Provider, instruction as get };
|
package/lib/index.js
CHANGED
|
@@ -10,44 +10,110 @@ 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
|
-
|
|
14
|
-
constructor() {
|
|
15
|
-
super(...arguments);
|
|
16
|
-
// drop first line (error factory) in stack trace.
|
|
17
|
-
this.stack = this.stack.replace(/\n.+/, "");
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function issues(register) {
|
|
21
|
-
const Library = {};
|
|
22
|
-
for (const name in register)
|
|
23
|
-
Library[name] = (...args) => new Issue(register[name].apply(null, args));
|
|
24
|
-
return Library;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const Oops$3 = issues({
|
|
13
|
+
const Oops$3 = mvc.issues({
|
|
28
14
|
NotFound: (name) => `Couldn't find ${name} in context; did you forget to use a Provider?`,
|
|
29
15
|
MultipleExist: (name) => `Did find ${name} in context, but multiple were defined by same Provider.`
|
|
30
16
|
});
|
|
31
17
|
const Global = new mvc.Register();
|
|
32
18
|
const LookupContext = React__default["default"].createContext(Global);
|
|
33
19
|
function useLookup() {
|
|
34
|
-
|
|
35
|
-
return React__default["default"].useContext(LookupContext);
|
|
36
|
-
}
|
|
37
|
-
catch (err) {
|
|
38
|
-
return Global;
|
|
39
|
-
}
|
|
20
|
+
return React__default["default"].useContext(LookupContext);
|
|
40
21
|
}
|
|
41
|
-
function useContext(
|
|
42
|
-
const instance = useLookup().get(
|
|
22
|
+
function useContext(required) {
|
|
23
|
+
const instance = useLookup().get(this);
|
|
43
24
|
if (instance === null)
|
|
44
|
-
throw Oops$3.MultipleExist(
|
|
25
|
+
throw Oops$3.MultipleExist(this);
|
|
45
26
|
if (!instance && required !== false)
|
|
46
|
-
throw Oops$3.NotFound(
|
|
27
|
+
throw Oops$3.NotFound(this);
|
|
47
28
|
return instance;
|
|
48
29
|
}
|
|
49
30
|
|
|
50
|
-
|
|
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({
|
|
51
117
|
AmbientRequired: (requested, requester) => `Attempted to find an instance of ${requested} in context. It is required by ${requester}, but one could not be found.`
|
|
52
118
|
});
|
|
53
119
|
const Pending = new WeakMap();
|
|
@@ -63,7 +129,7 @@ function getForGetInstruction(type, from, required) {
|
|
|
63
129
|
if (got)
|
|
64
130
|
callback(got);
|
|
65
131
|
else if (required)
|
|
66
|
-
throw Oops$2.AmbientRequired(type
|
|
132
|
+
throw Oops$2.AmbientRequired(type, from);
|
|
67
133
|
});
|
|
68
134
|
};
|
|
69
135
|
}
|
|
@@ -90,12 +156,10 @@ function getPending(subject) {
|
|
|
90
156
|
}
|
|
91
157
|
const instruction = mvc.get.using(getForGetInstruction);
|
|
92
158
|
|
|
93
|
-
function useModel(
|
|
159
|
+
function useModel(arg1, arg2) {
|
|
94
160
|
const instance = React__default["default"].useMemo(() => {
|
|
95
161
|
const callback = arg2 || arg1;
|
|
96
|
-
const instance =
|
|
97
|
-
? new source()
|
|
98
|
-
: source();
|
|
162
|
+
const instance = new this();
|
|
99
163
|
mvc.Control.for(instance);
|
|
100
164
|
if (typeof callback == "function")
|
|
101
165
|
callback(instance);
|
|
@@ -108,8 +172,7 @@ function useModel(source, arg1, arg2) {
|
|
|
108
172
|
if (arg1.length && instance instanceof mvc.Model)
|
|
109
173
|
instance.on(arg1, () => update(x => x + 1));
|
|
110
174
|
return () => {
|
|
111
|
-
|
|
112
|
-
instance.gc();
|
|
175
|
+
instance.gc();
|
|
113
176
|
};
|
|
114
177
|
}, []);
|
|
115
178
|
return instance;
|
|
@@ -125,8 +188,7 @@ function useModel(source, arg1, arg2) {
|
|
|
125
188
|
local.commit();
|
|
126
189
|
return () => {
|
|
127
190
|
local.release();
|
|
128
|
-
|
|
129
|
-
instance.gc();
|
|
191
|
+
instance.gc();
|
|
130
192
|
};
|
|
131
193
|
}, []);
|
|
132
194
|
return local.proxy;
|
|
@@ -143,139 +205,22 @@ function applyValues(target, values, keys) {
|
|
|
143
205
|
waiting.add(() => target.active = true);
|
|
144
206
|
}
|
|
145
207
|
|
|
146
|
-
const
|
|
147
|
-
function assignWeak(into, from) {
|
|
148
|
-
for (const K in from)
|
|
149
|
-
if (K in into)
|
|
150
|
-
into[K] = from[K];
|
|
151
|
-
}
|
|
152
|
-
function uid(object) {
|
|
153
|
-
let uid = UID_CACHE.get(object);
|
|
154
|
-
if (!uid)
|
|
155
|
-
UID_CACHE.set(object, uid = Math.random());
|
|
156
|
-
return uid;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function useTap(source, arg1, arg2) {
|
|
160
|
-
const instance = typeof source == "object"
|
|
161
|
-
? source
|
|
162
|
-
: "get" in source
|
|
163
|
-
? source.get()
|
|
164
|
-
: source();
|
|
165
|
-
const deps = [uid(instance)];
|
|
166
|
-
const state = React__default["default"].useState(0);
|
|
167
|
-
const local = React__default["default"].useMemo(() => {
|
|
168
|
-
const refresh = state[1].bind(null, x => x + 1);
|
|
169
|
-
if (typeof arg1 != "function")
|
|
170
|
-
return new TapSubscriber(instance, () => refresh, arg1);
|
|
171
|
-
const sub = new mvc.Subscriber(instance, () => update);
|
|
172
|
-
const spy = sub.proxy;
|
|
173
|
-
let make = () => arg1.call(spy, spy, forceUpdate);
|
|
174
|
-
function forceUpdate(passthru) {
|
|
175
|
-
if (typeof passthru == "function")
|
|
176
|
-
passthru = passthru();
|
|
177
|
-
if (make)
|
|
178
|
-
reassign(make());
|
|
179
|
-
else
|
|
180
|
-
refresh();
|
|
181
|
-
if (passthru)
|
|
182
|
-
return passthru.finally(refresh);
|
|
183
|
-
}
|
|
184
|
-
function reassign(next) {
|
|
185
|
-
value = next;
|
|
186
|
-
if (retry) {
|
|
187
|
-
retry();
|
|
188
|
-
retry = undefined;
|
|
189
|
-
}
|
|
190
|
-
else
|
|
191
|
-
refresh();
|
|
192
|
-
}
|
|
193
|
-
let retry;
|
|
194
|
-
let update;
|
|
195
|
-
let value = make();
|
|
196
|
-
if (value === null) {
|
|
197
|
-
sub.watch.clear();
|
|
198
|
-
make = undefined;
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
if (typeof value == "function") {
|
|
202
|
-
const get = value;
|
|
203
|
-
sub.watch.clear();
|
|
204
|
-
arg1 = () => get();
|
|
205
|
-
value = get();
|
|
206
|
-
}
|
|
207
|
-
if (value instanceof Promise) {
|
|
208
|
-
value.then(reassign);
|
|
209
|
-
value = undefined;
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
sub.commit();
|
|
213
|
-
update = () => {
|
|
214
|
-
const next = make();
|
|
215
|
-
if (notEqual(value, next))
|
|
216
|
-
reassign(next);
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
Object.defineProperty(sub, "proxy", {
|
|
220
|
-
get() {
|
|
221
|
-
if (value !== undefined)
|
|
222
|
-
return value;
|
|
223
|
-
if (arg2)
|
|
224
|
-
throw new Promise(res => retry = res);
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
return sub;
|
|
229
|
-
}, deps);
|
|
230
|
-
if (!local)
|
|
231
|
-
return null;
|
|
232
|
-
React__default["default"].useLayoutEffect(() => {
|
|
233
|
-
local.commit();
|
|
234
|
-
return () => local.release();
|
|
235
|
-
}, deps);
|
|
236
|
-
return local.proxy;
|
|
237
|
-
}
|
|
238
|
-
class TapSubscriber extends mvc.Subscriber {
|
|
239
|
-
constructor(parent, onUpdate, strict) {
|
|
240
|
-
super(parent, onUpdate);
|
|
241
|
-
this.strict = strict;
|
|
242
|
-
}
|
|
243
|
-
get(key, using) {
|
|
244
|
-
try {
|
|
245
|
-
const value = super.get(key, using);
|
|
246
|
-
if (value === undefined && this.strict === true)
|
|
247
|
-
this.parent.waitFor(key);
|
|
248
|
-
return value;
|
|
249
|
-
}
|
|
250
|
-
catch (err) {
|
|
251
|
-
if (err instanceof Promise && this.strict === false)
|
|
252
|
-
return;
|
|
253
|
-
throw err;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
/** Values are not equal for purposes of a refresh. */
|
|
258
|
-
const notEqual = (a, b) => (b !== a && (!Array.isArray(a) ||
|
|
259
|
-
!Array.isArray(b) ||
|
|
260
|
-
a.length !== b.length ||
|
|
261
|
-
a.some((x, i) => x !== b[i])));
|
|
262
|
-
|
|
263
|
-
const Oops$1 = issues({
|
|
208
|
+
const Oops$1 = mvc.issues({
|
|
264
209
|
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
265
210
|
});
|
|
266
211
|
function Consumer(props) {
|
|
267
212
|
const { get, has, children, for: type } = props;
|
|
268
213
|
if (typeof children == "function")
|
|
269
|
-
return children(
|
|
214
|
+
return children(type.get());
|
|
270
215
|
const callback = has || get;
|
|
271
216
|
if (typeof callback == "function")
|
|
272
|
-
callback(
|
|
217
|
+
callback(type.find(!!has));
|
|
273
218
|
else
|
|
274
219
|
throw Oops$1.BadProps();
|
|
275
220
|
return null;
|
|
276
221
|
}
|
|
277
222
|
|
|
278
|
-
const Oops = issues({
|
|
223
|
+
const Oops = mvc.issues({
|
|
279
224
|
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
280
225
|
});
|
|
281
226
|
function Provider(props) {
|
|
@@ -323,22 +268,19 @@ function useNewContext(include, assign) {
|
|
|
323
268
|
}
|
|
324
269
|
React__default["default"].useLayoutEffect(() => () => context.pop(), []);
|
|
325
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];
|
|
326
276
|
}
|
|
327
277
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
},
|
|
335
|
-
tap(arg1, arg2) {
|
|
336
|
-
return useTap(this, arg1, arg2);
|
|
337
|
-
},
|
|
338
|
-
meta(arg1, arg2) {
|
|
339
|
-
return useTap(() => this, arg1, arg2);
|
|
340
|
-
}
|
|
341
|
-
});
|
|
278
|
+
function bootstrap() {
|
|
279
|
+
this.find = useContext;
|
|
280
|
+
this.get = useGet;
|
|
281
|
+
this.use = useModel;
|
|
282
|
+
}
|
|
283
|
+
bootstrap.call(mvc.Model);
|
|
342
284
|
|
|
343
285
|
Object.defineProperty(exports, 'Debug', {
|
|
344
286
|
enumerable: true,
|
package/package.json
CHANGED
|
@@ -29,12 +29,13 @@
|
|
|
29
29
|
"preversion": "npm run test && npm run build",
|
|
30
30
|
"postversion": "git push & npm publish"
|
|
31
31
|
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@expressive/mvc": "^0.19.0"
|
|
34
|
+
},
|
|
32
35
|
"peerDependencies": {
|
|
33
|
-
"@expressive/mvc": "^0.17.1",
|
|
34
36
|
"react": ">=16.8.0 <19.0.0"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
|
-
"@expressive/mvc": "^0.18.1",
|
|
38
39
|
"@rollup/plugin-commonjs": "^22.0.0",
|
|
39
40
|
"@testing-library/react-hooks": "^8.0.0",
|
|
40
41
|
"@types/react": "^17.0.11",
|
|
@@ -49,5 +50,5 @@
|
|
|
49
50
|
"tslib": "^2.3.0",
|
|
50
51
|
"typescript": "^4.9.5"
|
|
51
52
|
},
|
|
52
|
-
"version": "0.
|
|
53
|
+
"version": "0.19.1"
|
|
53
54
|
}
|