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