@expressive/react 0.77.0 → 0.78.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 +228 -0
- package/dist/context-B-HNZeaN.d.ts +49 -0
- package/dist/context-BfeYQmAX.js +59 -0
- package/dist/context-BfeYQmAX.js.map +1 -0
- package/dist/index.d.ts +10 -131
- package/dist/index.js +73 -188
- package/dist/index.js.map +1 -1
- package/dist/{state.d.ts → runtime.d.ts} +74 -34
- package/dist/runtime.js +74 -0
- package/dist/runtime.js.map +1 -0
- package/package.json +17 -13
- package/LICENSE +0 -21
- package/dist/state.js +0 -105
- package/dist/state.js.map +0 -1
- package/dist/use-AIA25P7A.js +0 -63
- package/dist/use-AIA25P7A.js.map +0 -1
- package/dist/use-_Es6cU25.d.ts +0 -5
package/dist/index.js
CHANGED
|
@@ -1,158 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { r as Provider, t as Consumer } from "./context-BfeYQmAX.js";
|
|
2
|
+
import { Component, Component as Component$1, Context, Observer, def, get, hot, ref, set } from "@expressive/mvc";
|
|
3
|
+
import React, { Children, Suspense, createContext, createElement, isValidElement, useContext, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { host } from "@expressive/mvc/jsx-runtime";
|
|
6
|
+
import { Runtime, State, State as runtime_default, use } from "./runtime";
|
|
5
7
|
|
|
6
|
-
//#region src/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
function Provider(props) {
|
|
19
|
-
const { for: input, children, fallback, name, is, ...rest } = props;
|
|
20
|
-
const ambient = useContext(Layers);
|
|
21
|
-
const context = useHook((set) => {
|
|
22
|
-
set(new Context$2(ambient));
|
|
23
|
-
return () => context.pop();
|
|
24
|
-
});
|
|
25
|
-
context.set(input, is);
|
|
26
|
-
if (Object.keys(rest).length) {
|
|
27
|
-
const i = State$2.is(input) ? context.get(input) : input;
|
|
28
|
-
if (i instanceof State$2) i.set(rest);
|
|
29
|
-
}
|
|
30
|
-
return createElement(Layers.Provider, {
|
|
31
|
-
value: context,
|
|
32
|
-
children: fallback !== void 0 ? createElement(Suspense, {
|
|
33
|
-
fallback,
|
|
34
|
-
name
|
|
35
|
-
}, children) : children
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
//#endregion
|
|
40
|
-
//#region src/component.ts
|
|
41
|
-
const PENDING = /* @__PURE__ */ new WeakMap();
|
|
42
|
-
const INTERNAL = [
|
|
43
|
-
"updater",
|
|
44
|
-
"refs",
|
|
45
|
-
"_reactInternals",
|
|
46
|
-
"_reactInternalInstance"
|
|
47
|
-
];
|
|
48
|
-
var Component = class extends State$1 {
|
|
49
|
-
static {
|
|
50
|
-
this.contextType = Layers;
|
|
51
|
-
}
|
|
52
|
-
constructor(nextProps, ...rest) {
|
|
53
|
-
const { is, ...props } = nextProps;
|
|
54
|
-
rest = rest.filter((x) => !(x instanceof Context$2));
|
|
55
|
-
super(props, rest, is && ((x) => void is(x)), () => {
|
|
56
|
-
Object.defineProperty(this, "props", { enumerable: false });
|
|
57
|
-
});
|
|
58
|
-
this.fallback = set$1(null);
|
|
59
|
-
const existing = PENDING.get(nextProps);
|
|
60
|
-
if (existing) return existing;
|
|
61
|
-
PENDING.set(nextProps, this);
|
|
62
|
-
for (const key of INTERNAL) Object.defineProperty(this, key, {
|
|
63
|
-
configurable: true,
|
|
64
|
-
writable: true
|
|
65
|
-
});
|
|
66
|
-
this.props = nextProps;
|
|
67
|
-
this.set("props", () => {
|
|
68
|
-
this.set(this.props);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Output for this component. Override to define custom JSX.
|
|
73
|
-
*
|
|
74
|
-
* Properties accessed via `this` are reactive and will trigger a render when , in addition to props.
|
|
75
|
-
*
|
|
76
|
-
* Accepts optional parameter to receive extra props
|
|
77
|
-
* from JSX, beyond those merged to state properties.
|
|
78
|
-
*
|
|
79
|
-
* To declare extra props use `props = {} as { ... }` with expected shape.
|
|
80
|
-
* A default is required to satisfy TypeScript but safely ignored.
|
|
81
|
-
*
|
|
82
|
-
* ```ts
|
|
83
|
-
* render(props = {} as { label: string }) {
|
|
84
|
-
* return <span>{props.label}</span>;
|
|
85
|
-
* }
|
|
86
|
-
* ```
|
|
87
|
-
* Usable as:
|
|
88
|
-
* ```tsx
|
|
89
|
-
* <MyComponent label="Hello" />
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* These must be compatible with State-derived properties, or be unused.
|
|
93
|
-
* Props you declare as not-optional will be required by the JSX element.
|
|
94
|
-
*
|
|
95
|
-
* Without a parameter, children are accepted by default and passed through provider.
|
|
96
|
-
*/
|
|
97
|
-
render(props) {
|
|
98
|
-
return this.props.children || null;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
Object.defineProperties(Component.prototype, {
|
|
102
|
-
isReactComponent: { get: () => true },
|
|
103
|
-
state: {
|
|
104
|
-
get() {
|
|
105
|
-
return this.get();
|
|
106
|
-
},
|
|
107
|
-
set() {}
|
|
8
|
+
//#region src/jsx-runtime.ts
|
|
9
|
+
host({
|
|
10
|
+
jsx,
|
|
11
|
+
jsxs,
|
|
12
|
+
Fragment,
|
|
13
|
+
isElement: isValidElement,
|
|
14
|
+
childrenOf(children) {
|
|
15
|
+
return Children.toArray(children);
|
|
16
|
+
},
|
|
17
|
+
typeOf(node) {
|
|
18
|
+
return isValidElement(node) ? node.type : void 0;
|
|
108
19
|
},
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
set: bootstrap
|
|
20
|
+
propsOf(node) {
|
|
21
|
+
return isValidElement(node) ? node.props : {};
|
|
112
22
|
}
|
|
113
23
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
self,
|
|
142
|
-
children
|
|
143
|
-
}) : children;
|
|
144
|
-
}
|
|
145
|
-
Object.defineProperties(self, {
|
|
146
|
-
context: {
|
|
147
|
-
get: () => context,
|
|
148
|
-
set() {}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/boundary.ts
|
|
27
|
+
/** In-flight render attempts by tree position, per ambient context.
|
|
28
|
+
* When one commits, older uncommitted attempts at its slot are provably dead. */
|
|
29
|
+
const SLOTS = /* @__PURE__ */ new WeakMap();
|
|
30
|
+
/**
|
|
31
|
+
* React's attempt lifecycle: fiber-keyed stacking so a superseded (uncommitted)
|
|
32
|
+
* render attempt at a tree position is discarded when a later one commits.
|
|
33
|
+
*/
|
|
34
|
+
function dedupe(on, context) {
|
|
35
|
+
let slots = SLOTS.get(context.parent);
|
|
36
|
+
if (!slots) SLOTS.set(context.parent, slots = /* @__PURE__ */ new Map());
|
|
37
|
+
let slot = "";
|
|
38
|
+
for (let f = on._reactInternals; f; f = f.return) slot += (f.key ?? f.index) + ".";
|
|
39
|
+
let list = slots.get(slot);
|
|
40
|
+
if (!list) slots.set(slot, list = /* @__PURE__ */ new Set());
|
|
41
|
+
const entry = {
|
|
42
|
+
context,
|
|
43
|
+
commit() {
|
|
44
|
+
entry.committed = true;
|
|
45
|
+
for (const e of list) {
|
|
46
|
+
if (e === entry) break;
|
|
47
|
+
if (e.committed) continue;
|
|
48
|
+
e.context.pop();
|
|
49
|
+
list.delete(e);
|
|
50
|
+
}
|
|
149
51
|
},
|
|
150
|
-
|
|
151
|
-
|
|
52
|
+
remove() {
|
|
53
|
+
list.delete(entry);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
list.add(entry);
|
|
57
|
+
return entry;
|
|
152
58
|
}
|
|
153
59
|
var ErrorBoundary = class extends React.Component {
|
|
154
|
-
constructor(
|
|
155
|
-
super(
|
|
60
|
+
constructor(props) {
|
|
61
|
+
super(props);
|
|
156
62
|
this.state = {};
|
|
157
63
|
this.recovering = false;
|
|
158
64
|
}
|
|
@@ -177,49 +83,28 @@ var ErrorBoundary = class extends React.Component {
|
|
|
177
83
|
return this.props.self.fallback;
|
|
178
84
|
}
|
|
179
85
|
};
|
|
180
|
-
const SEEN = /* @__PURE__ */ new WeakSet();
|
|
181
|
-
Component.on((self) => {
|
|
182
|
-
let Type = self.constructor;
|
|
183
|
-
while (Type !== Component) {
|
|
184
|
-
if (SEEN.has(Type)) break;
|
|
185
|
-
SEEN.add(Type);
|
|
186
|
-
subcomponents(Type.prototype);
|
|
187
|
-
Type = Object.getPrototypeOf(Type);
|
|
188
|
-
}
|
|
189
|
-
subcomponents(self);
|
|
190
|
-
});
|
|
191
|
-
function subcomponents(self) {
|
|
192
|
-
for (const key of Object.getOwnPropertyNames(self)) {
|
|
193
|
-
if (!/^[A-Z]/.test(key)) continue;
|
|
194
|
-
const { get, value } = Object.getOwnPropertyDescriptor(self, key);
|
|
195
|
-
if (!get && typeof value !== "function") continue;
|
|
196
|
-
function subcomponent() {
|
|
197
|
-
const self = this.is;
|
|
198
|
-
let render = unbind(get ? get.call(self) : value);
|
|
199
|
-
const Component = (props) => render.call(useHook((set) => watch(self, set)), props);
|
|
200
|
-
Object.defineProperty(self, key, {
|
|
201
|
-
configurable: true,
|
|
202
|
-
get: () => Component,
|
|
203
|
-
set(fn) {
|
|
204
|
-
render = fn;
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
return Component;
|
|
208
|
-
}
|
|
209
|
-
Object.defineProperty(self, key, {
|
|
210
|
-
configurable: true,
|
|
211
|
-
get: subcomponent
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
86
|
|
|
216
87
|
//#endregion
|
|
217
88
|
//#region src/index.ts
|
|
218
|
-
|
|
219
|
-
Runtime
|
|
220
|
-
|
|
221
|
-
|
|
89
|
+
Object.defineProperty(Component$1.prototype, "isReactComponent", { value: true });
|
|
90
|
+
Object.assign(Runtime, {
|
|
91
|
+
dedupe,
|
|
92
|
+
ErrorBoundary,
|
|
93
|
+
createElement,
|
|
94
|
+
createContext,
|
|
95
|
+
useContext,
|
|
96
|
+
useEffect,
|
|
97
|
+
useState,
|
|
98
|
+
useRef,
|
|
99
|
+
Suspense,
|
|
100
|
+
ignore: [
|
|
101
|
+
"updater",
|
|
102
|
+
"refs",
|
|
103
|
+
"_reactInternals",
|
|
104
|
+
"_reactInternalInstance"
|
|
105
|
+
]
|
|
106
|
+
});
|
|
222
107
|
|
|
223
108
|
//#endregion
|
|
224
|
-
export { Component, Consumer, Context,
|
|
109
|
+
export { Component, Consumer, Context, Observer, Provider, State, def, runtime_default as default, get, hot, ref, set, use };
|
|
225
110
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Context","State","State","Context","set"],"sources":["../src/context.ts","../src/component.ts","../src/index.ts"],"sourcesContent":["import { State, Context } from '@expressive/state';\nimport {\n createContext,\n createElement,\n ReactNode,\n Suspense,\n useContext\n} from 'react';\nimport { useHook } from './runtime';\n\nconst Layers = createContext(Context.root);\n\nconst _get = Context.get;\n\nContext.get = (state?: State) => {\n if (!state)\n try {\n return useContext(Layers);\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) => ReactNode | 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?: ReactNode;\n\n /** A fallback tree to show when suspended. */\n fallback?: ReactNode;\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 = useContext(Layers);\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 createElement(Layers.Provider, {\n value: context,\n children:\n fallback !== undefined\n ? createElement(Suspense, { fallback, name }, children)\n : children\n });\n}\n\nexport { Consumer, Provider, Context, Layers };\n","import { watch, unbind, set } from '@expressive/state';\nimport React, { createElement, Suspense } from 'react';\nimport { Context, Layers } from './context';\nimport { useHook } from './runtime';\nimport { State } from './state';\n\nconst PENDING = new WeakMap<object, Component>();\nconst INTERNAL = [\n 'updater',\n 'refs',\n '_reactInternals',\n '_reactInternalInstance'\n];\n\nexport type StateProps<T extends State> = {\n [K in Exclude<keyof T, keyof Component>]?: T[K];\n};\n\ntype RenderProps<T> = T extends { render(props: infer P): any }\n ? {} extends P\n ? { children?: React.ReactNode }\n : P\n : { children?: React.ReactNode };\n\ninterface ComponentProps<T extends Component> {\n /** Callback for newly created instance. Only called once. */\n is?: (instance: T) => void;\n\n /** Fallback to show when suspended or in error recovery. */\n fallback?: React.ReactNode;\n}\n\nexport type Props<T extends Component> = StateProps<T> &\n ComponentProps<T> &\n RenderProps<T>;\n\ndeclare module '@expressive/state' {\n namespace State {\n export type { Component, Props };\n }\n}\n\nexport class Component extends State {\n static contextType = Layers;\n\n /**\n * All JSX attributes passed to this component.\n * Includes state-derived props, render props, and built-in props like `is` and `fallback`.\n *\n * Will incorperate extra props you declare as props parameter in `render` method.\n */\n declare readonly props: Props<this>;\n\n /** @deprecated Only to satisfy React JSX. Use `this.get(State)` instead. */\n declare readonly context: Context;\n /** @deprecated Only to satisfy React JSX. Use `this.get()` instead. */\n declare readonly state: State.Values<this>;\n /** @deprecated Only to satisfy React JSX. Not implemented. */\n declare setState: (state: any, callback?: () => void) => void;\n /** @deprecated Only to satisfy React JSX. Not implemented. */\n declare forceUpdate: (callback?: () => void) => void;\n\n /**\n * Content to display while this component or its children are suspended.\n * Will cover suspense by pending properties in `this.render` as well.\n * Can be set as a class property or overridden via the `fallback` JSX prop.\n *\n * Defaults to null - nothing will be rendered.\n */\n fallback: React.ReactNode = set(null);\n\n constructor(nextProps: any, ...rest: any[]) {\n const { is, ...props } = nextProps;\n rest = rest.filter((x) => !(x instanceof Context));\n super(props, rest, is && ((x) => void is(x)), () => {\n Object.defineProperty(this, 'props', { enumerable: false });\n });\n\n const existing = PENDING.get(nextProps);\n if (existing) return existing;\n PENDING.set(nextProps, this);\n\n for (const key of INTERNAL)\n Object.defineProperty(this, key, { configurable: true, writable: true });\n\n this.props = nextProps;\n this.set('props', () => {\n this.set(this.props as {});\n });\n }\n\n /**\n * Output for this component. Override to define custom JSX.\n *\n * Properties accessed via `this` are reactive and will trigger a render when , in addition to props.\n *\n * Accepts optional parameter to receive extra props\n * from JSX, beyond those merged to state properties.\n *\n * To declare extra props use `props = {} as { ... }` with expected shape.\n * A default is required to satisfy TypeScript but safely ignored.\n *\n * ```ts\n * render(props = {} as { label: string }) {\n * return <span>{props.label}</span>;\n * }\n * ```\n * Usable as:\n * ```tsx\n * <MyComponent label=\"Hello\" />\n * ```\n *\n * These must be compatible with State-derived properties, or be unused.\n * Props you declare as not-optional will be required by the JSX element.\n *\n * Without a parameter, children are accepted by default and passed through provider.\n */\n render(props?: {}): React.ReactNode {\n return this.props.children || null;\n }\n\n /**\n * Called when a child component throws during render.\n * While this is pending, `fallback` is displayed.\n * When resolved, the error boundary resets and `render` is called again.\n *\n * Override to handle errors - set `this.fallback` for error-specific UI,\n * await async recovery, or await user interaction before retrying. If you\n * assign a fallback within catch, it will be reverted after resolved.\n */\n catch?(error: Error): Promise<void> | void;\n}\n\nObject.defineProperties(Component.prototype, {\n isReactComponent: {\n get: () => true\n },\n state: {\n get(this: Component) {\n return this.get();\n },\n set() { }\n },\n context: {\n configurable: true,\n set: bootstrap\n }\n});\n\nfunction bootstrap(this: Component, context: Context) {\n const self = this.is;\n const render = unbind(this.render);\n\n context = context.push(self);\n\n let active: Component;\n\n function Render() {\n return render.call(active, self.props);\n }\n\n function AsComponent() {\n useHook((refresh) => {\n watch(self, (current) => {\n active = current;\n refresh();\n });\n\n return () => {\n self.set(null);\n context.pop();\n };\n });\n\n const children = createElement(Layers.Provider, {\n value: context,\n children: createElement(\n Suspense,\n { fallback: active.fallback, name: String(self) },\n createElement(Render)\n )\n });\n\n return active.catch\n ? createElement(ErrorBoundary, { self, children })\n : children;\n }\n\n Object.defineProperties(self, {\n context: {\n get: () => context,\n set() { }\n },\n render: {\n value: () => createElement(AsComponent)\n }\n });\n}\n\ninterface BoundaryProps {\n self: Component;\n children: React.ReactNode;\n}\n\nclass ErrorBoundary extends React.Component<BoundaryProps> {\n state = {} as { error?: Error };\n recovering = false;\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\nconst SEEN = new WeakSet<Function>();\n\nComponent.on((self) => {\n let Type = self.constructor as State.Extends;\n\n while (Type !== Component) {\n if (SEEN.has(Type)) break;\n SEEN.add(Type);\n subcomponents(Type.prototype);\n Type = Object.getPrototypeOf(Type);\n }\n\n subcomponents(self);\n});\n\nfunction subcomponents(self: State) {\n for (const key of Object.getOwnPropertyNames(self)) {\n if (!/^[A-Z]/.test(key)) continue;\n\n const { get, value } = Object.getOwnPropertyDescriptor(self, key)!;\n\n if (!get && typeof value !== 'function') continue;\n\n function subcomponent(this: Component) {\n const self = this.is;\n\n let render = unbind(get ? get.call(self) : value);\n\n const Component = (props: unknown) =>\n render.call(\n useHook<Component>((set) => watch(self, set)),\n props\n );\n\n Object.defineProperty(self, key, {\n configurable: true,\n get: () => Component,\n set(fn: Function) {\n render = fn;\n }\n });\n\n return Component;\n }\n\n Object.defineProperty(self, key, {\n configurable: true,\n get: subcomponent\n });\n }\n}","import { createElement, useEffect, useRef, useState } from 'react';\n\nimport { State } from './state';\nimport { Runtime } from './runtime';\nimport { Consumer, Provider } from './context';\n\nRuntime.createElement = createElement;\nRuntime.useEffect = useEffect;\nRuntime.useState = useState;\nRuntime.useRef = useRef;\n\nexport { State, State as default };\nexport { Context, Observable, def, get, ref, set, hot } from '@expressive/state';\nexport { Component } from './component';\nexport { use } from './use';\nexport { Consumer, Provider };\n"],"mappings":";;;;;;AAUA,MAAM,SAAS,cAAcA,UAAQ,KAAK;AAE1C,MAAM,OAAOA,UAAQ;AAErB,UAAQ,OAAO,UAAkB;AAC/B,KAAI,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c;GACd,KAAK;GACN,CAAC;;;;;;ACtRN,QAAQ,gBAAgB;AACxB,QAAQ,YAAY;AACpB,QAAQ,WAAW;AACnB,QAAQ,SAAS"}
|
|
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 './runtime';\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 } from './runtime';\nexport { Component, Context, Observer, def, get, ref, set, hot } from '@expressive/mvc';\nexport { Consumer, Provider } from './context';\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;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ACzC,MAAI,KAAK,WAAY,OAAM,KAAK,MAAM;AACtC,SAAO,KAAK,MAAM,KAAK;;;;;;ACvE3B,OAAO,eAAeA,YAAU,WAAW,oBAAoB,EAC7D,OAAO,MACR,CAAC;AAEF,OAAO,OAAO,SAAS;CACrB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,QAAQ;EACN;EACA;EACA;EACA;EACD;CACF,CAAC"}
|
|
@@ -1,31 +1,24 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import { State } from "@expressive/
|
|
1
|
+
import { i as provide, n as Context, r as Provider, t as Consumer } from "./context-B-HNZeaN.js";
|
|
2
|
+
import { Component, State as State$1 } from "@expressive/mvc";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
5
|
-
declare
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
//#region src/component.d.ts
|
|
5
|
+
declare module '@expressive/mvc' {
|
|
6
|
+
interface Component {
|
|
7
|
+
/** @deprecated Only to satisfy host JSX. Use `this.get(State)` instead. */
|
|
8
|
+
readonly context: Context;
|
|
9
|
+
/** @deprecated Only to satisfy host JSX. Use `this.get()` instead. */
|
|
10
|
+
readonly state: State.Values<this>;
|
|
11
|
+
/** @deprecated Only to satisfy host JSX. Use `this.set({})` instead. */
|
|
12
|
+
setState: (state: any, callback?: () => void) => void;
|
|
13
|
+
/** @deprecated Only to satisfy host JSX. Use `this.set(key)` instead. */
|
|
14
|
+
forceUpdate: (callback?: () => void) => void;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
13
17
|
//#endregion
|
|
14
|
-
//#region src/state.d.ts
|
|
18
|
+
//#region src/state.get.d.ts
|
|
15
19
|
/** Type may not be undefined - instead will be null. */
|
|
16
20
|
type NoVoid<T> = T extends undefined | void ? null : T;
|
|
17
|
-
declare module '@expressive/
|
|
18
|
-
interface UseState extends State {
|
|
19
|
-
/**
|
|
20
|
-
* Optional hook called when State.use() is invoked within a React component.
|
|
21
|
-
*
|
|
22
|
-
* This is called *every render* of the component, and will intercept
|
|
23
|
-
* arguments which would otherwise be passed into the State constructor.
|
|
24
|
-
*
|
|
25
|
-
* @param props Arguments passed to State.use().
|
|
26
|
-
*/
|
|
27
|
-
use?(...props: any[]): Promise<void> | void;
|
|
28
|
-
}
|
|
21
|
+
declare module '@expressive/mvc' {
|
|
29
22
|
namespace State {
|
|
30
23
|
type ForceRefresh = {
|
|
31
24
|
/** Request a refresh for current component. */(): void;
|
|
@@ -49,6 +42,31 @@ declare module '@expressive/state' {
|
|
|
49
42
|
};
|
|
50
43
|
type GetFactory<T extends State, R> = (this: T, current: T, refresh: ForceRefresh) => R;
|
|
51
44
|
type GetEffect<T extends State> = (this: T, current: T, refresh: ForceRefresh) => null;
|
|
45
|
+
/** Fetch instance of this class from context. */
|
|
46
|
+
function get<T extends State>(this: State.Extends<T>): T;
|
|
47
|
+
/** Fetch instance of this class optionally. */
|
|
48
|
+
function get<T extends State>(this: State.Extends<T>, required: false): T | undefined;
|
|
49
|
+
/** Fetch instance of this class from context. */
|
|
50
|
+
function get<T extends State>(this: State.Extends<T>, requireValues: true): Required<T>;
|
|
51
|
+
function get<T extends State, R>(this: State.Extends<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;
|
|
52
|
+
function get<T extends State>(this: State.Extends<T>, factory: GetEffect<T>): null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/state.use.d.ts
|
|
57
|
+
declare module '@expressive/mvc' {
|
|
58
|
+
interface UseState extends State {
|
|
59
|
+
/**
|
|
60
|
+
* Optional hook called when State.use() is invoked within a React component.
|
|
61
|
+
*
|
|
62
|
+
* This is called *every render* of the component, and will intercept
|
|
63
|
+
* arguments which would otherwise be passed into the State constructor.
|
|
64
|
+
*
|
|
65
|
+
* @param props Arguments passed to State.use().
|
|
66
|
+
*/
|
|
67
|
+
use?(...props: any[]): Promise<void> | void;
|
|
68
|
+
}
|
|
69
|
+
namespace State {
|
|
52
70
|
type UseArgs<T extends State> = T extends {
|
|
53
71
|
use(...props: infer P): any;
|
|
54
72
|
} ? P : State.Args<T>;
|
|
@@ -59,16 +77,38 @@ declare module '@expressive/state' {
|
|
|
59
77
|
* @returns Managed instance of this State.
|
|
60
78
|
*/
|
|
61
79
|
function use<T extends UseState>(this: State.Type<T>, ...args: UseArgs<T>): T;
|
|
62
|
-
/** Fetch instance of this class from context. */
|
|
63
|
-
function get<T extends State>(this: State.Extends<T>): T;
|
|
64
|
-
/** Fetch instance of this class optionally. */
|
|
65
|
-
function get<T extends State>(this: State.Extends<T>, required: false): T | undefined;
|
|
66
|
-
/** Fetch instance of this class from context. */
|
|
67
|
-
function get<T extends State>(this: State.Extends<T>, requireValues: true): Required<T>;
|
|
68
|
-
function get<T extends State, R>(this: State.Extends<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;
|
|
69
|
-
function get<T extends State>(this: State.Extends<T>, factory: GetEffect<T>): null;
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
//#endregion
|
|
73
|
-
|
|
74
|
-
|
|
83
|
+
//#region src/runtime.d.ts
|
|
84
|
+
declare const Runtime: {
|
|
85
|
+
/** Host own-property keys to trap out of observed state; assigned by each adapter. */ignore: string[];
|
|
86
|
+
createElement(type: any, props?: any, ...children: any[]): any;
|
|
87
|
+
createContext<T>(value: T): any;
|
|
88
|
+
useContext(context: any): any;
|
|
89
|
+
useState<S>(initial: S | (() => S)): [S, (next: (previous: S) => S) => void];
|
|
90
|
+
useEffect(effect: () => (() => void) | void, deps?: any[]): void;
|
|
91
|
+
useRef<T>(initial: T): {
|
|
92
|
+
current: T;
|
|
93
|
+
}; /** Per-render-attempt lifecycle, set by each adapter (React stacks attempts; others no-op). */
|
|
94
|
+
dedupe(from: Component, context: Context): {
|
|
95
|
+
commit(): void;
|
|
96
|
+
remove(): void;
|
|
97
|
+
}; /** Host error-boundary component, wrapping a Component whose `catch` is set. */
|
|
98
|
+
ErrorBoundary: unknown;
|
|
99
|
+
Suspense: any;
|
|
100
|
+
};
|
|
101
|
+
declare function useFactory<T extends Function>(factory: () => T): T;
|
|
102
|
+
declare function useReady<T>(callback: () => void): void;
|
|
103
|
+
/**
|
|
104
|
+
* Mount-effect with a refreshable return value, safe under React StrictMode.
|
|
105
|
+
*
|
|
106
|
+
* @param callback Setup handler; receives a setter, must return a cleanup.
|
|
107
|
+
* @returns Latest value published via the setter (`undefined` until set).
|
|
108
|
+
*/
|
|
109
|
+
declare function useHook<T = void>(callback: (refresh: (next: T) => void) => () => void): T;
|
|
110
|
+
/** Subscribe to an existing observable instance within a component. */
|
|
111
|
+
declare function use<T extends object>(subject: T): T;
|
|
112
|
+
//#endregion
|
|
113
|
+
export { Consumer, Context, Provider, Runtime, State$1 as State, provide, use, useFactory, useHook, useReady };
|
|
114
|
+
//# sourceMappingURL=runtime.d.ts.map
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { i as provide, n as Context, r as Provider, t as Consumer } from "./context-BfeYQmAX.js";
|
|
2
|
+
import { State, event, observer, watch } from "@expressive/mvc";
|
|
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
|
+
export { Consumer, Context, Provider, Runtime, State, provide, use, useFactory, useHook, useReady };
|
|
74
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","names":[],"sources":["../src/runtime.ts"],"sourcesContent":["import { event, watch, observer } from '@expressive/mvc';\nimport type { Component } from '@expressive/mvc';\nimport type { Context } from './context';\nimport './component';\nimport './state.get';\nimport './state.use';\n\nexport { State } from '@expressive/mvc';\nexport { Consumer, Provider, Context, provide } 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"],"mappings":";;;;AAUA,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,WAAW,EAAG,SAAQ,SAAS;;IAE9C,EAAE,CAAC;AAEN,QAAO,QAAQ;;;AAIjB,SAAgB,IAAsB,SAAY;CAChD,MAAM,EAAE,YAAY,QAAQ,OAKzB;EAAE,SAAS;EAAG,OAAO;EAAS,CAAC;CAElC,MAAM,SAAS,QAAQ,eAAe,QAAQ,UAAU,CAAC;AAEzD,KAAI,QAAQ,WAAW,SAAS;EAC9B,MAAM,SAAS,SAAS,QAAQ;AAEhC,MAAI,WAAW,OACb,OAAM,IAAI,MAAM,qCAAqC;AAEvD,UAAQ,WAAW;AACnB,UAAQ,SAAS;AAEjB,MAAI,WAAW,MAAM;AACnB,WAAQ,UAAU;AAClB,WAAQ,QAAQ;SACX;AACL,OAAI,CAAC,OAAO,MAAO,OAAM,QAAQ;GAEjC,IAAI,OAAO;AAEX,WAAQ,UAAU,MAAM,UAAU,MAAM,YAAY;AAClD,YAAQ,QAAQ;AAChB,QAAI,QAAQ,UAAU,CAAC,KACrB,SAAQ,MAAM,IAAI,EAAE;KACtB;AAEF,UAAO;;;AAIX,SAAQ,sBAAsB;AAC5B,MAAI,EAAE,QAAQ,UAAU,EAAG,SAAQ,WAAW;IAC7C,EAAE,CAAC;AAEN,QAAO,QAAQ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@expressive/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.78.0",
|
|
4
4
|
"description": "Use classes to define state in React!",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"expressive",
|
|
@@ -15,12 +15,20 @@
|
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
|
-
"url": "git+https://github.com/gabeklein/expressive-
|
|
18
|
+
"url": "git+https://github.com/gabeklein/expressive-mvc.git"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"type": "module",
|
|
24
|
+
"sideEffects": [
|
|
25
|
+
"./dist/index.js",
|
|
26
|
+
"./dist/runtime.js",
|
|
27
|
+
"./dist/component.js",
|
|
28
|
+
"./dist/state.get.js",
|
|
29
|
+
"./dist/state.use.js",
|
|
30
|
+
"./src/jsx-runtime.ts"
|
|
31
|
+
],
|
|
24
32
|
"main": "dist/index.js",
|
|
25
33
|
"types": "dist/index.d.ts",
|
|
26
34
|
"exports": {
|
|
@@ -28,9 +36,9 @@
|
|
|
28
36
|
"types": "./dist/index.d.ts",
|
|
29
37
|
"default": "./dist/index.js"
|
|
30
38
|
},
|
|
31
|
-
"./
|
|
32
|
-
"types": "./dist/
|
|
33
|
-
"default": "./dist/
|
|
39
|
+
"./runtime": {
|
|
40
|
+
"types": "./dist/runtime.d.ts",
|
|
41
|
+
"default": "./dist/runtime.js"
|
|
34
42
|
}
|
|
35
43
|
},
|
|
36
44
|
"engines": {
|
|
@@ -41,21 +49,18 @@
|
|
|
41
49
|
},
|
|
42
50
|
"scripts": {
|
|
43
51
|
"build": "tsdown",
|
|
44
|
-
"test": "tsc --noEmit &&
|
|
45
|
-
"preversion": "npm run test && npm run build"
|
|
52
|
+
"test": "tsc --noEmit && bun test --coverage --only-failures"
|
|
46
53
|
},
|
|
47
54
|
"dependencies": {
|
|
48
|
-
"@expressive/
|
|
55
|
+
"@expressive/mvc": "^0.78.0"
|
|
49
56
|
},
|
|
50
57
|
"devDependencies": {
|
|
51
58
|
"@testing-library/react": "^16.3.0",
|
|
52
59
|
"@types/react": "^19",
|
|
53
60
|
"@types/react-dom": "^19",
|
|
54
|
-
"@vitest/browser": "^4.0.18",
|
|
55
61
|
"react": "^19",
|
|
56
62
|
"react-dom": "^19",
|
|
57
|
-
"tsdown": "^0.20.1"
|
|
58
|
-
"vitest": "^4.0.18"
|
|
63
|
+
"tsdown": "^0.20.1"
|
|
59
64
|
},
|
|
60
65
|
"peerDependencies": {
|
|
61
66
|
"react": ">=16.8.0 <20.0.0"
|
|
@@ -64,6 +69,5 @@
|
|
|
64
69
|
"react": {
|
|
65
70
|
"optional": true
|
|
66
71
|
}
|
|
67
|
-
}
|
|
68
|
-
"gitHead": "785b18c626b96cd2a51fb33386b3bdeded0afceb"
|
|
72
|
+
}
|
|
69
73
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Gabe Klein
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|