@expressive/react 0.21.1 → 0.21.3
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 +9 -8
- package/lib/index.esm.js +37 -41
- package/lib/index.js +41 -49
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Model } from '@expressive/mvc';
|
|
2
2
|
export * from '@expressive/mvc';
|
|
3
|
-
import * as
|
|
4
|
-
import
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
5
|
+
import * as packages_mvc_src_context from 'packages/mvc/src/context';
|
|
5
6
|
|
|
6
7
|
declare namespace Consumer {
|
|
7
8
|
type HasProps<T extends Model> = {
|
|
@@ -32,7 +33,7 @@ declare namespace Consumer {
|
|
|
32
33
|
};
|
|
33
34
|
type Props<T extends Model> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
34
35
|
}
|
|
35
|
-
declare function Consumer<T extends Model>(props: Consumer.Props<T>):
|
|
36
|
+
declare function Consumer<T extends Model>(props: Consumer.Props<T>): react.ReactElement<any, any> | null;
|
|
36
37
|
|
|
37
38
|
type Class = new () => any;
|
|
38
39
|
declare namespace Provider {
|
|
@@ -44,17 +45,17 @@ declare namespace Provider {
|
|
|
44
45
|
type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
|
|
45
46
|
type NormalProps<E, I = Instance<E>> = {
|
|
46
47
|
for: E;
|
|
47
|
-
fallback?:
|
|
48
|
-
children?:
|
|
48
|
+
fallback?: ReactNode;
|
|
49
|
+
children?: ReactNode | ((instance: I) => ReactNode);
|
|
49
50
|
use?: Model.Compat<I>;
|
|
50
51
|
};
|
|
51
52
|
type MultipleProps<T extends Item> = {
|
|
52
53
|
for: Multiple<T>;
|
|
53
|
-
fallback?:
|
|
54
|
-
children?:
|
|
54
|
+
fallback?: ReactNode;
|
|
55
|
+
children?: ReactNode;
|
|
55
56
|
use?: Model.Compat<Instance<T>>;
|
|
56
57
|
};
|
|
57
58
|
}
|
|
58
|
-
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>):
|
|
59
|
+
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): react.FunctionComponentElement<react.ProviderProps<packages_mvc_src_context.Context>>;
|
|
59
60
|
|
|
60
61
|
export { Consumer, Provider };
|
package/lib/index.esm.js
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
1
|
import { Context, Model, Control } from '@expressive/mvc';
|
|
2
2
|
export * from '@expressive/mvc';
|
|
3
|
-
import
|
|
3
|
+
import { createContext, useContext, useMemo, useLayoutEffect, createElement, Suspense, useState } from 'react';
|
|
4
4
|
|
|
5
5
|
const LookupContext = createContext(new Context());
|
|
6
6
|
const useLookup = () => useContext(LookupContext);
|
|
7
7
|
const Pending = new WeakMap();
|
|
8
|
-
function hasModel(type, relativeTo, callback) {
|
|
9
|
-
let pending = Pending.get(relativeTo);
|
|
10
|
-
if (!pending)
|
|
11
|
-
Pending.set(relativeTo, pending = []);
|
|
12
|
-
pending.push(context => {
|
|
13
|
-
callback(context.get(type));
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
8
|
const Applied = new WeakMap();
|
|
17
9
|
function usePeerContext(instance) {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
10
|
+
const applied = Applied.get(instance);
|
|
11
|
+
if (applied)
|
|
20
12
|
useLookup();
|
|
21
|
-
else if (
|
|
13
|
+
else if (applied === undefined) {
|
|
22
14
|
const pending = Pending.get(instance);
|
|
23
15
|
if (pending) {
|
|
24
16
|
const local = useLookup();
|
|
@@ -27,27 +19,21 @@ function usePeerContext(instance) {
|
|
|
27
19
|
Applied.set(instance, true);
|
|
28
20
|
}
|
|
29
21
|
}
|
|
22
|
+
}
|
|
23
|
+
function getPeerContext(type, relativeTo, callback) {
|
|
24
|
+
let pending = Pending.get(relativeTo);
|
|
25
|
+
if (!pending)
|
|
26
|
+
Pending.set(relativeTo, pending = []);
|
|
27
|
+
pending.push(context => {
|
|
28
|
+
callback(context.get(type));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function setPeerContext(model, context) {
|
|
32
|
+
const pending = Pending.get(model);
|
|
33
|
+
if (pending)
|
|
34
|
+
pending.forEach(cb => cb(context));
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
const getModel = (type, adapter) => {
|
|
33
|
-
const context = useLookup();
|
|
34
|
-
const state = useState(0);
|
|
35
|
-
const hook = useMemo(() => {
|
|
36
|
-
return adapter(() => state[1](x => x + 1), cb => cb(context.get(type)));
|
|
37
|
-
}, []);
|
|
38
|
-
if (!hook)
|
|
39
|
-
return null;
|
|
40
|
-
useLayoutEffect(hook.mount, []);
|
|
41
|
-
return hook.render();
|
|
42
|
-
};
|
|
43
|
-
const useModel = (adapter) => {
|
|
44
|
-
const state = useState(0);
|
|
45
|
-
const hook = useMemo(() => (adapter(() => state[1](x => x + 1))), []);
|
|
46
|
-
usePeerContext(hook.instance);
|
|
47
|
-
useLayoutEffect(hook.mount, []);
|
|
48
|
-
return hook.render;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
37
|
function Consumer(props) {
|
|
52
38
|
const { children, has, get, for: Type } = props;
|
|
53
39
|
if (typeof children == "function")
|
|
@@ -55,8 +41,6 @@ function Consumer(props) {
|
|
|
55
41
|
const callback = has || get;
|
|
56
42
|
if (typeof callback == "function")
|
|
57
43
|
callback(Type.get(!!has));
|
|
58
|
-
else
|
|
59
|
-
throw new Error("Provider expects either a render function, 'get' or 'has' props.");
|
|
60
44
|
return null;
|
|
61
45
|
}
|
|
62
46
|
|
|
@@ -71,18 +55,30 @@ function Provider(props) {
|
|
|
71
55
|
for (const K in assign)
|
|
72
56
|
if (K in model)
|
|
73
57
|
model[K] = assign[K];
|
|
74
|
-
|
|
75
|
-
if (pending)
|
|
76
|
-
pending.forEach(cb => cb(context));
|
|
58
|
+
setPeerContext(model, context);
|
|
77
59
|
});
|
|
78
60
|
useLayoutEffect(() => () => context.pop(), []);
|
|
79
|
-
return
|
|
61
|
+
return createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
|
|
80
62
|
? props.children
|
|
81
|
-
:
|
|
63
|
+
: createElement(Suspense, { fallback: props.fallback || null }, props.children));
|
|
82
64
|
}
|
|
83
65
|
|
|
84
|
-
Control.
|
|
85
|
-
Control.
|
|
86
|
-
|
|
66
|
+
Control.has = getPeerContext;
|
|
67
|
+
Control.get = (adapter) => {
|
|
68
|
+
const context = useLookup();
|
|
69
|
+
const state = useState(0);
|
|
70
|
+
const hook = useMemo(() => adapter(state[1], context), []);
|
|
71
|
+
if (!hook)
|
|
72
|
+
return null;
|
|
73
|
+
useLayoutEffect(hook.mount, []);
|
|
74
|
+
return hook.render();
|
|
75
|
+
};
|
|
76
|
+
Control.use = (adapter) => {
|
|
77
|
+
const state = useState(0);
|
|
78
|
+
const hook = useMemo(() => adapter(state[1]), []);
|
|
79
|
+
usePeerContext(hook.instance);
|
|
80
|
+
useLayoutEffect(hook.mount, []);
|
|
81
|
+
return hook.render;
|
|
82
|
+
};
|
|
87
83
|
|
|
88
84
|
export { Consumer, Provider };
|
package/lib/index.js
CHANGED
|
@@ -3,29 +3,17 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var mvc = require('@expressive/mvc');
|
|
6
|
-
var
|
|
6
|
+
var react = require('react');
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
11
|
-
|
|
12
|
-
const LookupContext = React.createContext(new mvc.Context());
|
|
13
|
-
const useLookup = () => React.useContext(LookupContext);
|
|
8
|
+
const LookupContext = react.createContext(new mvc.Context());
|
|
9
|
+
const useLookup = () => react.useContext(LookupContext);
|
|
14
10
|
const Pending = new WeakMap();
|
|
15
|
-
function hasModel(type, relativeTo, callback) {
|
|
16
|
-
let pending = Pending.get(relativeTo);
|
|
17
|
-
if (!pending)
|
|
18
|
-
Pending.set(relativeTo, pending = []);
|
|
19
|
-
pending.push(context => {
|
|
20
|
-
callback(context.get(type));
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
11
|
const Applied = new WeakMap();
|
|
24
12
|
function usePeerContext(instance) {
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
13
|
+
const applied = Applied.get(instance);
|
|
14
|
+
if (applied)
|
|
27
15
|
useLookup();
|
|
28
|
-
else if (
|
|
16
|
+
else if (applied === undefined) {
|
|
29
17
|
const pending = Pending.get(instance);
|
|
30
18
|
if (pending) {
|
|
31
19
|
const local = useLookup();
|
|
@@ -34,27 +22,21 @@ function usePeerContext(instance) {
|
|
|
34
22
|
Applied.set(instance, true);
|
|
35
23
|
}
|
|
36
24
|
}
|
|
25
|
+
}
|
|
26
|
+
function getPeerContext(type, relativeTo, callback) {
|
|
27
|
+
let pending = Pending.get(relativeTo);
|
|
28
|
+
if (!pending)
|
|
29
|
+
Pending.set(relativeTo, pending = []);
|
|
30
|
+
pending.push(context => {
|
|
31
|
+
callback(context.get(type));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function setPeerContext(model, context) {
|
|
35
|
+
const pending = Pending.get(model);
|
|
36
|
+
if (pending)
|
|
37
|
+
pending.forEach(cb => cb(context));
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
const getModel = (type, adapter) => {
|
|
40
|
-
const context = useLookup();
|
|
41
|
-
const state = React.useState(0);
|
|
42
|
-
const hook = React.useMemo(() => {
|
|
43
|
-
return adapter(() => state[1](x => x + 1), cb => cb(context.get(type)));
|
|
44
|
-
}, []);
|
|
45
|
-
if (!hook)
|
|
46
|
-
return null;
|
|
47
|
-
React.useLayoutEffect(hook.mount, []);
|
|
48
|
-
return hook.render();
|
|
49
|
-
};
|
|
50
|
-
const useModel = (adapter) => {
|
|
51
|
-
const state = React.useState(0);
|
|
52
|
-
const hook = React.useMemo(() => (adapter(() => state[1](x => x + 1))), []);
|
|
53
|
-
usePeerContext(hook.instance);
|
|
54
|
-
React.useLayoutEffect(hook.mount, []);
|
|
55
|
-
return hook.render;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
40
|
function Consumer(props) {
|
|
59
41
|
const { children, has, get, for: Type } = props;
|
|
60
42
|
if (typeof children == "function")
|
|
@@ -62,15 +44,13 @@ function Consumer(props) {
|
|
|
62
44
|
const callback = has || get;
|
|
63
45
|
if (typeof callback == "function")
|
|
64
46
|
callback(Type.get(!!has));
|
|
65
|
-
else
|
|
66
|
-
throw new Error("Provider expects either a render function, 'get' or 'has' props.");
|
|
67
47
|
return null;
|
|
68
48
|
}
|
|
69
49
|
|
|
70
50
|
function Provider(props) {
|
|
71
51
|
let { for: included, use: assign } = props;
|
|
72
52
|
const ambient = useLookup();
|
|
73
|
-
const context =
|
|
53
|
+
const context = react.useMemo(() => ambient.push(), []);
|
|
74
54
|
if (typeof included == "function" || included instanceof mvc.Model)
|
|
75
55
|
included = { [0]: included };
|
|
76
56
|
context.include(included).forEach((explicit, model) => {
|
|
@@ -78,19 +58,31 @@ function Provider(props) {
|
|
|
78
58
|
for (const K in assign)
|
|
79
59
|
if (K in model)
|
|
80
60
|
model[K] = assign[K];
|
|
81
|
-
|
|
82
|
-
if (pending)
|
|
83
|
-
pending.forEach(cb => cb(context));
|
|
61
|
+
setPeerContext(model, context);
|
|
84
62
|
});
|
|
85
|
-
|
|
86
|
-
return
|
|
63
|
+
react.useLayoutEffect(() => () => context.pop(), []);
|
|
64
|
+
return react.createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
|
|
87
65
|
? props.children
|
|
88
|
-
:
|
|
66
|
+
: react.createElement(react.Suspense, { fallback: props.fallback || null }, props.children));
|
|
89
67
|
}
|
|
90
68
|
|
|
91
|
-
mvc.Control.
|
|
92
|
-
mvc.Control.
|
|
93
|
-
|
|
69
|
+
mvc.Control.has = getPeerContext;
|
|
70
|
+
mvc.Control.get = (adapter) => {
|
|
71
|
+
const context = useLookup();
|
|
72
|
+
const state = react.useState(0);
|
|
73
|
+
const hook = react.useMemo(() => adapter(state[1], context), []);
|
|
74
|
+
if (!hook)
|
|
75
|
+
return null;
|
|
76
|
+
react.useLayoutEffect(hook.mount, []);
|
|
77
|
+
return hook.render();
|
|
78
|
+
};
|
|
79
|
+
mvc.Control.use = (adapter) => {
|
|
80
|
+
const state = react.useState(0);
|
|
81
|
+
const hook = react.useMemo(() => adapter(state[1]), []);
|
|
82
|
+
usePeerContext(hook.instance);
|
|
83
|
+
react.useLayoutEffect(hook.mount, []);
|
|
84
|
+
return hook.render;
|
|
85
|
+
};
|
|
94
86
|
|
|
95
87
|
exports.Consumer = Consumer;
|
|
96
88
|
exports.Provider = Provider;
|
package/package.json
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"postversion": "git push & npm publish"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@expressive/mvc": "^0.21.
|
|
33
|
+
"@expressive/mvc": "^0.21.3"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"react": ">=16.8.0 <19.0.0"
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"react": ">=17.0.2",
|
|
43
43
|
"react-test-renderer": "^17.0.2"
|
|
44
44
|
},
|
|
45
|
-
"version": "0.21.
|
|
45
|
+
"version": "0.21.3"
|
|
46
46
|
}
|