@expressive/react 0.72.0 → 0.73.1
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
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
Object.defineProperty(exports, '__toESM', {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () {
|
|
32
|
+
return __toESM;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-kSYXY2_d.js');
|
|
2
|
+
let _expressive_mvc = require("@expressive/mvc");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
|
|
5
|
+
//#region src/context.ts
|
|
6
|
+
const Layers = (0, react.createContext)(new _expressive_mvc.Context());
|
|
7
|
+
_expressive_mvc.Context.use = (create) => {
|
|
8
|
+
const ambient = (0, react.useContext)(Layers);
|
|
9
|
+
return create ? (0, react.useMemo)(() => ambient.push(), [ambient]) : ambient;
|
|
10
|
+
};
|
|
11
|
+
function Consumer(props) {
|
|
12
|
+
return props.for.get((i) => props.children(i));
|
|
13
|
+
}
|
|
14
|
+
function Provider(props) {
|
|
15
|
+
const context = _expressive_mvc.Context.use(true);
|
|
16
|
+
(0, react.useEffect)(() => () => context.pop(), [context]);
|
|
17
|
+
context.use(props.for, (state) => {
|
|
18
|
+
if (props.forEach) {
|
|
19
|
+
const cleanup = props.forEach(state);
|
|
20
|
+
if (cleanup) state.set(cleanup, null);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return provide(context, props.children, props.fallback, props.name);
|
|
24
|
+
}
|
|
25
|
+
function provide(context, children, fallback, name) {
|
|
26
|
+
if (fallback !== void 0) children = (0, react.createElement)(react.Suspense, {
|
|
27
|
+
fallback,
|
|
28
|
+
name
|
|
29
|
+
}, children);
|
|
30
|
+
return (0, react.createElement)(Layers.Provider, {
|
|
31
|
+
key: context.id,
|
|
32
|
+
value: context,
|
|
33
|
+
children
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
Object.defineProperty(exports, 'Consumer', {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () {
|
|
41
|
+
return Consumer;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(exports, 'Layers', {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: function () {
|
|
47
|
+
return Layers;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(exports, 'Provider', {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
get: function () {
|
|
53
|
+
return Provider;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(exports, 'provide', {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get: function () {
|
|
59
|
+
return provide;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=context-DpbK9_2_.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-DpbK9_2_.js","names":["Context","Suspense"],"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":";;;;;AAWA,MAAa,kCAAuB,IAAIA,yBAAS,CAAC;AASlD,wBAAQ,OAAO,WAAqB;CAClC,MAAM,gCAAqB,OAAO;AAElC,QAAO,kCAAuB,QAAQ,MAAM,EAAE,CAAC,QAAQ,CAAC,GAAG;;AAmB7D,SAAS,SAA0B,OAA0B;AAC3D,QAAO,MAAM,IAAI,KAAK,MAAM,MAAM,SAAS,EAAE,CAAC;;AA6BhD,SAAS,SAA0B,OAA0B;CAC3D,MAAM,UAAUA,wBAAQ,IAAI,KAAK;AAEjC,kCAAsB,QAAQ,KAAK,EAAE,CAAC,QAAQ,CAAC;AAE/C,SAAQ,IAAI,MAAM,MAAM,UAAU;AAChC,MAAI,MAAM,SAAS;GACjB,MAAM,UAAU,MAAM,QAAQ,MAAM;AAEpC,OAAI,QAAS,OAAM,IAAI,SAAS,KAAK;;GAEvC;AAEF,QAAO,QAAQ,SAAS,MAAM,UAAU,MAAM,UAAU,MAAM,KAAK;;AAGrE,SAAS,QACP,SACA,UACA,UACA,MACW;AACX,KAAI,aAAa,OACf,qCAAyBC,gBAAU;EAAE;EAAU;EAAM,EAAE,SAAS;AAElE,iCAAqB,OAAO,UAAU;EACpC,KAAK,QAAQ;EACb,OAAO;EACP;EACD,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Layers, provide } from "./context.js";
|
|
2
|
+
import { Context as Context$1 } from "./index.js";
|
|
3
|
+
import { METHOD, event, watch } from "@expressive/mvc";
|
|
4
|
+
import { Pragma, ReactState } from "./state";
|
|
5
|
+
|
|
6
|
+
//#region src/component.ts
|
|
7
|
+
const OUTER = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
var Component = class extends ReactState {
|
|
9
|
+
static {
|
|
10
|
+
this.contextType = Layers;
|
|
11
|
+
}
|
|
12
|
+
get props() {
|
|
13
|
+
return this._props;
|
|
14
|
+
}
|
|
15
|
+
set props(props) {
|
|
16
|
+
this._props = props;
|
|
17
|
+
this.set(props);
|
|
18
|
+
}
|
|
19
|
+
get context() {
|
|
20
|
+
return Context$1.get(this);
|
|
21
|
+
}
|
|
22
|
+
set context(context) {
|
|
23
|
+
if (OUTER.get(this) === context) return;
|
|
24
|
+
OUTER.set(this, context);
|
|
25
|
+
context.push(this);
|
|
26
|
+
}
|
|
27
|
+
constructor({ is, ...props }) {
|
|
28
|
+
super(props, is);
|
|
29
|
+
const self = Render.bind(this, METHOD.get(this.render));
|
|
30
|
+
this.render = () => Pragma.createElement(self);
|
|
31
|
+
}
|
|
32
|
+
render() {
|
|
33
|
+
return typeof this.children === "function" ? this.children(this) : this.children;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(Component.prototype, "isReactComponent", { get: () => true });
|
|
37
|
+
function Render(render) {
|
|
38
|
+
const state = Pragma.useState(() => {
|
|
39
|
+
event(this);
|
|
40
|
+
const { context } = this;
|
|
41
|
+
let ready;
|
|
42
|
+
let active;
|
|
43
|
+
watch(this, (current) => {
|
|
44
|
+
active = current;
|
|
45
|
+
if (ready) state[1]((x) => x.bind(null));
|
|
46
|
+
});
|
|
47
|
+
const didMount = () => {
|
|
48
|
+
ready = true;
|
|
49
|
+
return () => {
|
|
50
|
+
context.pop();
|
|
51
|
+
this.set(null);
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const Render = () => render.call(active);
|
|
55
|
+
return () => {
|
|
56
|
+
ready = false;
|
|
57
|
+
Pragma.useEffect(didMount, []);
|
|
58
|
+
setTimeout(() => ready = true, 0);
|
|
59
|
+
return provide(context, Pragma.createElement(Render), active.fallback, String(this));
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return state[0]();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
export { Component };
|
|
67
|
+
//# sourceMappingURL=component.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component.js","names":["State","Context"],"sources":["../../src/component.ts"],"sourcesContent":["import { event, METHOD, watch } from '@expressive/mvc';\nimport React, { FunctionComponent, ReactNode } from 'react';\n\nimport { Pragma, ReactState as State } from './state';\nimport { provide, Layers } from './context';\nimport { Context } from '.';\n\nconst OUTER = new WeakMap<Component, Context>();\n\ntype HasProps<T extends State> = {\n [K in Exclude<keyof T, keyof State>]?: T[K];\n};\n\ntype 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\ntype 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 */\ntype 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. */\ninterface AsComponent extends State {\n render?(props: RenderProps<this>, self: this): React.ReactNode;\n fallback?: React.ReactNode;\n}\n\ninterface FC<\n T extends State,\n P extends State.Assign<T>\n> extends FunctionComponent<P & Props<T>> {\n displayName?: string;\n State: State.Extends<T>;\n}\n\ndeclare module './state' {\n namespace ReactState {\n export { ComponentProps, Props, RenderProps, AsComponent, FC };\n }\n}\n\nclass Component extends State {\n static contextType = Layers;\n\n private _props!: State.ComponentProps<this>;\n\n get props(): State.ComponentProps<this> {\n return this._props;\n }\n\n set props(props: State.ComponentProps<this>) {\n this._props = props;\n this.set(props as {});\n }\n\n get context(): Context {\n return Context.get(this)!;\n }\n\n set context(context: Context) {\n if (OUTER.get(this) === context) return;\n\n OUTER.set(this, context);\n context.push(this);\n }\n\n state!: State.Values<this>;\n children!: ReactNode | ((self: this) => ReactNode);\n fallback?: ReactNode;\n\n constructor({ is, ...props }: any) {\n super(props, is);\n const self = Render.bind(this, METHOD.get(this.render));\n this.render = () => Pragma.createElement(self);\n }\n\n render(): ReactNode {\n return typeof this.children === 'function'\n ? this.children(this)\n : this.children;\n }\n\n /** @deprecated This is purely for React JSX compatibility in typescript. */\n setState!: (state: any, callback?: () => void) => void;\n\n /** @deprecated This is purely for React JSX compatibility in typescript. */\n forceUpdate!: (callback?: () => void) => void;\n}\n\nObject.defineProperty(Component.prototype, 'isReactComponent', {\n get: () => true\n});\n\nfunction Render<T extends Component>(this: T, render: () => ReactNode) {\n const state = Pragma.useState(() => {\n event(this);\n\n const { context } = this;\n\n let ready: boolean | undefined;\n let active: T;\n\n watch(this, (current) => {\n active = current;\n\n if (ready) state[1]((x) => x.bind(null));\n });\n\n const didMount = () => {\n ready = true;\n return () => {\n context.pop();\n this.set(null);\n };\n };\n\n const Render = () => render.call(active);\n\n return () => {\n ready = false;\n\n Pragma.useEffect(didMount, []);\n setTimeout(() => (ready = true), 0);\n\n return provide(\n context,\n Pragma.createElement(Render),\n active.fallback,\n String(this)\n );\n };\n });\n\n return state[0]();\n}\n\nexport { Component, FC, AsComponent, Props, RenderProps, HasProps };\n"],"mappings":";;;;;;AAOA,MAAM,wBAAQ,IAAI,SAA6B;AAyD/C,IAAM,YAAN,cAAwBA,WAAM;;qBACP;;CAIrB,IAAI,QAAoC;AACtC,SAAO,KAAK;;CAGd,IAAI,MAAM,OAAmC;AAC3C,OAAK,SAAS;AACd,OAAK,IAAI,MAAY;;CAGvB,IAAI,UAAmB;AACrB,SAAOC,UAAQ,IAAI,KAAK;;CAG1B,IAAI,QAAQ,SAAkB;AAC5B,MAAI,MAAM,IAAI,KAAK,KAAK,QAAS;AAEjC,QAAM,IAAI,MAAM,QAAQ;AACxB,UAAQ,KAAK,KAAK;;CAOpB,YAAY,EAAE,IAAI,GAAG,SAAc;AACjC,QAAM,OAAO,GAAG;EAChB,MAAM,OAAO,OAAO,KAAK,MAAM,OAAO,IAAI,KAAK,OAAO,CAAC;AACvD,OAAK,eAAe,OAAO,cAAc,KAAK;;CAGhD,SAAoB;AAClB,SAAO,OAAO,KAAK,aAAa,aAC5B,KAAK,SAAS,KAAK,GACnB,KAAK;;;AAUb,OAAO,eAAe,UAAU,WAAW,oBAAoB,EAC7D,WAAW,MACZ,CAAC;AAEF,SAAS,OAAqC,QAAyB;CACrE,MAAM,QAAQ,OAAO,eAAe;AAClC,QAAM,KAAK;EAEX,MAAM,EAAE,YAAY;EAEpB,IAAI;EACJ,IAAI;AAEJ,QAAM,OAAO,YAAY;AACvB,YAAS;AAET,OAAI,MAAO,OAAM,IAAI,MAAM,EAAE,KAAK,KAAK,CAAC;IACxC;EAEF,MAAM,iBAAiB;AACrB,WAAQ;AACR,gBAAa;AACX,YAAQ,KAAK;AACb,SAAK,IAAI,KAAK;;;EAIlB,MAAM,eAAe,OAAO,KAAK,OAAO;AAExC,eAAa;AACX,WAAQ;AAER,UAAO,UAAU,UAAU,EAAE,CAAC;AAC9B,oBAAkB,QAAQ,MAAO,EAAE;AAEnC,UAAO,QACL,SACA,OAAO,cAAc,OAAO,EAC5B,OAAO,UACP,OAAO,KAAK,CACb;;GAEH;AAEF,QAAO,MAAM,IAAI"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Context } from "@expressive/mvc";
|
|
2
|
+
import { Suspense, createContext, createElement, useContext, useEffect, useMemo } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/context.ts
|
|
5
|
+
const Layers = createContext(new Context());
|
|
6
|
+
Context.use = (create) => {
|
|
7
|
+
const ambient = useContext(Layers);
|
|
8
|
+
return create ? useMemo(() => ambient.push(), [ambient]) : ambient;
|
|
9
|
+
};
|
|
10
|
+
function Consumer(props) {
|
|
11
|
+
return props.for.get((i) => props.children(i));
|
|
12
|
+
}
|
|
13
|
+
function Provider(props) {
|
|
14
|
+
const context = Context.use(true);
|
|
15
|
+
useEffect(() => () => context.pop(), [context]);
|
|
16
|
+
context.use(props.for, (state) => {
|
|
17
|
+
if (props.forEach) {
|
|
18
|
+
const cleanup = props.forEach(state);
|
|
19
|
+
if (cleanup) state.set(cleanup, null);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return provide(context, props.children, props.fallback, props.name);
|
|
23
|
+
}
|
|
24
|
+
function provide(context, children, fallback, name) {
|
|
25
|
+
if (fallback !== void 0) children = createElement(Suspense, {
|
|
26
|
+
fallback,
|
|
27
|
+
name
|
|
28
|
+
}, children);
|
|
29
|
+
return createElement(Layers.Provider, {
|
|
30
|
+
key: context.id,
|
|
31
|
+
value: context,
|
|
32
|
+
children
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
export { Consumer, Layers, Provider, provide };
|
|
38
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","names":[],"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":";;;;AAWA,MAAa,SAAS,cAAc,IAAI,SAAS,CAAC;AASlD,QAAQ,OAAO,WAAqB;CAClC,MAAM,UAAU,WAAW,OAAO;AAElC,QAAO,SAAS,cAAc,QAAQ,MAAM,EAAE,CAAC,QAAQ,CAAC,GAAG;;AAmB7D,SAAS,SAA0B,OAA0B;AAC3D,QAAO,MAAM,IAAI,KAAK,MAAM,MAAM,SAAS,EAAE,CAAC;;AA6BhD,SAAS,SAA0B,OAA0B;CAC3D,MAAM,UAAU,QAAQ,IAAI,KAAK;AAEjC,uBAAsB,QAAQ,KAAK,EAAE,CAAC,QAAQ,CAAC;AAE/C,SAAQ,IAAI,MAAM,MAAM,UAAU;AAChC,MAAI,MAAM,SAAS;GACjB,MAAM,UAAU,MAAM,QAAQ,MAAM;AAEpC,OAAI,QAAS,OAAM,IAAI,SAAS,KAAK;;GAEvC;AAEF,QAAO,QAAQ,SAAS,MAAM,UAAU,MAAM,UAAU,MAAM,KAAK;;AAGrE,SAAS,QACP,SACA,UACA,UACA,MACW;AACX,KAAI,aAAa,OACf,YAAW,cAAc,UAAU;EAAE;EAAU;EAAM,EAAE,SAAS;AAElE,QAAO,cAAc,OAAO,UAAU;EACpC,KAAK,QAAQ;EACb,OAAO;EACP;EACD,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,38 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "
|
|
1
|
+
import { Consumer, Provider } from "./context.js";
|
|
2
|
+
import { Component } from "./component.js";
|
|
3
|
+
import { Context, Observable, State, get, ref, set, use } from "@expressive/mvc";
|
|
4
|
+
import { Fragment, createElement, createElement as createElement$1, useEffect, useState } from "react";
|
|
5
|
+
import { Pragma, ReactState } from "./state";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
Context,
|
|
9
|
-
Observable,
|
|
10
|
-
State,
|
|
11
|
-
get,
|
|
12
|
-
use,
|
|
13
|
-
ref,
|
|
14
|
-
set
|
|
15
|
-
} from "@expressive/mvc";
|
|
16
|
-
import { createElement, useEffect, useState } from "react";
|
|
17
|
-
import { Pragma } from "./adapter";
|
|
18
|
-
import { Fragment, createElement as createElement2 } from "react";
|
|
7
|
+
//#region src/index.ts
|
|
19
8
|
Pragma.useEffect = useEffect;
|
|
20
9
|
Pragma.useState = useState;
|
|
21
|
-
Pragma.createElement = createElement;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Pragma,
|
|
29
|
-
Provider,
|
|
30
|
-
State,
|
|
31
|
-
createElement2 as createElement,
|
|
32
|
-
src_default as default,
|
|
33
|
-
get,
|
|
34
|
-
ref,
|
|
35
|
-
set,
|
|
36
|
-
use
|
|
37
|
-
};
|
|
10
|
+
Pragma.createElement = createElement$1;
|
|
11
|
+
State.get = ReactState.get;
|
|
12
|
+
State.use = ReactState.use;
|
|
13
|
+
State.as = ReactState.as;
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { Component, Consumer, Context, Fragment, Observable, Pragma, Provider, ReactState as State, ReactState as default, createElement, get, ref, set, use };
|
|
38
17
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"index.js","names":["createElement"],"sources":["../../src/index.ts"],"sourcesContent":["import { State } from '@expressive/mvc';\n\nimport { createElement, useEffect, useState } from 'react';\n\nimport { ReactState, Pragma } from './state';\n\nPragma.useEffect = useEffect;\nPragma.useState = useState;\nPragma.createElement = createElement;\n\n/**\n * Augmented State namespace to include ReactState hooks as static methods.\n *\n * This retroactively adds React support to agnostic State defined\n * by component libraries which import `@expressive/mvc` directly.\n */\ndeclare module '@expressive/mvc' {\n namespace State {\n export import as = ReactState.as;\n export import get = ReactState.get;\n export import use = ReactState.use;\n }\n}\n\nState.get = ReactState.get;\nState.use = ReactState.use;\nState.as = ReactState.as;\n\nexport { ReactState as State, ReactState as default };\nexport { Context, Observable, get, use, ref, set } from '@expressive/mvc';\nexport { Component } from './component';\nexport { Consumer, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { Pragma };"],"mappings":";;;;;;;AAMA,OAAO,YAAY;AACnB,OAAO,WAAW;AAClB,OAAO,gBAAgBA;AAgBvB,MAAM,MAAM,WAAW;AACvB,MAAM,MAAM,WAAW;AACvB,MAAM,KAAK,WAAW"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { Fragment } from "react";
|
|
2
2
|
import Runtime from "react/jsx-dev-runtime";
|
|
3
3
|
import { patch } from "./jsx-runtime";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
4
|
+
|
|
5
|
+
//#region src/jsx-dev-runtime.ts
|
|
6
|
+
const jsxDEV = patch.bind(Runtime.jsxDEV);
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { Fragment, jsxDEV };
|
|
10
10
|
//# sourceMappingURL=jsx-dev-runtime.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/jsx-dev-runtime.ts"],"sourcesContent":["import Runtime from 'react/jsx-dev-runtime';\n\nimport { patch } from './jsx-runtime';\n\nexport const jsxDEV = patch.bind(Runtime.jsxDEV);\n\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from 'react';\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"jsx-dev-runtime.js","names":[],"sources":["../../src/jsx-dev-runtime.ts"],"sourcesContent":["import Runtime from 'react/jsx-dev-runtime';\n\nimport { patch } from './jsx-runtime';\n\nexport const jsxDEV = patch.bind(Runtime.jsxDEV);\n\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from 'react';\n"],"mappings":";;;;;AAIA,MAAa,SAAS,MAAM,KAAK,QAAQ,OAAO"}
|
package/dist/esm/jsx-runtime.js
CHANGED
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
Render
|
|
3
|
-
} from "./chunk-UIHA4AT6.js";
|
|
4
|
-
import "./chunk-4MR23TOU.js";
|
|
5
|
-
|
|
6
|
-
// src/jsx-runtime.ts
|
|
7
|
-
import { State } from "@expressive/mvc";
|
|
8
|
-
import Runtime from "react/jsx-runtime";
|
|
1
|
+
import State$1 from "@expressive/mvc";
|
|
9
2
|
import { Fragment } from "react";
|
|
10
|
-
|
|
3
|
+
import { Render } from "./state";
|
|
4
|
+
import Runtime from "react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/jsx-runtime.ts
|
|
7
|
+
const RENDER = /* @__PURE__ */ new WeakMap();
|
|
11
8
|
function patch(type, ...args) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return this(type, ...args);
|
|
9
|
+
if (State$1.is(type)) if (RENDER.has(type)) type = RENDER.get(type);
|
|
10
|
+
else RENDER.set(type, type = Render.bind(type));
|
|
11
|
+
return this(type, ...args);
|
|
16
12
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
jsxs,
|
|
23
|
-
patch
|
|
24
|
-
};
|
|
13
|
+
const jsx = patch.bind(Runtime.jsx);
|
|
14
|
+
const jsxs = patch.bind(Runtime.jsxs);
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { Fragment, jsx, jsxs, patch };
|
|
25
18
|
//# sourceMappingURL=jsx-runtime.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/jsx-runtime.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"jsx-runtime.js","names":["State"],"sources":["../../src/jsx-runtime.ts"],"sourcesContent":["import State from '@expressive/mvc';\nimport Runtime from 'react/jsx-runtime';\nimport React from 'react';\n\nimport { Render, AsComponent, Props } from './state';\n\nexport declare namespace JSX {\n type ElementType =\n | State.Extends<AsComponent>\n | React.JSX.ElementType\n | ((props: {}, ref?: any) => void);\n\n type LibraryManagedAttributes<C, P> =\n // For normal class components, pull from props property explicitly because we dorked up ElementAttributesProperty.\n C extends new (...args: any[]) => { props: infer U }\n ? U\n : C extends State.Extends<infer U>\n ? Props<U>\n : React.JSX.LibraryManagedAttributes<C, P>;\n\n interface Element extends React.JSX.Element {}\n interface ElementClass extends React.JSX.ElementClass {}\n\n // This is a hack to make TypeScript happy - React's interface insists on `props` property existing.\n // I await the \"Find Out\" phase of this in git issues.\n interface ElementAttributesProperty {}\n interface ElementChildrenAttribute\n extends React.JSX.ElementChildrenAttribute {}\n\n interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}\n interface IntrinsicClassAttributes<T> extends React.JSX\n .IntrinsicClassAttributes<T> {}\n interface IntrinsicElements extends React.JSX.IntrinsicElements {}\n}\n\nconst RENDER = new WeakMap<Function, React.ComponentType>();\n\nexport function patch(\n this: (type: React.ElementType, ...args: any[]) => React.ReactElement,\n type: React.ElementType | State.Type,\n ...args: any[]\n): React.ReactElement {\n if (State.is(type))\n if (RENDER.has(type)) type = RENDER.get(type)!;\n else RENDER.set(type, (type = Render.bind(type)));\n\n return this(type, ...args);\n}\n\nexport const jsx = patch.bind(Runtime.jsx);\nexport const jsxs = patch.bind(Runtime.jsxs);\n\nexport { Fragment } from 'react';\n"],"mappings":";;;;;;AAmCA,MAAM,yBAAS,IAAI,SAAwC;AAE3D,SAAgB,MAEd,MACA,GAAG,MACiB;AACpB,KAAIA,QAAM,GAAG,KAAK,CAChB,KAAI,OAAO,IAAI,KAAK,CAAE,QAAO,OAAO,IAAI,KAAK;KACxC,QAAO,IAAI,MAAO,OAAO,OAAO,KAAK,KAAK,CAAE;AAEnD,QAAO,KAAK,MAAM,GAAG,KAAK;;AAG5B,MAAa,MAAM,MAAM,KAAK,QAAQ,IAAI;AAC1C,MAAa,OAAO,MAAM,KAAK,QAAQ,KAAK"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { provide } from "./context.js";
|
|
2
|
+
import { Context, METHOD, State, watch } from "@expressive/mvc";
|
|
3
|
+
|
|
4
|
+
//#region src/state.ts
|
|
5
|
+
const Pragma = {};
|
|
6
|
+
var ReactState = class ReactState extends State {
|
|
7
|
+
/**
|
|
8
|
+
* Create and manage instance of this State within React component.
|
|
9
|
+
*
|
|
10
|
+
* @param args Arguments to pass to constructor or `use` method (if defined).
|
|
11
|
+
* @returns Managed instance of this State.
|
|
12
|
+
*/
|
|
13
|
+
static use(...args) {
|
|
14
|
+
const ambient = Context.use();
|
|
15
|
+
const state = Pragma.useState(() => {
|
|
16
|
+
let ready;
|
|
17
|
+
let active;
|
|
18
|
+
let use = (...args) => Promise.all(args.flat().map((arg) => typeof arg == "object" && instance.set(arg)));
|
|
19
|
+
const instance = new this((x) => {
|
|
20
|
+
if (x instanceof ReactState && x.use) {
|
|
21
|
+
use = x.use?.bind(x);
|
|
22
|
+
use(...args);
|
|
23
|
+
} else return args;
|
|
24
|
+
});
|
|
25
|
+
const context = ambient.push(instance);
|
|
26
|
+
watch(instance, (current) => {
|
|
27
|
+
active = current;
|
|
28
|
+
if (ready) state[1]((x) => x.bind(null));
|
|
29
|
+
});
|
|
30
|
+
function didMount() {
|
|
31
|
+
ready = true;
|
|
32
|
+
return () => {
|
|
33
|
+
context.pop();
|
|
34
|
+
instance.set(null);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return (...args) => {
|
|
38
|
+
Pragma.useEffect(didMount, []);
|
|
39
|
+
if (ready) {
|
|
40
|
+
ready = false;
|
|
41
|
+
Promise.resolve(use(...args)).finally(() => ready = true);
|
|
42
|
+
}
|
|
43
|
+
return active;
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
return state[0](...args);
|
|
47
|
+
}
|
|
48
|
+
static get(argument) {
|
|
49
|
+
const context = Context.use();
|
|
50
|
+
const state = Pragma.useState(() => {
|
|
51
|
+
const instance = context.get(this);
|
|
52
|
+
if (!instance) if (argument === false) return () => void 0;
|
|
53
|
+
else throw new Error(`Could not find ${this} in context.`);
|
|
54
|
+
let ready;
|
|
55
|
+
let value;
|
|
56
|
+
function render() {
|
|
57
|
+
state[1]((x) => x.bind(null));
|
|
58
|
+
}
|
|
59
|
+
function refresh(action) {
|
|
60
|
+
if (typeof action == "function") action = action();
|
|
61
|
+
render();
|
|
62
|
+
if (action) return action.finally(render);
|
|
63
|
+
}
|
|
64
|
+
const unwatch = watch(instance, (current) => {
|
|
65
|
+
if (typeof argument === "function") {
|
|
66
|
+
const next = argument.call(current, current, refresh);
|
|
67
|
+
if (next === value) return;
|
|
68
|
+
value = next;
|
|
69
|
+
} else value = current;
|
|
70
|
+
if (ready) render();
|
|
71
|
+
}, argument === true);
|
|
72
|
+
if (value instanceof Promise) {
|
|
73
|
+
let error;
|
|
74
|
+
unwatch();
|
|
75
|
+
value.then((x) => value = x, (e) => error = e).finally(render);
|
|
76
|
+
value = null;
|
|
77
|
+
return () => {
|
|
78
|
+
if (error) throw error;
|
|
79
|
+
return value === void 0 ? null : value;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (value === null) {
|
|
83
|
+
unwatch();
|
|
84
|
+
return () => null;
|
|
85
|
+
}
|
|
86
|
+
function onMount() {
|
|
87
|
+
ready = true;
|
|
88
|
+
return unwatch;
|
|
89
|
+
}
|
|
90
|
+
return () => {
|
|
91
|
+
Pragma.useEffect(onMount, []);
|
|
92
|
+
return value === void 0 ? null : value;
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
return state[0]();
|
|
96
|
+
}
|
|
97
|
+
static as(render) {
|
|
98
|
+
const FC = Render.bind(this, { render });
|
|
99
|
+
return Object.assign(FC, {
|
|
100
|
+
displayName: this.name,
|
|
101
|
+
State: this
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
function Render(props, props2) {
|
|
106
|
+
const { is, ...rest } = {
|
|
107
|
+
...props,
|
|
108
|
+
...props2
|
|
109
|
+
};
|
|
110
|
+
const ambient = Context.use();
|
|
111
|
+
const state = Pragma.useState(() => {
|
|
112
|
+
const instance = this.new(rest, is && ((x) => void is(x)));
|
|
113
|
+
const context = ambient.push(instance);
|
|
114
|
+
let ready;
|
|
115
|
+
let active;
|
|
116
|
+
watch(instance, (current) => {
|
|
117
|
+
active = current;
|
|
118
|
+
if (ready) state[1]((x) => x.bind(null));
|
|
119
|
+
});
|
|
120
|
+
function didMount() {
|
|
121
|
+
ready = true;
|
|
122
|
+
return () => {
|
|
123
|
+
context.pop();
|
|
124
|
+
instance.set(null);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function Render(props) {
|
|
128
|
+
const render = METHOD.get(active.render) || props.render || active.render;
|
|
129
|
+
return render ? render.call(active, props, active) : props.children;
|
|
130
|
+
}
|
|
131
|
+
return (props) => {
|
|
132
|
+
ready = false;
|
|
133
|
+
Pragma.useEffect(didMount, []);
|
|
134
|
+
Promise.resolve(instance.set(props)).finally(() => {
|
|
135
|
+
ready = true;
|
|
136
|
+
});
|
|
137
|
+
return provide(context, Pragma.createElement(Render, props), props.fallback || active.fallback, String(instance));
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
return state[0](rest);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
export { Pragma, ReactState, Render };
|
|
145
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","names":[],"sources":["../../src/state.ts"],"sourcesContent":["import { State, Context, watch, METHOD } from '@expressive/mvc';\nimport { ReactNode } from 'react';\nimport { provide } from './context';\nimport type { AsComponent, FC, HasProps, Props, RenderProps } from './component';\n\nexport const 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\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\ntype GetFactory<T extends State, R> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n) => R;\n\ntype GetEffect<T extends State> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n) => null;\n\ntype UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n}\n ? P\n : State.Args<T>;\n\ndeclare namespace ReactState {\n export import Extends = State.Extends;\n export import Type = State.Type;\n export import Args = State.Args;\n export import Init = State.Init;\n export import Assign = State.Assign;\n export import Field = State.Field;\n export import Event = State.Event;\n export import Export = State.Export;\n export import Value = State.Value;\n export import Setter = State.Setter;\n export import OnEvent = State.OnEvent;\n export import OnUpdate = State.OnUpdate;\n export import Values = State.Values;\n export import Partial = State.Partial;\n export import Ref = State.Ref;\n export import Effect = State.Effect;\n export import EffectCallback = State.EffectCallback;\n\n export { GetFactory, GetEffect, UseArgs, HasProps };\n}\n\nabstract class ReactState extends State {\n /**\n * Optional hook is called when State.use() is invoked within a React component.\n *\n * This is called *every render* of the component, and will intercept\n * arguments which would otherwise be merged into the State.\n *\n * @param props Arguments passed to State.use().\n */\n protected use?(...props: any[]): Promise<void> | void;\n\n /**\n * Create and manage instance of this State within React component.\n *\n * @param args Arguments to pass to constructor or `use` method (if defined).\n * @returns Managed instance of this State.\n */\n static use<T extends ReactState>(\n this: State.Type<T>,\n ...args: UseArgs<T>\n ): T {\n const ambient = Context.use();\n const state = Pragma.useState(() => {\n let ready: boolean | undefined;\n let active: T;\n\n let use = (...args: State.Args<T>): Promise<unknown> | void =>\n Promise.all(\n args\n .flat()\n .map(\n (arg) =>\n typeof arg == 'object' && instance.set(arg as State.Assign<T>)\n )\n );\n\n // Create instance - pass args to constructor if no use method\n const instance = new this((x) => {\n if (x instanceof ReactState && x.use) {\n use = x.use?.bind(x);\n use(...args);\n } else return args;\n });\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(use(...args)).finally(() => (ready = true));\n }\n\n return active;\n };\n });\n\n return state[0](...args);\n }\n\n /** Fetch instance of this class from context. */\n static get<T extends State>(this: State.Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n static get<T extends State>(\n this: State.Extends<T>,\n required: false\n ): T | undefined;\n\n /** Fetch instance of this class from context. */\n static get<T extends State>(\n this: State.Extends<T>,\n requireValues: true\n ): Required<T>;\n\n static get<T extends State, R>(\n this: State.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 static get<T extends State>(\n this: State.Extends<T>,\n factory: GetEffect<T>\n ): null;\n\n static get<T extends State, R>(\n this: State.Extends<T>,\n argument?: boolean | 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 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 function onMount() {\n ready = true;\n return unwatch;\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\n static as<T extends AsComponent, P extends State.Assign<T>>(\n this: State.Type<T>,\n render: (props: P, self: T) => ReactNode\n ): FC<T, P> {\n const FC = Render.bind(this as State.Type, { render } as {});\n\n return Object.assign(FC, {\n displayName: this.name,\n State: this\n });\n }\n}\n\nexport function Render<T extends AsComponent>(\n this: State.Type<T>,\n props: Props<T>,\n props2?: 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: Props<T>) {\n const render = METHOD.get(active.render) || props.render || active.render;\n\n return render\n ? render.call(active, props as HasProps<T>, active)\n : props.children;\n }\n\n return (props: 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\nexport { ReactState };\nexport { AsComponent, Props };\n"],"mappings":"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"}
|