@expressive/react 0.35.5 → 0.36.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/dist/index.d.ts +26 -19
- package/dist/index.js +76 -73
- package/dist/index.mjs +76 -73
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Model, Context } from '@expressive/mvc';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
import { ReactElement, FunctionComponentElement, ProviderProps } from 'react';
|
|
2
|
+
export { Model, add, Model as default, get, ref, set, use } from '@expressive/mvc';
|
|
3
|
+
import { FunctionComponentElement, ReactNode } from 'react';
|
|
5
4
|
|
|
6
5
|
declare namespace Consumer {
|
|
7
6
|
type HasProps<T extends Model> = {
|
|
@@ -30,14 +29,18 @@ declare namespace Consumer {
|
|
|
30
29
|
* Similar to `get()`, updates to properties accessed in
|
|
31
30
|
* this function will cause a refresh when they change.
|
|
32
31
|
*/
|
|
33
|
-
children: (value: T) =>
|
|
32
|
+
children: (value: T) => JSX.Element | null;
|
|
34
33
|
};
|
|
35
34
|
type Props<T extends Model> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
36
35
|
}
|
|
37
|
-
declare function Consumer<T extends Model>(props: Consumer.Props<T>):
|
|
36
|
+
declare function Consumer<T extends Model>(props: Consumer.Props<T>): JSX.Element | null;
|
|
38
37
|
|
|
38
|
+
type Node = JSX.Element | Iterable<Node> | string | number | boolean | null | undefined;
|
|
39
39
|
declare namespace Provider {
|
|
40
|
-
type Element = FunctionComponentElement<
|
|
40
|
+
type Element = FunctionComponentElement<{
|
|
41
|
+
value: Context;
|
|
42
|
+
children?: ReactNode;
|
|
43
|
+
}>;
|
|
41
44
|
type Item = Model | Model.New;
|
|
42
45
|
type Multiple<T extends Item = Item> = {
|
|
43
46
|
[key: string | number]: T;
|
|
@@ -46,12 +49,12 @@ declare namespace Provider {
|
|
|
46
49
|
type Props<T extends Item = Item> = MultipleProps<T> | NormalProps<T>;
|
|
47
50
|
type NormalProps<E, I = Instance<E>> = {
|
|
48
51
|
for: E;
|
|
49
|
-
children?:
|
|
52
|
+
children?: Node;
|
|
50
53
|
use?: Model.Values<I>;
|
|
51
54
|
};
|
|
52
55
|
type MultipleProps<T extends Item> = {
|
|
53
56
|
for: Multiple<T>;
|
|
54
|
-
children?:
|
|
57
|
+
children?: Node;
|
|
55
58
|
use?: Model.Values<Instance<T>>;
|
|
56
59
|
};
|
|
57
60
|
}
|
|
@@ -61,37 +64,41 @@ declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): Pr
|
|
|
61
64
|
type NoVoid<T> = T extends undefined | void ? null : T;
|
|
62
65
|
declare module '@expressive/mvc' {
|
|
63
66
|
namespace Model {
|
|
64
|
-
namespace
|
|
65
|
-
type
|
|
67
|
+
namespace get {
|
|
68
|
+
type Factory<T extends Model, R> = (this: T, current: T, refresh: ForceRefresh) => R;
|
|
69
|
+
type ForceRefresh = {
|
|
66
70
|
/** Request a refresh for current component. */
|
|
67
71
|
(): void;
|
|
68
72
|
/**
|
|
69
73
|
* Request a refresh and again after promise either resolves or rejects.
|
|
70
74
|
*
|
|
71
|
-
* @
|
|
75
|
+
* @param waitFor Promise to wait for.
|
|
76
|
+
* @returns Promise which resolves, after refresh, to same value as `waitFor`.
|
|
72
77
|
*/
|
|
73
|
-
<T = void>(
|
|
78
|
+
<T = void>(waitFor: Promise<T>): Promise<T>;
|
|
74
79
|
/**
|
|
75
80
|
* Request refresh before and after async function.
|
|
76
81
|
* A refresh will occur both before and after the given function.
|
|
77
82
|
*
|
|
78
|
-
* Any actions performed before first `await` will occur prior to refresh.
|
|
83
|
+
* **Note:** Any actions performed before first `await` will occur prior to refresh.
|
|
79
84
|
*
|
|
80
|
-
* @
|
|
85
|
+
* @param invoke Async function to invoke.
|
|
86
|
+
* @returns Promise which resolves returned value after refresh.
|
|
81
87
|
*/
|
|
82
88
|
<T = void>(invoke: () => Promise<T>): Promise<T>;
|
|
83
89
|
};
|
|
84
90
|
}
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
namespace use {
|
|
92
|
+
type Callback<T extends Model> = (instance: T) => void;
|
|
93
|
+
}
|
|
87
94
|
/** Fetch instance of this class from context. */
|
|
88
95
|
function get<T extends Model>(this: Model.Type<T>, ignore?: true): T;
|
|
89
96
|
/** Fetch instance of this class optionally. May be undefined, but will never subscribe. */
|
|
90
97
|
function get<T extends Model>(this: Model.Type<T>, required: boolean): T | undefined;
|
|
91
|
-
function get<T extends Model, R>(this: Model.Type<T>, factory:
|
|
92
|
-
function get<T extends Model, R>(this: Model.Type<T>, factory:
|
|
98
|
+
function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, Promise<R> | R>): NoVoid<R>;
|
|
99
|
+
function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
|
|
93
100
|
function use<T extends Model>(this: Model.New<T>, apply?: Model.Values<T>, repeat?: boolean): T;
|
|
94
|
-
function use<T extends Model>(this: Model.New<T>, callback?:
|
|
101
|
+
function use<T extends Model>(this: Model.New<T>, callback?: use.Callback<T>, repeat?: boolean): T;
|
|
95
102
|
}
|
|
96
103
|
}
|
|
97
104
|
|
package/dist/index.js
CHANGED
|
@@ -15,35 +15,63 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
19
|
|
|
21
20
|
// src/index.ts
|
|
22
21
|
var src_exports = {};
|
|
23
22
|
__export(src_exports, {
|
|
24
23
|
Consumer: () => Consumer,
|
|
24
|
+
Model: () => import_mvc2.Model,
|
|
25
25
|
Provider: () => Provider,
|
|
26
|
-
|
|
26
|
+
add: () => import_mvc3.add,
|
|
27
|
+
default: () => import_mvc2.Model,
|
|
28
|
+
get: () => import_mvc3.get,
|
|
29
|
+
ref: () => import_mvc3.ref,
|
|
30
|
+
set: () => import_mvc3.set,
|
|
31
|
+
use: () => import_mvc3.use
|
|
27
32
|
});
|
|
28
33
|
module.exports = __toCommonJS(src_exports);
|
|
29
|
-
var
|
|
34
|
+
var import_mvc2 = require("@expressive/mvc");
|
|
30
35
|
|
|
31
|
-
// src/
|
|
36
|
+
// src/useContext.ts
|
|
32
37
|
var import_mvc = require("@expressive/mvc");
|
|
33
38
|
var import_react = require("react");
|
|
39
|
+
var import_react2 = require("react");
|
|
34
40
|
var Shared = (0, import_react.createContext)(new import_mvc.Context());
|
|
35
41
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
42
|
+
function useContext() {
|
|
43
|
+
return (0, import_react.useContext)(Shared);
|
|
44
|
+
}
|
|
45
|
+
function createContext(value, children) {
|
|
46
|
+
return (0, import_react.createElement)(Shared.Provider, { key: value.key, value }, children);
|
|
47
|
+
}
|
|
48
|
+
function getContext(model, resolve) {
|
|
49
|
+
const waiting = Register.get(model);
|
|
50
|
+
if (waiting instanceof import_mvc.Context)
|
|
51
|
+
resolve(waiting);
|
|
52
|
+
else if (waiting)
|
|
53
|
+
waiting.push(resolve);
|
|
54
|
+
else
|
|
55
|
+
Register.set(model, [resolve]);
|
|
56
|
+
}
|
|
57
|
+
function setContext(model, context = useContext()) {
|
|
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/useLocal.ts
|
|
36
65
|
function useLocal(apply, repeat) {
|
|
37
|
-
const state = (0,
|
|
38
|
-
const hook = (0, import_react.useMemo)(() => {
|
|
39
|
-
const instance = this.new();
|
|
40
|
-
let local;
|
|
41
|
-
let enabled;
|
|
66
|
+
const state = (0, import_react2.useState)(() => {
|
|
42
67
|
let shouldApply = !!apply;
|
|
43
|
-
|
|
68
|
+
let enabled;
|
|
69
|
+
let local;
|
|
70
|
+
const instance = this.new();
|
|
71
|
+
const release = instance.get((current) => {
|
|
44
72
|
local = current;
|
|
45
73
|
if (enabled)
|
|
46
|
-
state[1]((x) => x
|
|
74
|
+
state[1]((x) => x.bind(null));
|
|
47
75
|
});
|
|
48
76
|
return (props) => {
|
|
49
77
|
if (shouldApply) {
|
|
@@ -63,50 +91,31 @@ function useLocal(apply, repeat) {
|
|
|
63
91
|
else
|
|
64
92
|
enabled = true;
|
|
65
93
|
}
|
|
66
|
-
setContext(instance
|
|
67
|
-
(0,
|
|
94
|
+
setContext(instance);
|
|
95
|
+
(0, import_react2.useEffect)(() => {
|
|
68
96
|
enabled = true;
|
|
69
97
|
return () => {
|
|
70
|
-
|
|
98
|
+
release();
|
|
71
99
|
instance.set(null);
|
|
72
100
|
};
|
|
73
101
|
}, []);
|
|
74
102
|
return local;
|
|
75
103
|
};
|
|
76
|
-
}
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
function getContext(from, resolve) {
|
|
80
|
-
const context = Register.get(from);
|
|
81
|
-
if (context instanceof import_mvc.Context)
|
|
82
|
-
resolve(context);
|
|
83
|
-
else if (context)
|
|
84
|
-
context.push(resolve);
|
|
85
|
-
else
|
|
86
|
-
Register.set(from, [resolve]);
|
|
87
|
-
}
|
|
88
|
-
function setContext(model, context) {
|
|
89
|
-
const waiting = Register.get(model);
|
|
90
|
-
if (waiting instanceof Array)
|
|
91
|
-
waiting.forEach((cb) => cb(context));
|
|
92
|
-
Register.set(model, context);
|
|
104
|
+
});
|
|
105
|
+
return state[0](apply);
|
|
93
106
|
}
|
|
94
107
|
|
|
95
108
|
// src/useRemote.ts
|
|
96
|
-
var import_react2 = require("react");
|
|
97
109
|
function useRemote(argument) {
|
|
98
|
-
const context =
|
|
99
|
-
const state = (0, import_react2.useState)(
|
|
100
|
-
const hook = (0, import_react2.useMemo)(() => {
|
|
110
|
+
const context = useContext();
|
|
111
|
+
const state = (0, import_react2.useState)(() => {
|
|
101
112
|
const instance = context.get(this);
|
|
102
|
-
const refresh = () => state[1]((x) => x
|
|
103
|
-
let value;
|
|
104
|
-
let release;
|
|
113
|
+
const refresh = () => state[1]((x) => x.bind(null));
|
|
105
114
|
if (!instance)
|
|
106
|
-
if (argument
|
|
107
|
-
|
|
115
|
+
if (argument === false)
|
|
116
|
+
return new Function();
|
|
108
117
|
else
|
|
109
|
-
|
|
118
|
+
throw new Error(`Could not find ${this} in context.`);
|
|
110
119
|
if (typeof argument === "boolean")
|
|
111
120
|
return () => instance;
|
|
112
121
|
function forceUpdate(action) {
|
|
@@ -116,6 +125,8 @@ function useRemote(argument) {
|
|
|
116
125
|
if (action)
|
|
117
126
|
return action.finally(refresh);
|
|
118
127
|
}
|
|
128
|
+
let release;
|
|
129
|
+
let value;
|
|
119
130
|
release = instance.get((current) => {
|
|
120
131
|
if (typeof argument === "function") {
|
|
121
132
|
const next = argument.call(current, current, forceUpdate);
|
|
@@ -127,10 +138,6 @@ function useRemote(argument) {
|
|
|
127
138
|
if (release)
|
|
128
139
|
refresh();
|
|
129
140
|
});
|
|
130
|
-
if (value === null) {
|
|
131
|
-
release();
|
|
132
|
-
return () => null;
|
|
133
|
-
}
|
|
134
141
|
if (value instanceof Promise) {
|
|
135
142
|
let error;
|
|
136
143
|
release();
|
|
@@ -142,64 +149,60 @@ function useRemote(argument) {
|
|
|
142
149
|
return value === void 0 ? null : value;
|
|
143
150
|
};
|
|
144
151
|
}
|
|
152
|
+
if (value === null) {
|
|
153
|
+
release();
|
|
154
|
+
return () => null;
|
|
155
|
+
}
|
|
145
156
|
return () => {
|
|
146
157
|
(0, import_react2.useEffect)(() => release, []);
|
|
147
158
|
return value === void 0 ? null : value;
|
|
148
159
|
};
|
|
149
|
-
}
|
|
150
|
-
return
|
|
160
|
+
});
|
|
161
|
+
return state[0]();
|
|
151
162
|
}
|
|
152
163
|
|
|
153
164
|
// src/index.ts
|
|
154
|
-
|
|
165
|
+
var import_mvc3 = require("@expressive/mvc");
|
|
155
166
|
|
|
156
167
|
// src/consumer.ts
|
|
157
168
|
function Consumer(props) {
|
|
158
|
-
const { children, has, get, for: Type } = props;
|
|
169
|
+
const { children, has, get: get2, for: Type } = props;
|
|
159
170
|
const instance = Type.get(!!has);
|
|
160
|
-
const callback = children || has ||
|
|
171
|
+
const callback = children || has || get2;
|
|
161
172
|
if (typeof callback == "function")
|
|
162
173
|
callback(instance);
|
|
163
174
|
return null;
|
|
164
175
|
}
|
|
165
176
|
|
|
166
177
|
// src/provider.ts
|
|
167
|
-
var import_mvc2 = require("@expressive/mvc");
|
|
168
|
-
var import_react3 = require("react");
|
|
169
178
|
function Provider(props) {
|
|
170
|
-
let { for: included, use: assign
|
|
171
|
-
const context =
|
|
172
|
-
const value = (0,
|
|
173
|
-
if (!included)
|
|
174
|
-
reject(included);
|
|
175
|
-
if (typeof included == "function" || included instanceof import_mvc2.Model)
|
|
176
|
-
included = { [0]: included };
|
|
177
|
-
Object.entries(included).forEach(([K, V]) => {
|
|
178
|
-
if (!(import_mvc2.Model.is(V) || V instanceof import_mvc2.Model))
|
|
179
|
-
reject(`${V} as ${K}`);
|
|
180
|
-
});
|
|
179
|
+
let { for: included, use: assign } = props;
|
|
180
|
+
const context = useContext();
|
|
181
|
+
const value = (0, import_react2.useMemo)(() => context.push(), []);
|
|
181
182
|
value.include(included).forEach((isExplicit, model) => {
|
|
182
183
|
if (assign && isExplicit) {
|
|
183
184
|
for (const K in assign)
|
|
184
185
|
if (K in model)
|
|
185
|
-
model
|
|
186
|
+
model.set(K, assign[K]);
|
|
186
187
|
}
|
|
187
188
|
setContext(model, value);
|
|
188
189
|
});
|
|
189
|
-
(0,
|
|
190
|
-
return (
|
|
191
|
-
}
|
|
192
|
-
function reject(argument) {
|
|
193
|
-
throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
|
|
190
|
+
(0, import_react2.useEffect)(() => () => value.pop(), []);
|
|
191
|
+
return createContext(value, props.children);
|
|
194
192
|
}
|
|
195
193
|
|
|
196
194
|
// src/index.ts
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
195
|
+
import_mvc2.Model.get = useRemote;
|
|
196
|
+
import_mvc2.Model.use = useLocal;
|
|
197
|
+
import_mvc2.Context.get = getContext;
|
|
200
198
|
// Annotate the CommonJS export names for ESM import in node:
|
|
201
199
|
0 && (module.exports = {
|
|
202
200
|
Consumer,
|
|
201
|
+
Model,
|
|
203
202
|
Provider,
|
|
204
|
-
|
|
203
|
+
add,
|
|
204
|
+
get,
|
|
205
|
+
ref,
|
|
206
|
+
set,
|
|
207
|
+
use
|
|
205
208
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,49 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { Context as
|
|
2
|
+
import { Context as Context2, Model as Model2 } from "@expressive/mvc";
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/useContext.ts
|
|
5
5
|
import { Context } from "@expressive/mvc";
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
createContext as reactCreateContext,
|
|
8
|
+
useContext as reactUseContext,
|
|
9
|
+
createElement
|
|
10
|
+
} from "react";
|
|
11
|
+
import { useState, useEffect, useMemo } from "react";
|
|
12
|
+
var Shared = reactCreateContext(new Context());
|
|
8
13
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
14
|
+
function useContext() {
|
|
15
|
+
return reactUseContext(Shared);
|
|
16
|
+
}
|
|
17
|
+
function createContext(value, children) {
|
|
18
|
+
return createElement(Shared.Provider, { key: value.key, value }, children);
|
|
19
|
+
}
|
|
20
|
+
function getContext(model, resolve) {
|
|
21
|
+
const waiting = Register.get(model);
|
|
22
|
+
if (waiting instanceof Context)
|
|
23
|
+
resolve(waiting);
|
|
24
|
+
else if (waiting)
|
|
25
|
+
waiting.push(resolve);
|
|
26
|
+
else
|
|
27
|
+
Register.set(model, [resolve]);
|
|
28
|
+
}
|
|
29
|
+
function setContext(model, context = useContext()) {
|
|
30
|
+
const waiting = Register.get(model);
|
|
31
|
+
if (waiting instanceof Array)
|
|
32
|
+
waiting.forEach((cb) => cb(context));
|
|
33
|
+
Register.set(model, context);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/useLocal.ts
|
|
9
37
|
function useLocal(apply, repeat) {
|
|
10
|
-
const state = useState(
|
|
11
|
-
const hook = useMemo(() => {
|
|
12
|
-
const instance = this.new();
|
|
13
|
-
let local;
|
|
14
|
-
let enabled;
|
|
38
|
+
const state = useState(() => {
|
|
15
39
|
let shouldApply = !!apply;
|
|
16
|
-
|
|
40
|
+
let enabled;
|
|
41
|
+
let local;
|
|
42
|
+
const instance = this.new();
|
|
43
|
+
const release = instance.get((current) => {
|
|
17
44
|
local = current;
|
|
18
45
|
if (enabled)
|
|
19
|
-
state[1]((x) => x
|
|
46
|
+
state[1]((x) => x.bind(null));
|
|
20
47
|
});
|
|
21
48
|
return (props) => {
|
|
22
49
|
if (shouldApply) {
|
|
@@ -36,50 +63,31 @@ function useLocal(apply, repeat) {
|
|
|
36
63
|
else
|
|
37
64
|
enabled = true;
|
|
38
65
|
}
|
|
39
|
-
setContext(instance
|
|
66
|
+
setContext(instance);
|
|
40
67
|
useEffect(() => {
|
|
41
68
|
enabled = true;
|
|
42
69
|
return () => {
|
|
43
|
-
|
|
70
|
+
release();
|
|
44
71
|
instance.set(null);
|
|
45
72
|
};
|
|
46
73
|
}, []);
|
|
47
74
|
return local;
|
|
48
75
|
};
|
|
49
|
-
}
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
function getContext(from, resolve) {
|
|
53
|
-
const context = Register.get(from);
|
|
54
|
-
if (context instanceof Context)
|
|
55
|
-
resolve(context);
|
|
56
|
-
else if (context)
|
|
57
|
-
context.push(resolve);
|
|
58
|
-
else
|
|
59
|
-
Register.set(from, [resolve]);
|
|
60
|
-
}
|
|
61
|
-
function setContext(model, context) {
|
|
62
|
-
const waiting = Register.get(model);
|
|
63
|
-
if (waiting instanceof Array)
|
|
64
|
-
waiting.forEach((cb) => cb(context));
|
|
65
|
-
Register.set(model, context);
|
|
76
|
+
});
|
|
77
|
+
return state[0](apply);
|
|
66
78
|
}
|
|
67
79
|
|
|
68
80
|
// src/useRemote.ts
|
|
69
|
-
import { useContext as useContext2, useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
|
|
70
81
|
function useRemote(argument) {
|
|
71
|
-
const context =
|
|
72
|
-
const state =
|
|
73
|
-
const hook = useMemo2(() => {
|
|
82
|
+
const context = useContext();
|
|
83
|
+
const state = useState(() => {
|
|
74
84
|
const instance = context.get(this);
|
|
75
|
-
const refresh = () => state[1]((x) => x
|
|
76
|
-
let value;
|
|
77
|
-
let release;
|
|
85
|
+
const refresh = () => state[1]((x) => x.bind(null));
|
|
78
86
|
if (!instance)
|
|
79
|
-
if (argument
|
|
80
|
-
|
|
87
|
+
if (argument === false)
|
|
88
|
+
return new Function();
|
|
81
89
|
else
|
|
82
|
-
|
|
90
|
+
throw new Error(`Could not find ${this} in context.`);
|
|
83
91
|
if (typeof argument === "boolean")
|
|
84
92
|
return () => instance;
|
|
85
93
|
function forceUpdate(action) {
|
|
@@ -89,6 +97,8 @@ function useRemote(argument) {
|
|
|
89
97
|
if (action)
|
|
90
98
|
return action.finally(refresh);
|
|
91
99
|
}
|
|
100
|
+
let release;
|
|
101
|
+
let value;
|
|
92
102
|
release = instance.get((current) => {
|
|
93
103
|
if (typeof argument === "function") {
|
|
94
104
|
const next = argument.call(current, current, forceUpdate);
|
|
@@ -100,10 +110,6 @@ function useRemote(argument) {
|
|
|
100
110
|
if (release)
|
|
101
111
|
refresh();
|
|
102
112
|
});
|
|
103
|
-
if (value === null) {
|
|
104
|
-
release();
|
|
105
|
-
return () => null;
|
|
106
|
-
}
|
|
107
113
|
if (value instanceof Promise) {
|
|
108
114
|
let error;
|
|
109
115
|
release();
|
|
@@ -115,63 +121,60 @@ function useRemote(argument) {
|
|
|
115
121
|
return value === void 0 ? null : value;
|
|
116
122
|
};
|
|
117
123
|
}
|
|
124
|
+
if (value === null) {
|
|
125
|
+
release();
|
|
126
|
+
return () => null;
|
|
127
|
+
}
|
|
118
128
|
return () => {
|
|
119
|
-
|
|
129
|
+
useEffect(() => release, []);
|
|
120
130
|
return value === void 0 ? null : value;
|
|
121
131
|
};
|
|
122
|
-
}
|
|
123
|
-
return
|
|
132
|
+
});
|
|
133
|
+
return state[0]();
|
|
124
134
|
}
|
|
125
135
|
|
|
126
136
|
// src/index.ts
|
|
127
|
-
|
|
137
|
+
import { add, get, use, ref, set } from "@expressive/mvc";
|
|
128
138
|
|
|
129
139
|
// src/consumer.ts
|
|
130
140
|
function Consumer(props) {
|
|
131
|
-
const { children, has, get, for: Type } = props;
|
|
141
|
+
const { children, has, get: get2, for: Type } = props;
|
|
132
142
|
const instance = Type.get(!!has);
|
|
133
|
-
const callback = children || has ||
|
|
143
|
+
const callback = children || has || get2;
|
|
134
144
|
if (typeof callback == "function")
|
|
135
145
|
callback(instance);
|
|
136
146
|
return null;
|
|
137
147
|
}
|
|
138
148
|
|
|
139
149
|
// src/provider.ts
|
|
140
|
-
import { Model as Model2 } from "@expressive/mvc";
|
|
141
|
-
import { createElement, useContext as useContext3, useEffect as useEffect3, useMemo as useMemo3 } from "react";
|
|
142
150
|
function Provider(props) {
|
|
143
|
-
let { for: included, use: assign
|
|
144
|
-
const context =
|
|
145
|
-
const value =
|
|
146
|
-
if (!included)
|
|
147
|
-
reject(included);
|
|
148
|
-
if (typeof included == "function" || included instanceof Model2)
|
|
149
|
-
included = { [0]: included };
|
|
150
|
-
Object.entries(included).forEach(([K, V]) => {
|
|
151
|
-
if (!(Model2.is(V) || V instanceof Model2))
|
|
152
|
-
reject(`${V} as ${K}`);
|
|
153
|
-
});
|
|
151
|
+
let { for: included, use: assign } = props;
|
|
152
|
+
const context = useContext();
|
|
153
|
+
const value = useMemo(() => context.push(), []);
|
|
154
154
|
value.include(included).forEach((isExplicit, model) => {
|
|
155
155
|
if (assign && isExplicit) {
|
|
156
156
|
for (const K in assign)
|
|
157
157
|
if (K in model)
|
|
158
|
-
model
|
|
158
|
+
model.set(K, assign[K]);
|
|
159
159
|
}
|
|
160
160
|
setContext(model, value);
|
|
161
161
|
});
|
|
162
|
-
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
function reject(argument) {
|
|
166
|
-
throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
|
|
162
|
+
useEffect(() => () => value.pop(), []);
|
|
163
|
+
return createContext(value, props.children);
|
|
167
164
|
}
|
|
168
165
|
|
|
169
166
|
// src/index.ts
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
167
|
+
Model2.get = useRemote;
|
|
168
|
+
Model2.use = useLocal;
|
|
169
|
+
Context2.get = getContext;
|
|
173
170
|
export {
|
|
174
171
|
Consumer,
|
|
172
|
+
Model2 as Model,
|
|
175
173
|
Provider,
|
|
176
|
-
|
|
174
|
+
add,
|
|
175
|
+
Model2 as default,
|
|
176
|
+
get,
|
|
177
|
+
ref,
|
|
178
|
+
set,
|
|
179
|
+
use
|
|
177
180
|
};
|
package/package.json
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"preversion": "npm run test && npm run build"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@expressive/mvc": "^0.
|
|
34
|
+
"@expressive/mvc": "^0.36.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"react": ">=16.8.0 <19.0.0"
|
|
@@ -42,6 +42,6 @@
|
|
|
42
42
|
"react": "^18.2.0",
|
|
43
43
|
"react-test-renderer": "^18.2.0"
|
|
44
44
|
},
|
|
45
|
-
"version": "0.
|
|
46
|
-
"gitHead": "
|
|
45
|
+
"version": "0.36.0",
|
|
46
|
+
"gitHead": "09b42668a6cdd0eb10be56985e0b72bbead26a88"
|
|
47
47
|
}
|