@expressive/react 0.33.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.
- package/dist/index.d.ts +96 -0
- package/dist/index.js +202 -0
- package/dist/index.mjs +174 -0
- package/package.json +16 -15
- package/lib/index.d.ts +0 -59
- package/lib/index.esm.js +0 -90
- package/lib/index.js +0 -107
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Model, Context } from '@expressive/mvc';
|
|
2
|
+
export * from '@expressive/mvc';
|
|
3
|
+
export { Model as default } from '@expressive/mvc';
|
|
4
|
+
import { ReactElement, FunctionComponentElement, ProviderProps, ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
declare namespace Consumer {
|
|
7
|
+
type HasProps<T extends Model> = {
|
|
8
|
+
/** Type of controller to fetch from context. */
|
|
9
|
+
for: Model.Type<T>;
|
|
10
|
+
/**
|
|
11
|
+
* Getter function. Is called on every natural render of this component.
|
|
12
|
+
* Will throw if usable instance cannot be found in context.
|
|
13
|
+
*/
|
|
14
|
+
has: (value: T) => void;
|
|
15
|
+
};
|
|
16
|
+
type GetProps<T extends Model> = {
|
|
17
|
+
/** Type of controller to fetch from context. */
|
|
18
|
+
for: Model.Type<T>;
|
|
19
|
+
/** Getter function. Is called on every natural render of this component. */
|
|
20
|
+
get: (value: T | undefined) => void;
|
|
21
|
+
};
|
|
22
|
+
type RenderProps<T extends Model> = {
|
|
23
|
+
/** Type of controller to fetch from context. */
|
|
24
|
+
for: Model.Type<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Render function, will receive instance of desired controller.
|
|
27
|
+
*
|
|
28
|
+
* Similar to `get()`, updates to properties accessed in
|
|
29
|
+
* this function will cause a refresh when they change.
|
|
30
|
+
*/
|
|
31
|
+
children: (value: T) => ReactElement<any, any> | null;
|
|
32
|
+
};
|
|
33
|
+
type Props<T extends Model> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
34
|
+
}
|
|
35
|
+
declare function Consumer<T extends Model>(props: Consumer.Props<T>): ReactElement<any, any> | null;
|
|
36
|
+
|
|
37
|
+
declare namespace Provider {
|
|
38
|
+
type Element = FunctionComponentElement<ProviderProps<Context>>;
|
|
39
|
+
type Item = Model | Model.New;
|
|
40
|
+
type Multiple<T extends Item = Item> = {
|
|
41
|
+
[key: string | number]: T;
|
|
42
|
+
};
|
|
43
|
+
type Instance<E> = E extends (new () => any) ? InstanceType<E> : E extends Model ? E : never;
|
|
44
|
+
type Props<T extends Item = Item> = MultipleProps<T> | NormalProps<T>;
|
|
45
|
+
type NormalProps<E, I = Instance<E>> = {
|
|
46
|
+
for: E;
|
|
47
|
+
children?: ReactNode;
|
|
48
|
+
use?: Model.Values<I>;
|
|
49
|
+
};
|
|
50
|
+
type MultipleProps<T extends Item> = {
|
|
51
|
+
for: Multiple<T>;
|
|
52
|
+
children?: ReactNode;
|
|
53
|
+
use?: Model.Values<Instance<T>>;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): Provider.Element;
|
|
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
|
+
|
|
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,35 +12,36 @@
|
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
|
+
"expressive",
|
|
15
16
|
"react",
|
|
16
17
|
"mvc"
|
|
17
18
|
],
|
|
18
|
-
"main": "
|
|
19
|
-
"module": "
|
|
20
|
-
"types": "
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"module": "dist/index.mjs",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
21
22
|
"files": [
|
|
22
|
-
"
|
|
23
|
+
"dist/index.js",
|
|
24
|
+
"dist/index.mjs",
|
|
25
|
+
"dist/index.d.ts"
|
|
23
26
|
],
|
|
24
27
|
"scripts": {
|
|
25
|
-
"build": "
|
|
26
|
-
"
|
|
27
|
-
"
|
|
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
|
+
"@expressive/mvc": "^0.35.0-alpha.0"
|
|
33
35
|
},
|
|
34
36
|
"peerDependencies": {
|
|
35
37
|
"react": ">=16.8.0 <19.0.0"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@types/react": "^17.0.11",
|
|
40
|
+
"@types/react": "^18.2.14",
|
|
40
41
|
"@types/react-test-renderer": "^18.0.0",
|
|
41
|
-
"react": "
|
|
42
|
-
"react-test-renderer": "^
|
|
42
|
+
"react": "^18.2.0",
|
|
43
|
+
"react-test-renderer": "^18.2.0"
|
|
43
44
|
},
|
|
44
|
-
"version": "0.
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"version": "0.35.0-alpha.0",
|
|
46
|
+
"gitHead": "17db508f601400ac08e6f821376e7a9403525d5b"
|
|
46
47
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { Model, Context } from '@expressive/mvc';
|
|
2
|
-
export * from '@expressive/mvc';
|
|
3
|
-
export { Model, Model as default } from '@expressive/mvc';
|
|
4
|
-
import { ReactElement, ReactNode, FunctionComponentElement, ProviderProps } from 'react';
|
|
5
|
-
|
|
6
|
-
declare namespace Consumer {
|
|
7
|
-
type HasProps<T extends Model> = {
|
|
8
|
-
/** Type of controller to fetch from context. */
|
|
9
|
-
for: Model.Type<T>;
|
|
10
|
-
/**
|
|
11
|
-
* Getter function. Is called on every natural render of this component.
|
|
12
|
-
* Will throw if usable instance cannot be found in context.
|
|
13
|
-
*/
|
|
14
|
-
has: (value: T) => void;
|
|
15
|
-
};
|
|
16
|
-
type GetProps<T extends Model> = {
|
|
17
|
-
/** Type of controller to fetch from context. */
|
|
18
|
-
for: Model.Type<T>;
|
|
19
|
-
/** Getter function. Is called on every natural render of this component. */
|
|
20
|
-
get: (value: T | undefined) => void;
|
|
21
|
-
};
|
|
22
|
-
type RenderProps<T extends Model> = {
|
|
23
|
-
/** Type of controller to fetch from context. */
|
|
24
|
-
for: Model.Type<T>;
|
|
25
|
-
/**
|
|
26
|
-
* Render function, will receive instance of desired controller.
|
|
27
|
-
*
|
|
28
|
-
* Similar to `get()`, updates to properties accessed in
|
|
29
|
-
* this function will cause a refresh when they change.
|
|
30
|
-
*/
|
|
31
|
-
children: (value: T) => ReactElement<any, any> | null;
|
|
32
|
-
};
|
|
33
|
-
type Props<T extends Model> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
34
|
-
}
|
|
35
|
-
declare function Consumer<T extends Model>(props: Consumer.Props<T>): ReactElement<any, any> | null;
|
|
36
|
-
|
|
37
|
-
declare namespace Provider {
|
|
38
|
-
type Item = Model | Model.New;
|
|
39
|
-
type Multiple<T extends Item = Item> = {
|
|
40
|
-
[key: string | number]: T;
|
|
41
|
-
};
|
|
42
|
-
type Instance<E> = E extends (new () => any) ? InstanceType<E> : E extends Model ? E : never;
|
|
43
|
-
type Props<T extends Item = Item> = MultipleProps<T> | NormalProps<T>;
|
|
44
|
-
type NormalProps<E, I = Instance<E>> = {
|
|
45
|
-
for: E;
|
|
46
|
-
fallback?: ReactNode;
|
|
47
|
-
children?: ReactNode | ((instance: I) => ReactNode);
|
|
48
|
-
use?: Model.Values<I>;
|
|
49
|
-
};
|
|
50
|
-
type MultipleProps<T extends Item> = {
|
|
51
|
-
for: Multiple<T>;
|
|
52
|
-
fallback?: ReactNode;
|
|
53
|
-
children?: ReactNode;
|
|
54
|
-
use?: Model.Values<Instance<T>>;
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): FunctionComponentElement<ProviderProps<Context>>;
|
|
58
|
-
|
|
59
|
-
export { Consumer, Provider };
|
package/lib/index.esm.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { Context, Model, Control } from '@expressive/mvc';
|
|
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';
|
|
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}.`);
|
|
15
|
-
};
|
|
16
|
-
if (!included)
|
|
17
|
-
reject(included);
|
|
18
|
-
if (typeof included == "function" || included instanceof Model)
|
|
19
|
-
included = { [0]: included };
|
|
20
|
-
Object.entries(included).forEach(([K, V]) => {
|
|
21
|
-
if (!(Model.is(V) || V instanceof Model))
|
|
22
|
-
reject(`${V} as ${K}`);
|
|
23
|
-
});
|
|
24
|
-
context.include(included).forEach((isExplicit, model) => {
|
|
25
|
-
if (assign && isExplicit)
|
|
26
|
-
for (const K in assign)
|
|
27
|
-
if (K in model)
|
|
28
|
-
model[K] = assign[K];
|
|
29
|
-
const pending = Pending.get(model);
|
|
30
|
-
if (pending)
|
|
31
|
-
pending.forEach(cb => cb(context));
|
|
32
|
-
});
|
|
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));
|
|
37
|
-
}
|
|
38
|
-
|
|
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
|
-
};
|
|
89
|
-
|
|
90
|
-
export { Consumer, Provider };
|
package/lib/index.js
DELETED
|
@@ -1,107 +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 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}.`);
|
|
17
|
-
};
|
|
18
|
-
if (!included)
|
|
19
|
-
reject(included);
|
|
20
|
-
if (typeof included == "function" || included instanceof mvc.Model)
|
|
21
|
-
included = { [0]: included };
|
|
22
|
-
Object.entries(included).forEach(([K, V]) => {
|
|
23
|
-
if (!(mvc.Model.is(V) || V instanceof mvc.Model))
|
|
24
|
-
reject(`${V} as ${K}`);
|
|
25
|
-
});
|
|
26
|
-
context.include(included).forEach((isExplicit, model) => {
|
|
27
|
-
if (assign && isExplicit)
|
|
28
|
-
for (const K in assign)
|
|
29
|
-
if (K in model)
|
|
30
|
-
model[K] = assign[K];
|
|
31
|
-
const pending = Pending.get(model);
|
|
32
|
-
if (pending)
|
|
33
|
-
pending.forEach(cb => cb(context));
|
|
34
|
-
});
|
|
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));
|
|
39
|
-
}
|
|
40
|
-
|
|
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
|
-
}
|
|
50
|
-
|
|
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
|
-
Object.defineProperty(exports, 'default', {
|
|
97
|
-
enumerable: true,
|
|
98
|
-
get: function () { return mvc.Model; }
|
|
99
|
-
});
|
|
100
|
-
exports.Consumer = Consumer;
|
|
101
|
-
exports.Provider = Provider;
|
|
102
|
-
Object.keys(mvc).forEach(function (k) {
|
|
103
|
-
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
104
|
-
enumerable: true,
|
|
105
|
-
get: function () { return mvc[k]; }
|
|
106
|
-
});
|
|
107
|
-
});
|