@expressive/react 0.21.7 → 0.22.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 CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Model, Context } from '@expressive/mvc';
2
- export { Context, Control, Debug, Model, Model as default, get, ref, run, set, use } from '@expressive/mvc';
2
+ export * from '@expressive/mvc';
3
+ export { Model, Model as default } from '@expressive/mvc';
3
4
  import { ReactElement, ReactNode, FunctionComponentElement, ProviderProps } from 'react';
4
5
 
5
6
  declare namespace Consumer {
@@ -33,13 +34,12 @@ declare namespace Consumer {
33
34
  }
34
35
  declare function Consumer<T extends Model>(props: Consumer.Props<T>): ReactElement<any, any> | null;
35
36
 
36
- type Class = new () => any;
37
37
  declare namespace Provider {
38
38
  type Item = Model | Model.New;
39
39
  type Multiple<T extends Item = Item> = {
40
40
  [key: string | number]: T;
41
41
  };
42
- type Instance<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
42
+ type Instance<E> = E extends (new () => any) ? InstanceType<E> : E extends Model ? E : never;
43
43
  type Props<T extends Item = Item> = MultipleProps<T> | NormalProps<T>;
44
44
  type NormalProps<E, I = Instance<E>> = {
45
45
  for: E;
package/lib/index.esm.js CHANGED
@@ -1,54 +1,11 @@
1
1
  import { Context, Model, Control } from '@expressive/mvc';
2
- export { Context, Control, Debug, Model, Model as default, get, ref, run, set, use } from '@expressive/mvc';
2
+ export * from '@expressive/mvc';
3
+ export { Model, Model as default } from '@expressive/mvc';
3
4
  import { createContext, useContext, useMemo, useLayoutEffect, createElement, Suspense, useState } from 'react';
4
5
 
5
6
  const LookupContext = createContext(new Context());
6
- const useLookup = () => useContext(LookupContext);
7
7
  const Pending = new WeakMap();
8
- const Applied = new WeakMap();
9
- function usePeerContext(instance) {
10
- const applied = Applied.get(instance);
11
- if (applied)
12
- useLookup();
13
- else if (applied === undefined) {
14
- const pending = Pending.get(instance);
15
- if (pending) {
16
- const local = useLookup();
17
- pending.forEach(init => init(local));
18
- Applied.set(instance, local);
19
- Pending.delete(instance);
20
- }
21
- }
22
- }
23
- function setContext(model, has) {
24
- let pending = Pending.get(model);
25
- if (!pending)
26
- Pending.set(model, pending = []);
27
- pending.forEach(cb => cb(has));
28
- }
29
- function hasContext(model) {
30
- let pending = Pending.get(model);
31
- if (!pending)
32
- Pending.set(model, pending = []);
33
- return (callback) => {
34
- const applied = Applied.get(model);
35
- if (applied)
36
- callback(applied);
37
- else
38
- pending.push(callback);
39
- };
40
- }
41
-
42
- function Consumer(props) {
43
- const { children, has, get, for: Type } = props;
44
- if (typeof children == "function")
45
- return children(Type.get());
46
- const callback = has || get;
47
- if (typeof callback == "function")
48
- callback(Type.get(!!has));
49
- return null;
50
- }
51
-
8
+ const useLookup = () => useContext(LookupContext);
52
9
  function Provider(props) {
53
10
  let { for: included, use: assign } = props;
54
11
  const ambient = useLookup();
@@ -60,7 +17,9 @@ function Provider(props) {
60
17
  for (const K in assign)
61
18
  if (K in model)
62
19
  model[K] = assign[K];
63
- setContext(model, context);
20
+ const pending = Pending.get(model);
21
+ if (pending)
22
+ pending.forEach(cb => cb(context));
64
23
  });
65
24
  useLayoutEffect(() => () => context.pop(), []);
66
25
  return createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
@@ -68,7 +27,36 @@ function Provider(props) {
68
27
  : createElement(Suspense, { fallback: props.fallback || null }, props.children));
69
28
  }
70
29
 
71
- Control.has = hasContext;
30
+ function Consumer(props) {
31
+ const { children, has, get, for: Type } = props;
32
+ if (typeof children == "function")
33
+ return children(Type.get());
34
+ const callback = has || get;
35
+ if (typeof callback == "function")
36
+ callback(Type.get(!!has));
37
+ return null;
38
+ }
39
+
40
+ const Applied = new WeakMap();
41
+ Control.use = (adapter) => {
42
+ const state = useState(0);
43
+ const hook = useMemo(() => adapter(state[1]), []);
44
+ const instance = hook.local.is;
45
+ const applied = Applied.get(instance);
46
+ if (applied)
47
+ useLookup();
48
+ else if (applied === undefined) {
49
+ const pending = Pending.get(instance);
50
+ if (pending) {
51
+ const local = useLookup();
52
+ pending.forEach(init => init(local));
53
+ Applied.set(instance, local);
54
+ Pending.delete(instance);
55
+ }
56
+ }
57
+ useLayoutEffect(hook.mount, []);
58
+ return hook.render;
59
+ };
72
60
  Control.get = (adapter) => {
73
61
  const context = useLookup();
74
62
  const state = useState(0);
@@ -78,12 +66,17 @@ Control.get = (adapter) => {
78
66
  useLayoutEffect(hook.mount, []);
79
67
  return hook.render();
80
68
  };
81
- Control.use = (adapter) => {
82
- const state = useState(0);
83
- const hook = useMemo(() => adapter(state[1]), []);
84
- usePeerContext(hook.instance);
85
- useLayoutEffect(hook.mount, []);
86
- return hook.render;
69
+ Control.has = (model) => {
70
+ let pending = Pending.get(model);
71
+ if (!pending)
72
+ Pending.set(model, pending = []);
73
+ return (callback) => {
74
+ const applied = Applied.get(model);
75
+ if (applied)
76
+ callback(applied);
77
+ else
78
+ pending.push(callback);
79
+ };
87
80
  };
88
81
 
89
82
  export { Consumer, Provider };
package/lib/index.js CHANGED
@@ -6,52 +6,8 @@ var mvc = require('@expressive/mvc');
6
6
  var react = require('react');
7
7
 
8
8
  const LookupContext = react.createContext(new mvc.Context());
9
- const useLookup = () => react.useContext(LookupContext);
10
9
  const Pending = new WeakMap();
11
- const Applied = new WeakMap();
12
- function usePeerContext(instance) {
13
- const applied = Applied.get(instance);
14
- if (applied)
15
- useLookup();
16
- else if (applied === undefined) {
17
- const pending = Pending.get(instance);
18
- if (pending) {
19
- const local = useLookup();
20
- pending.forEach(init => init(local));
21
- Applied.set(instance, local);
22
- Pending.delete(instance);
23
- }
24
- }
25
- }
26
- function setContext(model, has) {
27
- let pending = Pending.get(model);
28
- if (!pending)
29
- Pending.set(model, pending = []);
30
- pending.forEach(cb => cb(has));
31
- }
32
- function hasContext(model) {
33
- let pending = Pending.get(model);
34
- if (!pending)
35
- Pending.set(model, pending = []);
36
- return (callback) => {
37
- const applied = Applied.get(model);
38
- if (applied)
39
- callback(applied);
40
- else
41
- pending.push(callback);
42
- };
43
- }
44
-
45
- function Consumer(props) {
46
- const { children, has, get, for: Type } = props;
47
- if (typeof children == "function")
48
- return children(Type.get());
49
- const callback = has || get;
50
- if (typeof callback == "function")
51
- callback(Type.get(!!has));
52
- return null;
53
- }
54
-
10
+ const useLookup = () => react.useContext(LookupContext);
55
11
  function Provider(props) {
56
12
  let { for: included, use: assign } = props;
57
13
  const ambient = useLookup();
@@ -63,7 +19,9 @@ function Provider(props) {
63
19
  for (const K in assign)
64
20
  if (K in model)
65
21
  model[K] = assign[K];
66
- setContext(model, context);
22
+ const pending = Pending.get(model);
23
+ if (pending)
24
+ pending.forEach(cb => cb(context));
67
25
  });
68
26
  react.useLayoutEffect(() => () => context.pop(), []);
69
27
  return react.createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
@@ -71,7 +29,36 @@ function Provider(props) {
71
29
  : react.createElement(react.Suspense, { fallback: props.fallback || null }, props.children));
72
30
  }
73
31
 
74
- mvc.Control.has = hasContext;
32
+ function Consumer(props) {
33
+ const { children, has, get, for: Type } = props;
34
+ if (typeof children == "function")
35
+ return children(Type.get());
36
+ const callback = has || get;
37
+ if (typeof callback == "function")
38
+ callback(Type.get(!!has));
39
+ return null;
40
+ }
41
+
42
+ const Applied = new WeakMap();
43
+ mvc.Control.use = (adapter) => {
44
+ const state = react.useState(0);
45
+ const hook = react.useMemo(() => adapter(state[1]), []);
46
+ const instance = hook.local.is;
47
+ const applied = Applied.get(instance);
48
+ if (applied)
49
+ useLookup();
50
+ else if (applied === undefined) {
51
+ const pending = Pending.get(instance);
52
+ if (pending) {
53
+ const local = useLookup();
54
+ pending.forEach(init => init(local));
55
+ Applied.set(instance, local);
56
+ Pending.delete(instance);
57
+ }
58
+ }
59
+ react.useLayoutEffect(hook.mount, []);
60
+ return hook.render;
61
+ };
75
62
  mvc.Control.get = (adapter) => {
76
63
  const context = useLookup();
77
64
  const state = react.useState(0);
@@ -81,26 +68,19 @@ mvc.Control.get = (adapter) => {
81
68
  react.useLayoutEffect(hook.mount, []);
82
69
  return hook.render();
83
70
  };
84
- mvc.Control.use = (adapter) => {
85
- const state = react.useState(0);
86
- const hook = react.useMemo(() => adapter(state[1]), []);
87
- usePeerContext(hook.instance);
88
- react.useLayoutEffect(hook.mount, []);
89
- return hook.render;
71
+ mvc.Control.has = (model) => {
72
+ let pending = Pending.get(model);
73
+ if (!pending)
74
+ Pending.set(model, pending = []);
75
+ return (callback) => {
76
+ const applied = Applied.get(model);
77
+ if (applied)
78
+ callback(applied);
79
+ else
80
+ pending.push(callback);
81
+ };
90
82
  };
91
83
 
92
- Object.defineProperty(exports, 'Context', {
93
- enumerable: true,
94
- get: function () { return mvc.Context; }
95
- });
96
- Object.defineProperty(exports, 'Control', {
97
- enumerable: true,
98
- get: function () { return mvc.Control; }
99
- });
100
- Object.defineProperty(exports, 'Debug', {
101
- enumerable: true,
102
- get: function () { return mvc.Debug; }
103
- });
104
84
  Object.defineProperty(exports, 'Model', {
105
85
  enumerable: true,
106
86
  get: function () { return mvc.Model; }
@@ -109,25 +89,11 @@ Object.defineProperty(exports, 'default', {
109
89
  enumerable: true,
110
90
  get: function () { return mvc.Model; }
111
91
  });
112
- Object.defineProperty(exports, 'get', {
113
- enumerable: true,
114
- get: function () { return mvc.get; }
115
- });
116
- Object.defineProperty(exports, 'ref', {
117
- enumerable: true,
118
- get: function () { return mvc.ref; }
119
- });
120
- Object.defineProperty(exports, 'run', {
121
- enumerable: true,
122
- get: function () { return mvc.run; }
123
- });
124
- Object.defineProperty(exports, 'set', {
125
- enumerable: true,
126
- get: function () { return mvc.set; }
127
- });
128
- Object.defineProperty(exports, 'use', {
129
- enumerable: true,
130
- get: function () { return mvc.use; }
131
- });
132
92
  exports.Consumer = Consumer;
133
93
  exports.Provider = Provider;
94
+ Object.keys(mvc).forEach(function (k) {
95
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
96
+ enumerable: true,
97
+ get: function () { return mvc[k]; }
98
+ });
99
+ });
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.7"
33
+ "@expressive/mvc": "^0.22.0"
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.7"
45
+ "version": "0.22.0"
46
46
  }