@expressive/react 0.21.0 → 0.21.2
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 +10 -9
- package/lib/index.esm.js +8 -13
- package/lib/index.js +22 -62
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Model } from '@expressive/mvc';
|
|
2
|
-
export
|
|
3
|
-
import * as
|
|
4
|
-
import
|
|
2
|
+
export * from '@expressive/mvc';
|
|
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,6 +1,6 @@
|
|
|
1
|
-
import { Context,
|
|
2
|
-
export
|
|
3
|
-
import
|
|
1
|
+
import { Context, Model, Control } from '@expressive/mvc';
|
|
2
|
+
export * from '@expressive/mvc';
|
|
3
|
+
import { createContext, useContext, useState, useMemo, useLayoutEffect, createElement, Suspense } from 'react';
|
|
4
4
|
|
|
5
5
|
const LookupContext = createContext(new Context());
|
|
6
6
|
const useLookup = () => useContext(LookupContext);
|
|
@@ -15,10 +15,10 @@ function hasModel(type, relativeTo, callback) {
|
|
|
15
15
|
}
|
|
16
16
|
const Applied = new WeakMap();
|
|
17
17
|
function usePeerContext(instance) {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
18
|
+
const applied = Applied.get(instance);
|
|
19
|
+
if (applied)
|
|
20
20
|
useLookup();
|
|
21
|
-
else if (
|
|
21
|
+
else if (applied === undefined) {
|
|
22
22
|
const pending = Pending.get(instance);
|
|
23
23
|
if (pending) {
|
|
24
24
|
const local = useLookup();
|
|
@@ -48,9 +48,6 @@ const useModel = (adapter) => {
|
|
|
48
48
|
return hook.render;
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
-
const Oops = issues({
|
|
52
|
-
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
53
|
-
});
|
|
54
51
|
function Consumer(props) {
|
|
55
52
|
const { children, has, get, for: Type } = props;
|
|
56
53
|
if (typeof children == "function")
|
|
@@ -58,8 +55,6 @@ function Consumer(props) {
|
|
|
58
55
|
const callback = has || get;
|
|
59
56
|
if (typeof callback == "function")
|
|
60
57
|
callback(Type.get(!!has));
|
|
61
|
-
else
|
|
62
|
-
throw Oops.BadProps();
|
|
63
58
|
return null;
|
|
64
59
|
}
|
|
65
60
|
|
|
@@ -79,9 +74,9 @@ function Provider(props) {
|
|
|
79
74
|
pending.forEach(cb => cb(context));
|
|
80
75
|
});
|
|
81
76
|
useLayoutEffect(() => () => context.pop(), []);
|
|
82
|
-
return
|
|
77
|
+
return createElement(LookupContext.Provider, { value: context }, props.fallback == false
|
|
83
78
|
? props.children
|
|
84
|
-
:
|
|
79
|
+
: createElement(Suspense, { fallback: props.fallback || null }, props.children));
|
|
85
80
|
}
|
|
86
81
|
|
|
87
82
|
Control.get = getModel;
|
package/lib/index.js
CHANGED
|
@@ -3,15 +3,10 @@
|
|
|
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 mvc__default = /*#__PURE__*/_interopDefaultLegacy(mvc);
|
|
11
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
12
|
-
|
|
13
|
-
const LookupContext = React.createContext(new mvc.Context());
|
|
14
|
-
const useLookup = () => React.useContext(LookupContext);
|
|
8
|
+
const LookupContext = react.createContext(new mvc.Context());
|
|
9
|
+
const useLookup = () => react.useContext(LookupContext);
|
|
15
10
|
const Pending = new WeakMap();
|
|
16
11
|
function hasModel(type, relativeTo, callback) {
|
|
17
12
|
let pending = Pending.get(relativeTo);
|
|
@@ -23,10 +18,10 @@ function hasModel(type, relativeTo, callback) {
|
|
|
23
18
|
}
|
|
24
19
|
const Applied = new WeakMap();
|
|
25
20
|
function usePeerContext(instance) {
|
|
26
|
-
const
|
|
27
|
-
if (
|
|
21
|
+
const applied = Applied.get(instance);
|
|
22
|
+
if (applied)
|
|
28
23
|
useLookup();
|
|
29
|
-
else if (
|
|
24
|
+
else if (applied === undefined) {
|
|
30
25
|
const pending = Pending.get(instance);
|
|
31
26
|
if (pending) {
|
|
32
27
|
const local = useLookup();
|
|
@@ -39,26 +34,23 @@ function usePeerContext(instance) {
|
|
|
39
34
|
|
|
40
35
|
const getModel = (type, adapter) => {
|
|
41
36
|
const context = useLookup();
|
|
42
|
-
const state =
|
|
43
|
-
const hook =
|
|
37
|
+
const state = react.useState(0);
|
|
38
|
+
const hook = react.useMemo(() => {
|
|
44
39
|
return adapter(() => state[1](x => x + 1), cb => cb(context.get(type)));
|
|
45
40
|
}, []);
|
|
46
41
|
if (!hook)
|
|
47
42
|
return null;
|
|
48
|
-
|
|
43
|
+
react.useLayoutEffect(hook.mount, []);
|
|
49
44
|
return hook.render();
|
|
50
45
|
};
|
|
51
46
|
const useModel = (adapter) => {
|
|
52
|
-
const state =
|
|
53
|
-
const hook =
|
|
47
|
+
const state = react.useState(0);
|
|
48
|
+
const hook = react.useMemo(() => (adapter(() => state[1](x => x + 1))), []);
|
|
54
49
|
usePeerContext(hook.instance);
|
|
55
|
-
|
|
50
|
+
react.useLayoutEffect(hook.mount, []);
|
|
56
51
|
return hook.render;
|
|
57
52
|
};
|
|
58
53
|
|
|
59
|
-
const Oops = mvc.issues({
|
|
60
|
-
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
61
|
-
});
|
|
62
54
|
function Consumer(props) {
|
|
63
55
|
const { children, has, get, for: Type } = props;
|
|
64
56
|
if (typeof children == "function")
|
|
@@ -66,15 +58,13 @@ function Consumer(props) {
|
|
|
66
58
|
const callback = has || get;
|
|
67
59
|
if (typeof callback == "function")
|
|
68
60
|
callback(Type.get(!!has));
|
|
69
|
-
else
|
|
70
|
-
throw Oops.BadProps();
|
|
71
61
|
return null;
|
|
72
62
|
}
|
|
73
63
|
|
|
74
64
|
function Provider(props) {
|
|
75
65
|
let { for: included, use: assign } = props;
|
|
76
66
|
const ambient = useLookup();
|
|
77
|
-
const context =
|
|
67
|
+
const context = react.useMemo(() => ambient.push(), []);
|
|
78
68
|
if (typeof included == "function" || included instanceof mvc.Model)
|
|
79
69
|
included = { [0]: included };
|
|
80
70
|
context.include(included).forEach((explicit, model) => {
|
|
@@ -86,51 +76,21 @@ function Provider(props) {
|
|
|
86
76
|
if (pending)
|
|
87
77
|
pending.forEach(cb => cb(context));
|
|
88
78
|
});
|
|
89
|
-
|
|
90
|
-
return
|
|
79
|
+
react.useLayoutEffect(() => () => context.pop(), []);
|
|
80
|
+
return react.createElement(LookupContext.Provider, { value: context }, props.fallback == false
|
|
91
81
|
? props.children
|
|
92
|
-
:
|
|
82
|
+
: react.createElement(react.Suspense, { fallback: props.fallback || null }, props.children));
|
|
93
83
|
}
|
|
94
84
|
|
|
95
85
|
mvc.Control.get = getModel;
|
|
96
86
|
mvc.Control.use = useModel;
|
|
97
87
|
mvc.Control.has = hasModel;
|
|
98
88
|
|
|
99
|
-
Object.defineProperty(exports, 'Control', {
|
|
100
|
-
enumerable: true,
|
|
101
|
-
get: function () { return mvc.Control; }
|
|
102
|
-
});
|
|
103
|
-
Object.defineProperty(exports, 'Debug', {
|
|
104
|
-
enumerable: true,
|
|
105
|
-
get: function () { return mvc.Debug; }
|
|
106
|
-
});
|
|
107
|
-
Object.defineProperty(exports, 'Model', {
|
|
108
|
-
enumerable: true,
|
|
109
|
-
get: function () { return mvc.Model; }
|
|
110
|
-
});
|
|
111
|
-
Object.defineProperty(exports, 'default', {
|
|
112
|
-
enumerable: true,
|
|
113
|
-
get: function () { return mvc__default["default"]; }
|
|
114
|
-
});
|
|
115
|
-
Object.defineProperty(exports, 'get', {
|
|
116
|
-
enumerable: true,
|
|
117
|
-
get: function () { return mvc.get; }
|
|
118
|
-
});
|
|
119
|
-
Object.defineProperty(exports, 'ref', {
|
|
120
|
-
enumerable: true,
|
|
121
|
-
get: function () { return mvc.ref; }
|
|
122
|
-
});
|
|
123
|
-
Object.defineProperty(exports, 'run', {
|
|
124
|
-
enumerable: true,
|
|
125
|
-
get: function () { return mvc.run; }
|
|
126
|
-
});
|
|
127
|
-
Object.defineProperty(exports, 'set', {
|
|
128
|
-
enumerable: true,
|
|
129
|
-
get: function () { return mvc.set; }
|
|
130
|
-
});
|
|
131
|
-
Object.defineProperty(exports, 'use', {
|
|
132
|
-
enumerable: true,
|
|
133
|
-
get: function () { return mvc.use; }
|
|
134
|
-
});
|
|
135
89
|
exports.Consumer = Consumer;
|
|
136
90
|
exports.Provider = Provider;
|
|
91
|
+
Object.keys(mvc).forEach(function (k) {
|
|
92
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
get: function () { return mvc[k]; }
|
|
95
|
+
});
|
|
96
|
+
});
|
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.2"
|
|
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.2"
|
|
46
46
|
}
|