@expressive/react 0.33.0 → 0.34.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,7 +1,7 @@
1
1
  import { Model, Context } from '@expressive/mvc';
2
2
  export * from '@expressive/mvc';
3
- export { Model, Model as default } from '@expressive/mvc';
4
- import { ReactElement, ReactNode, FunctionComponentElement, ProviderProps } from 'react';
3
+ export { Model as default } from '@expressive/mvc';
4
+ import { ReactElement, FunctionComponentElement, ProviderProps, ReactNode } from 'react';
5
5
 
6
6
  declare namespace Consumer {
7
7
  type HasProps<T extends Model> = {
@@ -35,6 +35,7 @@ declare namespace Consumer {
35
35
  declare function Consumer<T extends Model>(props: Consumer.Props<T>): ReactElement<any, any> | null;
36
36
 
37
37
  declare namespace Provider {
38
+ type Element = FunctionComponentElement<ProviderProps<Context>>;
38
39
  type Item = Model | Model.New;
39
40
  type Multiple<T extends Item = Item> = {
40
41
  [key: string | number]: T;
@@ -43,17 +44,15 @@ declare namespace Provider {
43
44
  type Props<T extends Item = Item> = MultipleProps<T> | NormalProps<T>;
44
45
  type NormalProps<E, I = Instance<E>> = {
45
46
  for: E;
46
- fallback?: ReactNode;
47
- children?: ReactNode | ((instance: I) => ReactNode);
47
+ children?: ReactNode;
48
48
  use?: Model.Values<I>;
49
49
  };
50
50
  type MultipleProps<T extends Item> = {
51
51
  for: Multiple<T>;
52
- fallback?: ReactNode;
53
52
  children?: ReactNode;
54
53
  use?: Model.Values<Instance<T>>;
55
54
  };
56
55
  }
57
- declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): FunctionComponentElement<ProviderProps<Context>>;
56
+ declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): Provider.Element;
58
57
 
59
58
  export { Consumer, Provider };
package/lib/index.esm.js CHANGED
@@ -1,18 +1,180 @@
1
- import { Context, Model, Control } from '@expressive/mvc';
1
+ import { Context, Control, Model, get } from '@expressive/mvc';
2
2
  export * from '@expressive/mvc';
3
- export { Model, Model as default } from '@expressive/mvc';
4
- import { createContext, useContext, useMemo, useLayoutEffect, createElement, Suspense, useState, useEffect } from 'react';
3
+ export { Model as default } from '@expressive/mvc';
4
+ import { createContext, useState, useMemo, useEffect, useContext, createElement } from 'react';
5
5
 
6
- const LookupContext = createContext(new Context());
7
- const Pending = new WeakMap();
8
- const useLookup = () => useContext(LookupContext);
9
- function Provider(props) {
10
- let { for: included, use: assign } = props;
11
- const ambient = useLookup();
12
- const context = useMemo(() => ambient.push(), []);
13
- const reject = (value) => {
14
- throw new Error(`Provider expects a Model instance or class but got ${value}.`);
6
+ const Applied = new WeakMap();
7
+ const Expects = new WeakMap();
8
+ const Shared = createContext(new Context());
9
+ function useLocal(apply, repeat) {
10
+ const state = useState(0);
11
+ const hook = useMemo(() => {
12
+ const instance = this.new();
13
+ const local = Control.watch(instance, () => onUpdate);
14
+ const refresh = () => state[1](x => x + 1);
15
+ let onUpdate;
16
+ let shouldApply = !!apply;
17
+ return (props) => {
18
+ if (shouldApply) {
19
+ onUpdate = undefined;
20
+ if (typeof props == "function")
21
+ props(instance);
22
+ else if (props)
23
+ for (const key in instance)
24
+ if (props.hasOwnProperty(key))
25
+ instance[key] = props[key];
26
+ if (!repeat)
27
+ shouldApply = false;
28
+ instance.set().then(() => onUpdate = refresh);
29
+ }
30
+ useModelContext(instance);
31
+ useEffect(() => {
32
+ onUpdate = refresh;
33
+ return () => {
34
+ onUpdate = null;
35
+ instance.null();
36
+ };
37
+ }, []);
38
+ return local;
39
+ };
40
+ }, []);
41
+ return hook(apply);
42
+ }
43
+ function useModelContext(applyTo) {
44
+ const context = useContext(Shared);
45
+ if (!applyTo || Applied.has(applyTo))
46
+ return context;
47
+ const pending = Expects.get(applyTo);
48
+ if (pending) {
49
+ pending.forEach(init => init(context));
50
+ Applied.set(applyTo, context);
51
+ Expects.delete(applyTo);
52
+ }
53
+ }
54
+ function getContext(from) {
55
+ let waiting = Expects.get(from);
56
+ if (!waiting)
57
+ Expects.set(from, waiting = []);
58
+ return (callback) => {
59
+ const applied = Applied.get(from);
60
+ if (applied)
61
+ callback(applied);
62
+ else
63
+ waiting.push(callback);
15
64
  };
65
+ }
66
+ function setContext(to, context) {
67
+ const pending = Expects.get(to);
68
+ if (pending)
69
+ pending.forEach(cb => cb(context));
70
+ }
71
+
72
+ function useRemote(argument) {
73
+ const context = useModelContext();
74
+ const state = useState(0);
75
+ const hook = useMemo(() => {
76
+ const notFound = () => new Error(`Could not find ${this} in context.`);
77
+ const refresh = () => state[1](x => x + 1);
78
+ const instance = context.get(this);
79
+ let onUpdate;
80
+ let value;
81
+ if (typeof argument !== "function") {
82
+ if (instance)
83
+ value = argument === undefined
84
+ ? Control.watch(instance, k => k ? onUpdate : undefined)
85
+ : instance;
86
+ else if (argument !== false)
87
+ throw notFound();
88
+ return () => {
89
+ useEffect(() => {
90
+ onUpdate = refresh;
91
+ return () => {
92
+ onUpdate = null;
93
+ };
94
+ }, []);
95
+ return value;
96
+ };
97
+ }
98
+ let compute = argument;
99
+ let suspense;
100
+ let getValue;
101
+ let factory;
102
+ let proxy;
103
+ if (!instance)
104
+ throw notFound();
105
+ function forceUpdate(action) {
106
+ if (typeof action == "function")
107
+ action = action();
108
+ if (getValue)
109
+ didUpdate(getValue());
110
+ else
111
+ refresh();
112
+ if (action)
113
+ return action.finally(refresh);
114
+ }
115
+ function didUpdate(got) {
116
+ value = got;
117
+ if (suspense) {
118
+ suspense();
119
+ suspense = undefined;
120
+ }
121
+ else
122
+ refresh();
123
+ }
124
+ proxy = Control.watch(instance, () => factory ? null : onUpdate);
125
+ getValue = () => compute.call(proxy, proxy, forceUpdate);
126
+ value = getValue();
127
+ if (value === null) {
128
+ getValue = undefined;
129
+ onUpdate = null;
130
+ return;
131
+ }
132
+ if (typeof value == "function") {
133
+ const get = value;
134
+ Control.watch(proxy, () => onUpdate);
135
+ factory = true;
136
+ compute = () => get();
137
+ value = get();
138
+ }
139
+ if (value instanceof Promise) {
140
+ onUpdate = null;
141
+ value.then(didUpdate);
142
+ value = undefined;
143
+ }
144
+ else
145
+ onUpdate = () => {
146
+ const next = getValue();
147
+ if (value !== next)
148
+ didUpdate(next);
149
+ };
150
+ return () => {
151
+ useEffect(() => () => {
152
+ onUpdate = null;
153
+ }, []);
154
+ if (value !== undefined)
155
+ return value;
156
+ if (onUpdate)
157
+ return null;
158
+ throw new Promise(res => suspense = res);
159
+ };
160
+ }, []);
161
+ return hook ? hook() : null;
162
+ }
163
+
164
+ function Consumer(props) {
165
+ const { children, has, get, for: Type } = props;
166
+ if (typeof children == "function")
167
+ return children(Type.get());
168
+ const callback = has || get;
169
+ if (typeof callback == "function")
170
+ callback(Type.get(!!has));
171
+ return null;
172
+ }
173
+
174
+ function Provider(props) {
175
+ let { for: included, use: assign, children } = props;
176
+ const context = useModelContext();
177
+ const value = useMemo(() => context.push(), []);
16
178
  if (!included)
17
179
  reject(included);
18
180
  if (typeof included == "function" || included instanceof Model)
@@ -21,70 +183,22 @@ function Provider(props) {
21
183
  if (!(Model.is(V) || V instanceof Model))
22
184
  reject(`${V} as ${K}`);
23
185
  });
24
- context.include(included).forEach((isExplicit, model) => {
186
+ value.include(included).forEach((isExplicit, model) => {
25
187
  if (assign && isExplicit)
26
188
  for (const K in assign)
27
189
  if (K in model)
28
190
  model[K] = assign[K];
29
- const pending = Pending.get(model);
30
- if (pending)
31
- pending.forEach(cb => cb(context));
191
+ setContext(model, value);
32
192
  });
33
- useLayoutEffect(() => () => context.pop(), []);
34
- return createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
35
- ? props.children
36
- : createElement(Suspense, { fallback: props.fallback || null }, props.children));
193
+ useEffect(() => () => value.pop(), []);
194
+ return createElement(Shared.Provider, { key: value.key, value }, children);
195
+ }
196
+ function reject(argument) {
197
+ throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
37
198
  }
38
199
 
39
- function Consumer(props) {
40
- const { children, has, get, for: Type } = props;
41
- if (typeof children == "function")
42
- return children(Type.get());
43
- const callback = has || get;
44
- if (typeof callback == "function")
45
- callback(Type.get(!!has));
46
- return null;
47
- }
48
-
49
- const Applied = new WeakMap();
50
- Control.use = (adapter) => {
51
- const state = useState(0);
52
- const hook = useMemo(() => adapter(state[1]), []);
53
- const applied = Applied.get(hook.instance);
54
- if (applied)
55
- useLookup();
56
- else if (applied === undefined) {
57
- const pending = Pending.get(hook.instance);
58
- if (pending) {
59
- const local = useLookup();
60
- pending.forEach(init => init(local));
61
- Applied.set(hook.instance, local);
62
- Pending.delete(hook.instance);
63
- }
64
- }
65
- useEffect(hook.mount, []);
66
- return hook.render;
67
- };
68
- Control.get = (adapter) => {
69
- const context = useLookup();
70
- const state = useState(0);
71
- const hook = useMemo(() => adapter(state[1], context), []);
72
- if (!hook)
73
- return null;
74
- useEffect(hook.mount, []);
75
- return hook.render();
76
- };
77
- Control.has = (model) => {
78
- let pending = Pending.get(model);
79
- if (!pending)
80
- Pending.set(model, pending = []);
81
- return (callback) => {
82
- const applied = Applied.get(model);
83
- if (applied)
84
- callback(applied);
85
- else
86
- pending.push(callback);
87
- };
88
- };
200
+ Model.get = useRemote;
201
+ Model.use = useLocal;
202
+ get.from = getContext;
89
203
 
90
204
  export { Consumer, Provider };
package/lib/index.js CHANGED
@@ -5,16 +5,178 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var mvc = require('@expressive/mvc');
6
6
  var react = require('react');
7
7
 
8
- const LookupContext = react.createContext(new mvc.Context());
9
- const Pending = new WeakMap();
10
- const useLookup = () => react.useContext(LookupContext);
11
- function Provider(props) {
12
- let { for: included, use: assign } = props;
13
- const ambient = useLookup();
14
- const context = react.useMemo(() => ambient.push(), []);
15
- const reject = (value) => {
16
- throw new Error(`Provider expects a Model instance or class but got ${value}.`);
8
+ const Applied = new WeakMap();
9
+ const Expects = new WeakMap();
10
+ const Shared = react.createContext(new mvc.Context());
11
+ function useLocal(apply, repeat) {
12
+ const state = react.useState(0);
13
+ const hook = react.useMemo(() => {
14
+ const instance = this.new();
15
+ const local = mvc.Control.watch(instance, () => onUpdate);
16
+ const refresh = () => state[1](x => x + 1);
17
+ let onUpdate;
18
+ let shouldApply = !!apply;
19
+ return (props) => {
20
+ if (shouldApply) {
21
+ onUpdate = undefined;
22
+ if (typeof props == "function")
23
+ props(instance);
24
+ else if (props)
25
+ for (const key in instance)
26
+ if (props.hasOwnProperty(key))
27
+ instance[key] = props[key];
28
+ if (!repeat)
29
+ shouldApply = false;
30
+ instance.set().then(() => onUpdate = refresh);
31
+ }
32
+ useModelContext(instance);
33
+ react.useEffect(() => {
34
+ onUpdate = refresh;
35
+ return () => {
36
+ onUpdate = null;
37
+ instance.null();
38
+ };
39
+ }, []);
40
+ return local;
41
+ };
42
+ }, []);
43
+ return hook(apply);
44
+ }
45
+ function useModelContext(applyTo) {
46
+ const context = react.useContext(Shared);
47
+ if (!applyTo || Applied.has(applyTo))
48
+ return context;
49
+ const pending = Expects.get(applyTo);
50
+ if (pending) {
51
+ pending.forEach(init => init(context));
52
+ Applied.set(applyTo, context);
53
+ Expects.delete(applyTo);
54
+ }
55
+ }
56
+ function getContext(from) {
57
+ let waiting = Expects.get(from);
58
+ if (!waiting)
59
+ Expects.set(from, waiting = []);
60
+ return (callback) => {
61
+ const applied = Applied.get(from);
62
+ if (applied)
63
+ callback(applied);
64
+ else
65
+ waiting.push(callback);
17
66
  };
67
+ }
68
+ function setContext(to, context) {
69
+ const pending = Expects.get(to);
70
+ if (pending)
71
+ pending.forEach(cb => cb(context));
72
+ }
73
+
74
+ function useRemote(argument) {
75
+ const context = useModelContext();
76
+ const state = react.useState(0);
77
+ const hook = react.useMemo(() => {
78
+ const notFound = () => new Error(`Could not find ${this} in context.`);
79
+ const refresh = () => state[1](x => x + 1);
80
+ const instance = context.get(this);
81
+ let onUpdate;
82
+ let value;
83
+ if (typeof argument !== "function") {
84
+ if (instance)
85
+ value = argument === undefined
86
+ ? mvc.Control.watch(instance, k => k ? onUpdate : undefined)
87
+ : instance;
88
+ else if (argument !== false)
89
+ throw notFound();
90
+ return () => {
91
+ react.useEffect(() => {
92
+ onUpdate = refresh;
93
+ return () => {
94
+ onUpdate = null;
95
+ };
96
+ }, []);
97
+ return value;
98
+ };
99
+ }
100
+ let compute = argument;
101
+ let suspense;
102
+ let getValue;
103
+ let factory;
104
+ let proxy;
105
+ if (!instance)
106
+ throw notFound();
107
+ function forceUpdate(action) {
108
+ if (typeof action == "function")
109
+ action = action();
110
+ if (getValue)
111
+ didUpdate(getValue());
112
+ else
113
+ refresh();
114
+ if (action)
115
+ return action.finally(refresh);
116
+ }
117
+ function didUpdate(got) {
118
+ value = got;
119
+ if (suspense) {
120
+ suspense();
121
+ suspense = undefined;
122
+ }
123
+ else
124
+ refresh();
125
+ }
126
+ proxy = mvc.Control.watch(instance, () => factory ? null : onUpdate);
127
+ getValue = () => compute.call(proxy, proxy, forceUpdate);
128
+ value = getValue();
129
+ if (value === null) {
130
+ getValue = undefined;
131
+ onUpdate = null;
132
+ return;
133
+ }
134
+ if (typeof value == "function") {
135
+ const get = value;
136
+ mvc.Control.watch(proxy, () => onUpdate);
137
+ factory = true;
138
+ compute = () => get();
139
+ value = get();
140
+ }
141
+ if (value instanceof Promise) {
142
+ onUpdate = null;
143
+ value.then(didUpdate);
144
+ value = undefined;
145
+ }
146
+ else
147
+ onUpdate = () => {
148
+ const next = getValue();
149
+ if (value !== next)
150
+ didUpdate(next);
151
+ };
152
+ return () => {
153
+ react.useEffect(() => () => {
154
+ onUpdate = null;
155
+ }, []);
156
+ if (value !== undefined)
157
+ return value;
158
+ if (onUpdate)
159
+ return null;
160
+ throw new Promise(res => suspense = res);
161
+ };
162
+ }, []);
163
+ return hook ? hook() : null;
164
+ }
165
+
166
+ function Consumer(props) {
167
+ const { children, has, get, for: Type } = props;
168
+ if (typeof children == "function")
169
+ return children(Type.get());
170
+ const callback = has || get;
171
+ if (typeof callback == "function")
172
+ callback(Type.get(!!has));
173
+ return null;
174
+ }
175
+
176
+ function Provider(props) {
177
+ let { for: included, use: assign, children } = props;
178
+ const context = useModelContext();
179
+ const value = react.useMemo(() => context.push(), []);
18
180
  if (!included)
19
181
  reject(included);
20
182
  if (typeof included == "function" || included instanceof mvc.Model)
@@ -23,76 +185,24 @@ function Provider(props) {
23
185
  if (!(mvc.Model.is(V) || V instanceof mvc.Model))
24
186
  reject(`${V} as ${K}`);
25
187
  });
26
- context.include(included).forEach((isExplicit, model) => {
188
+ value.include(included).forEach((isExplicit, model) => {
27
189
  if (assign && isExplicit)
28
190
  for (const K in assign)
29
191
  if (K in model)
30
192
  model[K] = assign[K];
31
- const pending = Pending.get(model);
32
- if (pending)
33
- pending.forEach(cb => cb(context));
193
+ setContext(model, value);
34
194
  });
35
- react.useLayoutEffect(() => () => context.pop(), []);
36
- return react.createElement(LookupContext.Provider, { value: context, key: context.key }, props.fallback == false
37
- ? props.children
38
- : react.createElement(react.Suspense, { fallback: props.fallback || null }, props.children));
195
+ react.useEffect(() => () => value.pop(), []);
196
+ return react.createElement(Shared.Provider, { key: value.key, value }, children);
197
+ }
198
+ function reject(argument) {
199
+ throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
39
200
  }
40
201
 
41
- function Consumer(props) {
42
- const { children, has, get, for: Type } = props;
43
- if (typeof children == "function")
44
- return children(Type.get());
45
- const callback = has || get;
46
- if (typeof callback == "function")
47
- callback(Type.get(!!has));
48
- return null;
49
- }
202
+ mvc.Model.get = useRemote;
203
+ mvc.Model.use = useLocal;
204
+ mvc.get.from = getContext;
50
205
 
51
- const Applied = new WeakMap();
52
- mvc.Control.use = (adapter) => {
53
- const state = react.useState(0);
54
- const hook = react.useMemo(() => adapter(state[1]), []);
55
- const applied = Applied.get(hook.instance);
56
- if (applied)
57
- useLookup();
58
- else if (applied === undefined) {
59
- const pending = Pending.get(hook.instance);
60
- if (pending) {
61
- const local = useLookup();
62
- pending.forEach(init => init(local));
63
- Applied.set(hook.instance, local);
64
- Pending.delete(hook.instance);
65
- }
66
- }
67
- react.useEffect(hook.mount, []);
68
- return hook.render;
69
- };
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.useEffect(hook.mount, []);
77
- return hook.render();
78
- };
79
- mvc.Control.has = (model) => {
80
- let pending = Pending.get(model);
81
- if (!pending)
82
- Pending.set(model, pending = []);
83
- return (callback) => {
84
- const applied = Applied.get(model);
85
- if (applied)
86
- callback(applied);
87
- else
88
- pending.push(callback);
89
- };
90
- };
91
-
92
- Object.defineProperty(exports, 'Model', {
93
- enumerable: true,
94
- get: function () { return mvc.Model; }
95
- });
96
206
  Object.defineProperty(exports, 'default', {
97
207
  enumerable: true,
98
208
  get: function () { return mvc.Model; }
package/package.json CHANGED
@@ -29,18 +29,17 @@
29
29
  "preversion": "npm run test && npm run build"
30
30
  },
31
31
  "dependencies": {
32
- "@expressive/mvc": "^0.33.0"
32
+ "@expressive/mvc": "^0.34.0"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "react": ">=16.8.0 <19.0.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@testing-library/react-hooks": "^8.0.0",
39
- "@types/react": "^17.0.11",
38
+ "@types/react": "^18.2.14",
40
39
  "@types/react-test-renderer": "^18.0.0",
41
- "react": ">=17.0.2",
42
- "react-test-renderer": "^17.0.2"
40
+ "react": "^18.2.0",
41
+ "react-test-renderer": "^18.2.0"
43
42
  },
44
- "version": "0.33.0",
45
- "gitHead": "b6cd486f362d8188fcf11dde504978c45638ab40"
43
+ "version": "0.34.0",
44
+ "gitHead": "7e5d2acc3a45b1d51e05c0b82516c6cbdcbe94fb"
46
45
  }