@expressive/react 0.80.0 → 0.82.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/README.md +1 -1
- package/dist/adapter.d.ts +4 -0
- package/dist/adapter.js +7 -237
- package/dist/adapter.js.map +1 -1
- package/dist/component-BqiJWCy1.js +248 -0
- package/dist/component-BqiJWCy1.js.map +1 -0
- package/dist/element-Dqygmyg3.js +49 -0
- package/dist/element-Dqygmyg3.js.map +1 -0
- package/dist/has.d.ts +2 -0
- package/dist/has.js +22 -0
- package/dist/has.js.map +1 -0
- package/dist/index.d.ts +17 -2
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/map.d.ts +2 -0
- package/dist/map.js +22 -0
- package/dist/map.js.map +1 -0
- package/package.json +12 -5
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<a href="https://www.npmjs.com/package/@expressive/react"><img alt="NPM" src="https://badge.fury.io/js/%40expressive%2Freact.svg"></a>
|
|
9
|
-
<img src="https://img.shields.io/
|
|
9
|
+
<a href="https://github.com/gabeklein/expressive-mvc/actions/workflows/coverage.yml"><img alt="Coverage" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fgabeklein%2Fexpressive-mvc%2Fbadges%2Fcoverage.json"></a>
|
|
10
10
|
</p>
|
|
11
11
|
|
|
12
12
|
---
|
package/dist/adapter.d.ts
CHANGED
|
@@ -121,6 +121,10 @@ declare module '@expressive/mvc' {
|
|
|
121
121
|
forceUpdate: (callback?: () => void) => void;
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Wrap a content element in its context provider, a Suspense boundary (unless
|
|
126
|
+
* `fallback` is `false`) and, when `catch` is set, the host error boundary.
|
|
127
|
+
*/
|
|
124
128
|
//#endregion
|
|
125
129
|
//#region src/runtime.d.ts
|
|
126
130
|
declare const Runtime: {
|
package/dist/adapter.js
CHANGED
|
@@ -1,74 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as Runtime, c as useHook, i as Provider, l as useReady, n as Consumer, o as use, s as useFactory } from "./component-BqiJWCy1.js";
|
|
2
|
+
import { Context, State, State as State$1 } from "@expressive/mvc";
|
|
3
|
+
import { observer, watch } from "@expressive/mvc/observable";
|
|
2
4
|
|
|
3
|
-
//#region src/runtime.ts
|
|
4
|
-
const Runtime = {};
|
|
5
|
-
function useFactory(factory) {
|
|
6
|
-
const ref = Runtime.useRef(null);
|
|
7
|
-
return ref.current || (ref.current = factory());
|
|
8
|
-
}
|
|
9
|
-
function useReady(callback) {
|
|
10
|
-
return Runtime.useEffect(() => void callback(), []);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Mount-effect with a refreshable return value, safe under React StrictMode.
|
|
14
|
-
*
|
|
15
|
-
* @param callback Setup handler; receives a setter, must return a cleanup.
|
|
16
|
-
* @returns Latest value published via the setter (`undefined` until set).
|
|
17
|
-
*/
|
|
18
|
-
function useHook(callback) {
|
|
19
|
-
const { current } = Runtime.useRef({ rendered: 0 });
|
|
20
|
-
current.update = Runtime.useState(() => {
|
|
21
|
-
if (!current.rendered) current.unmount = callback((next) => {
|
|
22
|
-
current.output = next;
|
|
23
|
-
if (current.mounted) current.update?.((x) => x + 1);
|
|
24
|
-
else if (current.update) current.pending = true;
|
|
25
|
-
});
|
|
26
|
-
return current.rendered++;
|
|
27
|
-
})[1];
|
|
28
|
-
Runtime.useEffect(() => {
|
|
29
|
-
current.mounted = true;
|
|
30
|
-
if (current.pending) {
|
|
31
|
-
current.pending = false;
|
|
32
|
-
current.update((x) => x + 1);
|
|
33
|
-
}
|
|
34
|
-
return () => {
|
|
35
|
-
if (--current.rendered < 1) current.unmount();
|
|
36
|
-
};
|
|
37
|
-
}, []);
|
|
38
|
-
return current.output;
|
|
39
|
-
}
|
|
40
|
-
/** Subscribe to an existing observable instance within a component. */
|
|
41
|
-
function use(subject) {
|
|
42
|
-
const { current } = Runtime.useRef({
|
|
43
|
-
mounted: 0,
|
|
44
|
-
proxy: subject
|
|
45
|
-
});
|
|
46
|
-
const update = Runtime.useState(() => current.mounted++)[1];
|
|
47
|
-
if (current.source !== subject) {
|
|
48
|
-
const status = observer(subject);
|
|
49
|
-
if (status === void 0) throw new Error("Provided object is not observable.");
|
|
50
|
-
current.unwatch?.();
|
|
51
|
-
current.source = subject;
|
|
52
|
-
if (status === null) {
|
|
53
|
-
current.unwatch = void 0;
|
|
54
|
-
current.proxy = subject;
|
|
55
|
-
} else {
|
|
56
|
-
if (!status.ready) event(subject);
|
|
57
|
-
let init = true;
|
|
58
|
-
current.unwatch = watch(subject, (next, changed) => {
|
|
59
|
-
current.proxy = next;
|
|
60
|
-
if (changed.length && !init) update((x) => x + 1);
|
|
61
|
-
});
|
|
62
|
-
init = false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
Runtime.useEffect(() => () => {
|
|
66
|
-
if (--current.mounted < 1) current.unwatch?.();
|
|
67
|
-
}, []);
|
|
68
|
-
return current.proxy;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
//#endregion
|
|
72
5
|
//#region src/state.get.ts
|
|
73
6
|
State$1.get = function get(argument) {
|
|
74
7
|
const next = Runtime.useState(0)[1];
|
|
@@ -99,13 +32,15 @@ State$1.get = function get(argument) {
|
|
|
99
32
|
unwatch = () => {};
|
|
100
33
|
return release;
|
|
101
34
|
}
|
|
102
|
-
|
|
35
|
+
let first = true;
|
|
36
|
+
unwatch = watch(next, (current) => {
|
|
103
37
|
if (typeof argument === "function") {
|
|
104
38
|
const next = argument.call(current, current, refresh);
|
|
105
39
|
if (next === value) return;
|
|
106
40
|
value = next;
|
|
107
41
|
} else value = current;
|
|
108
|
-
if (
|
|
42
|
+
if (!first) update();
|
|
43
|
+
first = false;
|
|
109
44
|
}, argument === true);
|
|
110
45
|
if (mounted) {
|
|
111
46
|
pending = true;
|
|
@@ -176,171 +111,6 @@ State$1.use = function use(...args) {
|
|
|
176
111
|
})(args);
|
|
177
112
|
};
|
|
178
113
|
|
|
179
|
-
//#endregion
|
|
180
|
-
//#region src/context.ts
|
|
181
|
-
let shared;
|
|
182
|
-
/**
|
|
183
|
-
* Lazily-created context carrying the active {@link Context} down the tree.
|
|
184
|
-
* Lazy because the framework's `createContext` arrives via {@link Runtime},
|
|
185
|
-
* which an adapter's entry populates at load - after this module evaluates.
|
|
186
|
-
*/
|
|
187
|
-
function Layers() {
|
|
188
|
-
return shared || (shared = Runtime.createContext(Context$1.root));
|
|
189
|
-
}
|
|
190
|
-
/** Read the ambient {@link Context} from the nearest Layers provider. */
|
|
191
|
-
function useAmbient() {
|
|
192
|
-
return Runtime.useContext(Layers());
|
|
193
|
-
}
|
|
194
|
-
/** Wrap `children` in a {@link Layers} provider carrying `context` down the tree. */
|
|
195
|
-
function provide(context, children) {
|
|
196
|
-
return Runtime.createElement(Layers().Provider, {
|
|
197
|
-
value: context,
|
|
198
|
-
children
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
Object.defineProperty(Component, "contextType", {
|
|
202
|
-
configurable: true,
|
|
203
|
-
get: Layers
|
|
204
|
-
});
|
|
205
|
-
const _get = Context$1.get;
|
|
206
|
-
Context$1.get = (state) => {
|
|
207
|
-
if (!state) try {
|
|
208
|
-
return useAmbient();
|
|
209
|
-
} catch {}
|
|
210
|
-
return _get(state);
|
|
211
|
-
};
|
|
212
|
-
function Consumer(props) {
|
|
213
|
-
return props.for.get((i) => props.children(i));
|
|
214
|
-
}
|
|
215
|
-
function Provider(props) {
|
|
216
|
-
const { for: input, children, fallback, name, is, ...rest } = props;
|
|
217
|
-
const ambient = useAmbient();
|
|
218
|
-
const context = useHook((set) => {
|
|
219
|
-
set(new Context$1(ambient));
|
|
220
|
-
return () => context.pop();
|
|
221
|
-
});
|
|
222
|
-
context.set(input, is);
|
|
223
|
-
if (Object.keys(rest).length) {
|
|
224
|
-
const i = State$1.is(input) ? context.get(input) : input;
|
|
225
|
-
if (i instanceof State$1) i.set(rest);
|
|
226
|
-
}
|
|
227
|
-
return provide(context, fallback !== void 0 ? Runtime.createElement(Runtime.Suspense, {
|
|
228
|
-
fallback,
|
|
229
|
-
name
|
|
230
|
-
}, children) : children);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
//#endregion
|
|
234
|
-
//#region src/component.ts
|
|
235
|
-
Object.defineProperties(Component.prototype, {
|
|
236
|
-
state: {
|
|
237
|
-
set() {},
|
|
238
|
-
get: Component.prototype.get
|
|
239
|
-
},
|
|
240
|
-
context: { set: bootstrap }
|
|
241
|
-
});
|
|
242
|
-
/**
|
|
243
|
-
* `State.on` handler that prepares a Component's prototype at bootstrap, before
|
|
244
|
-
* mvc classifies its members:
|
|
245
|
-
*
|
|
246
|
-
* - On the root Component, host own-property keys are trapped so each lands as a
|
|
247
|
-
* plain own property (out of observed state); each adapter assigns its own set.
|
|
248
|
-
* - capitalized methods are rewritten into subcomponents as non-configurable
|
|
249
|
-
* getters, so bootstrap skips them too.
|
|
250
|
-
*
|
|
251
|
-
* (Sealing `render` as the content-render seam is handled by core itself.)
|
|
252
|
-
*
|
|
253
|
-
* `before` covers the per-instance case: a capitalized function assigned as an
|
|
254
|
-
* instance field (e.g. `Sidebar = Sidebar` to inject or override one), promoted
|
|
255
|
-
* before `observe` so it is not mistaken for reactive state.
|
|
256
|
-
*/
|
|
257
|
-
Component.on({
|
|
258
|
-
type(type) {
|
|
259
|
-
if (type === Component) for (const key of Runtime.ignore) Object.defineProperty(Component.prototype, key, { set(value) {
|
|
260
|
-
Object.defineProperty(this, key, {
|
|
261
|
-
value,
|
|
262
|
-
writable: true
|
|
263
|
-
});
|
|
264
|
-
} });
|
|
265
|
-
subcomponents(type.prototype);
|
|
266
|
-
},
|
|
267
|
-
before(self) {
|
|
268
|
-
subcomponents(self, true);
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
function bootstrap(context) {
|
|
272
|
-
context = context.push();
|
|
273
|
-
context.set(this, () => () => this.set(null));
|
|
274
|
-
Object.defineProperties(this, {
|
|
275
|
-
context: {
|
|
276
|
-
get: () => context,
|
|
277
|
-
set() {}
|
|
278
|
-
},
|
|
279
|
-
render: { value: render(this, context) }
|
|
280
|
-
});
|
|
281
|
-
this.set(null, () => {
|
|
282
|
-
Object.defineProperty(this, "props", {
|
|
283
|
-
value: this.props,
|
|
284
|
-
writable: true
|
|
285
|
-
});
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Per-instance render host: subscribe the instance, render inside its context
|
|
290
|
-
* provider, wrap pending in Suspense and (when `catch` is set) the host error
|
|
291
|
-
* boundary. Host differences live behind `Runtime.dedupe`/`Runtime.ErrorBoundary`.
|
|
292
|
-
*/
|
|
293
|
-
function render(from, context) {
|
|
294
|
-
const { createElement: create } = Runtime;
|
|
295
|
-
const { commit, remove } = Runtime.dedupe(from, context);
|
|
296
|
-
const content = from.render;
|
|
297
|
-
const Render = () => content.call(from, from.props);
|
|
298
|
-
const Component = () => {
|
|
299
|
-
from = useHook((refresh) => {
|
|
300
|
-
if (observer(from) !== null) watch(from, refresh);
|
|
301
|
-
return () => {
|
|
302
|
-
remove();
|
|
303
|
-
context.pop();
|
|
304
|
-
};
|
|
305
|
-
}) || from;
|
|
306
|
-
useReady(commit);
|
|
307
|
-
const rendered = create(Render);
|
|
308
|
-
const children = provide(context, from.fallback === false ? rendered : create(Runtime.Suspense, {
|
|
309
|
-
fallback: from.fallback,
|
|
310
|
-
name: String(from)
|
|
311
|
-
}, rendered));
|
|
312
|
-
return from.catch ? create(Runtime.ErrorBoundary, {
|
|
313
|
-
self: from,
|
|
314
|
-
children
|
|
315
|
-
}) : children;
|
|
316
|
-
};
|
|
317
|
-
return () => create(Component);
|
|
318
|
-
}
|
|
319
|
-
/** Rewrite each own capitalized function on `target` into a subcomponent. */
|
|
320
|
-
function subcomponents(target, configurable) {
|
|
321
|
-
for (const key of Object.getOwnPropertyNames(target)) {
|
|
322
|
-
if (!/^[A-Z]/.test(key)) continue;
|
|
323
|
-
const { value } = Object.getOwnPropertyDescriptor(target, key);
|
|
324
|
-
if (typeof value != "function") continue;
|
|
325
|
-
Object.defineProperty(target, key, {
|
|
326
|
-
configurable,
|
|
327
|
-
get() {
|
|
328
|
-
const owner = this.is;
|
|
329
|
-
let render = unbind(value);
|
|
330
|
-
const Component = (props) => render.call(useHook((set) => watch(owner, set)) || owner, props);
|
|
331
|
-
Object.defineProperty(owner, key, {
|
|
332
|
-
configurable: true,
|
|
333
|
-
get: () => Component,
|
|
334
|
-
set(fn) {
|
|
335
|
-
render = fn;
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
return Component;
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
114
|
//#endregion
|
|
345
115
|
export { Consumer, Provider, Runtime, State, use };
|
|
346
116
|
//# sourceMappingURL=adapter.js.map
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","names":["Context","State"],"sources":["../src/runtime.ts","../src/state.get.ts","../src/state.use.ts","../src/context.ts","../src/component.ts"],"sourcesContent":["import { event, watch, observer } from '@expressive/mvc';\nimport type { Component } from '@expressive/mvc';\nimport type { Context } from './context';\n\nexport const Runtime = {} as {\n /** Host own-property keys to trap out of observed state; assigned by each adapter. */\n ignore: string[];\n createElement(type: any, props?: any, ...children: any[]): any;\n createContext<T>(value: T): any;\n useContext(context: any): any;\n useState<S>(initial: S | (() => S)): [S, (next: (previous: S) => S) => void];\n useEffect(effect: () => (() => void) | void, deps?: any[]): void;\n useRef<T>(initial: T): { current: T };\n /** Per-render-attempt lifecycle, set by each adapter (React stacks attempts; others no-op). */\n dedupe(from: Component, context: Context): { commit(): void; remove(): void };\n /** Host error-boundary component, wrapping a Component whose `catch` is set. */\n ErrorBoundary: unknown;\n Suspense: any;\n};\n\nexport function useFactory<T extends Function>(factory: () => T) {\n const ref = Runtime.useRef<T | null>(null);\n return ref.current || (ref.current = factory());\n}\n\nexport function useReady<T>(callback: () => void) {\n return Runtime.useEffect(() => void callback(), []);\n}\n\n/**\n * Mount-effect with a refreshable return value, safe under React StrictMode.\n *\n * @param callback Setup handler; receives a setter, must return a cleanup.\n * @returns Latest value published via the setter (`undefined` until set).\n */\nexport function useHook<T = void>(\n callback: (refresh: (next: T) => void) => () => void\n) {\n const { current } = Runtime.useRef(\n { rendered: 0 } as {\n rendered: number;\n mounted?: boolean;\n pending?: boolean;\n unmount: () => void;\n update?: (next: (previous: number) => number) => void;\n output: T;\n }\n );\n\n current.update = Runtime.useState(() => {\n if (!current.rendered)\n current.unmount = callback((next) => {\n current.output = next;\n if (current.mounted) current.update?.((x) => x + 1);\n else if (current.update) current.pending = true;\n });\n\n return current.rendered++;\n })[1];\n\n Runtime.useEffect(() => {\n current.mounted = true;\n if (current.pending) {\n current.pending = false;\n current.update!((x) => x + 1);\n }\n return () => {\n if (--current.rendered < 1) current.unmount();\n }\n }, []);\n\n return current.output;\n}\n\n/** Subscribe to an existing observable instance within a component. */\nexport function use<T extends object>(subject: T) {\n const { current } = Runtime.useRef<{\n proxy: T;\n source?: T;\n mounted: number;\n unwatch?: () => void;\n }>({ mounted: 0, proxy: subject });\n\n const update = Runtime.useState(() => current.mounted++)[1];\n\n if (current.source !== subject) {\n const status = observer(subject);\n\n if (status === undefined)\n throw new Error('Provided object is not observable.');\n\n current.unwatch?.();\n current.source = subject;\n\n if (status === null) {\n current.unwatch = undefined;\n current.proxy = subject;\n } else {\n if (!status.ready) event(subject);\n\n let init = true;\n\n current.unwatch = watch(subject, (next, changed) => {\n current.proxy = next;\n if (changed.length && !init)\n update((x) => x + 1);\n });\n\n init = false;\n }\n }\n\n Runtime.useEffect(() => () => {\n if (--current.mounted < 1) current.unwatch?.();\n }, []);\n\n return current.proxy;\n}\n","import { State, Context, observer, watch } from '@expressive/mvc';\nimport { Runtime, useFactory, useHook, useReady } from './runtime';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ndeclare module '@expressive/mvc' {\n namespace State {\n type ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n\n /**\n * Request a refresh and again after promise either resolves or rejects.\n *\n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n *\n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n *\n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n };\n\n type GetFactory<T extends State, R> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => R;\n\n type GetEffect<T extends State> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => null;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(this: State.Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n function get<T extends State>(\n this: State.Extends<T>,\n required: false\n ): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(\n this: State.Extends<T>,\n requireValues: true\n ): Required<T>;\n\n function get<T extends State, R>(\n this: State.Extends<T>,\n factory: GetFactory<T, Promise<R> | R>\n ): NoVoid<R>;\n\n function get<T extends State>(\n this: State.Extends<T>,\n factory: GetEffect<T>\n ): null;\n }\n}\n\nState.get = function get<T extends State>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const next = Runtime.useState(0)[1];\n const local = Context.get();\n const render = useFactory(() => {\n let unwatch: (() => void) | undefined;\n let mounted = false;\n let pending = false;\n let value: any;\n\n function update() {\n pending = false;\n next((x) => x + 1);\n }\n\n function refresh<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if (typeof action == 'function') action = action();\n update();\n if (action instanceof Promise) return action.finally(update);\n }\n\n function release() {\n unsubscribe();\n unwatch?.();\n unwatch = undefined;\n }\n\n function attach(next: T) {\n unwatch?.();\n\n if (observer(next) === null) {\n value = typeof argument === 'function'\n ? argument.call(next, next, refresh)\n : next;\n unwatch = () => {};\n return release;\n }\n\n unwatch = watch(\n next,\n (current, changed) => {\n if (typeof argument === 'function') {\n const next = argument.call(current, current, refresh);\n if (next === value) return;\n value = next;\n } else {\n value = current;\n }\n\n if (changed.length) update();\n },\n argument === true\n );\n\n if (mounted) {\n pending = true;\n queueMicrotask(() => pending && update());\n }\n\n return release;\n }\n\n const unsubscribe = local.get(this, attach);\n\n if (!unwatch) {\n unsubscribe();\n if (argument === false) return () => undefined;\n throw new Error(`Could not find ${this} in context.`);\n }\n\n if (value === null) {\n release();\n return () => null;\n }\n\n if (value instanceof Promise) {\n let error: Error | undefined;\n\n release();\n\n value\n .then(\n (x) => (value = x),\n (e) => (error = e)\n )\n .finally(update);\n\n value = null;\n\n return () => {\n if (error) throw error;\n return value === undefined ? null : value;\n };\n }\n\n return () => {\n pending = false;\n useReady(() => mounted = true);\n useHook(() => release);\n return value === undefined ? null : value;\n };\n });\n\n return render();\n};\n","import { State, Context, watch } from '@expressive/mvc';\nimport { useFactory, useHook, useReady } from './runtime';\n\ndeclare module '@expressive/mvc' {\n interface UseState extends State {\n /**\n * Optional hook called when State.use() is invoked within a React component.\n *\n * This is called *every render* of the component, and will intercept\n * arguments which would otherwise be passed into the State constructor.\n *\n * @param props Arguments passed to State.use().\n */\n use?(...props: any[]): Promise<void> | void;\n }\n\n namespace State {\n type UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n }\n ? P\n : State.Args<T>;\n\n /**\n * Create and manage instance of this State within React component.\n *\n * @param args Arguments to pass to constructor or `use` method (if defined).\n * @returns Managed instance of this State.\n */\n function use<T extends UseState>(\n this: State.Type<T>,\n ...args: UseArgs<T>\n ): T;\n }\n}\n\nState.use = function use<T extends State>(\n this: State.Type<T>,\n ...args: State.UseArgs<T>\n) {\n const outer = Context.get();\n const render = useFactory(() => {\n const add = (arg: unknown) =>\n typeof arg == 'object' && instance.set(arg as State.Assign<T>);\n\n let use = (...args: State.Args<T>) => Promise.all(args.flat().map(add));\n\n const instance = new this((x) => {\n if ('use' in x && typeof x.use == 'function') {\n use = x.use.bind(x);\n use(...args);\n }\n else return args;\n });\n\n const context = outer.push(instance);\n\n let ready = false;\n\n return (args: State.Args<T>) => {\n if (ready) {\n ready = false;\n Promise.resolve(use(...args)).finally(() => {\n ready = true;\n });\n }\n\n useReady(() => ready = true);\n\n return useHook<T>((update) => {\n watch(instance, update);\n return () => {\n context.pop();\n instance.set(null);\n };\n });\n };\n });\n\n return render(args);\n};\n","import { State, Context, Component } from '@expressive/mvc';\nimport { Runtime, useHook } from './runtime';\n\nlet shared: any;\n\n/**\n * Lazily-created context carrying the active {@link Context} down the tree.\n * Lazy because the framework's `createContext` arrives via {@link Runtime},\n * which an adapter's entry populates at load - after this module evaluates.\n */\nfunction Layers() {\n return shared || (shared = Runtime.createContext(Context.root));\n}\n\n/** Read the ambient {@link Context} from the nearest Layers provider. */\nfunction useAmbient() {\n return Runtime.useContext(Layers());\n}\n\n/** Wrap `children` in a {@link Layers} provider carrying `context` down the tree. */\nfunction provide(context: Context, children: any) {\n return Runtime.createElement(Layers().Provider, { value: context, children });\n}\n\n// Class components consume the active Context through the host's `contextType`\nObject.defineProperty(Component, 'contextType', { configurable: true, get: Layers });\n\nconst _get = Context.get;\n\nContext.get = (state?: State) => {\n if (!state)\n try {\n return useAmbient();\n } catch { }\n\n return _get(state);\n};\n\ndeclare namespace Consumer {\n type Props<T extends State> = {\n /** Type of controller to fetch from context. */\n for: State.Extends<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `State.get()`, updates to properties accessed in\n * this function will cause a refresh when they change.\n */\n children: (value: T) => Component.Node | void;\n };\n}\n\nfunction Consumer<T extends State>(props: Consumer.Props<T>) {\n return props.for.get((i) => props.children(i));\n}\n\ndeclare namespace Provider {\n type ForEach<T> = (state: T) => void | (() => void);\n\n interface SharedProps {\n /**\n * Children to render within this Provider.\n */\n children?: Component.Node;\n\n /** A fallback tree to show when suspended. */\n fallback?: Component.Node;\n\n /**\n * A name for this Suspense boundary for instrumentation purposes.\n * The name will help identify this boundary in React DevTools.\n */\n name?: string | undefined;\n }\n\n type ForSingleProps<T extends State> = SharedProps & {\n for: T | State.Type<T>;\n is: (instance: T) => void;\n } & { [K in State.Field<T>]?: T[K] };\n\n type ForMultipleProps<T extends State> = SharedProps & {\n for: Context.Accept<T>;\n is?: ForEach<T>;\n };\n\n type Props<T extends State = State> = ForSingleProps<T> | ForMultipleProps<T>;\n}\n\nfunction Provider<T extends State>(props: Provider.Props<T>) {\n const {\n for: input,\n children,\n fallback,\n name,\n is,\n ...rest\n } = props as Provider.ForSingleProps<T> & Provider.ForMultipleProps<T>;\n\n const ambient = useAmbient();\n const context = useHook<Context>((set) => {\n set(new Context(ambient));\n return () => context.pop();\n });\n\n context.set(input, is);\n\n if (Object.keys(rest).length) {\n const i = State.is(input) ? context.get(input) : input;\n if (i instanceof State) i.set(rest);\n }\n\n return provide(context,\n fallback !== undefined\n ? Runtime.createElement(Runtime.Suspense, { fallback, name }, children)\n : children\n );\n}\n\nexport { Consumer, Provider, Context, provide };\n","import { Component, watch, unbind, observer } from '@expressive/mvc';\nimport { provide, type Context } from './context';\nimport { Runtime, useHook, useReady } from './runtime';\n\ndeclare module '@expressive/mvc' {\n interface Component {\n /** @deprecated Only to satisfy host JSX. Use `this.get(State)` instead. */\n readonly context: Context;\n /** @deprecated Only to satisfy host JSX. Use `this.get()` instead. */\n readonly state: State.Values<this>;\n /** @deprecated Only to satisfy host JSX. Use `this.set({})` instead. */\n setState: (state: any, callback?: () => void) => void;\n /** @deprecated Only to satisfy host JSX. Use `this.set(key)` instead. */\n forceUpdate: (callback?: () => void) => void;\n }\n}\n\n// Host-agnostic seams: `state` is a read-only values bag; `context`'s setter\n// pushes a child context, registers the instance for teardown, and installs its\n// per-instance render host. Host-specific descriptors stay in each adapter.\nObject.defineProperties(Component.prototype, {\n state: {\n set() {},\n get: Component.prototype.get\n },\n context: {\n set: bootstrap\n }\n});\n\n/**\n * `State.on` handler that prepares a Component's prototype at bootstrap, before\n * mvc classifies its members:\n *\n * - On the root Component, host own-property keys are trapped so each lands as a\n * plain own property (out of observed state); each adapter assigns its own set.\n * - capitalized methods are rewritten into subcomponents as non-configurable\n * getters, so bootstrap skips them too.\n *\n * (Sealing `render` as the content-render seam is handled by core itself.)\n *\n * `before` covers the per-instance case: a capitalized function assigned as an\n * instance field (e.g. `Sidebar = Sidebar` to inject or override one), promoted\n * before `observe` so it is not mistaken for reactive state.\n */\nComponent.on({\n type(type) {\n if (type === Component)\n for (const key of Runtime.ignore)\n Object.defineProperty(Component.prototype, key, {\n set(value) {\n Object.defineProperty(this, key, { value, writable: true });\n }\n });\n\n // capitalized methods into subcomponents\n subcomponents(type.prototype);\n },\n before(self){\n // capitalized instance fields into subcomponents\n subcomponents(self, true);\n }\n});\n\nfunction bootstrap(this: Component, context: Context){\n context = context.push();\n context.set(this, () => () => this.set(null));\n\n Object.defineProperties(this, {\n context: {\n get: () => context,\n set() {}\n },\n render: {\n value: render(this, context)\n }\n });\n\n this.set(null, () => {\n Object.defineProperty(this, 'props', {\n value: this.props,\n writable: true\n });\n })\n}\n\n/**\n * Per-instance render host: subscribe the instance, render inside its context\n * provider, wrap pending in Suspense and (when `catch` is set) the host error\n * boundary. Host differences live behind `Runtime.dedupe`/`Runtime.ErrorBoundary`.\n */\nfunction render(from: Component, context: Context) {\n const { createElement: create } = Runtime;\n const { commit, remove } = Runtime.dedupe(from, context);\n\n const content = from.render;\n const Render = () => content.call(from, from.props);\n const Component = () => {\n from = useHook<Component>((refresh) => {\n if (observer(from) !== null)\n watch(from, refresh);\n return () => {\n remove();\n context.pop();\n };\n }) || from;\n\n useReady(commit);\n\n const rendered = create(Render);\n const children = provide(context,\n from.fallback === false\n ? rendered\n : create(Runtime.Suspense,\n { fallback: from.fallback, name: String(from) },\n rendered)\n );\n\n return from.catch\n ? create(Runtime.ErrorBoundary, { self: from, children })\n : children;\n };\n\n return () => create(Component);\n}\n\n/** Rewrite each own capitalized function on `target` into a subcomponent. */\nfunction subcomponents(target: object, configurable?: boolean) {\n for (const key of Object.getOwnPropertyNames(target)) {\n if (!/^[A-Z]/.test(key)) continue;\n const { value } = Object.getOwnPropertyDescriptor(target, key)!;\n if (typeof value != 'function') continue;\n Object.defineProperty(target, key, {\n configurable,\n get(this: Component) {\n const owner = this.is;\n let render = unbind(value);\n const Component = (props: unknown) =>\n render.call(\n useHook<Component>((set) => watch(owner, set)) || owner,\n props\n );\n\n Object.defineProperty(owner, key, {\n configurable: true,\n get: () => Component,\n set(fn: Function) {\n render = fn;\n }\n });\n\n return Component;\n }\n });\n }\n}\n"],"mappings":";;;AAIA,MAAa,UAAU,EAAE;AAgBzB,SAAgB,WAA+B,SAAkB;CAC/D,MAAM,MAAM,QAAQ,OAAiB,KAAK;AAC1C,QAAO,IAAI,YAAY,IAAI,UAAU,SAAS;;AAGhD,SAAgB,SAAY,UAAsB;AAChD,QAAO,QAAQ,gBAAgB,KAAK,UAAU,EAAE,EAAE,CAAC;;;;;;;;AASrD,SAAgB,QACd,UACA;CACA,MAAM,EAAE,YAAY,QAAQ,OAC1B,EAAE,UAAU,GAAG,CAQhB;AAED,SAAQ,SAAS,QAAQ,eAAe;AACtC,MAAI,CAAC,QAAQ,SACX,SAAQ,UAAU,UAAU,SAAS;AACnC,WAAQ,SAAS;AACjB,OAAI,QAAQ,QAAS,SAAQ,UAAU,MAAM,IAAI,EAAE;YAC1C,QAAQ,OAAQ,SAAQ,UAAU;IAC3C;AAEJ,SAAO,QAAQ;GACf,CAAC;AAEH,SAAQ,gBAAgB;AACtB,UAAQ,UAAU;AAClB,MAAI,QAAQ,SAAS;AACnB,WAAQ,UAAU;AAClB,WAAQ,QAAS,MAAM,IAAI,EAAE;;AAE/B,eAAa;AACX,OAAI,EAAE,QAAQ,W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QAAQ;CAExD,MAAM,UAAU,KAAK;CACrB,MAAM,eAAe,QAAQ,KAAK,MAAM,KAAK,MAAM;CACnD,MAAM,kBAAkB;AACtB,SAAO,SAAoB,YAAY;AACrC,OAAI,SAAS,KAAK,KAAK,KACrB,OAAM,MAAM,QAAQ;AACtB,gBAAa;AACX,YAAQ;AACR,YAAQ,KAAK;;IAEf,IAAI;AAEN,WAAS,OAAO;EAEhB,MAAM,WAAW,OAAO,OAAO;EAC/B,MAAM,WAAW,QAAQ,SACvB,KAAK,aAAa,QACd,WACA,OAAO,QAAQ,UACf;GAAE,UAAU,KAAK;GAAU,MAAM,OAAO,KAAK;GAAE,EAC/C,SAAS,CACd;AAED,SAAO,KAAK,QACR,OAAO,QAAQ,eAAe;GAAE,MAAM;GAAM;GAAU,CAAC,GACvD;;AAGN,cAAa,OAAO,UAAU;;;AAIhC,SAAS,cAAc,QAAgB,cAAwB;AAC7D,MAAK,MAAM,OAAO,OAAO,oBAAoB,OAAO,EAAE;AACpD,MAAI,CAAC,SAAS,KAAK,IAAI,CAAE;EACzB,MAAM,EAAE,UAAU,OAAO,yBAAyB,QAAQ,IAAI;AAC9D,MAAI,OAAO,SAAS,WAAY;AAChC,SAAO,eAAe,QAAQ,KAAK;GACjC;GACA,MAAqB;IACnB,MAAM,QAAQ,KAAK;IACnB,IAAI,SAAS,OAAO,MAAM;IAC1B,MAAM,aAAa,UACjB,OAAO,KACL,SAAoB,QAAQ,MAAM,OAAO,IAAI,CAAC,IAAI,OAClD,MACD;AAEH,WAAO,eAAe,OAAO,KAAK;KAChC,cAAc;KACd,WAAW;KACX,IAAI,IAAc;AAChB,eAAS;;KAEZ,CAAC;AAEF,WAAO;;GAEV,CAAC"}
|
|
1
|
+
{"version":3,"file":"adapter.js","names":[],"sources":["../src/state.get.ts","../src/state.use.ts"],"sourcesContent":["import { State, Context } from '@expressive/mvc';\nimport { observer, watch } from '@expressive/mvc/observable';\nimport { Runtime, useFactory, useHook, useReady } from './runtime';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ndeclare module '@expressive/mvc' {\n namespace State {\n type ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n\n /**\n * Request a refresh and again after promise either resolves or rejects.\n *\n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n *\n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n *\n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n };\n\n type GetFactory<T extends State, R> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => R;\n\n type GetEffect<T extends State> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => null;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(this: State.Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n function get<T extends State>(\n this: State.Extends<T>,\n required: false\n ): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(\n this: State.Extends<T>,\n requireValues: true\n ): Required<T>;\n\n function get<T extends State, R>(\n this: State.Extends<T>,\n factory: GetFactory<T, Promise<R> | R>\n ): NoVoid<R>;\n\n function get<T extends State>(\n this: State.Extends<T>,\n factory: GetEffect<T>\n ): null;\n }\n}\n\nState.get = function get<T extends State>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const next = Runtime.useState(0)[1];\n const local = Context.get();\n const render = useFactory(() => {\n let unwatch: (() => void) | undefined;\n let mounted = false;\n let pending = false;\n let value: any;\n\n function update() {\n pending = false;\n next((x) => x + 1);\n }\n\n function refresh<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if (typeof action == 'function') action = action();\n update();\n if (action instanceof Promise) return action.finally(update);\n }\n\n function release() {\n unsubscribe();\n unwatch?.();\n unwatch = undefined;\n }\n\n function attach(next: T) {\n unwatch?.();\n\n if (observer(next) === null) {\n value = typeof argument === 'function'\n ? argument.call(next, next, refresh)\n : next;\n unwatch = () => {};\n return release;\n }\n\n let first = true;\n\n unwatch = watch(\n next,\n (current) => {\n if (typeof argument === 'function') {\n const next = argument.call(current, current, refresh);\n if (next === value) return;\n value = next;\n } else {\n value = current;\n }\n\n if (!first) update();\n first = false;\n },\n argument === true\n );\n\n if (mounted) {\n pending = true;\n queueMicrotask(() => pending && update());\n }\n\n return release;\n }\n\n const unsubscribe = local.get(this, attach);\n\n if (!unwatch) {\n unsubscribe();\n if (argument === false) return () => undefined;\n throw new Error(`Could not find ${this} in context.`);\n }\n\n if (value === null) {\n release();\n return () => null;\n }\n\n if (value instanceof Promise) {\n let error: Error | undefined;\n\n release();\n\n value\n .then(\n (x) => (value = x),\n (e) => (error = e)\n )\n .finally(update);\n\n value = null;\n\n return () => {\n if (error) throw error;\n return value === undefined ? null : value;\n };\n }\n\n return () => {\n pending = false;\n useReady(() => mounted = true);\n useHook(() => release);\n return value === undefined ? null : value;\n };\n });\n\n return render();\n};\n","import { State, Context } from '@expressive/mvc';\nimport { watch } from '@expressive/mvc/observable';\nimport { useFactory, useHook, useReady } from './runtime';\n\ndeclare module '@expressive/mvc' {\n interface UseState extends State {\n /**\n * Optional hook called when State.use() is invoked within a React component.\n *\n * This is called *every render* of the component, and will intercept\n * arguments which would otherwise be passed into the State constructor.\n *\n * @param props Arguments passed to State.use().\n */\n use?(...props: any[]): Promise<void> | void;\n }\n\n namespace State {\n type UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n }\n ? P\n : State.Args<T>;\n\n /**\n * Create and manage instance of this State within React component.\n *\n * @param args Arguments to pass to constructor or `use` method (if defined).\n * @returns Managed instance of this State.\n */\n function use<T extends UseState>(\n this: State.Type<T>,\n ...args: UseArgs<T>\n ): T;\n }\n}\n\nState.use = function use<T extends State>(\n this: State.Type<T>,\n ...args: State.UseArgs<T>\n) {\n const outer = Context.get();\n const render = useFactory(() => {\n const add = (arg: unknown) =>\n typeof arg == 'object' && instance.set(arg as State.Assign<T>);\n\n let use = (...args: State.Args<T>) => Promise.all(args.flat().map(add));\n\n const instance = new this((x) => {\n if ('use' in x && typeof x.use == 'function') {\n use = x.use.bind(x);\n use(...args);\n }\n else return args;\n });\n\n const context = outer.push(instance);\n\n let ready = false;\n\n return (args: State.Args<T>) => {\n if (ready) {\n ready = false;\n Promise.resolve(use(...args)).finally(() => {\n ready = true;\n });\n }\n\n useReady(() => ready = true);\n\n return useHook<T>((update) => {\n watch(instance, update);\n return () => {\n context.pop();\n instance.set(null);\n };\n });\n };\n });\n\n return render(args);\n};\n"],"mappings":";;;;;AAwEA,QAAM,MAAM,SAAS,IAEnB,UACA;CACA,MAAM,OAAO,QAAQ,SAAS,EAAE,CAAC;CACjC,MAAM,QAAQ,QAAQ,KAAK;AAuG3B,QAtGe,iBAAiB;EAC9B,IAAI;EACJ,IAAI,UAAU;EACd,IAAI,UAAU;EACd,IAAI;EAEJ,SAAS,SAAS;AAChB,aAAU;AACV,SAAM,MAAM,IAAI,EAAE;;EAGpB,SAAS,QAAW,QAA+C;AACjE,OAAI,OAAO,UAAU,WAAY,UAAS,QAAQ;AAClD,WAAQ;AACR,OAAI,kBAAkB,QAAS,QAAO,OAAO,QAAQ,OAAO;;EAG9D,SAAS,UAAU;AACjB,gBAAa;AACb,cAAW;AACX,aAAU;;EAGZ,SAAS,OAAO,MAAS;AACvB,cAAW;AAEX,OAAI,SAAS,KAAK,KAAK,MAAM;AAC3B,YAAQ,OAAO,aAAa,aACxB,SAAS,KAAK,MAAM,MAAM,QAAQ,GAClC;AACJ,oBAAgB;AAChB,WAAO;;GAGT,IAAI,QAAQ;AAEZ,aAAU,MACR,OACC,YAAY;AACX,QAAI,OAAO,aAAa,YAAY;KAClC,MAAM,OAAO,SAAS,KAAK,SAAS,SAAS,QAAQ;AACrD,SAAI,SAAS,MAAO;AACpB,aAAQ;UAER,SAAQ;AAGV,QA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C1C,aAAQ;MACR;;AAGJ,kBAAe,QAAQ,KAAK;AAE5B,UAAO,SAAY,WAAW;AAC5B,UAAM,UAAU,OAAO;AACvB,iBAAa;AACX,aAAQ,KAAK;AACb,cAAS,IAAI,KAAK;;KAEpB;;GAEJ,CAEY,KAAK"}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { Component, Context as Context$1, State, unbind } from "@expressive/mvc";
|
|
2
|
+
import { event, observer, watch } from "@expressive/mvc/observable";
|
|
3
|
+
|
|
4
|
+
//#region src/runtime.ts
|
|
5
|
+
const Runtime = {};
|
|
6
|
+
function useFactory(factory) {
|
|
7
|
+
const ref = Runtime.useRef(null);
|
|
8
|
+
return ref.current || (ref.current = factory());
|
|
9
|
+
}
|
|
10
|
+
function useReady(callback) {
|
|
11
|
+
return Runtime.useEffect(() => void callback(), []);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Mount-effect with a refreshable return value, safe under React StrictMode.
|
|
15
|
+
*
|
|
16
|
+
* @param callback Setup handler; receives a setter, must return a cleanup.
|
|
17
|
+
* @returns Latest value published via the setter (`undefined` until set).
|
|
18
|
+
*/
|
|
19
|
+
function useHook(callback) {
|
|
20
|
+
const { current } = Runtime.useRef({ rendered: 0 });
|
|
21
|
+
current.update = Runtime.useState(() => {
|
|
22
|
+
if (!current.rendered) current.unmount = callback((next) => {
|
|
23
|
+
current.output = next;
|
|
24
|
+
if (current.mounted) current.update?.((x) => x + 1);
|
|
25
|
+
else if (current.update) current.pending = true;
|
|
26
|
+
});
|
|
27
|
+
return current.rendered++;
|
|
28
|
+
})[1];
|
|
29
|
+
Runtime.useEffect(() => {
|
|
30
|
+
current.mounted = true;
|
|
31
|
+
if (current.pending) {
|
|
32
|
+
current.pending = false;
|
|
33
|
+
current.update((x) => x + 1);
|
|
34
|
+
}
|
|
35
|
+
return () => {
|
|
36
|
+
if (--current.rendered < 1) current.unmount();
|
|
37
|
+
};
|
|
38
|
+
}, []);
|
|
39
|
+
return current.output;
|
|
40
|
+
}
|
|
41
|
+
/** Subscribe to an existing observable instance within a component. */
|
|
42
|
+
function use(subject) {
|
|
43
|
+
const { current } = Runtime.useRef({
|
|
44
|
+
mounted: 0,
|
|
45
|
+
proxy: subject
|
|
46
|
+
});
|
|
47
|
+
const update = Runtime.useState(() => current.mounted++)[1];
|
|
48
|
+
if (current.source !== subject) {
|
|
49
|
+
const status = observer(subject);
|
|
50
|
+
if (status === void 0) throw new Error("Provided object is not observable.");
|
|
51
|
+
current.unwatch?.();
|
|
52
|
+
current.source = subject;
|
|
53
|
+
if (status === null) {
|
|
54
|
+
current.unwatch = void 0;
|
|
55
|
+
current.proxy = subject;
|
|
56
|
+
} else {
|
|
57
|
+
if (!status.ready) event(subject);
|
|
58
|
+
let init = true;
|
|
59
|
+
current.unwatch = watch(subject, (next, changed) => {
|
|
60
|
+
current.proxy = next;
|
|
61
|
+
if (changed.length && !init) update((x) => x + 1);
|
|
62
|
+
});
|
|
63
|
+
init = false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
Runtime.useEffect(() => () => {
|
|
67
|
+
if (--current.mounted < 1) current.unwatch?.();
|
|
68
|
+
}, []);
|
|
69
|
+
return current.proxy;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/context.ts
|
|
74
|
+
let shared;
|
|
75
|
+
/**
|
|
76
|
+
* Lazily-created context carrying the active {@link Context} down the tree.
|
|
77
|
+
* Lazy because the framework's `createContext` arrives via {@link Runtime},
|
|
78
|
+
* which an adapter's entry populates at load - after this module evaluates.
|
|
79
|
+
*/
|
|
80
|
+
function Layers() {
|
|
81
|
+
return shared || (shared = Runtime.createContext(Context$1.root));
|
|
82
|
+
}
|
|
83
|
+
/** Read the ambient {@link Context} from the nearest Layers provider. */
|
|
84
|
+
function useAmbient() {
|
|
85
|
+
return Runtime.useContext(Layers());
|
|
86
|
+
}
|
|
87
|
+
/** Wrap `children` in a {@link Layers} provider carrying `context` down the tree. */
|
|
88
|
+
function provide(context, children) {
|
|
89
|
+
return Runtime.createElement(Layers().Provider, {
|
|
90
|
+
value: context,
|
|
91
|
+
children
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
Object.defineProperty(Component, "contextType", {
|
|
95
|
+
configurable: true,
|
|
96
|
+
get: Layers
|
|
97
|
+
});
|
|
98
|
+
const _get = Context$1.get;
|
|
99
|
+
Context$1.get = (state) => {
|
|
100
|
+
if (!state) try {
|
|
101
|
+
return useAmbient();
|
|
102
|
+
} catch {}
|
|
103
|
+
return _get(state);
|
|
104
|
+
};
|
|
105
|
+
function Consumer(props) {
|
|
106
|
+
return props.for.get((i) => props.children(i));
|
|
107
|
+
}
|
|
108
|
+
function Provider(props) {
|
|
109
|
+
const { for: input, children, fallback, name, is, ...rest } = props;
|
|
110
|
+
const ambient = useAmbient();
|
|
111
|
+
const context = useHook((set) => {
|
|
112
|
+
set(new Context$1(ambient));
|
|
113
|
+
return () => context.pop();
|
|
114
|
+
});
|
|
115
|
+
context.set(input, is);
|
|
116
|
+
if (Object.keys(rest).length) {
|
|
117
|
+
const i = State.is(input) ? context.get(input) : input;
|
|
118
|
+
if (i instanceof State) i.set(rest);
|
|
119
|
+
}
|
|
120
|
+
return provide(context, fallback !== void 0 ? Runtime.createElement(Runtime.Suspense, {
|
|
121
|
+
fallback,
|
|
122
|
+
name
|
|
123
|
+
}, children) : children);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/component.ts
|
|
128
|
+
Object.defineProperties(Component.prototype, {
|
|
129
|
+
state: {
|
|
130
|
+
set() {},
|
|
131
|
+
get: Component.prototype.get
|
|
132
|
+
},
|
|
133
|
+
context: { set: bootstrap }
|
|
134
|
+
});
|
|
135
|
+
/**
|
|
136
|
+
* `State.on` handler that prepares a Component's prototype at bootstrap, before
|
|
137
|
+
* mvc classifies its members:
|
|
138
|
+
*
|
|
139
|
+
* - On the root Component, host own-property keys are trapped so each lands as a
|
|
140
|
+
* plain own property (out of observed state); each adapter assigns its own set.
|
|
141
|
+
* - capitalized methods are rewritten into subcomponents as non-configurable
|
|
142
|
+
* getters, so bootstrap skips them too.
|
|
143
|
+
*
|
|
144
|
+
* (Sealing `render` as the content-render seam is handled by core itself.)
|
|
145
|
+
*
|
|
146
|
+
* `before` covers the per-instance case: a capitalized function assigned as an
|
|
147
|
+
* instance field (e.g. `Sidebar = Sidebar` to inject or override one), promoted
|
|
148
|
+
* before `observe` so it is not mistaken for reactive state.
|
|
149
|
+
*/
|
|
150
|
+
Component.on({
|
|
151
|
+
type(type) {
|
|
152
|
+
if (type === Component) for (const key of Runtime.ignore) Object.defineProperty(Component.prototype, key, { set(value) {
|
|
153
|
+
Object.defineProperty(this, key, {
|
|
154
|
+
value,
|
|
155
|
+
writable: true
|
|
156
|
+
});
|
|
157
|
+
} });
|
|
158
|
+
subcomponents(type.prototype);
|
|
159
|
+
},
|
|
160
|
+
before(self) {
|
|
161
|
+
subcomponents(self, true);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
function bootstrap(context) {
|
|
165
|
+
context = context.push();
|
|
166
|
+
context.set(this, () => () => this.set(null));
|
|
167
|
+
Object.defineProperties(this, {
|
|
168
|
+
context: {
|
|
169
|
+
get: () => context,
|
|
170
|
+
set() {}
|
|
171
|
+
},
|
|
172
|
+
render: { value: render(this, context) }
|
|
173
|
+
});
|
|
174
|
+
this.set(null, () => {
|
|
175
|
+
Object.defineProperty(this, "props", {
|
|
176
|
+
value: this.props,
|
|
177
|
+
writable: true
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Wrap a content element in its context provider, a Suspense boundary (unless
|
|
183
|
+
* `fallback` is `false`) and, when `catch` is set, the host error boundary.
|
|
184
|
+
*/
|
|
185
|
+
function frame(from, context, children) {
|
|
186
|
+
const { createElement: create } = Runtime;
|
|
187
|
+
if (from.fallback !== false) children = create(Runtime.Suspense, {
|
|
188
|
+
fallback: from.fallback,
|
|
189
|
+
name: String(from)
|
|
190
|
+
}, children);
|
|
191
|
+
children = provide(context, children);
|
|
192
|
+
return from.catch ? create(Runtime.ErrorBoundary, {
|
|
193
|
+
self: from,
|
|
194
|
+
children
|
|
195
|
+
}) : children;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Ownership host for `<Component/>`: React owns the instance, so its render
|
|
199
|
+
* threads the bootstrap-pushed context through `Runtime.dedupe` (React stacks
|
|
200
|
+
* render attempts) and tears that context down - destroying the instance - on
|
|
201
|
+
* unmount.
|
|
202
|
+
*/
|
|
203
|
+
function render(from, context) {
|
|
204
|
+
const { createElement: create } = Runtime;
|
|
205
|
+
const { commit, remove } = Runtime.dedupe(from, context);
|
|
206
|
+
const content = from.render;
|
|
207
|
+
const Render = () => content.call(from, from.props);
|
|
208
|
+
const Component = () => {
|
|
209
|
+
from = useHook((refresh) => {
|
|
210
|
+
if (observer(from) !== null) watch(from, refresh);
|
|
211
|
+
return () => {
|
|
212
|
+
remove();
|
|
213
|
+
context.pop();
|
|
214
|
+
};
|
|
215
|
+
}) || from;
|
|
216
|
+
useReady(commit);
|
|
217
|
+
return frame(from, context, create(Render));
|
|
218
|
+
};
|
|
219
|
+
return () => create(Component);
|
|
220
|
+
}
|
|
221
|
+
/** Rewrite each own capitalized function on `target` into a subcomponent. */
|
|
222
|
+
function subcomponents(target, configurable) {
|
|
223
|
+
for (const key of Object.getOwnPropertyNames(target)) {
|
|
224
|
+
if (!/^[A-Z]/.test(key)) continue;
|
|
225
|
+
const { value } = Object.getOwnPropertyDescriptor(target, key);
|
|
226
|
+
if (typeof value != "function") continue;
|
|
227
|
+
Object.defineProperty(target, key, {
|
|
228
|
+
configurable,
|
|
229
|
+
get() {
|
|
230
|
+
const owner = this.is;
|
|
231
|
+
let render = unbind(value);
|
|
232
|
+
const Component = (props) => render.call(useHook((set) => watch(owner, set)) || owner, props);
|
|
233
|
+
Object.defineProperty(owner, key, {
|
|
234
|
+
configurable: true,
|
|
235
|
+
get: () => Component,
|
|
236
|
+
set(fn) {
|
|
237
|
+
render = fn;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
return Component;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
//#endregion
|
|
247
|
+
export { Runtime as a, useHook as c, Provider as i, useReady as l, Consumer as n, use as o, Context$1 as r, useFactory as s, frame as t };
|
|
248
|
+
//# sourceMappingURL=component-BqiJWCy1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component-BqiJWCy1.js","names":["Context"],"sources":["../src/runtime.ts","../src/context.ts","../src/component.ts"],"sourcesContent":["import { event, watch, observer } from '@expressive/mvc/observable';\nimport type { Component } from '@expressive/mvc';\nimport type { Context } from './context';\n\nexport const Runtime = {} as {\n /** Host own-property keys to trap out of observed state; assigned by each adapter. */\n ignore: string[];\n createElement(type: any, props?: any, ...children: any[]): any;\n createContext<T>(value: T): any;\n useContext(context: any): any;\n useState<S>(initial: S | (() => S)): [S, (next: (previous: S) => S) => void];\n useEffect(effect: () => (() => void) | void, deps?: any[]): void;\n useRef<T>(initial: T): { current: T };\n /** Per-render-attempt lifecycle, set by each adapter (React stacks attempts; others no-op). */\n dedupe(from: Component, context: Context): { commit(): void; remove(): void };\n /** Host error-boundary component, wrapping a Component whose `catch` is set. */\n ErrorBoundary: unknown;\n Suspense: any;\n};\n\nexport function useFactory<T extends Function>(factory: () => T) {\n const ref = Runtime.useRef<T | null>(null);\n return ref.current || (ref.current = factory());\n}\n\nexport function useReady<T>(callback: () => void) {\n return Runtime.useEffect(() => void callback(), []);\n}\n\n/**\n * Mount-effect with a refreshable return value, safe under React StrictMode.\n *\n * @param callback Setup handler; receives a setter, must return a cleanup.\n * @returns Latest value published via the setter (`undefined` until set).\n */\nexport function useHook<T = void>(\n callback: (refresh: (next: T) => void) => () => void\n) {\n const { current } = Runtime.useRef(\n { rendered: 0 } as {\n rendered: number;\n mounted?: boolean;\n pending?: boolean;\n unmount: () => void;\n update?: (next: (previous: number) => number) => void;\n output: T;\n }\n );\n\n current.update = Runtime.useState(() => {\n if (!current.rendered)\n current.unmount = callback((next) => {\n current.output = next;\n if (current.mounted) current.update?.((x) => x + 1);\n else if (current.update) current.pending = true;\n });\n\n return current.rendered++;\n })[1];\n\n Runtime.useEffect(() => {\n current.mounted = true;\n if (current.pending) {\n current.pending = false;\n current.update!((x) => x + 1);\n }\n return () => {\n if (--current.rendered < 1) current.unmount();\n }\n }, []);\n\n return current.output;\n}\n\n/** Subscribe to an existing observable instance within a component. */\nexport function use<T extends object>(subject: T) {\n const { current } = Runtime.useRef<{\n proxy: T;\n source?: T;\n mounted: number;\n unwatch?: () => void;\n }>({ mounted: 0, proxy: subject });\n\n const update = Runtime.useState(() => current.mounted++)[1];\n\n if (current.source !== subject) {\n const status = observer(subject);\n\n if (status === undefined)\n throw new Error('Provided object is not observable.');\n\n current.unwatch?.();\n current.source = subject;\n\n if (status === null) {\n current.unwatch = undefined;\n current.proxy = subject;\n } else {\n if (!status.ready) event(subject);\n\n let init = true;\n\n current.unwatch = watch(subject, (next, changed) => {\n current.proxy = next;\n if (changed.length && !init)\n update((x) => x + 1);\n });\n\n init = false;\n }\n }\n\n Runtime.useEffect(() => () => {\n if (--current.mounted < 1) current.unwatch?.();\n }, []);\n\n return current.proxy;\n}\n","import { State, Context, Component } from '@expressive/mvc';\nimport { Runtime, useHook } from './runtime';\n\nlet shared: any;\n\n/**\n * Lazily-created context carrying the active {@link Context} down the tree.\n * Lazy because the framework's `createContext` arrives via {@link Runtime},\n * which an adapter's entry populates at load - after this module evaluates.\n */\nfunction Layers() {\n return shared || (shared = Runtime.createContext(Context.root));\n}\n\n/** Read the ambient {@link Context} from the nearest Layers provider. */\nfunction useAmbient() {\n return Runtime.useContext(Layers());\n}\n\n/** Wrap `children` in a {@link Layers} provider carrying `context` down the tree. */\nfunction provide(context: Context, children: any) {\n return Runtime.createElement(Layers().Provider, { value: context, children });\n}\n\n// Class components consume the active Context through the host's `contextType`\nObject.defineProperty(Component, 'contextType', { configurable: true, get: Layers });\n\nconst _get = Context.get;\n\nContext.get = (state?: State) => {\n if (!state)\n try {\n return useAmbient();\n } catch { }\n\n return _get(state);\n};\n\ndeclare namespace Consumer {\n type Props<T extends State> = {\n /** Type of controller to fetch from context. */\n for: State.Extends<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `State.get()`, updates to properties accessed in\n * this function will cause a refresh when they change.\n */\n children: (value: T) => Component.Node | void;\n };\n}\n\nfunction Consumer<T extends State>(props: Consumer.Props<T>) {\n return props.for.get((i) => props.children(i));\n}\n\ndeclare namespace Provider {\n type ForEach<T> = (state: T) => void | (() => void);\n\n interface SharedProps {\n /**\n * Children to render within this Provider.\n */\n children?: Component.Node;\n\n /** A fallback tree to show when suspended. */\n fallback?: Component.Node;\n\n /**\n * A name for this Suspense boundary for instrumentation purposes.\n * The name will help identify this boundary in React DevTools.\n */\n name?: string | undefined;\n }\n\n type ForSingleProps<T extends State> = SharedProps & {\n for: T | State.Type<T>;\n is: (instance: T) => void;\n } & { [K in State.Field<T>]?: T[K] };\n\n type ForMultipleProps<T extends State> = SharedProps & {\n for: Context.Accept<T>;\n is?: ForEach<T>;\n };\n\n type Props<T extends State = State> = ForSingleProps<T> | ForMultipleProps<T>;\n}\n\nfunction Provider<T extends State>(props: Provider.Props<T>) {\n const {\n for: input,\n children,\n fallback,\n name,\n is,\n ...rest\n } = props as Provider.ForSingleProps<T> & Provider.ForMultipleProps<T>;\n\n const ambient = useAmbient();\n const context = useHook<Context>((set) => {\n set(new Context(ambient));\n return () => context.pop();\n });\n\n context.set(input, is);\n\n if (Object.keys(rest).length) {\n const i = State.is(input) ? context.get(input) : input;\n if (i instanceof State) i.set(rest);\n }\n\n return provide(context,\n fallback !== undefined\n ? Runtime.createElement(Runtime.Suspense, { fallback, name }, children)\n : children\n );\n}\n\nexport { Consumer, Provider, Context, provide };\n","import { Component, unbind } from '@expressive/mvc';\nimport { watch, observer } from '@expressive/mvc/observable';\nimport { provide, type Context } from './context';\nimport { Runtime, useHook, useReady } from './runtime';\n\ndeclare module '@expressive/mvc' {\n interface Component {\n /** @deprecated Only to satisfy host JSX. Use `this.get(State)` instead. */\n readonly context: Context;\n /** @deprecated Only to satisfy host JSX. Use `this.get()` instead. */\n readonly state: State.Values<this>;\n /** @deprecated Only to satisfy host JSX. Use `this.set({})` instead. */\n setState: (state: any, callback?: () => void) => void;\n /** @deprecated Only to satisfy host JSX. Use `this.set(key)` instead. */\n forceUpdate: (callback?: () => void) => void;\n }\n}\n\n// Host-agnostic seams: `state` is a read-only values bag; `context`'s setter\n// pushes a child context, registers the instance for teardown, and installs its\n// per-instance render host. Host-specific descriptors stay in each adapter.\nObject.defineProperties(Component.prototype, {\n state: {\n set() {},\n get: Component.prototype.get\n },\n context: {\n set: bootstrap\n }\n});\n\n/**\n * `State.on` handler that prepares a Component's prototype at bootstrap, before\n * mvc classifies its members:\n *\n * - On the root Component, host own-property keys are trapped so each lands as a\n * plain own property (out of observed state); each adapter assigns its own set.\n * - capitalized methods are rewritten into subcomponents as non-configurable\n * getters, so bootstrap skips them too.\n *\n * (Sealing `render` as the content-render seam is handled by core itself.)\n *\n * `before` covers the per-instance case: a capitalized function assigned as an\n * instance field (e.g. `Sidebar = Sidebar` to inject or override one), promoted\n * before `observe` so it is not mistaken for reactive state.\n */\nComponent.on({\n type(type) {\n if (type === Component)\n for (const key of Runtime.ignore)\n Object.defineProperty(Component.prototype, key, {\n set(value) {\n Object.defineProperty(this, key, { value, writable: true });\n }\n });\n\n // capitalized methods into subcomponents\n subcomponents(type.prototype);\n },\n before(self){\n // capitalized instance fields into subcomponents\n subcomponents(self, true);\n }\n});\n\nfunction bootstrap(this: Component, context: Context){\n context = context.push();\n context.set(this, () => () => this.set(null));\n\n Object.defineProperties(this, {\n context: {\n get: () => context,\n set() {}\n },\n render: {\n value: render(this, context)\n }\n });\n\n this.set(null, () => {\n Object.defineProperty(this, 'props', {\n value: this.props,\n writable: true\n });\n })\n}\n\n/**\n * Wrap a content element in its context provider, a Suspense boundary (unless\n * `fallback` is `false`) and, when `catch` is set, the host error boundary.\n */\nfunction frame(from: Component, context: Context, children: unknown) {\n const { createElement: create } = Runtime;\n\n if(from.fallback !== false)\n children = create(\n Runtime.Suspense,\n { fallback: from.fallback, name: String(from) },\n children\n )\n\n children = provide(context, children);\n\n return from.catch\n ? create(Runtime.ErrorBoundary, { self: from, children })\n : children;\n}\n\n/**\n * Ownership host for `<Component/>`: React owns the instance, so its render\n * threads the bootstrap-pushed context through `Runtime.dedupe` (React stacks\n * render attempts) and tears that context down - destroying the instance - on\n * unmount.\n */\nfunction render(from: Component, context: Context) {\n const { createElement: create } = Runtime;\n const { commit, remove } = Runtime.dedupe(from, context);\n\n const content = from.render;\n const Render = () => content.call(from, from.props);\n const Component = () => {\n from = useHook<Component>((refresh) => {\n if (observer(from) !== null) watch(from, refresh);\n\n return () => {\n remove();\n context.pop();\n };\n }) || from;\n\n useReady(commit);\n\n return frame(from, context, create(Render));\n };\n\n return () => create(Component);\n}\n\n/** Rewrite each own capitalized function on `target` into a subcomponent. */\nfunction subcomponents(target: object, configurable?: boolean) {\n for (const key of Object.getOwnPropertyNames(target)) {\n if (!/^[A-Z]/.test(key)) continue;\n const { value } = Object.getOwnPropertyDescriptor(target, key)!;\n if (typeof value != 'function') continue;\n Object.defineProperty(target, key, {\n configurable,\n get(this: Component) {\n const owner = this.is;\n let render = unbind(value);\n const Component = (props: unknown) =>\n render.call(\n useHook<Component>((set) => watch(owner, set)) || owner,\n props\n );\n\n Object.defineProperty(owner, key, {\n configurable: true,\n get: () => Component,\n set(fn: Function) {\n render = fn;\n }\n });\n\n return Component;\n }\n });\n }\n}\n\nexport { frame };\n"],"mappings":";;;;AAIA,MAAa,UAAU,EAAE;AAgBzB,SAAgB,WAA+B,SAAkB;CAC/D,MAAM,MAAM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KAAK;CACrB,MAAM,eAAe,QAAQ,KAAK,MAAM,KAAK,MAAM;CACnD,MAAM,kBAAkB;AACtB,SAAO,SAAoB,YAAY;AACrC,OAAI,SAAS,KAAK,KAAK,KAAM,OAAM,MAAM,QAAQ;AAEjD,gBAAa;AACX,YAAQ;AACR,YAAQ,KAAK;;IAEf,IAAI;AAEN,WAAS,OAAO;AAEhB,SAAO,MAAM,MAAM,SAAS,OAAO,OAAO,CAAC;;AAG7C,cAAa,OAAO,UAAU;;;AAIhC,SAAS,cAAc,QAAgB,cAAwB;AAC7D,MAAK,MAAM,OAAO,OAAO,oBAAoB,OAAO,EAAE;AACpD,MAAI,CAAC,SAAS,KAAK,IAAI,CAAE;EACzB,MAAM,EAAE,UAAU,OAAO,yBAAyB,QAAQ,IAAI;AAC9D,MAAI,OAAO,SAAS,WAAY;AAChC,SAAO,eAAe,QAAQ,KAAK;GACjC;GACA,MAAqB;IACnB,MAAM,QAAQ,KAAK;IACnB,IAAI,SAAS,OAAO,MAAM;IAC1B,MAAM,aAAa,UACjB,OAAO,KACL,SAAoB,QAAQ,MAAM,OAAO,IAAI,CAAC,IAAI,OAClD,MACD;AAEH,WAAO,eAAe,OAAO,KAAK;KAChC,cAAc;KACd,WAAW;KACX,IAAI,IAAc;AAChB,eAAS;;KAEZ,CAAC;AAEF,WAAO;;GAEV,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { a as Runtime, c as useHook, r as Context$1, s as useFactory, t as frame } from "./component-BqiJWCy1.js";
|
|
2
|
+
import { Component } from "@expressive/mvc";
|
|
3
|
+
import { observer, watch } from "@expressive/mvc/observable";
|
|
4
|
+
|
|
5
|
+
//#region src/element.ts
|
|
6
|
+
let template;
|
|
7
|
+
/**
|
|
8
|
+
* Borrow a host element's descriptors so `self` passes as a React element.
|
|
9
|
+
* `props` false keeps the target's own `props` (Component supplies its own);
|
|
10
|
+
* an object installs it as the element's props (collections have none).
|
|
11
|
+
*/
|
|
12
|
+
function seam(self, props, type, key) {
|
|
13
|
+
if (!template) template = Runtime.createElement("template");
|
|
14
|
+
const descriptors = Object.getOwnPropertyDescriptors(template);
|
|
15
|
+
const store = descriptors._store?.value;
|
|
16
|
+
if (props === false) delete descriptors.props;
|
|
17
|
+
if (store) descriptors._store.value = Object.create(Object.getPrototypeOf(store), Object.getOwnPropertyDescriptors(store));
|
|
18
|
+
Object.defineProperties(self, {
|
|
19
|
+
...descriptors,
|
|
20
|
+
$$typeof: { value: template.$$typeof },
|
|
21
|
+
...props ? { props: { value: props } } : void 0,
|
|
22
|
+
key: { value: key },
|
|
23
|
+
type: { value: type }
|
|
24
|
+
});
|
|
25
|
+
return template.$$typeof;
|
|
26
|
+
}
|
|
27
|
+
Object.defineProperty(Component.prototype, "$$typeof", { get() {
|
|
28
|
+
const self = this.is;
|
|
29
|
+
return seam(self, false, Element.bind(self), self.key);
|
|
30
|
+
} });
|
|
31
|
+
function Element() {
|
|
32
|
+
const outer = Context$1.get();
|
|
33
|
+
return useFactory(() => {
|
|
34
|
+
let from = this;
|
|
35
|
+
const context = outer.push(this);
|
|
36
|
+
const Content = () => from.render(from.props);
|
|
37
|
+
return () => {
|
|
38
|
+
from = useHook((refresh) => {
|
|
39
|
+
if (observer(this) !== null) watch(this, refresh);
|
|
40
|
+
return () => context.pop();
|
|
41
|
+
}) || this;
|
|
42
|
+
return frame(from, context, Runtime.createElement(Content));
|
|
43
|
+
};
|
|
44
|
+
})();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { seam as t };
|
|
49
|
+
//# sourceMappingURL=element-Dqygmyg3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element-Dqygmyg3.js","names":["Context"],"sources":["../src/element.ts"],"sourcesContent":["import { Component } from '@expressive/mvc';\nimport { watch, observer } from '@expressive/mvc/observable';\nimport { Context } from './context';\nimport { Runtime, useFactory, useHook } from './runtime';\nimport { frame } from './component';\n\ndeclare module '@expressive/mvc' {\n interface Component {\n /** @deprecated Only to satisfy host JSX. */\n readonly type: typeof Component;\n }\n}\n\nlet template: any;\n\n/**\n * Borrow a host element's descriptors so `self` passes as a React element.\n * `props` false keeps the target's own `props` (Component supplies its own);\n * an object installs it as the element's props (collections have none).\n */\nexport function seam(\n self: object,\n props: object | false,\n type: Function,\n key: unknown\n) {\n if (!template) template = Runtime.createElement('template');\n\n const descriptors = Object.getOwnPropertyDescriptors(template);\n const store = descriptors._store?.value;\n\n if (props === false) delete (descriptors as any).props;\n\n if (store)\n descriptors._store.value = Object.create(\n Object.getPrototypeOf(store),\n Object.getOwnPropertyDescriptors(store)\n );\n\n Object.defineProperties(self, {\n ...descriptors,\n $$typeof: { value: template.$$typeof },\n ...(props ? { props: { value: props } } : undefined),\n key: { value: key },\n type: { value: type }\n });\n\n return template.$$typeof;\n}\n\nObject.defineProperty(Component.prototype, '$$typeof', {\n get(this: Component) {\n const self = this.is;\n return seam(self, false, Element.bind(self), self.key);\n }\n});\n\nfunction Element(this: Component){\n const outer = Context.get();\n const render = useFactory(() => {\n let from = this;\n const context = outer.push(this);\n const Content = () => from.render(from.props);\n\n return () => {\n from = useHook<Component>((refresh) => {\n if (observer(this) !== null) watch(this, refresh);\n return () => context.pop();\n }) || this;\n\n return frame(from, context, Runtime.createElement(Content));\n };\n });\n\n return render();\n}\n"],"mappings":";;;;;AAaA,IAAI;;;;;;AAOJ,SAAgB,KACd,MACA,OACA,MACA,KACA;AACA,KAAI,CAAC,SAAU,YAAW,QAAQ,cAAc,WAAW;CAE3D,MAAM,cAAc,OAAO,0BAA0B,SAAS;CAC9D,MAAM,QAAQ,YAAY,QAAQ;AAElC,KAAI,UAAU,MAAO,QAAQ,YAAoB;AAEjD,KAAI,MACF,aAAY,OAAO,QAAQ,OAAO,OAChC,OAAO,eAAe,MAAM,EAC5B,OAAO,0BAA0B,MAAM,CACxC;AAEH,QAAO,iBAAiB,MAAM;EAC5B,GAAG;EACH,UAAU,EAAE,OAAO,SAAS,UAAU;EACtC,GAAI,QAAQ,EAAE,OAAO,EAAE,OAAO,OAAO,EAAE,GAAG;EAC1C,KAAK,EAAE,OAAO,KAAK;EACnB,MAAM,EAAE,OAAO,MAAM;EACtB,CAAC;AAEF,QAAO,SAAS;;AAGlB,OAAO,eAAe,UAAU,WAAW,YAAY,EACrD,MAAqB;CACnB,MAAM,OAAO,KAAK;AAClB,QAAO,KAAK,MAAM,OAAO,QAAQ,KAAK,KAAK,EAAE,KAAK,IAAI;GAEzD,CAAC;AAEF,SAAS,UAAwB;CAC/B,MAAM,QAAQA,UAAQ,KAAK;AAgB3B,QAfe,iBAAiB;EAC9B,IAAI,OAAO;EACX,MAAM,UAAU,MAAM,KAAK,KAAK;EAChC,MAAM,gBAAgB,KAAK,OAAO,KAAK,MAAM;AAE7C,eAAa;AACX,UAAO,SAAoB,YAAY;AACrC,QAAI,SAAS,KAAK,KAAK,KAAM,OAAM,MAAM,QAAQ;AACjD,iBAAa,QAAQ,KAAK;KAC1B,IAAI;AAEN,UAAO,MAAM,MAAM,SAAS,QAAQ,cAAc,QAAQ,CAAC;;GAE7D,EAEa"}
|
package/dist/has.d.ts
ADDED
package/dist/has.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { o as use } from "./component-BqiJWCy1.js";
|
|
2
|
+
import { t as seam } from "./element-Dqygmyg3.js";
|
|
3
|
+
import { has } from "@expressive/mvc";
|
|
4
|
+
|
|
5
|
+
//#region src/has.ts
|
|
6
|
+
for (const Collection of [has.List, has.Pool]) Object.defineProperty(Collection.prototype, "$$typeof", { get() {
|
|
7
|
+
const self = source(this);
|
|
8
|
+
return seam(self, {}, Members.bind(self), null);
|
|
9
|
+
} });
|
|
10
|
+
function Members() {
|
|
11
|
+
return [...use(this)];
|
|
12
|
+
}
|
|
13
|
+
/** Resolve a collection's canonical instance behind any subscriber proxies. */
|
|
14
|
+
function source(from) {
|
|
15
|
+
let self = from;
|
|
16
|
+
for (let proto = Object.getPrototypeOf(self); proto instanceof has.List || proto instanceof has.Pool; proto = Object.getPrototypeOf(self)) self = proto;
|
|
17
|
+
return self;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { has };
|
|
22
|
+
//# sourceMappingURL=has.js.map
|
package/dist/has.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"has.js","names":[],"sources":["../src/has.ts"],"sourcesContent":["import { has } from '@expressive/mvc';\nimport { use } from './runtime';\nimport { seam } from './element';\n\nfor (const Collection of [has.List, has.Pool])\n Object.defineProperty(Collection.prototype, '$$typeof', {\n get() {\n const self = source(this) as has.List<unknown>;\n return seam(self, {}, Members.bind(self), null);\n }\n });\n\nfunction Members(this: has.List<unknown> | has.Pool<unknown>) {\n return [...use(this)];\n}\n\n/** Resolve a collection's canonical instance behind any subscriber proxies. */\nfunction source(from: object) {\n let self = from;\n\n for (\n let proto = Object.getPrototypeOf(self);\n proto instanceof has.List || proto instanceof has.Pool;\n proto = Object.getPrototypeOf(self)\n )\n self = proto;\n\n return self;\n}\n\nexport { has };\n"],"mappings":";;;;;AAIA,KAAK,MAAM,cAAc,CAAC,IAAI,MAAM,IAAI,KAAK,CAC3C,QAAO,eAAe,WAAW,WAAW,YAAY,EACtD,MAAM;CACJ,MAAM,OAAO,OAAO,KAAK;AACzB,QAAO,KAAK,MAAM,EAAE,EAAE,QAAQ,KAAK,KAAK,EAAE,KAAK;GAElD,CAAC;AAEJ,SAAS,UAAqD;AAC5D,QAAO,CAAC,GAAG,IAAI,KAAK,CAAC;;;AAIvB,SAAS,OAAO,MAAc;CAC5B,IAAI,OAAO;AAEX,MACE,IAAI,QAAQ,OAAO,eAAe,KAAK,EACvC,iBAAiB,IAAI,QAAQ,iBAAiB,IAAI,MAClD,QAAQ,OAAO,eAAe,KAAK,CAEnC,QAAO;AAET,QAAO"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { has } from "./has.js";
|
|
2
|
+
import { map } from "./map.js";
|
|
3
|
+
import { Component as Component$1, Context, def, get, ref, set } from "@expressive/mvc";
|
|
2
4
|
import { JSX, ReactNode } from "react";
|
|
3
5
|
import { Consumer, Provider, State, State as adapter_default, use } from "./adapter";
|
|
4
6
|
|
|
7
|
+
//#region src/element.d.ts
|
|
8
|
+
declare module '@expressive/mvc' {
|
|
9
|
+
interface Component {
|
|
10
|
+
/** @deprecated Only to satisfy host JSX. */
|
|
11
|
+
readonly type: typeof Component;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Borrow a host element's descriptors so `self` passes as a React element.
|
|
16
|
+
* `props` false keeps the target's own `props` (Component supplies its own);
|
|
17
|
+
* an object installs it as the element's props (collections have none).
|
|
18
|
+
*/
|
|
19
|
+
//#endregion
|
|
5
20
|
//#region src/jsx-runtime.d.ts
|
|
6
21
|
declare module '@expressive/mvc/jsx-runtime' {
|
|
7
22
|
interface Host {
|
|
@@ -10,5 +25,5 @@ declare module '@expressive/mvc/jsx-runtime' {
|
|
|
10
25
|
}
|
|
11
26
|
}
|
|
12
27
|
//#endregion
|
|
13
|
-
export { Component, Consumer, Context,
|
|
28
|
+
export { Component$1 as Component, Consumer, Context, Provider, State, def, adapter_default as default, get, has, map, ref, set, use };
|
|
14
29
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./component-BqiJWCy1.js";
|
|
2
|
+
import { has } from "./has.js";
|
|
3
|
+
import { map } from "./map.js";
|
|
4
|
+
import { Component, Component as Component$1, Context, def, get, ref, set } from "@expressive/mvc";
|
|
2
5
|
import React, { Children, Suspense, createContext, createElement, isValidElement, useContext, useEffect, useRef, useState } from "react";
|
|
3
6
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
7
|
import { host } from "@expressive/mvc/jsx-runtime";
|
|
@@ -105,5 +108,5 @@ Object.assign(Runtime, {
|
|
|
105
108
|
});
|
|
106
109
|
|
|
107
110
|
//#endregion
|
|
108
|
-
export { Component, Consumer, Context,
|
|
111
|
+
export { Component, Consumer, Context, Provider, State, def, adapter_default as default, get, has, map, ref, set, use };
|
|
109
112
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Component"],"sources":["../src/jsx-runtime.ts","../src/boundary.ts","../src/index.ts"],"sourcesContent":["import { Children, isValidElement } from 'react';\nimport { Fragment, jsx, jsxs } from 'react/jsx-runtime';\nimport { host } from '@expressive/mvc/jsx-runtime';\n\nimport type { Component } from '@expressive/mvc';\nimport type { JSX as ReactJSX, ReactNode } from 'react';\n\ndeclare module '@expressive/mvc/jsx-runtime' {\n interface Host {\n node: ReactNode;\n intrinsics: ReactJSX.IntrinsicElements;\n }\n}\n\nhost({\n jsx,\n jsxs,\n Fragment,\n isElement: isValidElement,\n childrenOf(children): Component.Node[] {\n return Children.toArray(children as React.ReactNode);\n },\n typeOf(node){\n return isValidElement(node) ? node.type : undefined;\n },\n propsOf(node){\n return isValidElement(node) ? node.props as Record<string, unknown> : {};\n }\n});\n","import { Component } from '@expressive/mvc';\nimport React from 'react';\nimport { Context } from './context';\n\ninterface Entry {\n committed?: boolean;\n context: Context;\n commit: () => void;\n remove: () => void;\n};\n\n/** In-flight render attempts by tree position, per ambient context.\n * When one commits, older uncommitted attempts at its slot are provably dead. */\nconst SLOTS = new WeakMap<Context, Map<string, Set<Entry>>>();\n\n/**\n * React's attempt lifecycle: fiber-keyed stacking so a superseded (uncommitted)\n * render attempt at a tree position is discarded when a later one commits.\n */\nexport function dedupe(on: Component, context: Context) {\n let slots = SLOTS.get(context.parent!);\n if (!slots) SLOTS.set(context.parent!, (slots = new Map()));\n\n // key/index path to root: stable across render attempts of this element\n let slot = '';\n for (let f = (on as any)._reactInternals; f; f = f.return)\n slot += (f.key ?? f.index) + '.';\n\n let list = slots.get(slot);\n if (!list) slots.set(slot, (list = new Set()));\n\n const entry: Entry = {\n context,\n commit() {\n entry.committed = true;\n\n for (const e of list) {\n if (e === entry) break;\n if (e.committed) continue;\n e.context.pop();\n list.delete(e);\n }\n },\n remove() {\n list.delete(entry);\n }\n };\n\n list.add(entry);\n return entry;\n};\n\nexport class ErrorBoundary extends React.Component<{\n self: Component;\n children: React.ReactNode;\n}> {\n state = {} as { error?: Error };\n recovering = false;\n\n constructor(props: ErrorBoundary['props']) {\n super(props);\n }\n\n static getDerivedStateFromError(error: Error) {\n return { error };\n }\n\n componentDidCatch(error: Error) {\n const { self } = this.props;\n const { fallback } = self;\n const reset = (error?: Error) => {\n this.recovering = true;\n this.setState({ error });\n };\n\n Promise.resolve(self.catch!(error))\n .then(() => reset(), reset)\n .finally(() => self.set({ fallback }, true));\n }\n\n componentDidUpdate() {\n this.recovering = false;\n }\n\n render() {\n if (!this.state.error) return this.props.children;\n if (this.recovering) throw this.state.error;\n return this.props.self.fallback;\n }\n};\n","import { Component } from '@expressive/mvc';\nimport {\n createContext,\n createElement,\n Suspense,\n useContext,\n useEffect,\n useRef,\n useState\n} from 'react';\n\nimport './jsx-runtime';\nimport { Runtime } from './adapter';\nimport { ErrorBoundary, dedupe } from './boundary';\n\n// React detects class components by this brand (preact reads `prototype.render`).\nObject.defineProperty(Component.prototype, 'isReactComponent', {\n value: true\n});\n\nObject.assign(Runtime, {\n dedupe,\n ErrorBoundary,\n createElement,\n createContext,\n useContext,\n useEffect,\n useState,\n useRef,\n Suspense,\n ignore: [\n 'updater',\n 'refs',\n '_reactInternals',\n '_reactInternalInstance'\n ]\n});\n\nexport { State, State as default, use, Consumer, Provider } from './adapter';\nexport { Component, Context,
|
|
1
|
+
{"version":3,"file":"index.js","names":["Component"],"sources":["../src/jsx-runtime.ts","../src/boundary.ts","../src/index.ts"],"sourcesContent":["import { Children, isValidElement } from 'react';\nimport { Fragment, jsx, jsxs } from 'react/jsx-runtime';\nimport { host } from '@expressive/mvc/jsx-runtime';\n\nimport type { Component } from '@expressive/mvc';\nimport type { JSX as ReactJSX, ReactNode } from 'react';\n\ndeclare module '@expressive/mvc/jsx-runtime' {\n interface Host {\n node: ReactNode;\n intrinsics: ReactJSX.IntrinsicElements;\n }\n}\n\nhost({\n jsx,\n jsxs,\n Fragment,\n isElement: isValidElement,\n childrenOf(children): Component.Node[] {\n return Children.toArray(children as React.ReactNode);\n },\n typeOf(node){\n return isValidElement(node) ? node.type : undefined;\n },\n propsOf(node){\n return isValidElement(node) ? node.props as Record<string, unknown> : {};\n }\n});\n","import { Component } from '@expressive/mvc';\nimport React from 'react';\nimport { Context } from './context';\n\ninterface Entry {\n committed?: boolean;\n context: Context;\n commit: () => void;\n remove: () => void;\n};\n\n/** In-flight render attempts by tree position, per ambient context.\n * When one commits, older uncommitted attempts at its slot are provably dead. */\nconst SLOTS = new WeakMap<Context, Map<string, Set<Entry>>>();\n\n/**\n * React's attempt lifecycle: fiber-keyed stacking so a superseded (uncommitted)\n * render attempt at a tree position is discarded when a later one commits.\n */\nexport function dedupe(on: Component, context: Context) {\n let slots = SLOTS.get(context.parent!);\n if (!slots) SLOTS.set(context.parent!, (slots = new Map()));\n\n // key/index path to root: stable across render attempts of this element\n let slot = '';\n for (let f = (on as any)._reactInternals; f; f = f.return)\n slot += (f.key ?? f.index) + '.';\n\n let list = slots.get(slot);\n if (!list) slots.set(slot, (list = new Set()));\n\n const entry: Entry = {\n context,\n commit() {\n entry.committed = true;\n\n for (const e of list) {\n if (e === entry) break;\n if (e.committed) continue;\n e.context.pop();\n list.delete(e);\n }\n },\n remove() {\n list.delete(entry);\n }\n };\n\n list.add(entry);\n return entry;\n};\n\nexport class ErrorBoundary extends React.Component<{\n self: Component;\n children: React.ReactNode;\n}> {\n state = {} as { error?: Error };\n recovering = false;\n\n constructor(props: ErrorBoundary['props']) {\n super(props);\n }\n\n static getDerivedStateFromError(error: Error) {\n return { error };\n }\n\n componentDidCatch(error: Error) {\n const { self } = this.props;\n const { fallback } = self;\n const reset = (error?: Error) => {\n this.recovering = true;\n this.setState({ error });\n };\n\n Promise.resolve(self.catch!(error))\n .then(() => reset(), reset)\n .finally(() => self.set({ fallback }, true));\n }\n\n componentDidUpdate() {\n this.recovering = false;\n }\n\n render() {\n if (!this.state.error) return this.props.children;\n if (this.recovering) throw this.state.error;\n return this.props.self.fallback;\n }\n};\n","import { Component } from '@expressive/mvc';\nimport {\n createContext,\n createElement,\n Suspense,\n useContext,\n useEffect,\n useRef,\n useState\n} from 'react';\n\nimport './element';\nimport './jsx-runtime';\n\nimport { Runtime } from './adapter';\nimport { ErrorBoundary, dedupe } from './boundary';\n\n// React detects class components by this brand (preact reads `prototype.render`).\nObject.defineProperty(Component.prototype, 'isReactComponent', {\n value: true\n});\n\nObject.assign(Runtime, {\n dedupe,\n ErrorBoundary,\n createElement,\n createContext,\n useContext,\n useEffect,\n useState,\n useRef,\n Suspense,\n ignore: [\n 'updater',\n 'refs',\n '_reactInternals',\n '_reactInternalInstance'\n ]\n});\n\nexport { State, State as default, use, Consumer, Provider } from './adapter';\nexport { Component, Context, def, get, ref, set } from '@expressive/mvc';\nexport { has } from './has';\nexport { map } from './map';\n"],"mappings":";;;;;;;;;;AAcA,KAAK;CACH;CACA;CACA;CACA,WAAW;CACX,WAAW,UAA4B;AACrC,SAAO,SAAS,QAAQ,SAA4B;;CAEtD,OAAO,MAAK;AACV,SAAO,eAAe,KAAK,GAAG,KAAK,OAAO;;CAE5C,QAAQ,MAAK;AACX,SAAO,eAAe,KAAK,GAAG,KAAK,QAAmC,EAAE;;CAE3E,CAAC;;;;;;ACfF,MAAM,wBAAQ,IAAI,SAA2C;;;;;AAM7D,SAAgB,OAAO,IAAe,SAAkB;CACtD,IAAI,QAAQ,MAAM,IAAI,QAAQ,OAAQ;AACtC,KAAI,CAAC,MAAO,OAAM,IAAI,QAAQ,QAAU,wBAAQ,IAAI,KAAK,CAAE;CAG3D,IAAI,OAAO;AACX,MAAK,IAAI,I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,CAAC"}
|
package/dist/map.d.ts
ADDED
package/dist/map.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { o as use } from "./component-BqiJWCy1.js";
|
|
2
|
+
import { t as seam } from "./element-Dqygmyg3.js";
|
|
3
|
+
import { map } from "@expressive/mvc";
|
|
4
|
+
|
|
5
|
+
//#region src/map.ts
|
|
6
|
+
Object.defineProperty(map.Managed.prototype, "$$typeof", { get() {
|
|
7
|
+
const self = source(this);
|
|
8
|
+
return seam(self, {}, Values.bind(self), null);
|
|
9
|
+
} });
|
|
10
|
+
function Values() {
|
|
11
|
+
return [...use(this).values()];
|
|
12
|
+
}
|
|
13
|
+
/** Resolve a map's canonical instance behind any subscriber proxies. */
|
|
14
|
+
function source(from) {
|
|
15
|
+
let self = from;
|
|
16
|
+
for (let proto = Object.getPrototypeOf(self); proto instanceof map.Managed; proto = Object.getPrototypeOf(self)) self = proto;
|
|
17
|
+
return self;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { map };
|
|
22
|
+
//# sourceMappingURL=map.js.map
|
package/dist/map.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.js","names":[],"sources":["../src/map.ts"],"sourcesContent":["import { map } from '@expressive/mvc';\nimport { use } from './runtime';\nimport { seam } from './element';\n\nObject.defineProperty(map.Managed.prototype, '$$typeof', {\n get() {\n const self = source(this) as map.Managed<unknown, unknown>;\n return seam(self, {}, Values.bind(self), null);\n }\n});\n\nfunction Values(this: map.Managed<unknown, unknown>) {\n return [...use(this).values()];\n}\n\n/** Resolve a map's canonical instance behind any subscriber proxies. */\nfunction source(from: object) {\n let self = from;\n\n for (\n let proto = Object.getPrototypeOf(self);\n proto instanceof map.Managed;\n proto = Object.getPrototypeOf(self)\n )\n self = proto;\n\n return self;\n}\n\nexport { map };\n"],"mappings":";;;;;AAIA,OAAO,eAAe,IAAI,QAAQ,WAAW,YAAY,EACvD,MAAM;CACJ,MAAM,OAAO,OAAO,KAAK;AACzB,QAAO,KAAK,MAAM,EAAE,EAAE,OAAO,KAAK,KAAK,EAAE,KAAK;GAEjD,CAAC;AAEF,SAAS,SAA4C;AACnD,QAAO,CAAC,GAAG,IAAI,KAAK,CAAC,QAAQ,CAAC;;;AAIhC,SAAS,OAAO,MAAc;CAC5B,IAAI,OAAO;AAEX,MACE,IAAI,QAAQ,OAAO,eAAe,KAAK,EACvC,iBAAiB,IAAI,SACrB,QAAQ,OAAO,eAAe,KAAK,CAEnC,QAAO;AAET,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@expressive/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.82.0",
|
|
4
|
+
"description": "Class-based reactive state for React. Define models as plain classes; components update when the values they read change.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"expressive",
|
|
7
|
+
"mvc",
|
|
7
8
|
"react",
|
|
8
|
-
"state"
|
|
9
|
+
"state",
|
|
10
|
+
"state-management",
|
|
11
|
+
"reactive",
|
|
12
|
+
"observable",
|
|
13
|
+
"model"
|
|
9
14
|
],
|
|
10
15
|
"license": "MIT",
|
|
11
16
|
"author": {
|
|
@@ -27,7 +32,9 @@
|
|
|
27
32
|
"./src/jsx-runtime.ts",
|
|
28
33
|
"./src/state.get.ts",
|
|
29
34
|
"./src/state.use.ts",
|
|
30
|
-
"./src/component.ts"
|
|
35
|
+
"./src/component.ts",
|
|
36
|
+
"./src/map.ts",
|
|
37
|
+
"./src/has.ts"
|
|
31
38
|
],
|
|
32
39
|
"main": "dist/index.js",
|
|
33
40
|
"types": "dist/index.d.ts",
|
|
@@ -52,7 +59,7 @@
|
|
|
52
59
|
"test": "tsc --noEmit && bun test --coverage --only-failures"
|
|
53
60
|
},
|
|
54
61
|
"dependencies": {
|
|
55
|
-
"@expressive/mvc": "^0.
|
|
62
|
+
"@expressive/mvc": "^0.81.0"
|
|
56
63
|
},
|
|
57
64
|
"devDependencies": {
|
|
58
65
|
"@testing-library/react": "^16.3.0",
|