@expressive/react 0.72.0 → 0.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-kSYXY2_d.js +34 -0
- package/dist/context-DpbK9_2_.js +62 -0
- package/dist/context-DpbK9_2_.js.map +1 -0
- package/dist/esm/component.js +67 -0
- package/dist/esm/component.js.map +1 -0
- package/dist/esm/context.js +38 -0
- package/dist/esm/context.js.map +1 -0
- package/dist/esm/index.js +13 -34
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jsx-dev-runtime.js +7 -7
- package/dist/esm/jsx-dev-runtime.js.map +1 -1
- package/dist/esm/jsx-runtime.js +14 -21
- package/dist/esm/jsx-runtime.js.map +1 -1
- package/dist/esm/state.js +145 -0
- package/dist/esm/state.js.map +1 -0
- package/dist/index-zVvhW0uD.d.ts +130 -0
- package/dist/index.d.ts +2 -52
- package/dist/index.js +125 -89
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +9 -7
- package/dist/jsx-dev-runtime.js +15 -43
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +21 -27
- package/dist/jsx-runtime.js +24 -125
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/state.d.ts +88 -0
- package/dist/state.js +148 -0
- package/dist/state.js.map +1 -0
- package/package.json +10 -9
- package/dist/adapter.d.ts +0 -105
- package/dist/adapter.js +0 -207
- package/dist/adapter.js.map +0 -1
- package/dist/esm/adapter.js +0 -113
- package/dist/esm/adapter.js.map +0 -1
- package/dist/esm/chunk-4MR23TOU.js +0 -45
- package/dist/esm/chunk-4MR23TOU.js.map +0 -1
- package/dist/esm/chunk-UIHA4AT6.js +0 -58
- package/dist/esm/chunk-UIHA4AT6.js.map +0 -1
package/dist/adapter.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/adapter.ts
|
|
21
|
-
var adapter_exports = {};
|
|
22
|
-
__export(adapter_exports, {
|
|
23
|
-
Pragma: () => Pragma4
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(adapter_exports);
|
|
26
|
-
|
|
27
|
-
// src/state.as.ts
|
|
28
|
-
var import_mvc2 = require("@expressive/mvc");
|
|
29
|
-
|
|
30
|
-
// src/context.ts
|
|
31
|
-
var import_mvc = require("@expressive/mvc");
|
|
32
|
-
var import_react = require("react");
|
|
33
|
-
var Layers = (0, import_react.createContext)(new import_mvc.Context());
|
|
34
|
-
import_mvc.Context.use = (create) => {
|
|
35
|
-
const ambient = (0, import_react.useContext)(Layers);
|
|
36
|
-
return create ? (0, import_react.useMemo)(() => ambient.push(), [ambient]) : ambient;
|
|
37
|
-
};
|
|
38
|
-
function provide(context, children, fallback, name) {
|
|
39
|
-
if (fallback !== void 0)
|
|
40
|
-
children = (0, import_react.createElement)(import_react.Suspense, { fallback, name }, children);
|
|
41
|
-
return (0, import_react.createElement)(Layers.Provider, {
|
|
42
|
-
key: context.id,
|
|
43
|
-
value: context,
|
|
44
|
-
children
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/state.as.ts
|
|
49
|
-
var import_adapter = require("./adapter");
|
|
50
|
-
import_mvc2.State.as = function(render) {
|
|
51
|
-
const FC = Render.bind(this, { render });
|
|
52
|
-
return Object.assign(FC, {
|
|
53
|
-
displayName: this.name,
|
|
54
|
-
State: this
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
function Render(props, props2) {
|
|
58
|
-
const { is, ...rest } = { ...props, ...props2 };
|
|
59
|
-
const ambient = import_mvc2.Context.use();
|
|
60
|
-
const state = import_adapter.Pragma.useState(() => {
|
|
61
|
-
const instance = this.new(rest, is && ((x) => void is(x)));
|
|
62
|
-
const context = ambient.push(instance);
|
|
63
|
-
let ready;
|
|
64
|
-
let active;
|
|
65
|
-
(0, import_mvc2.watch)(instance, (current) => {
|
|
66
|
-
active = current;
|
|
67
|
-
if (ready) state[1]((x) => x.bind(null));
|
|
68
|
-
});
|
|
69
|
-
function didMount() {
|
|
70
|
-
ready = true;
|
|
71
|
-
return () => {
|
|
72
|
-
context.pop();
|
|
73
|
-
instance.set(null);
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
function Render2(props3) {
|
|
77
|
-
const render = import_mvc2.METHOD.get(active.render) || props3.render || active.render;
|
|
78
|
-
return render ? render.call(active, props3, active) : props3.children;
|
|
79
|
-
}
|
|
80
|
-
return (props3) => {
|
|
81
|
-
ready = false;
|
|
82
|
-
import_adapter.Pragma.useEffect(didMount, []);
|
|
83
|
-
Promise.resolve(instance.set(props3)).finally(() => {
|
|
84
|
-
ready = true;
|
|
85
|
-
});
|
|
86
|
-
return provide(
|
|
87
|
-
context,
|
|
88
|
-
import_adapter.Pragma.createElement(Render2, props3),
|
|
89
|
-
props3.fallback || active.fallback,
|
|
90
|
-
String(instance)
|
|
91
|
-
);
|
|
92
|
-
};
|
|
93
|
-
});
|
|
94
|
-
return state[0](rest);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// src/state.use.ts
|
|
98
|
-
var import_mvc3 = require("@expressive/mvc");
|
|
99
|
-
var import_adapter2 = require("./adapter");
|
|
100
|
-
import_mvc3.State.use = function(...args) {
|
|
101
|
-
const ambient = import_mvc3.Context.use();
|
|
102
|
-
const state = import_adapter2.Pragma.useState(() => {
|
|
103
|
-
let ready;
|
|
104
|
-
let active;
|
|
105
|
-
const instance = this.new(
|
|
106
|
-
(x) => x.use ? Promise.resolve(x.use(...args)) : args
|
|
107
|
-
);
|
|
108
|
-
const context = ambient.push(instance);
|
|
109
|
-
(0, import_mvc3.watch)(instance, (current) => {
|
|
110
|
-
active = current;
|
|
111
|
-
if (ready) state[1]((x) => x.bind(null));
|
|
112
|
-
});
|
|
113
|
-
function didMount() {
|
|
114
|
-
ready = true;
|
|
115
|
-
return () => {
|
|
116
|
-
context.pop();
|
|
117
|
-
instance.set(null);
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return (...args2) => {
|
|
121
|
-
import_adapter2.Pragma.useEffect(didMount, []);
|
|
122
|
-
if (ready) {
|
|
123
|
-
ready = false;
|
|
124
|
-
Promise.resolve(
|
|
125
|
-
instance.use ? instance.use(...args2) : Promise.all(
|
|
126
|
-
args2.flat().map(
|
|
127
|
-
(arg) => typeof arg == "object" && instance.set(arg)
|
|
128
|
-
)
|
|
129
|
-
)
|
|
130
|
-
).finally(() => {
|
|
131
|
-
ready = true;
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
return active;
|
|
135
|
-
};
|
|
136
|
-
});
|
|
137
|
-
return state[0](...args);
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
// src/state.get.ts
|
|
141
|
-
var import_mvc4 = require("@expressive/mvc");
|
|
142
|
-
var import_adapter3 = require("./adapter");
|
|
143
|
-
import_mvc4.State.get = function(argument) {
|
|
144
|
-
const context = import_mvc4.Context.use();
|
|
145
|
-
const state = import_adapter3.Pragma.useState(() => {
|
|
146
|
-
const instance = context.get(this);
|
|
147
|
-
if (!instance)
|
|
148
|
-
if (argument === false) return () => void 0;
|
|
149
|
-
else throw new Error(`Could not find ${this} in context.`);
|
|
150
|
-
let ready;
|
|
151
|
-
let value;
|
|
152
|
-
function render() {
|
|
153
|
-
state[1]((x) => x.bind(null));
|
|
154
|
-
}
|
|
155
|
-
function refresh(action) {
|
|
156
|
-
if (typeof action == "function") action = action();
|
|
157
|
-
render();
|
|
158
|
-
if (action) return action.finally(render);
|
|
159
|
-
}
|
|
160
|
-
function onMount() {
|
|
161
|
-
ready = true;
|
|
162
|
-
return unwatch;
|
|
163
|
-
}
|
|
164
|
-
const unwatch = (0, import_mvc4.watch)(
|
|
165
|
-
instance,
|
|
166
|
-
(current) => {
|
|
167
|
-
if (typeof argument === "function") {
|
|
168
|
-
const next = argument.call(current, current, refresh);
|
|
169
|
-
if (next === value) return;
|
|
170
|
-
value = next;
|
|
171
|
-
} else value = current;
|
|
172
|
-
if (ready) render();
|
|
173
|
-
},
|
|
174
|
-
argument === true
|
|
175
|
-
);
|
|
176
|
-
if (value instanceof Promise) {
|
|
177
|
-
let error;
|
|
178
|
-
unwatch();
|
|
179
|
-
value.then(
|
|
180
|
-
(x) => value = x,
|
|
181
|
-
(e) => error = e
|
|
182
|
-
).finally(render);
|
|
183
|
-
value = null;
|
|
184
|
-
return () => {
|
|
185
|
-
if (error) throw error;
|
|
186
|
-
return value === void 0 ? null : value;
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
if (value === null) {
|
|
190
|
-
unwatch();
|
|
191
|
-
return () => null;
|
|
192
|
-
}
|
|
193
|
-
return () => {
|
|
194
|
-
import_adapter3.Pragma.useEffect(onMount, []);
|
|
195
|
-
return value === void 0 ? null : value;
|
|
196
|
-
};
|
|
197
|
-
});
|
|
198
|
-
return state[0]();
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
// src/adapter.ts
|
|
202
|
-
var Pragma4 = {};
|
|
203
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
204
|
-
0 && (module.exports = {
|
|
205
|
-
Pragma
|
|
206
|
-
});
|
|
207
|
-
//# sourceMappingURL=adapter.js.map
|
package/dist/adapter.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/adapter.ts","../src/state.as.ts","../src/context.ts","../src/state.use.ts","../src/state.get.ts"],"sourcesContent":["namespace Pragma {}\n\nconst Pragma = {} as {\n useState<S>(initial: () => S): [S, (next: (previous: S) => S) => void];\n useEffect(effect: () => () => void, deps?: any[]): void;\n createElement(type: any, props?: any, ...children: any[]): any;\n};\n\nexport { Pragma };\n\nimport './state.as';\nimport './state.use';\nimport './state.get';\n","import { Context, watch, METHOD, State } from '@expressive/mvc';\nimport { FunctionComponent, ReactNode } from 'react';\n\nimport { provide } from './context';\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace State {\n type HasProps<T extends State> = {\n [K in Exclude<keyof T, keyof State>]?: T[K];\n };\n\n type ComponentProps<T extends State> = HasProps<T> & {\n /**\n * Callback for newly created instance. Only called once.\n * @returns Callback to run when instance is destroyed.\n */\n is?: (instance: T) => void;\n\n /**\n * A fallback react tree to show when suspended.\n * If not provided, `fallback` property of the State will be used.\n */\n fallback?: React.ReactNode;\n };\n\n type Props<T extends State> = T extends {\n render(props: infer P, self: any): any;\n }\n ? ComponentProps<T> & Omit<P, keyof AsComponent>\n : ComponentProps<T> & { children?: React.ReactNode };\n\n /**\n * Props which will not conflict with a State's use as a Component.\n *\n * Built-in properties must be optional, as they will always be omitted.\n */\n type RenderProps<T extends State> = HasProps<T> & {\n is?: never;\n get?: never;\n set?: never;\n };\n\n /** State which is not incompatable as Component in React. */\n interface AsComponent extends State.Valid {\n render?(props: RenderProps<this>, self: this): React.ReactNode;\n fallback?: React.ReactNode;\n }\n\n interface FC<\n T extends State,\n P extends State.Assign<T>\n > extends FunctionComponent<P & State.Props<T>> {\n displayName?: string;\n State: State.Extends<T>;\n }\n\n function as<T extends State, P extends State.Assign<T>>(\n this: State.New<T>,\n render: (props: P, self: T) => ReactNode\n ): FC<T, P>;\n }\n}\n\nState.as = function <T extends State.AsComponent, P extends State.Assign<T>>(\n this: State.Class<T>,\n render: (props: P, self: T) => ReactNode\n): State.FC<T, P> {\n const FC = Render.bind(this as State.Class, { render } as {});\n\n return Object.assign(FC, {\n displayName: this.name,\n State: this\n });\n};\n\nexport function Render<T extends State.AsComponent>(\n this: State.Class<T>,\n props: State.Props<T>,\n props2?: State.Props<T>\n) {\n const { is, ...rest } = { ...props, ...props2 };\n\n const ambient = Context.use();\n const state = Pragma.useState<(props: any) => any>(() => {\n const instance = this.new(rest as {}, is && ((x) => void is(x)));\n const context = ambient.push(instance);\n\n let ready: boolean | undefined;\n let active: T;\n\n watch(instance, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n function didMount() {\n ready = true;\n return () => {\n context.pop();\n instance.set(null);\n };\n }\n\n function Render(props: State.Props<T>) {\n const render = METHOD.get(active.render) || props.render || active.render;\n\n return render\n ? render.call(active, props as State.HasProps<T>, active)\n : props.children;\n }\n\n return (props: State.RenderProps<T>) => {\n ready = false;\n\n Pragma.useEffect(didMount, []);\n Promise.resolve(instance.set(props as {})).finally(() => {\n ready = true;\n });\n\n return provide(\n context,\n Pragma.createElement(Render, props as any),\n props.fallback || active.fallback,\n String(instance)\n );\n };\n });\n\n return state[0](rest);\n}\n","import State, { Context } from '@expressive/mvc';\nimport {\n createContext,\n createElement,\n ReactNode,\n Suspense,\n useContext,\n useEffect,\n useMemo\n} from 'react';\n\nexport const Layers = createContext(new Context());\n\ndeclare module '@expressive/mvc' {\n namespace Context {\n function use(create?: true): Context;\n function use(create: boolean): Context | null | undefined;\n }\n}\n\nContext.use = (create?: boolean) => {\n const ambient = useContext(Layers);\n\n return create ? useMemo(() => ambient.push(), [ambient]) : ambient;\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 interface Props<T extends State> {\n /** State or group of States to provide to descendant Consumers. */\n for: Context.Accept<T>;\n\n /**\n * Callback to run for each provided State.\n */\n forEach?: Context.Expect<T>;\n\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\nfunction Provider<T extends State>(props: Provider.Props<T>) {\n const context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.use(props.for, (state) => {\n if (props.forEach) {\n const cleanup = props.forEach(state);\n\n if (cleanup) state.set(cleanup, null);\n }\n });\n\n return provide(context, props.children, props.fallback, props.name);\n}\n\nfunction provide(\n context: Context,\n children: ReactNode,\n fallback?: ReactNode,\n name?: string | undefined\n): ReactNode {\n if (fallback !== undefined)\n children = createElement(Suspense, { fallback, name }, children);\n\n return createElement(Layers.Provider, {\n key: context.id,\n value: context,\n children\n });\n}\n\nexport { Consumer, Provider, provide };\n","import { State, Context, watch } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace State {\n interface Valid {\n use?(...props: any[]): Promise<void> | void;\n }\n\n type UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n }\n ? P\n : State.Args<T>;\n\n function use<T extends State>(this: New<T>, ...args: UseArgs<T>): T;\n }\n}\n\nState.use = function <T extends State.Valid>(\n this: State.New<T>,\n ...args: any[]\n) {\n const ambient = Context.use();\n const state = Pragma.useState(() => {\n let ready: boolean | undefined;\n let active: T;\n\n const instance = this.new((x) =>\n x.use ? Promise.resolve(x.use(...args)) : args\n );\n const context = ambient.push(instance);\n\n watch(instance, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n function didMount() {\n ready = true;\n return () => {\n context.pop();\n instance.set(null);\n };\n }\n\n return (...args: State.Args<T>) => {\n Pragma.useEffect(didMount, []);\n\n if (ready) {\n ready = false;\n\n Promise.resolve(\n instance.use\n ? instance.use(...args)\n : Promise.all(\n args\n .flat()\n .map(\n (arg) =>\n typeof arg == 'object' &&\n instance.set(arg as State.Assign<T>)\n )\n )\n ).finally(() => {\n ready = true;\n });\n }\n\n return active;\n };\n });\n\n return state[0](...args);\n};\n","import { Context, watch, State } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype 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\ndeclare module '@expressive/mvc' {\n namespace State {\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: Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n function get<T extends State>(\n this: 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: Extends<T>,\n requireValues: true\n ): Required<T>;\n\n function get<T extends State, R>(\n this: Extends<T>,\n factory: GetFactory<T, Promise<R> | R>\n ): NoVoid<R>;\n\n // TODO: eagerly match this so any nulls are caught - would prevent updates.\n function get<T extends State>(\n this: Extends<T>,\n factory: GetEffect<T>\n ): null;\n }\n}\n\nState.get = function <T extends State, R>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const context = Context.use();\n const state = Pragma.useState(() => {\n const instance = context.get(this);\n\n if (!instance)\n if (argument === false) return () => undefined;\n else throw new Error(`Could not find ${this} in context.`);\n\n let ready: boolean | undefined;\n let value: any;\n\n function render() {\n state[1]((x) => x.bind(null));\n }\n\n function refresh<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if (typeof action == 'function') action = action();\n\n render();\n\n if (action) return action.finally(render);\n }\n\n function onMount() {\n ready = true;\n return unwatch;\n }\n\n const unwatch = watch(\n instance,\n (current) => {\n if (typeof argument === 'function') {\n const next = argument.call(current, current, refresh);\n\n if (next === value) return;\n\n value = next;\n } else value = current;\n\n if (ready) render();\n },\n argument === true\n );\n\n if (value instanceof Promise) {\n let error: Error | undefined;\n\n unwatch();\n\n value\n .then(\n (x) => (value = x),\n (e) => (error = e)\n )\n .finally(render);\n\n value = null;\n\n return () => {\n if (error) throw error;\n\n return value === undefined ? null : value;\n };\n }\n\n if (value === null) {\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useEffect(onMount, []);\n return value === undefined ? null : value;\n };\n });\n\n return state[0]() as R;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;;;ACAA,IAAAC,cAA8C;;;ACA9C,iBAA+B;AAC/B,mBAQO;AAEA,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AASjD,mBAAQ,MAAM,CAAC,WAAqB;AAClC,QAAM,cAAU,yBAAW,MAAM;AAEjC,SAAO,aAAS,sBAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,OAAO,CAAC,I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AU,MAAM;AAClB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,6BAAO,UAAU,SAAS,CAAC,CAAC;AAC5B,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,MAAM,CAAC,EAAE;AAClB;;;AJxJA,IAAMC,UAAS,CAAC;","names":["Pragma","import_mvc","Render","props","import_mvc","import_adapter","args","import_mvc","import_adapter","Pragma"]}
|
package/dist/esm/adapter.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import "./chunk-UIHA4AT6.js";
|
|
2
|
-
import "./chunk-4MR23TOU.js";
|
|
3
|
-
|
|
4
|
-
// src/state.use.ts
|
|
5
|
-
import { State, Context, watch } from "@expressive/mvc";
|
|
6
|
-
import { Pragma } from "./adapter";
|
|
7
|
-
State.use = function(...args) {
|
|
8
|
-
const ambient = Context.use();
|
|
9
|
-
const state = Pragma.useState(() => {
|
|
10
|
-
let ready;
|
|
11
|
-
let active;
|
|
12
|
-
const instance = this.new(
|
|
13
|
-
(x) => x.use ? Promise.resolve(x.use(...args)) : args
|
|
14
|
-
);
|
|
15
|
-
const context = ambient.push(instance);
|
|
16
|
-
watch(instance, (current) => {
|
|
17
|
-
active = current;
|
|
18
|
-
if (ready) state[1]((x) => x.bind(null));
|
|
19
|
-
});
|
|
20
|
-
function didMount() {
|
|
21
|
-
ready = true;
|
|
22
|
-
return () => {
|
|
23
|
-
context.pop();
|
|
24
|
-
instance.set(null);
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
return (...args2) => {
|
|
28
|
-
Pragma.useEffect(didMount, []);
|
|
29
|
-
if (ready) {
|
|
30
|
-
ready = false;
|
|
31
|
-
Promise.resolve(
|
|
32
|
-
instance.use ? instance.use(...args2) : Promise.all(
|
|
33
|
-
args2.flat().map(
|
|
34
|
-
(arg) => typeof arg == "object" && instance.set(arg)
|
|
35
|
-
)
|
|
36
|
-
)
|
|
37
|
-
).finally(() => {
|
|
38
|
-
ready = true;
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
return active;
|
|
42
|
-
};
|
|
43
|
-
});
|
|
44
|
-
return state[0](...args);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// src/state.get.ts
|
|
48
|
-
import { Context as Context2, watch as watch2, State as State2 } from "@expressive/mvc";
|
|
49
|
-
import { Pragma as Pragma2 } from "./adapter";
|
|
50
|
-
State2.get = function(argument) {
|
|
51
|
-
const context = Context2.use();
|
|
52
|
-
const state = Pragma2.useState(() => {
|
|
53
|
-
const instance = context.get(this);
|
|
54
|
-
if (!instance)
|
|
55
|
-
if (argument === false) return () => void 0;
|
|
56
|
-
else throw new Error(`Could not find ${this} in context.`);
|
|
57
|
-
let ready;
|
|
58
|
-
let value;
|
|
59
|
-
function render() {
|
|
60
|
-
state[1]((x) => x.bind(null));
|
|
61
|
-
}
|
|
62
|
-
function refresh(action) {
|
|
63
|
-
if (typeof action == "function") action = action();
|
|
64
|
-
render();
|
|
65
|
-
if (action) return action.finally(render);
|
|
66
|
-
}
|
|
67
|
-
function onMount() {
|
|
68
|
-
ready = true;
|
|
69
|
-
return unwatch;
|
|
70
|
-
}
|
|
71
|
-
const unwatch = watch2(
|
|
72
|
-
instance,
|
|
73
|
-
(current) => {
|
|
74
|
-
if (typeof argument === "function") {
|
|
75
|
-
const next = argument.call(current, current, refresh);
|
|
76
|
-
if (next === value) return;
|
|
77
|
-
value = next;
|
|
78
|
-
} else value = current;
|
|
79
|
-
if (ready) render();
|
|
80
|
-
},
|
|
81
|
-
argument === true
|
|
82
|
-
);
|
|
83
|
-
if (value instanceof Promise) {
|
|
84
|
-
let error;
|
|
85
|
-
unwatch();
|
|
86
|
-
value.then(
|
|
87
|
-
(x) => value = x,
|
|
88
|
-
(e) => error = e
|
|
89
|
-
).finally(render);
|
|
90
|
-
value = null;
|
|
91
|
-
return () => {
|
|
92
|
-
if (error) throw error;
|
|
93
|
-
return value === void 0 ? null : value;
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
if (value === null) {
|
|
97
|
-
unwatch();
|
|
98
|
-
return () => null;
|
|
99
|
-
}
|
|
100
|
-
return () => {
|
|
101
|
-
Pragma2.useEffect(onMount, []);
|
|
102
|
-
return value === void 0 ? null : value;
|
|
103
|
-
};
|
|
104
|
-
});
|
|
105
|
-
return state[0]();
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// src/adapter.ts
|
|
109
|
-
var Pragma3 = {};
|
|
110
|
-
export {
|
|
111
|
-
Pragma3 as Pragma
|
|
112
|
-
};
|
|
113
|
-
//# sourceMappingURL=adapter.js.map
|
package/dist/esm/adapter.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/state.use.ts","../../src/state.get.ts","../../src/adapter.ts"],"sourcesContent":["import { State, Context, watch } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace State {\n interface Valid {\n use?(...props: any[]): Promise<void> | void;\n }\n\n type UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n }\n ? P\n : State.Args<T>;\n\n function use<T extends State>(this: New<T>, ...args: UseArgs<T>): T;\n }\n}\n\nState.use = function <T extends State.Valid>(\n this: State.New<T>,\n ...args: any[]\n) {\n const ambient = Context.use();\n const state = Pragma.useState(() => {\n let ready: boolean | undefined;\n let active: T;\n\n const instance = this.new((x) =>\n x.use ? Promise.resolve(x.use(...args)) : args\n );\n const context = ambient.push(instance);\n\n watch(instance, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n function didMount() {\n ready = true;\n return () => {\n context.pop();\n instance.set(null);\n };\n }\n\n return (...args: State.Args<T>) => {\n Pragma.useEffect(didMount, []);\n\n if (ready) {\n ready = false;\n\n Promise.resolve(\n instance.use\n ? instance.use(...args)\n : Promise.all(\n args\n .flat()\n .map(\n (arg) =>\n typeof arg == 'object' &&\n instance.set(arg as State.Assign<T>)\n )\n )\n ).finally(() => {\n ready = true;\n });\n }\n\n return active;\n };\n });\n\n return state[0](...args);\n};\n","import { Context, watch, State } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype 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\ndeclare module '@expressive/mvc' {\n namespace State {\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: Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n function get<T extends State>(\n this: 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: Extends<T>,\n requireValues: true\n ): Required<T>;\n\n function get<T extends State, R>(\n this: Extends<T>,\n factory: GetFactory<T, Promise<R> | R>\n ): NoVoid<R>;\n\n // TODO: eagerly match this so any nulls are caught - would prevent updates.\n function get<T extends State>(\n this: Extends<T>,\n factory: GetEffect<T>\n ): null;\n }\n}\n\nState.get = function <T extends State, R>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const context = Context.use();\n const state = Pragma.useState(() => {\n const instance = context.get(this);\n\n if (!instance)\n if (argument === false) return () => undefined;\n else throw new Error(`Could not find ${this} in context.`);\n\n let ready: boolean | undefined;\n let value: any;\n\n function render() {\n state[1]((x) => x.bind(null));\n }\n\n function refresh<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if (typeof action == 'function') action = action();\n\n render();\n\n if (action) return action.finally(render);\n }\n\n function onMount() {\n ready = true;\n return unwatch;\n }\n\n const unwatch = watch(\n instance,\n (current) => {\n if (typeof argument === 'function') {\n const next = argument.call(current, current, refresh);\n\n if (next === value) return;\n\n value = next;\n } else value = current;\n\n if (ready) render();\n },\n argument === true\n );\n\n if (value instanceof Promise) {\n let error: Error | undefined;\n\n unwatch();\n\n value\n .then(\n (x) => (value = x),\n (e) => (error = e)\n )\n .finally(render);\n\n value = null;\n\n return () => {\n if (error) throw error;\n\n return value === undefined ? null : value;\n };\n }\n\n if (value === null) {\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useEffect(onMount, []);\n return value === undefined ? null : value;\n };\n });\n\n return state[0]() as R;\n};\n","namespace Pragma {}\n\nconst Pragma = {} as {\n useState<S>(initial: () => S): [S, (next: (previous: S) => S) => void];\n useEffect(effect: () => () => void, deps?: any[]): void;\n createElement(type: any, props?: any, ...children: any[]): any;\n};\n\nexport { Pragma };\n\nimport './state.as';\nimport './state.use';\nimport './state.get';\n"],"mappings":";;;;AAAA,SAAS,OAAO,SAAS,aAAa;AAEtC,SAAS,cAAc;AAkBvB,MAAM,MAAM,YAEP,MACH;AACA,QAAM,UAAU,QAAQ,IAAI;AAC5B,QAAM,QAAQ,OAAO,SAAS,MAAM;AAClC,QAAI;AACJ,QAAI;AAEJ,UAAM,WAAW,KAAK;AAAA,MAAI,CAAC,MACzB,EAAE,MAAM,QAAQ,QAAQ,EAAE,IAAI,GAAG,IAAI,CAAC,IAAI;AAAA,IAC5C;AACA,UAAM,UAAU,QAAQ,KAAK,QAAQ;AAErC,UAAM,UAAU,CAAC,YAAY;AAC3B,eAAS;AAET,UAAI,MAAO,OAAM,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC;AAAA,IACzC,CAAC;AAED,a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AACX,YAAI,OAAO,aAAa,YAAY;AAClC,gBAAM,OAAO,SAAS,KAAK,SAAS,SAAS,OAAO;AAEpD,cAAI,SAAS,MAAO;AAEpB,kBAAQ;AAAA,QACV,MAAO,SAAQ;AAEf,YAAI,MAAO,QAAO;AAAA,MACpB;AAAA,MACA,aAAa;AAAA,IACf;AAEA,QAAI,iBAAiB,SAAS;AAC5B,UAAI;AAEJ,cAAQ;AAER,YACG;AAAA,QACC,CAAC,MAAO,QAAQ;AAAA,QAChB,CAAC,MAAO,QAAQ;AAAA,MAClB,EACC,QAAQ,MAAM;AAEjB,cAAQ;AAER,aAAO,MAAM;AACX,YAAI,MAAO,OAAM;AAEjB,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAI,UAAU,MAAM;AAClB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,MAAAE,QAAO,UAAU,SAAS,CAAC,CAAC;AAC5B,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,MAAM,CAAC,EAAE;AAClB;;;ACxJA,IAAMC,UAAS,CAAC;","names":["args","Context","watch","State","Pragma","Pragma"]}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// src/context.ts
|
|
2
|
-
import { Context } from "@expressive/mvc";
|
|
3
|
-
import {
|
|
4
|
-
createContext,
|
|
5
|
-
createElement,
|
|
6
|
-
Suspense,
|
|
7
|
-
useContext,
|
|
8
|
-
useEffect,
|
|
9
|
-
useMemo
|
|
10
|
-
} from "react";
|
|
11
|
-
var Layers = createContext(new Context());
|
|
12
|
-
Context.use = (create) => {
|
|
13
|
-
const ambient = useContext(Layers);
|
|
14
|
-
return create ? useMemo(() => ambient.push(), [ambient]) : ambient;
|
|
15
|
-
};
|
|
16
|
-
function Consumer(props) {
|
|
17
|
-
return props.for.get((i) => props.children(i));
|
|
18
|
-
}
|
|
19
|
-
function Provider(props) {
|
|
20
|
-
const context = Context.use(true);
|
|
21
|
-
useEffect(() => () => context.pop(), [context]);
|
|
22
|
-
context.use(props.for, (state) => {
|
|
23
|
-
if (props.forEach) {
|
|
24
|
-
const cleanup = props.forEach(state);
|
|
25
|
-
if (cleanup) state.set(cleanup, null);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
return provide(context, props.children, props.fallback, props.name);
|
|
29
|
-
}
|
|
30
|
-
function provide(context, children, fallback, name) {
|
|
31
|
-
if (fallback !== void 0)
|
|
32
|
-
children = createElement(Suspense, { fallback, name }, children);
|
|
33
|
-
return createElement(Layers.Provider, {
|
|
34
|
-
key: context.id,
|
|
35
|
-
value: context,
|
|
36
|
-
children
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export {
|
|
41
|
-
Consumer,
|
|
42
|
-
Provider,
|
|
43
|
-
provide
|
|
44
|
-
};
|
|
45
|
-
//# sourceMappingURL=chunk-4MR23TOU.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/context.ts"],"sourcesContent":["import State, { Context } from '@expressive/mvc';\nimport {\n createContext,\n createElement,\n ReactNode,\n Suspense,\n useContext,\n useEffect,\n useMemo\n} from 'react';\n\nexport const Layers = createContext(new Context());\n\ndeclare module '@expressive/mvc' {\n namespace Context {\n function use(create?: true): Context;\n function use(create: boolean): Context | null | undefined;\n }\n}\n\nContext.use = (create?: boolean) => {\n const ambient = useContext(Layers);\n\n return create ? useMemo(() => ambient.push(), [ambient]) : ambient;\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 interface Props<T extends State> {\n /** State or group of States to provide to descendant Consumers. */\n for: Context.Accept<T>;\n\n /**\n * Callback to run for each provided State.\n */\n forEach?: Context.Expect<T>;\n\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\nfunction Provider<T extends State>(props: Provider.Props<T>) {\n const context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.use(props.for, (state) => {\n if (props.forEach) {\n const cleanup = props.forEach(state);\n\n if (cleanup) state.set(cleanup, null);\n }\n });\n\n return provide(context, props.children, props.fallback, props.name);\n}\n\nfunction provide(\n context: Context,\n children: ReactNode,\n fallback?: ReactNode,\n name?: string | undefined\n): ReactNode {\n if (fallback !== undefined)\n children = createElement(Suspense, { fallback, name }, children);\n\n return createElement(Layers.Provider, {\n key: context.id,\n value: context,\n children\n });\n}\n\nexport { Consumer, Provider, provide };\n"],"mappings":";AAAA,SAAgB,eAAe;AAC/B;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEA,IAAM,SAAS,cAAc,IAAI,QAAQ,CAAC;AASjD,QAAQ,MAAM,CAAC,WAAqB;AAClC,QAAM,UAAU,WAAW,MAAM;AAEjC,SAAO,SAAS,QAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,OAAO,CAAC,IAAI;AAC7D;AAkBA,SAAS,SAA0B,OAA0B;AAC3D,SAAO,MAAM,IAAI,IAAI,CAAC,MAAM,MAAM,SAAS,CAAC,CAAC;AAC/C;AA4BA,SAAS,SAA0B,OAA0B;AAC3D,QAAM,UAAU,QAAQ,IAAI,IAAI;AAEhC,YAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,IAAI,MAAM,KAAK,CAAC,UAAU;AAChC,QAAI,MAAM,SAAS;AACjB,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAI,QAAS,OAAM,IAAI,SAAS,IAAI;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,QAAQ,SAAS,MAAM,UAAU,MAAM,UAAU,MAAM,IAAI;AACpE;AAEA,SAAS,QACP,SACA,UACA,UACA,MACW;AACX,MAAI,aAAa;AACf,eAAW,cAAc,UAAU,EAAE,UAAU,KAAK,GAAG,QAAQ;AAEjE,SAAO,cAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;AAAA,IACP;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
provide
|
|
3
|
-
} from "./chunk-4MR23TOU.js";
|
|
4
|
-
|
|
5
|
-
// src/state.as.ts
|
|
6
|
-
import { Context, watch, METHOD, State } from "@expressive/mvc";
|
|
7
|
-
import { Pragma } from "./adapter";
|
|
8
|
-
State.as = function(render) {
|
|
9
|
-
const FC = Render.bind(this, { render });
|
|
10
|
-
return Object.assign(FC, {
|
|
11
|
-
displayName: this.name,
|
|
12
|
-
State: this
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
function Render(props, props2) {
|
|
16
|
-
const { is, ...rest } = { ...props, ...props2 };
|
|
17
|
-
const ambient = Context.use();
|
|
18
|
-
const state = Pragma.useState(() => {
|
|
19
|
-
const instance = this.new(rest, is && ((x) => void is(x)));
|
|
20
|
-
const context = ambient.push(instance);
|
|
21
|
-
let ready;
|
|
22
|
-
let active;
|
|
23
|
-
watch(instance, (current) => {
|
|
24
|
-
active = current;
|
|
25
|
-
if (ready) state[1]((x) => x.bind(null));
|
|
26
|
-
});
|
|
27
|
-
function didMount() {
|
|
28
|
-
ready = true;
|
|
29
|
-
return () => {
|
|
30
|
-
context.pop();
|
|
31
|
-
instance.set(null);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function Render2(props3) {
|
|
35
|
-
const render = METHOD.get(active.render) || props3.render || active.render;
|
|
36
|
-
return render ? render.call(active, props3, active) : props3.children;
|
|
37
|
-
}
|
|
38
|
-
return (props3) => {
|
|
39
|
-
ready = false;
|
|
40
|
-
Pragma.useEffect(didMount, []);
|
|
41
|
-
Promise.resolve(instance.set(props3)).finally(() => {
|
|
42
|
-
ready = true;
|
|
43
|
-
});
|
|
44
|
-
return provide(
|
|
45
|
-
context,
|
|
46
|
-
Pragma.createElement(Render2, props3),
|
|
47
|
-
props3.fallback || active.fallback,
|
|
48
|
-
String(instance)
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
});
|
|
52
|
-
return state[0](rest);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export {
|
|
56
|
-
Render
|
|
57
|
-
};
|
|
58
|
-
//# sourceMappingURL=chunk-UIHA4AT6.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/state.as.ts"],"sourcesContent":["import { Context, watch, METHOD, State } from '@expressive/mvc';\nimport { FunctionComponent, ReactNode } from 'react';\n\nimport { provide } from './context';\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace State {\n type HasProps<T extends State> = {\n [K in Exclude<keyof T, keyof State>]?: T[K];\n };\n\n type ComponentProps<T extends State> = HasProps<T> & {\n /**\n * Callback for newly created instance. Only called once.\n * @returns Callback to run when instance is destroyed.\n */\n is?: (instance: T) => void;\n\n /**\n * A fallback react tree to show when suspended.\n * If not provided, `fallback` property of the State will be used.\n */\n fallback?: React.ReactNode;\n };\n\n type Props<T extends State> = T extends {\n render(props: infer P, self: any): any;\n }\n ? ComponentProps<T> & Omit<P, keyof AsComponent>\n : ComponentProps<T> & { children?: React.ReactNode };\n\n /**\n * Props which will not conflict with a State's use as a Component.\n *\n * Built-in properties must be optional, as they will always be omitted.\n */\n type RenderProps<T extends State> = HasProps<T> & {\n is?: never;\n get?: never;\n set?: never;\n };\n\n /** State which is not incompatable as Component in React. */\n interface AsComponent extends State.Valid {\n render?(props: RenderProps<this>, self: this): React.ReactNode;\n fallback?: React.ReactNode;\n }\n\n interface FC<\n T extends State,\n P extends State.Assign<T>\n > extends FunctionComponent<P & State.Props<T>> {\n displayName?: string;\n State: State.Extends<T>;\n }\n\n function as<T extends State, P extends State.Assign<T>>(\n this: State.New<T>,\n render: (props: P, self: T) => ReactNode\n ): FC<T, P>;\n }\n}\n\nState.as = function <T extends State.AsComponent, P extends State.Assign<T>>(\n this: State.Class<T>,\n render: (props: P, self: T) => ReactNode\n): State.FC<T, P> {\n const FC = Render.bind(this as State.Class, { render } as {});\n\n return Object.assign(FC, {\n displayName: this.name,\n State: this\n });\n};\n\nexport function Render<T extends State.AsComponent>(\n this: State.Class<T>,\n props: State.Props<T>,\n props2?: State.Props<T>\n) {\n const { is, ...rest } = { ...props, ...props2 };\n\n const ambient = Context.use();\n const state = Pragma.useState<(props: any) => any>(() => {\n const instance = this.new(rest as {}, is && ((x) => void is(x)));\n const context = ambient.push(instance);\n\n let ready: boolean | undefined;\n let active: T;\n\n watch(instance, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n function didMount() {\n ready = true;\n return () => {\n context.pop();\n instance.set(null);\n };\n }\n\n function Render(props: State.Props<T>) {\n const render = METHOD.get(active.render) || props.render || active.render;\n\n return render\n ? render.call(active, props as State.HasProps<T>, active)\n : props.children;\n }\n\n return (props: State.RenderProps<T>) => {\n ready = false;\n\n Pragma.useEffect(didMount, []);\n Promise.resolve(instance.set(props as {})).finally(() => {\n ready = true;\n });\n\n return provide(\n context,\n Pragma.createElement(Render, props as any),\n props.fallback || active.fallback,\n String(instance)\n );\n };\n });\n\n return state[0](rest);\n}\n"],"mappings":";;;;;AAAA,SAAS,SAAS,OAAO,QAAQ,aAAa;AAI9C,SAAS,cAAc;AA4DvB,MAAM,KAAK,SAET,QACgB;AAChB,QAAM,KAAK,OAAO,KAAK,MAAqB,EAAE,OAAO,C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