@expressive/react 0.75.3 → 0.77.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/LICENSE +21 -0
- package/dist/index.d.ts +13 -12
- package/dist/index.js +68 -150
- package/dist/index.js.map +1 -1
- package/dist/state.d.ts +6 -7
- package/dist/state.js +39 -76
- package/dist/state.js.map +1 -1
- package/dist/use-AIA25P7A.js +63 -0
- package/dist/use-AIA25P7A.js.map +1 -0
- package/dist/use-_Es6cU25.d.ts +5 -0
- package/package.json +8 -8
- package/dist/esm/component.js +0 -195
- package/dist/esm/component.js.map +0 -1
- package/dist/esm/context.js +0 -42
- package/dist/esm/context.js.map +0 -1
- package/dist/esm/index.js +0 -15
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/state.js +0 -138
- package/dist/esm/state.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as use } from "./use-_Es6cU25.js";
|
|
2
2
|
import * as react from "react";
|
|
3
3
|
import React, { ReactNode } from "react";
|
|
4
|
-
import {
|
|
4
|
+
import { State, State as State$1 } from "./state";
|
|
5
|
+
import { Context, Context as Context$2, Observable, State as State$2, def, get, hot, ref, set } from "@expressive/state";
|
|
5
6
|
|
|
6
7
|
//#region src/context.d.ts
|
|
7
8
|
declare namespace Consumer {
|
|
@@ -38,27 +39,27 @@ declare namespace Provider {
|
|
|
38
39
|
is: (instance: T) => void;
|
|
39
40
|
} & { [K in State$2.Field<T>]?: T[K] };
|
|
40
41
|
type ForMultipleProps<T extends State$2> = SharedProps & {
|
|
41
|
-
for: Context$
|
|
42
|
+
for: Context$2.Accept<T>;
|
|
42
43
|
is?: ForEach<T>;
|
|
43
44
|
};
|
|
44
45
|
type Props<T extends State$2 = State$2> = ForSingleProps<T> | ForMultipleProps<T>;
|
|
45
46
|
}
|
|
46
|
-
declare function Provider<T extends State$2>(props: Provider.Props<T>): react.FunctionComponentElement<react.ProviderProps<Context$
|
|
47
|
+
declare function Provider<T extends State$2>(props: Provider.Props<T>): react.FunctionComponentElement<react.ProviderProps<Context$2>>;
|
|
47
48
|
//#endregion
|
|
48
49
|
//#region src/component.d.ts
|
|
49
50
|
type StateProps<T extends State$1> = { [K in Exclude<keyof T, keyof Component>]?: T[K] };
|
|
50
51
|
type RenderProps<T> = T extends {
|
|
51
52
|
render(props: infer P): any;
|
|
52
53
|
} ? {} extends P ? {
|
|
53
|
-
children?: ReactNode;
|
|
54
|
+
children?: React.ReactNode;
|
|
54
55
|
} : P : {
|
|
55
|
-
children?: ReactNode;
|
|
56
|
+
children?: React.ReactNode;
|
|
56
57
|
};
|
|
57
58
|
interface ComponentProps<T extends Component> {
|
|
58
59
|
/** Callback for newly created instance. Only called once. */
|
|
59
60
|
is?: (instance: T) => void;
|
|
60
61
|
/** Fallback to show when suspended or in error recovery. */
|
|
61
|
-
fallback?: ReactNode;
|
|
62
|
+
fallback?: React.ReactNode;
|
|
62
63
|
}
|
|
63
64
|
type Props<T extends Component> = StateProps<T> & ComponentProps<T> & RenderProps<T>;
|
|
64
65
|
declare module '@expressive/state' {
|
|
@@ -67,7 +68,7 @@ declare module '@expressive/state' {
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
declare class Component extends State$1 {
|
|
70
|
-
static contextType: React.Context<Context$
|
|
71
|
+
static contextType: React.Context<Context$2>;
|
|
71
72
|
/**
|
|
72
73
|
* All JSX attributes passed to this component.
|
|
73
74
|
* Includes state-derived props, render props, and built-in props like `is` and `fallback`.
|
|
@@ -76,7 +77,7 @@ declare class Component extends State$1 {
|
|
|
76
77
|
*/
|
|
77
78
|
readonly props: Props<this>;
|
|
78
79
|
/** @deprecated Only to satisfy React JSX. Use `this.get(State)` instead. */
|
|
79
|
-
readonly context: Context$
|
|
80
|
+
readonly context: Context$2;
|
|
80
81
|
/** @deprecated Only to satisfy React JSX. Use `this.get()` instead. */
|
|
81
82
|
readonly state: State$1.Values<this>;
|
|
82
83
|
/** @deprecated Only to satisfy React JSX. Not implemented. */
|
|
@@ -90,7 +91,7 @@ declare class Component extends State$1 {
|
|
|
90
91
|
*
|
|
91
92
|
* Defaults to null - nothing will be rendered.
|
|
92
93
|
*/
|
|
93
|
-
fallback: ReactNode;
|
|
94
|
+
fallback: React.ReactNode;
|
|
94
95
|
constructor(nextProps: any, ...rest: any[]);
|
|
95
96
|
/**
|
|
96
97
|
* Output for this component. Override to define custom JSX.
|
|
@@ -118,7 +119,7 @@ declare class Component extends State$1 {
|
|
|
118
119
|
*
|
|
119
120
|
* Without a parameter, children are accepted by default and passed through provider.
|
|
120
121
|
*/
|
|
121
|
-
render(props?: {}): ReactNode;
|
|
122
|
+
render(props?: {}): React.ReactNode;
|
|
122
123
|
/**
|
|
123
124
|
* Called when a child component throws during render.
|
|
124
125
|
* While this is pending, `fallback` is displayed.
|
|
@@ -131,5 +132,5 @@ declare class Component extends State$1 {
|
|
|
131
132
|
catch?(error: Error): Promise<void> | void;
|
|
132
133
|
}
|
|
133
134
|
//#endregion
|
|
134
|
-
export { Component, Consumer, Context, Observable, Provider, State, State as default, def, get, ref, set };
|
|
135
|
+
export { Component, Consumer, Context, Observable, Provider, State, State as default, def, get, hot, ref, set, use };
|
|
135
136
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,68 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
-
key = keys[i];
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
-
__defProp(to, key, {
|
|
15
|
-
get: ((k) => from[k]).bind(null, key),
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
-
value: mod,
|
|
25
|
-
enumerable: true
|
|
26
|
-
}) : target, mod));
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
let react = require("react");
|
|
30
|
-
react = __toESM(react);
|
|
31
|
-
let src_state = require("./state");
|
|
32
|
-
let _expressive_state = require("@expressive/state");
|
|
1
|
+
import { i as useHook, n as Runtime, t as use } from "./use-AIA25P7A.js";
|
|
2
|
+
import React, { Suspense, createContext, createElement, useContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { State, State as State$1 } from "./state";
|
|
4
|
+
import { Context, Context as Context$2, Observable, State as State$2, def, get, hot, ref, set, set as set$1, unbind, watch } from "@expressive/state";
|
|
33
5
|
|
|
34
6
|
//#region src/context.ts
|
|
35
|
-
const Layers =
|
|
36
|
-
const _get =
|
|
37
|
-
|
|
38
|
-
if (state)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return _expressive_state.Context.root;
|
|
43
|
-
}
|
|
7
|
+
const Layers = createContext(Context$2.root);
|
|
8
|
+
const _get = Context$2.get;
|
|
9
|
+
Context$2.get = (state) => {
|
|
10
|
+
if (!state) try {
|
|
11
|
+
return useContext(Layers);
|
|
12
|
+
} catch {}
|
|
13
|
+
return _get(state);
|
|
44
14
|
};
|
|
45
15
|
function Consumer(props) {
|
|
46
16
|
return props.for.get((i) => props.children(i));
|
|
47
17
|
}
|
|
48
18
|
function Provider(props) {
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const context =
|
|
52
|
-
|
|
53
|
-
|
|
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);
|
|
54
26
|
if (Object.keys(rest).length) {
|
|
55
|
-
|
|
56
|
-
if (
|
|
27
|
+
const i = State$2.is(input) ? context.get(input) : input;
|
|
28
|
+
if (i instanceof State$2) i.set(rest);
|
|
57
29
|
}
|
|
58
|
-
(
|
|
59
|
-
if (fallback !== void 0) children = (0, react.createElement)(react.Suspense, {
|
|
60
|
-
fallback,
|
|
61
|
-
name
|
|
62
|
-
}, children);
|
|
63
|
-
return (0, react.createElement)(Layers.Provider, {
|
|
30
|
+
return createElement(Layers.Provider, {
|
|
64
31
|
value: context,
|
|
65
|
-
children
|
|
32
|
+
children: fallback !== void 0 ? createElement(Suspense, {
|
|
33
|
+
fallback,
|
|
34
|
+
name
|
|
35
|
+
}, children) : children
|
|
66
36
|
});
|
|
67
37
|
}
|
|
68
38
|
|
|
@@ -75,17 +45,17 @@ const INTERNAL = [
|
|
|
75
45
|
"_reactInternals",
|
|
76
46
|
"_reactInternalInstance"
|
|
77
47
|
];
|
|
78
|
-
var Component = class extends
|
|
48
|
+
var Component = class extends State$1 {
|
|
79
49
|
static {
|
|
80
50
|
this.contextType = Layers;
|
|
81
51
|
}
|
|
82
52
|
constructor(nextProps, ...rest) {
|
|
83
53
|
const { is, ...props } = nextProps;
|
|
84
|
-
rest = rest.filter((x) => !(x instanceof
|
|
54
|
+
rest = rest.filter((x) => !(x instanceof Context$2));
|
|
85
55
|
super(props, rest, is && ((x) => void is(x)), () => {
|
|
86
56
|
Object.defineProperty(this, "props", { enumerable: false });
|
|
87
57
|
});
|
|
88
|
-
this.fallback =
|
|
58
|
+
this.fallback = set$1(null);
|
|
89
59
|
const existing = PENDING.get(nextProps);
|
|
90
60
|
if (existing) return existing;
|
|
91
61
|
PENDING.set(nextProps, this);
|
|
@@ -143,35 +113,31 @@ Object.defineProperties(Component.prototype, {
|
|
|
143
113
|
});
|
|
144
114
|
function bootstrap(context) {
|
|
145
115
|
const self = this.is;
|
|
146
|
-
const render =
|
|
116
|
+
const render = unbind(this.render);
|
|
147
117
|
context = context.push(self);
|
|
148
|
-
let ready = false;
|
|
149
118
|
let active;
|
|
150
119
|
function Render() {
|
|
151
120
|
return render.call(active, self.props);
|
|
152
121
|
}
|
|
153
122
|
function AsComponent() {
|
|
154
|
-
(
|
|
155
|
-
|
|
123
|
+
useHook((refresh) => {
|
|
124
|
+
watch(self, (current) => {
|
|
156
125
|
active = current;
|
|
157
|
-
|
|
126
|
+
refresh();
|
|
158
127
|
});
|
|
159
128
|
return () => {
|
|
160
129
|
self.set(null);
|
|
161
130
|
context.pop();
|
|
162
131
|
};
|
|
163
132
|
});
|
|
164
|
-
(
|
|
165
|
-
ready = true;
|
|
166
|
-
}, []);
|
|
167
|
-
const children = (0, react.createElement)(Layers.Provider, {
|
|
133
|
+
const children = createElement(Layers.Provider, {
|
|
168
134
|
value: context,
|
|
169
|
-
children:
|
|
135
|
+
children: createElement(Suspense, {
|
|
170
136
|
fallback: active.fallback,
|
|
171
137
|
name: String(self)
|
|
172
|
-
},
|
|
138
|
+
}, createElement(Render))
|
|
173
139
|
});
|
|
174
|
-
return active.catch ?
|
|
140
|
+
return active.catch ? createElement(ErrorBoundary, {
|
|
175
141
|
self,
|
|
176
142
|
children
|
|
177
143
|
}) : children;
|
|
@@ -181,10 +147,10 @@ function bootstrap(context) {
|
|
|
181
147
|
get: () => context,
|
|
182
148
|
set() {}
|
|
183
149
|
},
|
|
184
|
-
render: { value: () =>
|
|
150
|
+
render: { value: () => createElement(AsComponent) }
|
|
185
151
|
});
|
|
186
152
|
}
|
|
187
|
-
var ErrorBoundary = class extends
|
|
153
|
+
var ErrorBoundary = class extends React.Component {
|
|
188
154
|
constructor(..._args) {
|
|
189
155
|
super(..._args);
|
|
190
156
|
this.state = {};
|
|
@@ -212,96 +178,48 @@ var ErrorBoundary = class extends react.default.Component {
|
|
|
212
178
|
}
|
|
213
179
|
};
|
|
214
180
|
const SEEN = /* @__PURE__ */ new WeakSet();
|
|
215
|
-
function subcomponents(proto) {
|
|
216
|
-
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
217
|
-
if (!/^[A-Z]/.test(key)) continue;
|
|
218
|
-
const { get, value } = Object.getOwnPropertyDescriptor(proto, key);
|
|
219
|
-
if (!get && typeof value !== "function") continue;
|
|
220
|
-
Object.defineProperty(proto, key, {
|
|
221
|
-
configurable: true,
|
|
222
|
-
get() {
|
|
223
|
-
const self = this.is;
|
|
224
|
-
let render = (0, _expressive_state.unbind)(get ? get.call(this) : value);
|
|
225
|
-
function Sub(props) {
|
|
226
|
-
const ref = (0, react.useRef)(null);
|
|
227
|
-
(0, src_state.useMount)((refresh) => (0, _expressive_state.watch)(self, (current) => {
|
|
228
|
-
ref.current = current;
|
|
229
|
-
refresh();
|
|
230
|
-
}));
|
|
231
|
-
return render.call(ref.current, props);
|
|
232
|
-
}
|
|
233
|
-
Object.defineProperty(self, key, {
|
|
234
|
-
configurable: true,
|
|
235
|
-
get: () => Sub,
|
|
236
|
-
set(fn) {
|
|
237
|
-
render = fn;
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
return Sub;
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
181
|
Component.on((self) => {
|
|
246
182
|
let Type = self.constructor;
|
|
247
183
|
while (Type !== Component) {
|
|
248
|
-
const proto = Type.prototype;
|
|
249
184
|
if (SEEN.has(Type)) break;
|
|
250
185
|
SEEN.add(Type);
|
|
251
|
-
subcomponents(
|
|
186
|
+
subcomponents(Type.prototype);
|
|
252
187
|
Type = Object.getPrototypeOf(Type);
|
|
253
188
|
}
|
|
254
189
|
subcomponents(self);
|
|
255
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
|
+
}
|
|
256
215
|
|
|
257
216
|
//#endregion
|
|
258
217
|
//#region src/index.ts
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
218
|
+
Runtime.createElement = createElement;
|
|
219
|
+
Runtime.useEffect = useEffect;
|
|
220
|
+
Runtime.useState = useState;
|
|
221
|
+
Runtime.useRef = useRef;
|
|
263
222
|
|
|
264
223
|
//#endregion
|
|
265
|
-
|
|
266
|
-
exports.Consumer = Consumer;
|
|
267
|
-
Object.defineProperty(exports, 'Context', {
|
|
268
|
-
enumerable: true,
|
|
269
|
-
get: function () {
|
|
270
|
-
return _expressive_state.Context;
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
Object.defineProperty(exports, 'Observable', {
|
|
274
|
-
enumerable: true,
|
|
275
|
-
get: function () {
|
|
276
|
-
return _expressive_state.Observable;
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
exports.Provider = Provider;
|
|
280
|
-
exports.State = src_state.State;
|
|
281
|
-
exports.default = src_state.State;
|
|
282
|
-
exports.__toESM = __toESM;
|
|
283
|
-
Object.defineProperty(exports, 'def', {
|
|
284
|
-
enumerable: true,
|
|
285
|
-
get: function () {
|
|
286
|
-
return _expressive_state.def;
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
Object.defineProperty(exports, 'get', {
|
|
290
|
-
enumerable: true,
|
|
291
|
-
get: function () {
|
|
292
|
-
return _expressive_state.get;
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
Object.defineProperty(exports, 'ref', {
|
|
296
|
-
enumerable: true,
|
|
297
|
-
get: function () {
|
|
298
|
-
return _expressive_state.ref;
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
Object.defineProperty(exports, 'set', {
|
|
302
|
-
enumerable: true,
|
|
303
|
-
get: function () {
|
|
304
|
-
return _expressive_state.set;
|
|
305
|
-
}
|
|
306
|
-
});
|
|
224
|
+
export { Component, Consumer, Context, Observable, Provider, State, State as default, def, get, hot, ref, set, use };
|
|
307
225
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Context","State","Suspense","State","Context","Suspense","React","useEffect","useState","createElement","useRef"],"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 useRef\n} from 'react';\nimport { useMount } from './state';\n\nconst Layers = createContext(new Context());\n\nconst _get = Context.get;\n\nContext.get = (state?: State) => {\n if (state) return _get(state);\n try {\n return useContext(Layers);\n } catch {\n return Context.root;\n }\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 ambient = useContext(Layers);\n const ref = useRef<Context | null>(null);\n const context = ref.current || (ref.current = new Context(ambient));\n\n let {\n for: input,\n children,\n fallback,\n name,\n ...rest\n } = props as Provider.ForSingleProps<T> & Provider.ForMultipleProps<T>;\n\n context.set(input, (added) => rest.is?.(added));\n\n if (Object.keys(rest).length) {\n if (State.is(input)) input = context.get(input) as T;\n if (input instanceof State) input.set(rest as State.Assign<T>);\n }\n\n useMount(() => () => context.pop());\n\n if (fallback !== undefined)\n children = createElement(Suspense, { fallback, name }, children);\n\n return createElement(Layers.Provider, { value: context, children });\n}\n\nexport { Consumer, Provider, Context, Layers };\n","import { watch, unbind, set } from '@expressive/state';\nimport React, {\n createElement,\n Suspense,\n useEffect,\n useRef,\n type ReactNode\n} from 'react';\nimport { Context, Layers } from './context';\nimport { State, useMount } 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?: ReactNode }\n : P\n : { children?: 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?: 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\nclass 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: 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?: {}): 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 ready = false;\n let active: Component;\n\n function Render() {\n return render.call(active, self.props);\n }\n\n function AsComponent() {\n useMount((refresh) => {\n watch(self, (current) => {\n active = current;\n if (ready) refresh();\n });\n\n return () => {\n self.set(null);\n context.pop();\n };\n });\n\n useEffect(() => {\n ready = true;\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: 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\nfunction subcomponents(proto: State) {\n for (const key of Object.getOwnPropertyNames(proto)) {\n if (!/^[A-Z]/.test(key)) continue;\n\n const { get, value } = Object.getOwnPropertyDescriptor(proto, key)!;\n\n if (!get && typeof value !== 'function') continue;\n\n Object.defineProperty(proto, key, {\n configurable: true,\n get(this: Component) {\n const self = this.is;\n\n let render = unbind(get ? get.call(this) : value);\n\n function Sub(props: any) {\n const ref = useRef<State>(null);\n\n useMount((refresh) =>\n watch(self, (current) => {\n ref.current = current;\n refresh();\n })\n );\n\n return render.call(ref.current, props);\n }\n\n Object.defineProperty(self, key, {\n configurable: true,\n get: () => Sub,\n set(fn: Function) {\n render = fn;\n }\n });\n\n return Sub;\n }\n });\n }\n}\n\nComponent.on((self) => {\n let Type = self.constructor as State.Extends;\n\n while (Type !== Component) {\n const proto = Type.prototype as any;\n\n if (SEEN.has(Type)) break;\n SEEN.add(Type);\n subcomponents(proto);\n Type = Object.getPrototypeOf(Type);\n }\n\n subcomponents(self);\n});\n\nexport { Component };\n","import { createElement, useEffect, useRef, useState } from 'react';\n\nimport { Pragma, State } from './state';\nimport { Consumer, Provider } from './context';\n\nPragma.useEffect = useEffect;\nPragma.useState = useState;\nPragma.createElement = createElement;\nPragma.useRef = useRef;\n\nexport { State, State as default };\nexport { Context, Observable, def, get, ref, set } from '@expressive/state';\nexport { Component } from './component';\nexport { Consumer, Provider };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,kCAAuB,IAAIA,2BAAS,CAAC;AAE3C,MAAM,OAAOA,0BAAQ;AAErB,0BAAQ,OAAO,UAAkB;AAC/B,KAAI,MAAO,QAAO,KAAK,MAAM;AAC7B,KAAI;AACF,+BAAkB,OAAO;SACnB;AACN,SAAOA,0BAAQ;;;AAoBnB,SAAS,SAA0B,OAA0B;AAC3D,QAAO,MAAM,IAAI,KAAK,MAAM,MAAM,SAAS,EAAE,CAAC;;AAmChD,SAAS,SAA0B,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,eAAc,KAAK;EACnB;;;;AC1SF,iBAAO,YAAYC;AACnB,iBAAO,WAAWC;AAClB,iBAAO,gBAAgBC;AACvB,iBAAO,SAASC"}
|
|
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,CAAC,MACH,KAAI;AACF,SAAO,WAAW,OAAO;SACnB;AAEV,QAAO,KAAK,MAAM;;AAmBpB,SAAS,SAA0B,OAA0B;AAC3D,QAAO,MAAM,IAAI,KAAK,MAAM,MAAM,SAAS,EAAE,CAAC;;AAmChD,SAAS,SAA0B,OAA0B;CAC3D,MAAM,EACJ,KAAK,OACL,UACA,UACA,MACA,IACA,GAAG,SACD;CAEJ,MAAM,UAAU,WAAW,OAAO;CAClC,MAAM,UAAU,SAAkB,QAAQ;AACxC,MAAI,IAAIA,UAAQ,QAAQ,CAAC;AACzB,eAAa,QAAQ,KAAK;GAC1B;AAEF,SAAQ,IAAI,OAAO,GAAG;AAEtB,KAAI,OAAO,KAAK,KAAK,CAAC,QAAQ;EAC5B,MAAM,IAAIC,QAAM,GAAG,MAAM,GAAG,QAAQ,IAAI,MAAM,GAAG;AACjD,MAAI,aAAaA,QAAO,GAAE,IAAI,KAAK;;AAGrC,QAAO,cAAc,OAAO,UAAU;EACpC,OAAO;EACP,UACE,aAAa,SACT,cAAc,UAAU;GAAE;GAAU;GAAM,EAAE,SAAS,GACrD;EACP,CAAC;;;;;AClG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MAAM,OAAO,OAAO,oBAAoB,KAAK,EAAE;AAClD,MAAI,CAAC,SAAS,KAAK,IAAI,CAAE;EAEzB,MAAM,EAAE,KAAK,UAAU,OAAO,yBAAyB,MAAM,IAAI;AAEjE,MAAI,CAAC,OAAO,OAAO,UAAU,WAAY;EAEzC,SAAS,eAA8B;GACrC,MAAM,OAAO,KAAK;GAElB,IAAI,SAAS,OAAO,MAAM,IAAI,KAAK,KAAK,GAAG,MAAM;GAEjD,MAAM,aAAa,UACjB,OAAO,KACL,SAAoB,QAAQ,MAAM,MAAM,IAAI,CAAC,EAC7C,MACD;AAEH,UAAO,eAAe,MAAM,KAAK;IAC/B,cAAc;IACd,WAAW;IACX,IAAI,IAAc;AAChB,cAAS;;IAEZ,CAAC;AAEF,UAAO;;AAGT,SAAO,eAAe,MAAM,KAAK;GAC/B,cAAc;GACd,KAAK;GACN,CAAC;;;;;;ACtRN,QAAQ,gBAAgB;AACxB,QAAQ,YAAY;AACpB,QAAQ,WAAW;AACnB,QAAQ,SAAS"}
|
package/dist/state.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { t as use } from "./use-_Es6cU25.js";
|
|
1
2
|
import { State } from "@expressive/state";
|
|
2
3
|
|
|
3
|
-
//#region src/
|
|
4
|
-
declare const
|
|
4
|
+
//#region src/runtime.d.ts
|
|
5
|
+
declare const Runtime: {
|
|
5
6
|
createElement(type: any, props?: any, ...children: any[]): any;
|
|
6
7
|
useState<S>(initial: S | (() => S)): [S, (next: (previous: S) => S) => void];
|
|
7
8
|
useEffect(effect: () => (() => void) | void, deps?: any[]): void;
|
|
@@ -9,6 +10,8 @@ declare const Pragma: {
|
|
|
9
10
|
current: T;
|
|
10
11
|
};
|
|
11
12
|
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/state.d.ts
|
|
12
15
|
/** Type may not be undefined - instead will be null. */
|
|
13
16
|
type NoVoid<T> = T extends undefined | void ? null : T;
|
|
14
17
|
declare module '@expressive/state' {
|
|
@@ -66,10 +69,6 @@ declare module '@expressive/state' {
|
|
|
66
69
|
function get<T extends State>(this: State.Extends<T>, factory: GetEffect<T>): null;
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
|
-
/**
|
|
70
|
-
* useEffect which tolerates react StrictMode double-invoking effects on mount.
|
|
71
|
-
*/
|
|
72
|
-
declare function useMount(callback: (refresh: () => void) => (() => void) | void): void;
|
|
73
72
|
//#endregion
|
|
74
|
-
export {
|
|
73
|
+
export { Runtime, State, use };
|
|
75
74
|
//# sourceMappingURL=state.d.ts.map
|