@expressive/react 0.35.5 → 0.35.6
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 +88 -75
- package/dist/index.mjs +72 -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
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,35 +17,71 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
18
|
-
var
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
19
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
29
|
|
|
21
30
|
// src/index.ts
|
|
22
31
|
var src_exports = {};
|
|
23
32
|
__export(src_exports, {
|
|
24
33
|
Consumer: () => Consumer,
|
|
34
|
+
Model: () => import_mvc2.Model,
|
|
25
35
|
Provider: () => Provider,
|
|
26
|
-
|
|
36
|
+
add: () => import_mvc3.add,
|
|
37
|
+
default: () => import_mvc2.Model,
|
|
38
|
+
get: () => import_mvc3.get,
|
|
39
|
+
ref: () => import_mvc3.ref,
|
|
40
|
+
set: () => import_mvc3.set,
|
|
41
|
+
use: () => import_mvc3.use
|
|
27
42
|
});
|
|
28
43
|
module.exports = __toCommonJS(src_exports);
|
|
29
|
-
var
|
|
44
|
+
var import_mvc2 = require("@expressive/mvc");
|
|
30
45
|
|
|
31
|
-
// src/
|
|
46
|
+
// src/useContext.ts
|
|
32
47
|
var import_mvc = require("@expressive/mvc");
|
|
33
|
-
var import_react = require("react");
|
|
34
|
-
var
|
|
48
|
+
var import_react = __toESM(require("react"));
|
|
49
|
+
var import_react2 = require("react");
|
|
50
|
+
var Shared = import_react.default.createContext(new import_mvc.Context());
|
|
35
51
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
52
|
+
function useContext() {
|
|
53
|
+
return import_react.default.useContext(Shared);
|
|
54
|
+
}
|
|
55
|
+
function createContext(value, children) {
|
|
56
|
+
return import_react.default.createElement(Shared.Provider, { key: value.key, value }, children);
|
|
57
|
+
}
|
|
58
|
+
function getContext(model, resolve) {
|
|
59
|
+
const waiting = Register.get(model);
|
|
60
|
+
if (waiting instanceof import_mvc.Context)
|
|
61
|
+
resolve(waiting);
|
|
62
|
+
else if (waiting)
|
|
63
|
+
waiting.push(resolve);
|
|
64
|
+
else
|
|
65
|
+
Register.set(model, [resolve]);
|
|
66
|
+
}
|
|
67
|
+
function setContext(model, context = useContext()) {
|
|
68
|
+
const waiting = Register.get(model);
|
|
69
|
+
if (waiting instanceof Array)
|
|
70
|
+
waiting.forEach((cb) => cb(context));
|
|
71
|
+
Register.set(model, context);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/useLocal.ts
|
|
36
75
|
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;
|
|
76
|
+
const state = (0, import_react2.useState)(() => {
|
|
42
77
|
let shouldApply = !!apply;
|
|
43
|
-
|
|
78
|
+
let enabled;
|
|
79
|
+
let local;
|
|
80
|
+
const instance = this.new();
|
|
81
|
+
const release = instance.get((current) => {
|
|
44
82
|
local = current;
|
|
45
83
|
if (enabled)
|
|
46
|
-
state[1]((x) => x
|
|
84
|
+
state[1]((x) => x.bind(null));
|
|
47
85
|
});
|
|
48
86
|
return (props) => {
|
|
49
87
|
if (shouldApply) {
|
|
@@ -63,50 +101,31 @@ function useLocal(apply, repeat) {
|
|
|
63
101
|
else
|
|
64
102
|
enabled = true;
|
|
65
103
|
}
|
|
66
|
-
setContext(instance
|
|
67
|
-
(0,
|
|
104
|
+
setContext(instance);
|
|
105
|
+
(0, import_react2.useEffect)(() => {
|
|
68
106
|
enabled = true;
|
|
69
107
|
return () => {
|
|
70
|
-
|
|
108
|
+
release();
|
|
71
109
|
instance.set(null);
|
|
72
110
|
};
|
|
73
111
|
}, []);
|
|
74
112
|
return local;
|
|
75
113
|
};
|
|
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);
|
|
114
|
+
});
|
|
115
|
+
return state[0](apply);
|
|
93
116
|
}
|
|
94
117
|
|
|
95
118
|
// src/useRemote.ts
|
|
96
|
-
var import_react2 = require("react");
|
|
97
119
|
function useRemote(argument) {
|
|
98
|
-
const context =
|
|
99
|
-
const state = (0, import_react2.useState)(
|
|
100
|
-
const hook = (0, import_react2.useMemo)(() => {
|
|
120
|
+
const context = useContext();
|
|
121
|
+
const state = (0, import_react2.useState)(() => {
|
|
101
122
|
const instance = context.get(this);
|
|
102
|
-
const refresh = () => state[1]((x) => x
|
|
103
|
-
let value;
|
|
104
|
-
let release;
|
|
123
|
+
const refresh = () => state[1]((x) => x.bind(null));
|
|
105
124
|
if (!instance)
|
|
106
|
-
if (argument
|
|
107
|
-
|
|
125
|
+
if (argument === false)
|
|
126
|
+
return new Function();
|
|
108
127
|
else
|
|
109
|
-
|
|
128
|
+
throw new Error(`Could not find ${this} in context.`);
|
|
110
129
|
if (typeof argument === "boolean")
|
|
111
130
|
return () => instance;
|
|
112
131
|
function forceUpdate(action) {
|
|
@@ -116,6 +135,8 @@ function useRemote(argument) {
|
|
|
116
135
|
if (action)
|
|
117
136
|
return action.finally(refresh);
|
|
118
137
|
}
|
|
138
|
+
let release;
|
|
139
|
+
let value;
|
|
119
140
|
release = instance.get((current) => {
|
|
120
141
|
if (typeof argument === "function") {
|
|
121
142
|
const next = argument.call(current, current, forceUpdate);
|
|
@@ -127,10 +148,6 @@ function useRemote(argument) {
|
|
|
127
148
|
if (release)
|
|
128
149
|
refresh();
|
|
129
150
|
});
|
|
130
|
-
if (value === null) {
|
|
131
|
-
release();
|
|
132
|
-
return () => null;
|
|
133
|
-
}
|
|
134
151
|
if (value instanceof Promise) {
|
|
135
152
|
let error;
|
|
136
153
|
release();
|
|
@@ -142,64 +159,60 @@ function useRemote(argument) {
|
|
|
142
159
|
return value === void 0 ? null : value;
|
|
143
160
|
};
|
|
144
161
|
}
|
|
162
|
+
if (value === null) {
|
|
163
|
+
release();
|
|
164
|
+
return () => null;
|
|
165
|
+
}
|
|
145
166
|
return () => {
|
|
146
167
|
(0, import_react2.useEffect)(() => release, []);
|
|
147
168
|
return value === void 0 ? null : value;
|
|
148
169
|
};
|
|
149
|
-
}
|
|
150
|
-
return
|
|
170
|
+
});
|
|
171
|
+
return state[0]();
|
|
151
172
|
}
|
|
152
173
|
|
|
153
174
|
// src/index.ts
|
|
154
|
-
|
|
175
|
+
var import_mvc3 = require("@expressive/mvc");
|
|
155
176
|
|
|
156
177
|
// src/consumer.ts
|
|
157
178
|
function Consumer(props) {
|
|
158
|
-
const { children, has, get, for: Type } = props;
|
|
179
|
+
const { children, has, get: get2, for: Type } = props;
|
|
159
180
|
const instance = Type.get(!!has);
|
|
160
|
-
const callback = children || has ||
|
|
181
|
+
const callback = children || has || get2;
|
|
161
182
|
if (typeof callback == "function")
|
|
162
183
|
callback(instance);
|
|
163
184
|
return null;
|
|
164
185
|
}
|
|
165
186
|
|
|
166
187
|
// src/provider.ts
|
|
167
|
-
var import_mvc2 = require("@expressive/mvc");
|
|
168
|
-
var import_react3 = require("react");
|
|
169
188
|
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
|
-
});
|
|
189
|
+
let { for: included, use: assign } = props;
|
|
190
|
+
const context = useContext();
|
|
191
|
+
const value = (0, import_react2.useMemo)(() => context.push(), []);
|
|
181
192
|
value.include(included).forEach((isExplicit, model) => {
|
|
182
193
|
if (assign && isExplicit) {
|
|
183
194
|
for (const K in assign)
|
|
184
195
|
if (K in model)
|
|
185
|
-
model
|
|
196
|
+
model.set(K, assign[K]);
|
|
186
197
|
}
|
|
187
198
|
setContext(model, value);
|
|
188
199
|
});
|
|
189
|
-
(0,
|
|
190
|
-
return (
|
|
191
|
-
}
|
|
192
|
-
function reject(argument) {
|
|
193
|
-
throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
|
|
200
|
+
(0, import_react2.useEffect)(() => () => value.pop(), []);
|
|
201
|
+
return createContext(value, props.children);
|
|
194
202
|
}
|
|
195
203
|
|
|
196
204
|
// src/index.ts
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
205
|
+
import_mvc2.Model.get = useRemote;
|
|
206
|
+
import_mvc2.Model.use = useLocal;
|
|
207
|
+
import_mvc2.Context.get = getContext;
|
|
200
208
|
// Annotate the CommonJS export names for ESM import in node:
|
|
201
209
|
0 && (module.exports = {
|
|
202
210
|
Consumer,
|
|
211
|
+
Model,
|
|
203
212
|
Provider,
|
|
204
|
-
|
|
213
|
+
add,
|
|
214
|
+
get,
|
|
215
|
+
ref,
|
|
216
|
+
set,
|
|
217
|
+
use
|
|
205
218
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,45 @@
|
|
|
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 React from "react";
|
|
7
|
+
import { useState, useEffect, useMemo } from "react";
|
|
8
|
+
var Shared = React.createContext(new Context());
|
|
8
9
|
var Register = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
function useContext() {
|
|
11
|
+
return React.useContext(Shared);
|
|
12
|
+
}
|
|
13
|
+
function createContext(value, children) {
|
|
14
|
+
return React.createElement(Shared.Provider, { key: value.key, value }, children);
|
|
15
|
+
}
|
|
16
|
+
function getContext(model, resolve) {
|
|
17
|
+
const waiting = Register.get(model);
|
|
18
|
+
if (waiting instanceof Context)
|
|
19
|
+
resolve(waiting);
|
|
20
|
+
else if (waiting)
|
|
21
|
+
waiting.push(resolve);
|
|
22
|
+
else
|
|
23
|
+
Register.set(model, [resolve]);
|
|
24
|
+
}
|
|
25
|
+
function setContext(model, context = useContext()) {
|
|
26
|
+
const waiting = Register.get(model);
|
|
27
|
+
if (waiting instanceof Array)
|
|
28
|
+
waiting.forEach((cb) => cb(context));
|
|
29
|
+
Register.set(model, context);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/useLocal.ts
|
|
9
33
|
function useLocal(apply, repeat) {
|
|
10
|
-
const state = useState(
|
|
11
|
-
const hook = useMemo(() => {
|
|
12
|
-
const instance = this.new();
|
|
13
|
-
let local;
|
|
14
|
-
let enabled;
|
|
34
|
+
const state = useState(() => {
|
|
15
35
|
let shouldApply = !!apply;
|
|
16
|
-
|
|
36
|
+
let enabled;
|
|
37
|
+
let local;
|
|
38
|
+
const instance = this.new();
|
|
39
|
+
const release = instance.get((current) => {
|
|
17
40
|
local = current;
|
|
18
41
|
if (enabled)
|
|
19
|
-
state[1]((x) => x
|
|
42
|
+
state[1]((x) => x.bind(null));
|
|
20
43
|
});
|
|
21
44
|
return (props) => {
|
|
22
45
|
if (shouldApply) {
|
|
@@ -36,50 +59,31 @@ function useLocal(apply, repeat) {
|
|
|
36
59
|
else
|
|
37
60
|
enabled = true;
|
|
38
61
|
}
|
|
39
|
-
setContext(instance
|
|
62
|
+
setContext(instance);
|
|
40
63
|
useEffect(() => {
|
|
41
64
|
enabled = true;
|
|
42
65
|
return () => {
|
|
43
|
-
|
|
66
|
+
release();
|
|
44
67
|
instance.set(null);
|
|
45
68
|
};
|
|
46
69
|
}, []);
|
|
47
70
|
return local;
|
|
48
71
|
};
|
|
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);
|
|
72
|
+
});
|
|
73
|
+
return state[0](apply);
|
|
66
74
|
}
|
|
67
75
|
|
|
68
76
|
// src/useRemote.ts
|
|
69
|
-
import { useContext as useContext2, useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
|
|
70
77
|
function useRemote(argument) {
|
|
71
|
-
const context =
|
|
72
|
-
const state =
|
|
73
|
-
const hook = useMemo2(() => {
|
|
78
|
+
const context = useContext();
|
|
79
|
+
const state = useState(() => {
|
|
74
80
|
const instance = context.get(this);
|
|
75
|
-
const refresh = () => state[1]((x) => x
|
|
76
|
-
let value;
|
|
77
|
-
let release;
|
|
81
|
+
const refresh = () => state[1]((x) => x.bind(null));
|
|
78
82
|
if (!instance)
|
|
79
|
-
if (argument
|
|
80
|
-
|
|
83
|
+
if (argument === false)
|
|
84
|
+
return new Function();
|
|
81
85
|
else
|
|
82
|
-
|
|
86
|
+
throw new Error(`Could not find ${this} in context.`);
|
|
83
87
|
if (typeof argument === "boolean")
|
|
84
88
|
return () => instance;
|
|
85
89
|
function forceUpdate(action) {
|
|
@@ -89,6 +93,8 @@ function useRemote(argument) {
|
|
|
89
93
|
if (action)
|
|
90
94
|
return action.finally(refresh);
|
|
91
95
|
}
|
|
96
|
+
let release;
|
|
97
|
+
let value;
|
|
92
98
|
release = instance.get((current) => {
|
|
93
99
|
if (typeof argument === "function") {
|
|
94
100
|
const next = argument.call(current, current, forceUpdate);
|
|
@@ -100,10 +106,6 @@ function useRemote(argument) {
|
|
|
100
106
|
if (release)
|
|
101
107
|
refresh();
|
|
102
108
|
});
|
|
103
|
-
if (value === null) {
|
|
104
|
-
release();
|
|
105
|
-
return () => null;
|
|
106
|
-
}
|
|
107
109
|
if (value instanceof Promise) {
|
|
108
110
|
let error;
|
|
109
111
|
release();
|
|
@@ -115,63 +117,60 @@ function useRemote(argument) {
|
|
|
115
117
|
return value === void 0 ? null : value;
|
|
116
118
|
};
|
|
117
119
|
}
|
|
120
|
+
if (value === null) {
|
|
121
|
+
release();
|
|
122
|
+
return () => null;
|
|
123
|
+
}
|
|
118
124
|
return () => {
|
|
119
|
-
|
|
125
|
+
useEffect(() => release, []);
|
|
120
126
|
return value === void 0 ? null : value;
|
|
121
127
|
};
|
|
122
|
-
}
|
|
123
|
-
return
|
|
128
|
+
});
|
|
129
|
+
return state[0]();
|
|
124
130
|
}
|
|
125
131
|
|
|
126
132
|
// src/index.ts
|
|
127
|
-
|
|
133
|
+
import { add, get, use, ref, set } from "@expressive/mvc";
|
|
128
134
|
|
|
129
135
|
// src/consumer.ts
|
|
130
136
|
function Consumer(props) {
|
|
131
|
-
const { children, has, get, for: Type } = props;
|
|
137
|
+
const { children, has, get: get2, for: Type } = props;
|
|
132
138
|
const instance = Type.get(!!has);
|
|
133
|
-
const callback = children || has ||
|
|
139
|
+
const callback = children || has || get2;
|
|
134
140
|
if (typeof callback == "function")
|
|
135
141
|
callback(instance);
|
|
136
142
|
return null;
|
|
137
143
|
}
|
|
138
144
|
|
|
139
145
|
// 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
146
|
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
|
-
});
|
|
147
|
+
let { for: included, use: assign } = props;
|
|
148
|
+
const context = useContext();
|
|
149
|
+
const value = useMemo(() => context.push(), []);
|
|
154
150
|
value.include(included).forEach((isExplicit, model) => {
|
|
155
151
|
if (assign && isExplicit) {
|
|
156
152
|
for (const K in assign)
|
|
157
153
|
if (K in model)
|
|
158
|
-
model
|
|
154
|
+
model.set(K, assign[K]);
|
|
159
155
|
}
|
|
160
156
|
setContext(model, value);
|
|
161
157
|
});
|
|
162
|
-
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
function reject(argument) {
|
|
166
|
-
throw new Error(`Provider expects a Model instance or class but got ${argument}.`);
|
|
158
|
+
useEffect(() => () => value.pop(), []);
|
|
159
|
+
return createContext(value, props.children);
|
|
167
160
|
}
|
|
168
161
|
|
|
169
162
|
// src/index.ts
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
163
|
+
Model2.get = useRemote;
|
|
164
|
+
Model2.use = useLocal;
|
|
165
|
+
Context2.get = getContext;
|
|
173
166
|
export {
|
|
174
167
|
Consumer,
|
|
168
|
+
Model2 as Model,
|
|
175
169
|
Provider,
|
|
176
|
-
|
|
170
|
+
add,
|
|
171
|
+
Model2 as default,
|
|
172
|
+
get,
|
|
173
|
+
ref,
|
|
174
|
+
set,
|
|
175
|
+
use
|
|
177
176
|
};
|
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.35.
|
|
34
|
+
"@expressive/mvc": "^0.35.6"
|
|
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.35.
|
|
46
|
-
"gitHead": "
|
|
45
|
+
"version": "0.35.6",
|
|
46
|
+
"gitHead": "35d41f894a2cb042e7fdbb7fde16ec4eab76efdf"
|
|
47
47
|
}
|