@expressive/react 0.18.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 +69 -0
- package/lib/index.esm.js +336 -0
- package/lib/index.js +354 -0
- package/package.json +53 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { get, Model } from '@expressive/mvc';
|
|
2
|
+
export * from '@expressive/mvc';
|
|
3
|
+
export { Model } from '@expressive/mvc';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
|
|
6
|
+
declare const instruction: typeof get;
|
|
7
|
+
|
|
8
|
+
type Class = new (...args: any[]) => any;
|
|
9
|
+
|
|
10
|
+
declare namespace Consumer {
|
|
11
|
+
type HasProps<E extends Class> = {
|
|
12
|
+
/** @deprecated - use for instead */
|
|
13
|
+
of?: never;
|
|
14
|
+
/** Type of controller to fetch from context. */
|
|
15
|
+
for: E;
|
|
16
|
+
/**
|
|
17
|
+
* Getter function. Is called on every natural render of this component.
|
|
18
|
+
* Will throw if usable instance cannot be found in context.
|
|
19
|
+
*/
|
|
20
|
+
has: (value: InstanceType<E>) => void;
|
|
21
|
+
};
|
|
22
|
+
type GetProps<E extends Class> = {
|
|
23
|
+
/** @deprecated - use for instead */
|
|
24
|
+
of?: never;
|
|
25
|
+
/** Type of controller to fetch from context. */
|
|
26
|
+
for: E;
|
|
27
|
+
/** Getter function. Is called on every natural render of this component. */
|
|
28
|
+
get: (value: InstanceType<E> | undefined) => void;
|
|
29
|
+
};
|
|
30
|
+
type RenderProps<E extends Class> = {
|
|
31
|
+
/** @deprecated - use for instead */
|
|
32
|
+
of?: never;
|
|
33
|
+
/** Type of controller to fetch from context. */
|
|
34
|
+
for: E;
|
|
35
|
+
/**
|
|
36
|
+
* Render function, will receive instance of desired controller.
|
|
37
|
+
*
|
|
38
|
+
* Similar to `tap()`, updates to properties accessed in
|
|
39
|
+
* this function will cause a refresh when they change.
|
|
40
|
+
*/
|
|
41
|
+
children: (value: InstanceType<E>) => React.ReactElement<any, any> | null;
|
|
42
|
+
};
|
|
43
|
+
type Props<T extends Class> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
44
|
+
}
|
|
45
|
+
declare function Consumer<T extends Class>(props: Consumer.Props<T>): any;
|
|
46
|
+
|
|
47
|
+
declare namespace Provider {
|
|
48
|
+
type Item = Model | Model.New;
|
|
49
|
+
type Multiple<T extends Item = Item> = T[] | {
|
|
50
|
+
[key: string]: T;
|
|
51
|
+
};
|
|
52
|
+
type Instance<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
|
|
53
|
+
type NormalProps<E, I = Instance<E>> = {
|
|
54
|
+
for: E;
|
|
55
|
+
fallback?: React.ReactNode;
|
|
56
|
+
children?: React.ReactNode | ((instance: I) => React.ReactNode);
|
|
57
|
+
use?: Model.Compat<I>;
|
|
58
|
+
};
|
|
59
|
+
type MultipleProps<T extends Item> = {
|
|
60
|
+
for: Multiple<T>;
|
|
61
|
+
fallback?: React.ReactNode;
|
|
62
|
+
children?: React.ReactNode;
|
|
63
|
+
use?: Model.Compat<Instance<T>>;
|
|
64
|
+
};
|
|
65
|
+
type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
|
|
66
|
+
}
|
|
67
|
+
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): JSX.Element;
|
|
68
|
+
|
|
69
|
+
export { Consumer, Provider, instruction as get };
|
package/lib/index.esm.js
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { Register, get, Internal, Model, Control, Subscriber } from '@expressive/mvc';
|
|
2
|
+
export * from '@expressive/mvc';
|
|
3
|
+
export { Model } from '@expressive/mvc';
|
|
4
|
+
import React, { Suspense } from 'react';
|
|
5
|
+
|
|
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
|
+
const Oops$3 = issues({
|
|
21
|
+
NotFound: (name) => `Couldn't find ${name} in context; did you forget to use a Provider?`,
|
|
22
|
+
MultipleExist: (name) => `Did find ${name} in context, but multiple were defined by same Provider.`
|
|
23
|
+
});
|
|
24
|
+
const Global = new Register();
|
|
25
|
+
const LookupContext = React.createContext(Global);
|
|
26
|
+
function useLookup() {
|
|
27
|
+
try {
|
|
28
|
+
return React.useContext(LookupContext);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
return Global;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function useContext(Type, required) {
|
|
35
|
+
const instance = useLookup().get(Type);
|
|
36
|
+
if (instance === null)
|
|
37
|
+
throw Oops$3.MultipleExist(Type.name);
|
|
38
|
+
if (!instance && required !== false)
|
|
39
|
+
throw Oops$3.NotFound(Type.name);
|
|
40
|
+
return instance;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const Oops$2 = issues({
|
|
44
|
+
AmbientRequired: (requested, requester) => `Attempted to find an instance of ${requested} in context. It is required by ${requester}, but one could not be found.`
|
|
45
|
+
});
|
|
46
|
+
const Pending = new WeakMap();
|
|
47
|
+
const Applied = new WeakMap();
|
|
48
|
+
function getForGetInstruction(type, from, required) {
|
|
49
|
+
return (callback) => {
|
|
50
|
+
const item = Internal.getParent(from, type);
|
|
51
|
+
if (item)
|
|
52
|
+
callback(item);
|
|
53
|
+
else
|
|
54
|
+
getPending(from).push(context => {
|
|
55
|
+
const got = context.get(type);
|
|
56
|
+
if (got)
|
|
57
|
+
callback(got);
|
|
58
|
+
else if (required)
|
|
59
|
+
throw Oops$2.AmbientRequired(type.name, from);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function usePeerContext(subject) {
|
|
64
|
+
if (Applied.has(subject)) {
|
|
65
|
+
if (Applied.get(subject))
|
|
66
|
+
useLookup();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const pending = Pending.get(subject);
|
|
70
|
+
if (pending) {
|
|
71
|
+
const local = useLookup();
|
|
72
|
+
for (const init of pending)
|
|
73
|
+
init(local);
|
|
74
|
+
Pending.delete(subject);
|
|
75
|
+
}
|
|
76
|
+
Applied.set(subject, !!pending);
|
|
77
|
+
}
|
|
78
|
+
function getPending(subject) {
|
|
79
|
+
let pending = Pending.get(subject);
|
|
80
|
+
if (!pending)
|
|
81
|
+
Pending.set(subject, pending = []);
|
|
82
|
+
return pending;
|
|
83
|
+
}
|
|
84
|
+
const instruction = get.using(getForGetInstruction);
|
|
85
|
+
|
|
86
|
+
function useModel(source, arg1, arg2) {
|
|
87
|
+
const instance = React.useMemo(() => {
|
|
88
|
+
const callback = arg2 || arg1;
|
|
89
|
+
const instance = Model.isTypeof(source)
|
|
90
|
+
? new source()
|
|
91
|
+
: source();
|
|
92
|
+
Control.for(instance);
|
|
93
|
+
if (typeof callback == "function")
|
|
94
|
+
callback(instance);
|
|
95
|
+
return instance;
|
|
96
|
+
}, []);
|
|
97
|
+
usePeerContext(instance);
|
|
98
|
+
if (Array.isArray(arg1)) {
|
|
99
|
+
const update = React.useState(0)[1];
|
|
100
|
+
React.useLayoutEffect(() => {
|
|
101
|
+
if (arg1.length && instance instanceof Model)
|
|
102
|
+
instance.on(arg1, () => update(x => x + 1));
|
|
103
|
+
return () => {
|
|
104
|
+
if (Model.isTypeof(source))
|
|
105
|
+
instance.gc();
|
|
106
|
+
};
|
|
107
|
+
}, []);
|
|
108
|
+
return instance;
|
|
109
|
+
}
|
|
110
|
+
const state = React.useState(0);
|
|
111
|
+
const local = React.useMemo(() => {
|
|
112
|
+
const refresh = state[1].bind(null, x => x + 1);
|
|
113
|
+
return new Subscriber(instance, () => refresh);
|
|
114
|
+
}, []);
|
|
115
|
+
if (typeof arg1 == "object")
|
|
116
|
+
applyValues(local, arg1, arg2);
|
|
117
|
+
React.useLayoutEffect(() => {
|
|
118
|
+
local.commit();
|
|
119
|
+
return () => {
|
|
120
|
+
local.release();
|
|
121
|
+
if (Model.isTypeof(source))
|
|
122
|
+
instance.gc();
|
|
123
|
+
};
|
|
124
|
+
}, []);
|
|
125
|
+
return local.proxy;
|
|
126
|
+
}
|
|
127
|
+
// TODO: move back to MVC pending a generic use-case.
|
|
128
|
+
function applyValues(target, values, keys) {
|
|
129
|
+
const { waiting, subject } = target.parent;
|
|
130
|
+
target.active = false;
|
|
131
|
+
if (!keys)
|
|
132
|
+
keys = Object.getOwnPropertyNames(subject);
|
|
133
|
+
for (const key of keys)
|
|
134
|
+
if (key in values)
|
|
135
|
+
subject[key] = values[key];
|
|
136
|
+
waiting.add(() => target.active = true);
|
|
137
|
+
}
|
|
138
|
+
|
|
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
|
+
const Oops$1 = issues({
|
|
257
|
+
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
258
|
+
});
|
|
259
|
+
function Consumer(props) {
|
|
260
|
+
const { get, has, children, for: type } = props;
|
|
261
|
+
if (typeof children == "function")
|
|
262
|
+
return children(useTap(type));
|
|
263
|
+
const callback = has || get;
|
|
264
|
+
if (typeof callback == "function")
|
|
265
|
+
callback(useContext(type, !!has));
|
|
266
|
+
else
|
|
267
|
+
throw Oops$1.BadProps();
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const Oops = issues({
|
|
272
|
+
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
273
|
+
});
|
|
274
|
+
function Provider(props) {
|
|
275
|
+
const context = useNewContext(props.for, props.use);
|
|
276
|
+
return (React.createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
277
|
+
? props.children
|
|
278
|
+
: (React.createElement(Suspense, { fallback: props.fallback || null }, props.children))));
|
|
279
|
+
}
|
|
280
|
+
function useNewContext(include, assign) {
|
|
281
|
+
const init = new Set();
|
|
282
|
+
const ambient = useLookup();
|
|
283
|
+
const context = React.useMemo(() => {
|
|
284
|
+
if (!include)
|
|
285
|
+
throw Oops.NoType();
|
|
286
|
+
return ambient.push();
|
|
287
|
+
}, []);
|
|
288
|
+
function register(key, input) {
|
|
289
|
+
let instance;
|
|
290
|
+
if (context.register.get(key) === input)
|
|
291
|
+
instance = typeof input == "object"
|
|
292
|
+
? input
|
|
293
|
+
: context.get(input);
|
|
294
|
+
else
|
|
295
|
+
instance = context.add(input, key);
|
|
296
|
+
init.add(instance);
|
|
297
|
+
if (assign)
|
|
298
|
+
assignWeak(instance, assign);
|
|
299
|
+
}
|
|
300
|
+
if (typeof include == "function" || include instanceof Model)
|
|
301
|
+
register(0, include);
|
|
302
|
+
else
|
|
303
|
+
Object.entries(include).forEach(e => register(...e));
|
|
304
|
+
for (const instance of init) {
|
|
305
|
+
Control.for(instance).state.forEach(value => {
|
|
306
|
+
// TODO: should this run repeatedly?
|
|
307
|
+
if (Internal.getParent(value) === instance) {
|
|
308
|
+
context.add(value);
|
|
309
|
+
init.add(value);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
for (const apply of getPending(instance))
|
|
313
|
+
apply(context);
|
|
314
|
+
// TODO: add test to validate this.
|
|
315
|
+
init.delete(instance);
|
|
316
|
+
}
|
|
317
|
+
React.useLayoutEffect(() => () => context.pop(), []);
|
|
318
|
+
return context;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
Object.assign(Model, {
|
|
322
|
+
use(arg1, arg2) {
|
|
323
|
+
return useModel(this, arg1, arg2);
|
|
324
|
+
},
|
|
325
|
+
get(required) {
|
|
326
|
+
return useContext(this, required);
|
|
327
|
+
},
|
|
328
|
+
tap(arg1, arg2) {
|
|
329
|
+
return useTap(this, arg1, arg2);
|
|
330
|
+
},
|
|
331
|
+
meta(arg1, arg2) {
|
|
332
|
+
return useTap(() => this, arg1, arg2);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
export { Consumer, Provider, instruction as get };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var mvc = require('@expressive/mvc');
|
|
6
|
+
var React = require('react');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
11
|
+
|
|
12
|
+
class Issue extends Error {
|
|
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({
|
|
27
|
+
NotFound: (name) => `Couldn't find ${name} in context; did you forget to use a Provider?`,
|
|
28
|
+
MultipleExist: (name) => `Did find ${name} in context, but multiple were defined by same Provider.`
|
|
29
|
+
});
|
|
30
|
+
const Global = new mvc.Register();
|
|
31
|
+
const LookupContext = React__default["default"].createContext(Global);
|
|
32
|
+
function useLookup() {
|
|
33
|
+
try {
|
|
34
|
+
return React__default["default"].useContext(LookupContext);
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
return Global;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function useContext(Type, required) {
|
|
41
|
+
const instance = useLookup().get(Type);
|
|
42
|
+
if (instance === null)
|
|
43
|
+
throw Oops$3.MultipleExist(Type.name);
|
|
44
|
+
if (!instance && required !== false)
|
|
45
|
+
throw Oops$3.NotFound(Type.name);
|
|
46
|
+
return instance;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const Oops$2 = issues({
|
|
50
|
+
AmbientRequired: (requested, requester) => `Attempted to find an instance of ${requested} in context. It is required by ${requester}, but one could not be found.`
|
|
51
|
+
});
|
|
52
|
+
const Pending = new WeakMap();
|
|
53
|
+
const Applied = new WeakMap();
|
|
54
|
+
function getForGetInstruction(type, from, required) {
|
|
55
|
+
return (callback) => {
|
|
56
|
+
const item = mvc.Internal.getParent(from, type);
|
|
57
|
+
if (item)
|
|
58
|
+
callback(item);
|
|
59
|
+
else
|
|
60
|
+
getPending(from).push(context => {
|
|
61
|
+
const got = context.get(type);
|
|
62
|
+
if (got)
|
|
63
|
+
callback(got);
|
|
64
|
+
else if (required)
|
|
65
|
+
throw Oops$2.AmbientRequired(type.name, from);
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function usePeerContext(subject) {
|
|
70
|
+
if (Applied.has(subject)) {
|
|
71
|
+
if (Applied.get(subject))
|
|
72
|
+
useLookup();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const pending = Pending.get(subject);
|
|
76
|
+
if (pending) {
|
|
77
|
+
const local = useLookup();
|
|
78
|
+
for (const init of pending)
|
|
79
|
+
init(local);
|
|
80
|
+
Pending.delete(subject);
|
|
81
|
+
}
|
|
82
|
+
Applied.set(subject, !!pending);
|
|
83
|
+
}
|
|
84
|
+
function getPending(subject) {
|
|
85
|
+
let pending = Pending.get(subject);
|
|
86
|
+
if (!pending)
|
|
87
|
+
Pending.set(subject, pending = []);
|
|
88
|
+
return pending;
|
|
89
|
+
}
|
|
90
|
+
const instruction = mvc.get.using(getForGetInstruction);
|
|
91
|
+
|
|
92
|
+
function useModel(source, arg1, arg2) {
|
|
93
|
+
const instance = React__default["default"].useMemo(() => {
|
|
94
|
+
const callback = arg2 || arg1;
|
|
95
|
+
const instance = mvc.Model.isTypeof(source)
|
|
96
|
+
? new source()
|
|
97
|
+
: source();
|
|
98
|
+
mvc.Control.for(instance);
|
|
99
|
+
if (typeof callback == "function")
|
|
100
|
+
callback(instance);
|
|
101
|
+
return instance;
|
|
102
|
+
}, []);
|
|
103
|
+
usePeerContext(instance);
|
|
104
|
+
if (Array.isArray(arg1)) {
|
|
105
|
+
const update = React__default["default"].useState(0)[1];
|
|
106
|
+
React__default["default"].useLayoutEffect(() => {
|
|
107
|
+
if (arg1.length && instance instanceof mvc.Model)
|
|
108
|
+
instance.on(arg1, () => update(x => x + 1));
|
|
109
|
+
return () => {
|
|
110
|
+
if (mvc.Model.isTypeof(source))
|
|
111
|
+
instance.gc();
|
|
112
|
+
};
|
|
113
|
+
}, []);
|
|
114
|
+
return instance;
|
|
115
|
+
}
|
|
116
|
+
const state = React__default["default"].useState(0);
|
|
117
|
+
const local = React__default["default"].useMemo(() => {
|
|
118
|
+
const refresh = state[1].bind(null, x => x + 1);
|
|
119
|
+
return new mvc.Subscriber(instance, () => refresh);
|
|
120
|
+
}, []);
|
|
121
|
+
if (typeof arg1 == "object")
|
|
122
|
+
applyValues(local, arg1, arg2);
|
|
123
|
+
React__default["default"].useLayoutEffect(() => {
|
|
124
|
+
local.commit();
|
|
125
|
+
return () => {
|
|
126
|
+
local.release();
|
|
127
|
+
if (mvc.Model.isTypeof(source))
|
|
128
|
+
instance.gc();
|
|
129
|
+
};
|
|
130
|
+
}, []);
|
|
131
|
+
return local.proxy;
|
|
132
|
+
}
|
|
133
|
+
// TODO: move back to MVC pending a generic use-case.
|
|
134
|
+
function applyValues(target, values, keys) {
|
|
135
|
+
const { waiting, subject } = target.parent;
|
|
136
|
+
target.active = false;
|
|
137
|
+
if (!keys)
|
|
138
|
+
keys = Object.getOwnPropertyNames(subject);
|
|
139
|
+
for (const key of keys)
|
|
140
|
+
if (key in values)
|
|
141
|
+
subject[key] = values[key];
|
|
142
|
+
waiting.add(() => target.active = true);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const UID_CACHE = new WeakMap();
|
|
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({
|
|
263
|
+
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
264
|
+
});
|
|
265
|
+
function Consumer(props) {
|
|
266
|
+
const { get, has, children, for: type } = props;
|
|
267
|
+
if (typeof children == "function")
|
|
268
|
+
return children(useTap(type));
|
|
269
|
+
const callback = has || get;
|
|
270
|
+
if (typeof callback == "function")
|
|
271
|
+
callback(useContext(type, !!has));
|
|
272
|
+
else
|
|
273
|
+
throw Oops$1.BadProps();
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const Oops = issues({
|
|
278
|
+
NoType: () => "Provider 'for' prop must be Model, typeof Model or a collection of them."
|
|
279
|
+
});
|
|
280
|
+
function Provider(props) {
|
|
281
|
+
const context = useNewContext(props.for, props.use);
|
|
282
|
+
return (React__default["default"].createElement(LookupContext.Provider, { value: context }, props.fallback === false
|
|
283
|
+
? props.children
|
|
284
|
+
: (React__default["default"].createElement(React.Suspense, { fallback: props.fallback || null }, props.children))));
|
|
285
|
+
}
|
|
286
|
+
function useNewContext(include, assign) {
|
|
287
|
+
const init = new Set();
|
|
288
|
+
const ambient = useLookup();
|
|
289
|
+
const context = React__default["default"].useMemo(() => {
|
|
290
|
+
if (!include)
|
|
291
|
+
throw Oops.NoType();
|
|
292
|
+
return ambient.push();
|
|
293
|
+
}, []);
|
|
294
|
+
function register(key, input) {
|
|
295
|
+
let instance;
|
|
296
|
+
if (context.register.get(key) === input)
|
|
297
|
+
instance = typeof input == "object"
|
|
298
|
+
? input
|
|
299
|
+
: context.get(input);
|
|
300
|
+
else
|
|
301
|
+
instance = context.add(input, key);
|
|
302
|
+
init.add(instance);
|
|
303
|
+
if (assign)
|
|
304
|
+
assignWeak(instance, assign);
|
|
305
|
+
}
|
|
306
|
+
if (typeof include == "function" || include instanceof mvc.Model)
|
|
307
|
+
register(0, include);
|
|
308
|
+
else
|
|
309
|
+
Object.entries(include).forEach(e => register(...e));
|
|
310
|
+
for (const instance of init) {
|
|
311
|
+
mvc.Control.for(instance).state.forEach(value => {
|
|
312
|
+
// TODO: should this run repeatedly?
|
|
313
|
+
if (mvc.Internal.getParent(value) === instance) {
|
|
314
|
+
context.add(value);
|
|
315
|
+
init.add(value);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
for (const apply of getPending(instance))
|
|
319
|
+
apply(context);
|
|
320
|
+
// TODO: add test to validate this.
|
|
321
|
+
init.delete(instance);
|
|
322
|
+
}
|
|
323
|
+
React__default["default"].useLayoutEffect(() => () => context.pop(), []);
|
|
324
|
+
return context;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
Object.assign(mvc.Model, {
|
|
328
|
+
use(arg1, arg2) {
|
|
329
|
+
return useModel(this, arg1, arg2);
|
|
330
|
+
},
|
|
331
|
+
get(required) {
|
|
332
|
+
return useContext(this, required);
|
|
333
|
+
},
|
|
334
|
+
tap(arg1, arg2) {
|
|
335
|
+
return useTap(this, arg1, arg2);
|
|
336
|
+
},
|
|
337
|
+
meta(arg1, arg2) {
|
|
338
|
+
return useTap(() => this, arg1, arg2);
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
Object.defineProperty(exports, 'Model', {
|
|
343
|
+
enumerable: true,
|
|
344
|
+
get: function () { return mvc.Model; }
|
|
345
|
+
});
|
|
346
|
+
exports.Consumer = Consumer;
|
|
347
|
+
exports.Provider = Provider;
|
|
348
|
+
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
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@expressive/react",
|
|
3
|
+
"description": "Use classes to define state in React!",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Gabe Klein",
|
|
6
|
+
"email": "gabe@termtm.com",
|
|
7
|
+
"url": "https://github.com/gabeklein"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": "https://github.com/gabeklein/expressive-mvc",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react",
|
|
16
|
+
"mvc"
|
|
17
|
+
],
|
|
18
|
+
"main": "lib/index.js",
|
|
19
|
+
"module": "lib/index.esm.js",
|
|
20
|
+
"types": "lib/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"lib"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "rollup --config",
|
|
26
|
+
"watch": "jest --watchAll",
|
|
27
|
+
"test": "jest --collectCoverage",
|
|
28
|
+
"prebuild": "rm -rf lib/",
|
|
29
|
+
"preversion": "npm run test && npm run build",
|
|
30
|
+
"postversion": "git push & npm publish"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@expressive/mvc": "^0.17.1",
|
|
34
|
+
"react": ">=16.8.0 <19.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@expressive/mvc": "^0.18.0",
|
|
38
|
+
"@rollup/plugin-commonjs": "^22.0.0",
|
|
39
|
+
"@testing-library/react-hooks": "^8.0.0",
|
|
40
|
+
"@types/react": "^17.0.11",
|
|
41
|
+
"@types/react-test-renderer": "^18.0.0",
|
|
42
|
+
"jest": "^27.5.1",
|
|
43
|
+
"react": ">=17.0.2",
|
|
44
|
+
"react-test-renderer": "^17.0.2",
|
|
45
|
+
"rollup": "^2.61.1",
|
|
46
|
+
"rollup-plugin-dts": "^4.2.1",
|
|
47
|
+
"rollup-plugin-typescript2": "^0.31.1",
|
|
48
|
+
"ts-jest": "^27.0.3",
|
|
49
|
+
"tslib": "^2.3.0",
|
|
50
|
+
"typescript": "^4.9.5"
|
|
51
|
+
},
|
|
52
|
+
"version": "0.18.0"
|
|
53
|
+
}
|