@expressive/react 0.21.2 → 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.esm.js CHANGED
@@ -1,18 +1,10 @@
1
1
  import { Context, Model, Control } from '@expressive/mvc';
2
2
  export * from '@expressive/mvc';
3
- import { createContext, useContext, useState, useMemo, useLayoutEffect, createElement, Suspense } from 'react';
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
10
  const applied = Applied.get(instance);
@@ -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")
@@ -69,18 +55,30 @@ function Provider(props) {
69
55
  for (const K in assign)
70
56
  if (K in model)
71
57
  model[K] = assign[K];
72
- const pending = Pending.get(model);
73
- if (pending)
74
- pending.forEach(cb => cb(context));
58
+ setPeerContext(model, context);
75
59
  });
76
60
  useLayoutEffect(() => () => context.pop(), []);
77
- return createElement(LookupContext.Provider, { value: context }, props.fallback == false
61
+ return createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
78
62
  ? props.children
79
63
  : createElement(Suspense, { fallback: props.fallback || null }, props.children));
80
64
  }
81
65
 
82
- Control.get = getModel;
83
- Control.use = useModel;
84
- Control.has = hasModel;
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
+ };
85
83
 
86
84
  export { Consumer, Provider };
package/lib/index.js CHANGED
@@ -8,14 +8,6 @@ var react = require('react');
8
8
  const LookupContext = react.createContext(new mvc.Context());
9
9
  const useLookup = () => react.useContext(LookupContext);
10
10
  const Pending = new WeakMap();
11
- function hasModel(type, relativeTo, callback) {
12
- let pending = Pending.get(relativeTo);
13
- if (!pending)
14
- Pending.set(relativeTo, pending = []);
15
- pending.push(context => {
16
- callback(context.get(type));
17
- });
18
- }
19
11
  const Applied = new WeakMap();
20
12
  function usePeerContext(instance) {
21
13
  const applied = Applied.get(instance);
@@ -30,27 +22,21 @@ function usePeerContext(instance) {
30
22
  Applied.set(instance, true);
31
23
  }
32
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));
33
38
  }
34
39
 
35
- const getModel = (type, adapter) => {
36
- const context = useLookup();
37
- const state = react.useState(0);
38
- const hook = react.useMemo(() => {
39
- return adapter(() => state[1](x => x + 1), cb => cb(context.get(type)));
40
- }, []);
41
- if (!hook)
42
- return null;
43
- react.useLayoutEffect(hook.mount, []);
44
- return hook.render();
45
- };
46
- const useModel = (adapter) => {
47
- const state = react.useState(0);
48
- const hook = react.useMemo(() => (adapter(() => state[1](x => x + 1))), []);
49
- usePeerContext(hook.instance);
50
- react.useLayoutEffect(hook.mount, []);
51
- return hook.render;
52
- };
53
-
54
40
  function Consumer(props) {
55
41
  const { children, has, get, for: Type } = props;
56
42
  if (typeof children == "function")
@@ -72,19 +58,31 @@ function Provider(props) {
72
58
  for (const K in assign)
73
59
  if (K in model)
74
60
  model[K] = assign[K];
75
- const pending = Pending.get(model);
76
- if (pending)
77
- pending.forEach(cb => cb(context));
61
+ setPeerContext(model, context);
78
62
  });
79
63
  react.useLayoutEffect(() => () => context.pop(), []);
80
- return react.createElement(LookupContext.Provider, { value: context }, props.fallback == false
64
+ return react.createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
81
65
  ? props.children
82
66
  : react.createElement(react.Suspense, { fallback: props.fallback || null }, props.children));
83
67
  }
84
68
 
85
- mvc.Control.get = getModel;
86
- mvc.Control.use = useModel;
87
- mvc.Control.has = hasModel;
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
+ };
88
86
 
89
87
  exports.Consumer = Consumer;
90
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.2"
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.2"
45
+ "version": "0.21.3"
46
46
  }