@expressive/react 0.34.0 → 0.35.0-alpha.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.
@@ -55,4 +55,42 @@ declare namespace Provider {
55
55
  }
56
56
  declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): Provider.Element;
57
57
 
58
+ /** Type may not be undefined - instead will be null. */
59
+ type NoVoid<T> = T extends undefined | void ? null : T;
60
+ declare module '@expressive/mvc' {
61
+ namespace Model {
62
+ namespace GetFactory {
63
+ type Refresh = {
64
+ /** Request a refresh for current component. */
65
+ (): void;
66
+ /**
67
+ * Request a refresh and again after promise either resolves or rejects.
68
+ *
69
+ * @returns Promise which resolves, after refresh, to same value as input.
70
+ */
71
+ <T = void>(passthru: Promise<T>): Promise<T>;
72
+ /**
73
+ * Request refresh before and after async function.
74
+ * A refresh will occur both before and after the given function.
75
+ *
76
+ * Any actions performed before first `await` will occur prior to refresh.
77
+ *
78
+ * @returns Promise which resolves, after refresh, return value.
79
+ */
80
+ <T = void>(invoke: () => Promise<T>): Promise<T>;
81
+ };
82
+ }
83
+ type GetFactory<T extends Model, R> = (this: T, current: T, refresh: GetFactory.Refresh) => R;
84
+ type UseCallback<T extends Model> = (instance: T) => void;
85
+ /** Fetch instance of this class from context. */
86
+ function get<T extends Model>(this: Model.Type<T>, ignore?: true): T;
87
+ /** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
88
+ function get<T extends Model>(this: Model.Type<T>, required: boolean): T | undefined;
89
+ function get<T extends Model, R>(this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;
90
+ function get<T extends Model, R>(this: Model.Type<T>, factory: GetFactory<T, null>): NoVoid<R> | null;
91
+ function use<T extends Model>(this: Model.New<T>, apply?: Model.Values<T>, repeat?: boolean): T;
92
+ function use<T extends Model>(this: Model.New<T>, callback?: UseCallback<T>, repeat?: boolean): T;
93
+ }
94
+ }
95
+
58
96
  export { Consumer, Provider };
package/dist/index.js ADDED
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ Consumer: () => Consumer,
25
+ Provider: () => Provider,
26
+ default: () => import_mvc3.Model
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+ var import_mvc3 = require("@expressive/mvc");
30
+
31
+ // src/useLocal.ts
32
+ var import_mvc = require("@expressive/mvc");
33
+ var import_react = require("react");
34
+ var Shared = (0, import_react.createContext)(new import_mvc.Context());
35
+ var Register = /* @__PURE__ */ new WeakMap();
36
+ function useLocal(apply, repeat) {
37
+ const state = (0, import_react.useState)(0);
38
+ const hook = (0, import_react.useMemo)(() => {
39
+ const instance = this.new();
40
+ let local;
41
+ let enabled;
42
+ let shouldApply = !!apply;
43
+ const detach = instance.get((current) => {
44
+ local = current;
45
+ if (enabled)
46
+ state[1]((x) => x + 1);
47
+ });
48
+ return (props) => {
49
+ if (shouldApply) {
50
+ enabled = false;
51
+ if (typeof props == "function")
52
+ props(instance);
53
+ else if (props) {
54
+ for (const key in instance)
55
+ if (props.hasOwnProperty(key))
56
+ instance[key] = props[key];
57
+ }
58
+ if (!repeat)
59
+ shouldApply = false;
60
+ instance.set().then(() => enabled = true);
61
+ }
62
+ setContext(instance, (0, import_react.useContext)(Shared));
63
+ (0, import_react.useEffect)(() => {
64
+ enabled = true;
65
+ return () => {
66
+ detach();
67
+ instance.set(null);
68
+ };
69
+ }, []);
70
+ return local;
71
+ };
72
+ }, []);
73
+ return hook(apply);
74
+ }
75
+ function getContext(from, resolve) {
76
+ const context = Register.get(from);
77
+ if (context instanceof import_mvc.Context)
78
+ resolve(context);
79
+ else if (context)
80
+ context.push(resolve);
81
+ else
82
+ Register.set(from, [resolve]);
83
+ }
84
+ function setContext(model, context) {
85
+ const waiting = Register.get(model);
86
+ if (waiting instanceof Array)
87
+ waiting.forEach((cb) => cb(context));
88
+ Register.set(model, context);
89
+ }
90
+
91
+ // src/useRemote.ts
92
+ var import_react2 = require("react");
93
+ function useRemote(argument) {
94
+ const context = (0, import_react2.useContext)(Shared);
95
+ const state = (0, import_react2.useState)(0);
96
+ const hook = (0, import_react2.useMemo)(() => {
97
+ const instance = context.get(this);
98
+ const refresh = () => state[1]((x) => x + 1);
99
+ let value;
100
+ let release;
101
+ if (!instance)
102
+ if (argument !== false)
103
+ throw new Error(`Could not find ${this} in context.`);
104
+ else
105
+ return void 0;
106
+ if (typeof argument === "boolean")
107
+ return () => instance;
108
+ function forceUpdate(action) {
109
+ if (typeof action == "function")
110
+ action = action();
111
+ refresh();
112
+ if (action)
113
+ return action.finally(refresh);
114
+ }
115
+ release = instance.get((current) => {
116
+ if (typeof argument === "function") {
117
+ const next = argument.call(current, current, forceUpdate);
118
+ if (next === value)
119
+ return;
120
+ value = next;
121
+ } else
122
+ value = current;
123
+ if (release)
124
+ refresh();
125
+ });
126
+ if (value === null) {
127
+ release();
128
+ return () => null;
129
+ }
130
+ if (value instanceof Promise) {
131
+ let error;
132
+ release();
133
+ value.then((x) => value = x).catch((e) => error = e).finally(refresh);
134
+ value = null;
135
+ return () => {
136
+ if (error)
137
+ throw error;
138
+ return value === void 0 ? null : value;
139
+ };
140
+ }
141
+ return () => {
142
+ (0, import_react2.useEffect)(() => release, []);
143
+ return value === void 0 ? null : value;
144
+ };
145
+ }, []);
146
+ return hook && hook();
147
+ }
148
+
149
+ // src/index.ts
150
+ __reExport(src_exports, require("@expressive/mvc"), module.exports);
151
+
152
+ // src/consumer.ts
153
+ function Consumer(props) {
154
+ const { children, has, get, for: Type } = props;
155
+ if (typeof children == "function")
156
+ return children(Type.get());
157
+ const callback = has || get;
158
+ if (typeof callback == "function")
159
+ callback(Type.get(!!has));
160
+ return null;
161
+ }
162
+
163
+ // src/provider.ts
164
+ var import_mvc2 = require("@expressive/mvc");
165
+ var import_react3 = require("react");
166
+ function Provider(props) {
167
+ let { for: included, use: assign, children } = props;
168
+ const context = (0, import_react3.useContext)(Shared);
169
+ const value = (0, import_react3.useMemo)(() => context.push(), []);
170
+ if (!included)
171
+ reject(included);
172
+ if (typeof included == "function" || included instanceof import_mvc2.Model)
173
+ included = { [0]: included };
174
+ Object.entries(included).forEach(([K, V]) => {
175
+ if (!(import_mvc2.Model.is(V) || V instanceof import_mvc2.Model))
176
+ reject(`${V} as ${K}`);
177
+ });
178
+ value.include(included).forEach((isExplicit, model) => {
179
+ if (assign && isExplicit) {
180
+ for (const K in assign)
181
+ if (K in model)
182
+ model[K] = assign[K];
183
+ }
184
+ setContext(model, value);
185
+ });
186
+ (0, import_react3.useEffect)(() => () => value.pop(), []);
187
+ return (0, import_react3.createElement)(Shared.Provider, { key: value.key, value }, children);
188
+ }
189
+ function reject(argument) {
190
+ throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
191
+ }
192
+
193
+ // src/index.ts
194
+ import_mvc3.Model.get = useRemote;
195
+ import_mvc3.Model.use = useLocal;
196
+ import_mvc3.Context.resolve = getContext;
197
+ // Annotate the CommonJS export names for ESM import in node:
198
+ 0 && (module.exports = {
199
+ Consumer,
200
+ Provider,
201
+ ...require("@expressive/mvc")
202
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,174 @@
1
+ // src/index.ts
2
+ import { Context as Context3, Model as Model3 } from "@expressive/mvc";
3
+
4
+ // src/useLocal.ts
5
+ import { Context } from "@expressive/mvc";
6
+ import { createContext, useContext, useEffect, useMemo, useState } from "react";
7
+ var Shared = createContext(new Context());
8
+ var Register = /* @__PURE__ */ new WeakMap();
9
+ function useLocal(apply, repeat) {
10
+ const state = useState(0);
11
+ const hook = useMemo(() => {
12
+ const instance = this.new();
13
+ let local;
14
+ let enabled;
15
+ let shouldApply = !!apply;
16
+ const detach = instance.get((current) => {
17
+ local = current;
18
+ if (enabled)
19
+ state[1]((x) => x + 1);
20
+ });
21
+ return (props) => {
22
+ if (shouldApply) {
23
+ enabled = false;
24
+ if (typeof props == "function")
25
+ props(instance);
26
+ else if (props) {
27
+ for (const key in instance)
28
+ if (props.hasOwnProperty(key))
29
+ instance[key] = props[key];
30
+ }
31
+ if (!repeat)
32
+ shouldApply = false;
33
+ instance.set().then(() => enabled = true);
34
+ }
35
+ setContext(instance, useContext(Shared));
36
+ useEffect(() => {
37
+ enabled = true;
38
+ return () => {
39
+ detach();
40
+ instance.set(null);
41
+ };
42
+ }, []);
43
+ return local;
44
+ };
45
+ }, []);
46
+ return hook(apply);
47
+ }
48
+ function getContext(from, resolve) {
49
+ const context = Register.get(from);
50
+ if (context instanceof Context)
51
+ resolve(context);
52
+ else if (context)
53
+ context.push(resolve);
54
+ else
55
+ Register.set(from, [resolve]);
56
+ }
57
+ function setContext(model, context) {
58
+ const waiting = Register.get(model);
59
+ if (waiting instanceof Array)
60
+ waiting.forEach((cb) => cb(context));
61
+ Register.set(model, context);
62
+ }
63
+
64
+ // src/useRemote.ts
65
+ import { useContext as useContext2, useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
66
+ function useRemote(argument) {
67
+ const context = useContext2(Shared);
68
+ const state = useState2(0);
69
+ const hook = useMemo2(() => {
70
+ const instance = context.get(this);
71
+ const refresh = () => state[1]((x) => x + 1);
72
+ let value;
73
+ let release;
74
+ if (!instance)
75
+ if (argument !== false)
76
+ throw new Error(`Could not find ${this} in context.`);
77
+ else
78
+ return void 0;
79
+ if (typeof argument === "boolean")
80
+ return () => instance;
81
+ function forceUpdate(action) {
82
+ if (typeof action == "function")
83
+ action = action();
84
+ refresh();
85
+ if (action)
86
+ return action.finally(refresh);
87
+ }
88
+ release = instance.get((current) => {
89
+ if (typeof argument === "function") {
90
+ const next = argument.call(current, current, forceUpdate);
91
+ if (next === value)
92
+ return;
93
+ value = next;
94
+ } else
95
+ value = current;
96
+ if (release)
97
+ refresh();
98
+ });
99
+ if (value === null) {
100
+ release();
101
+ return () => null;
102
+ }
103
+ if (value instanceof Promise) {
104
+ let error;
105
+ release();
106
+ value.then((x) => value = x).catch((e) => error = e).finally(refresh);
107
+ value = null;
108
+ return () => {
109
+ if (error)
110
+ throw error;
111
+ return value === void 0 ? null : value;
112
+ };
113
+ }
114
+ return () => {
115
+ useEffect2(() => release, []);
116
+ return value === void 0 ? null : value;
117
+ };
118
+ }, []);
119
+ return hook && hook();
120
+ }
121
+
122
+ // src/index.ts
123
+ export * from "@expressive/mvc";
124
+
125
+ // src/consumer.ts
126
+ function Consumer(props) {
127
+ const { children, has, get, for: Type } = props;
128
+ if (typeof children == "function")
129
+ return children(Type.get());
130
+ const callback = has || get;
131
+ if (typeof callback == "function")
132
+ callback(Type.get(!!has));
133
+ return null;
134
+ }
135
+
136
+ // src/provider.ts
137
+ import { Model as Model2 } from "@expressive/mvc";
138
+ import { createElement, useContext as useContext3, useEffect as useEffect3, useMemo as useMemo3 } from "react";
139
+ function Provider(props) {
140
+ let { for: included, use: assign, children } = props;
141
+ const context = useContext3(Shared);
142
+ const value = useMemo3(() => context.push(), []);
143
+ if (!included)
144
+ reject(included);
145
+ if (typeof included == "function" || included instanceof Model2)
146
+ included = { [0]: included };
147
+ Object.entries(included).forEach(([K, V]) => {
148
+ if (!(Model2.is(V) || V instanceof Model2))
149
+ reject(`${V} as ${K}`);
150
+ });
151
+ value.include(included).forEach((isExplicit, model) => {
152
+ if (assign && isExplicit) {
153
+ for (const K in assign)
154
+ if (K in model)
155
+ model[K] = assign[K];
156
+ }
157
+ setContext(model, value);
158
+ });
159
+ useEffect3(() => () => value.pop(), []);
160
+ return createElement(Shared.Provider, { key: value.key, value }, children);
161
+ }
162
+ function reject(argument) {
163
+ throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
164
+ }
165
+
166
+ // src/index.ts
167
+ Model3.get = useRemote;
168
+ Model3.use = useLocal;
169
+ Context3.resolve = getContext;
170
+ export {
171
+ Consumer,
172
+ Provider,
173
+ Model3 as default
174
+ };
package/package.json CHANGED
@@ -12,24 +12,26 @@
12
12
  "access": "public"
13
13
  },
14
14
  "keywords": [
15
+ "expressive",
15
16
  "react",
16
17
  "mvc"
17
18
  ],
18
- "main": "lib/index.js",
19
- "module": "lib/index.esm.js",
20
- "types": "lib/index.d.ts",
19
+ "main": "dist/index.js",
20
+ "module": "dist/index.mjs",
21
+ "types": "dist/index.d.ts",
21
22
  "files": [
22
- "lib"
23
+ "dist/index.js",
24
+ "dist/index.mjs",
25
+ "dist/index.d.ts"
23
26
  ],
24
27
  "scripts": {
25
- "build": "rollup --config",
26
- "watch": "jest --watchAll",
27
- "test": "jest --collectCoverage",
28
- "prebuild": "rm -rf lib/",
28
+ "build": "tsup src/index.ts --dts --format esm,cjs",
29
+ "test": "tsc --noEmit && jest --collectCoverage",
30
+ "prebuild": "rm -rf dist",
29
31
  "preversion": "npm run test && npm run build"
30
32
  },
31
33
  "dependencies": {
32
- "@expressive/mvc": "^0.34.0"
34
+ "@expressive/mvc": "^0.35.0-alpha.0"
33
35
  },
34
36
  "peerDependencies": {
35
37
  "react": ">=16.8.0 <19.0.0"
@@ -40,6 +42,6 @@
40
42
  "react": "^18.2.0",
41
43
  "react-test-renderer": "^18.2.0"
42
44
  },
43
- "version": "0.34.0",
44
- "gitHead": "7e5d2acc3a45b1d51e05c0b82516c6cbdcbe94fb"
45
+ "version": "0.35.0-alpha.0",
46
+ "gitHead": "17db508f601400ac08e6f821376e7a9403525d5b"
45
47
  }
package/lib/index.esm.js DELETED
@@ -1,204 +0,0 @@
1
- import { Context, Control, Model, get } from '@expressive/mvc';
2
- export * from '@expressive/mvc';
3
- export { Model as default } from '@expressive/mvc';
4
- import { createContext, useState, useMemo, useEffect, useContext, createElement } from 'react';
5
-
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);
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(), []);
178
- if (!included)
179
- reject(included);
180
- if (typeof included == "function" || included instanceof Model)
181
- included = { [0]: included };
182
- Object.entries(included).forEach(([K, V]) => {
183
- if (!(Model.is(V) || V instanceof Model))
184
- reject(`${V} as ${K}`);
185
- });
186
- value.include(included).forEach((isExplicit, model) => {
187
- if (assign && isExplicit)
188
- for (const K in assign)
189
- if (K in model)
190
- model[K] = assign[K];
191
- setContext(model, value);
192
- });
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}.`);
198
- }
199
-
200
- Model.get = useRemote;
201
- Model.use = useLocal;
202
- get.from = getContext;
203
-
204
- export { Consumer, Provider };
package/lib/index.js DELETED
@@ -1,217 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var mvc = require('@expressive/mvc');
6
- var react = require('react');
7
-
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);
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(), []);
180
- if (!included)
181
- reject(included);
182
- if (typeof included == "function" || included instanceof mvc.Model)
183
- included = { [0]: included };
184
- Object.entries(included).forEach(([K, V]) => {
185
- if (!(mvc.Model.is(V) || V instanceof mvc.Model))
186
- reject(`${V} as ${K}`);
187
- });
188
- value.include(included).forEach((isExplicit, model) => {
189
- if (assign && isExplicit)
190
- for (const K in assign)
191
- if (K in model)
192
- model[K] = assign[K];
193
- setContext(model, value);
194
- });
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}.`);
200
- }
201
-
202
- mvc.Model.get = useRemote;
203
- mvc.Model.use = useLocal;
204
- mvc.get.from = getContext;
205
-
206
- Object.defineProperty(exports, 'default', {
207
- enumerable: true,
208
- get: function () { return mvc.Model; }
209
- });
210
- exports.Consumer = Consumer;
211
- exports.Provider = Provider;
212
- Object.keys(mvc).forEach(function (k) {
213
- if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
214
- enumerable: true,
215
- get: function () { return mvc[k]; }
216
- });
217
- });