@expressive/react 0.73.1 → 0.74.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 → chunk-BE-pF4vm.js} +1 -1
- package/dist/{context-DpbK9_2_.js → context-DEag_HVs.js} +6 -6
- package/dist/context-DEag_HVs.js.map +1 -0
- package/dist/esm/context.js +1 -1
- package/dist/esm/context.js.map +1 -1
- package/dist/esm/index.js +2 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jsx-runtime.js +41 -3
- package/dist/esm/jsx-runtime.js.map +1 -1
- package/dist/esm/state.js +59 -31
- package/dist/esm/state.js.map +1 -1
- package/dist/index.d.ts +66 -2
- package/dist/index.js +13 -74
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +3 -3
- package/dist/jsx-dev-runtime.js +2 -1
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +26 -7
- package/dist/jsx-runtime.js +43 -5
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/state.d.ts +40 -6
- package/dist/state.js +65 -37
- package/dist/state.js.map +1 -1
- package/package.json +13 -9
- package/dist/context-DpbK9_2_.js.map +0 -1
- package/dist/esm/component.js +0 -67
- package/dist/esm/component.js.map +0 -1
- package/dist/index-zVvhW0uD.d.ts +0 -130
package/dist/jsx-runtime.d.ts
CHANGED
|
@@ -1,13 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import State$1 from "@expressive/mvc";
|
|
1
|
+
import { State } from "@expressive/state";
|
|
3
2
|
import React, { Fragment } from "react";
|
|
4
3
|
|
|
5
4
|
//#region src/jsx-runtime.d.ts
|
|
5
|
+
type Reserved = keyof State | 'render' | 'fallback';
|
|
6
|
+
type StateProps<T extends State> = { [K in Exclude<keyof T, Reserved>]?: T[K] };
|
|
7
|
+
type ComponentProps<T extends State> = StateProps<T> & {
|
|
8
|
+
is?: (instance: T) => void;
|
|
9
|
+
fallback?: React.ReactNode;
|
|
10
|
+
};
|
|
11
|
+
type ValidProps<T extends State> = StateProps<T> & {
|
|
12
|
+
is?: never;
|
|
13
|
+
get?: never;
|
|
14
|
+
set?: never;
|
|
15
|
+
};
|
|
16
|
+
interface AsComponent extends State {
|
|
17
|
+
render?(props: ValidProps<this>, self: this): React.ReactNode;
|
|
18
|
+
fallback?: React.ReactNode;
|
|
19
|
+
}
|
|
20
|
+
type Props<T extends State> = T extends {
|
|
21
|
+
render(props: infer P, self: any): any;
|
|
22
|
+
} ? ComponentProps<T> & Omit<P, keyof AsComponent> : ComponentProps<T> & {
|
|
23
|
+
children?: React.ReactNode;
|
|
24
|
+
};
|
|
6
25
|
declare namespace JSX {
|
|
7
|
-
type ElementType = State
|
|
26
|
+
type ElementType = State.Extends<AsComponent> | React.JSX.ElementType | ((props: {}, ref?: any) => void);
|
|
8
27
|
type LibraryManagedAttributes<C, P> = C extends (new (...args: any[]) => {
|
|
9
28
|
props: infer U;
|
|
10
|
-
}) ? U : C extends State
|
|
29
|
+
}) ? U : C extends State.Extends<infer U> ? Props<U> : React.JSX.LibraryManagedAttributes<C, P>;
|
|
11
30
|
interface Element extends React.JSX.Element {}
|
|
12
31
|
interface ElementClass extends React.JSX.ElementClass {}
|
|
13
32
|
interface ElementAttributesProperty {}
|
|
@@ -16,9 +35,9 @@ declare namespace JSX {
|
|
|
16
35
|
interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}
|
|
17
36
|
interface IntrinsicElements extends React.JSX.IntrinsicElements {}
|
|
18
37
|
}
|
|
19
|
-
declare function patch(this: (type: React.ElementType, ...args: any[]) => React.ReactElement, type: React.ElementType | State
|
|
20
|
-
declare const jsx: (type: React.ElementType<any, keyof React.JSX.IntrinsicElements> | State
|
|
21
|
-
declare const jsxs: (type: React.ElementType<any, keyof React.JSX.IntrinsicElements> | State
|
|
38
|
+
declare function patch(this: (type: React.ElementType, ...args: any[]) => React.ReactElement, type: React.ElementType | State.Type, ...args: any[]): React.ReactElement;
|
|
39
|
+
declare const jsx: (type: React.ElementType<any, keyof React.JSX.IntrinsicElements> | State.Type<State>, ...args: any[]) => React.ReactElement<unknown, string | React.JSXElementConstructor<any>>;
|
|
40
|
+
declare const jsxs: (type: React.ElementType<any, keyof React.JSX.IntrinsicElements> | State.Type<State>, ...args: any[]) => React.ReactElement<unknown, string | React.JSXElementConstructor<any>>;
|
|
22
41
|
//#endregion
|
|
23
42
|
export { Fragment, JSX, jsx, jsxs, patch };
|
|
24
43
|
//# sourceMappingURL=jsx-runtime.d.ts.map
|
package/dist/jsx-runtime.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_chunk = require('./chunk-BE-pF4vm.js');
|
|
3
|
+
const require_context = require('./context-DEag_HVs.js');
|
|
4
|
+
let _expressive_state = require("@expressive/state");
|
|
4
5
|
let react = require("react");
|
|
5
6
|
let src_state = require("./state");
|
|
6
7
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
@@ -9,12 +10,49 @@ react_jsx_runtime = require_chunk.__toESM(react_jsx_runtime);
|
|
|
9
10
|
//#region src/jsx-runtime.ts
|
|
10
11
|
const RENDER = /* @__PURE__ */ new WeakMap();
|
|
11
12
|
function patch(type, ...args) {
|
|
12
|
-
if (
|
|
13
|
-
else RENDER.set(type, type =
|
|
13
|
+
if (_expressive_state.State.is(type)) if (RENDER.has(type)) type = RENDER.get(type);
|
|
14
|
+
else RENDER.set(type, type = Render.bind(type));
|
|
14
15
|
return this(type, ...args);
|
|
15
16
|
}
|
|
16
17
|
const jsx = patch.bind(react_jsx_runtime.default.jsx);
|
|
17
18
|
const jsxs = patch.bind(react_jsx_runtime.default.jsxs);
|
|
19
|
+
function Render(props, props2) {
|
|
20
|
+
const { is, ...rest } = {
|
|
21
|
+
...props,
|
|
22
|
+
...props2
|
|
23
|
+
};
|
|
24
|
+
const ambient = _expressive_state.Context.use();
|
|
25
|
+
const state = src_state.Pragma.useState(() => {
|
|
26
|
+
const instance = this.new(rest, is && ((x) => void is(x)));
|
|
27
|
+
const context = ambient.push(instance);
|
|
28
|
+
let ready;
|
|
29
|
+
let active;
|
|
30
|
+
(0, _expressive_state.watch)(instance, (current) => {
|
|
31
|
+
active = current;
|
|
32
|
+
if (ready) state[1]((x) => x.bind(null));
|
|
33
|
+
});
|
|
34
|
+
function didMount() {
|
|
35
|
+
ready = true;
|
|
36
|
+
return () => {
|
|
37
|
+
context.pop();
|
|
38
|
+
instance.set(null);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function Render(props) {
|
|
42
|
+
const render = _expressive_state.METHOD.get(active.render) || active.render;
|
|
43
|
+
return render ? render.call(active, props, active) : props.children;
|
|
44
|
+
}
|
|
45
|
+
return (props) => {
|
|
46
|
+
ready = false;
|
|
47
|
+
src_state.Pragma.useEffect(didMount, []);
|
|
48
|
+
Promise.resolve(instance.set(props)).finally(() => {
|
|
49
|
+
ready = true;
|
|
50
|
+
});
|
|
51
|
+
return require_context.provide(context, src_state.Pragma.createElement(Render, props), props.fallback || active.fallback, String(instance));
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
return state[0](rest);
|
|
55
|
+
}
|
|
18
56
|
|
|
19
57
|
//#endregion
|
|
20
58
|
Object.defineProperty(exports, 'Fragment', {
|
package/dist/jsx-runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsx-runtime.js","names":["State","
|
|
1
|
+
{"version":3,"file":"jsx-runtime.js","names":["State","Runtime","Context","Pragma","METHOD","provide"],"sources":["../src/jsx-runtime.ts"],"sourcesContent":["import { State, Context, watch, METHOD } from '@expressive/state';\n\nimport Runtime from 'react/jsx-runtime';\nimport React from 'react';\n\nimport { Pragma } from './state';\nimport { provide } from './context';\n\ntype Reserved = keyof State | 'render' | 'fallback';\n\ntype StateProps<T extends State> = {\n [K in Exclude<keyof T, Reserved>]?: T[K];\n};\n\ntype ComponentProps<T extends State> = StateProps<T> & {\n is?: (instance: T) => void;\n fallback?: React.ReactNode;\n};\n\ntype ValidProps<T extends State> = StateProps<T> & {\n is?: never;\n get?: never;\n set?: never;\n};\n\ninterface AsComponent extends State {\n render?(props: ValidProps<this>, self: this): React.ReactNode;\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\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\nfunction 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) || active.render;\n\n return render\n ? render.call(active, props as StateProps<T>, active)\n : props.children;\n }\n\n return (props: Props<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 { Fragment } from 'react';\n"],"mappings":";;;;;;;;;;AAiEA,MAAM,yBAAS,IAAI,SAAwC;AAE3D,SAAgB,MAEd,MACA,GAAG,MACiB;AACpB,KAAIA,wBAAM,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,KAAKC,0BAAQ,IAAI;AAC1C,MAAa,OAAO,MAAM,KAAKA,0BAAQ,KAAK;AAE5C,SAAS,OAEP,OACA,QACA;CACA,MAAM,EAAE,IAAI,GAAG,SAAS;EAAE,GAAG;EAAO,GAAG;EAAQ;CAE/C,MAAM,UAAUC,0BAAQ,KAAK;CAC7B,MAAM,QAAQC,iBAAO,eAAoC;EACvD,MAAM,WAAW,KAAK,IAAI,MAAY,QAAQ,MAAM,KAAK,GAAG,EAAE,EAAE;EAChE,MAAM,UAAU,QAAQ,KAAK,SAAS;EAEtC,IAAI;EACJ,IAAI;AAEJ,+BAAM,WAAW,YAAY;AAC3B,YAAS;AAET,OAAI,MAAO,OAAM,IAAI,MAAM,EAAE,KAAK,KAAK,CAAC;IACxC;EAEF,SAAS,WAAW;AAClB,WAAQ;AACR,gBAAa;AACX,YAAQ,KAAK;AACb,aAAS,IAAI,KAAK;;;EAItB,SAAS,OAAO,OAAiB;GAC/B,MAAM,SAASC,yBAAO,IAAI,OAAO,OAAO,IAAI,OAAO;AAEnD,UAAO,SACH,OAAO,KAAK,QAAQ,OAAwB,OAAO,GACnD,MAAM;;AAGZ,UAAQ,UAAoB;AAC1B,WAAQ;AAER,oBAAO,UAAU,UAAU,EAAE,CAAC;AAC9B,WAAQ,QAAQ,SAAS,IAAI,MAAY,CAAC,CAAC,cAAc;AACvD,YAAQ;KACR;AAEF,UAAOC,wBACL,SACAF,iBAAO,cAAc,QAAQ,MAAa,EAC1C,MAAM,YAAY,OAAO,UACzB,OAAO,SAAS,CACjB;;GAEH;AAEF,QAAO,MAAM,GAAG,KAAK"}
|
package/dist/state.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { State } from "@expressive/mvc";
|
|
1
|
+
import { Context, State } from "@expressive/state";
|
|
3
2
|
import { ReactNode } from "react";
|
|
4
3
|
|
|
5
4
|
//#region src/state.d.ts
|
|
@@ -35,6 +34,39 @@ type GetEffect<T extends State> = (this: T, current: T, refresh: ForceRefresh) =
|
|
|
35
34
|
type UseArgs<T extends State> = T extends {
|
|
36
35
|
use(...props: infer P): any;
|
|
37
36
|
} ? P : State.Args<T>;
|
|
37
|
+
type Props<T extends State> = { [K in Exclude<keyof T, keyof Component>]?: T[K] };
|
|
38
|
+
type Render<T extends State, P extends object> = ((props: P & Props<T>, self: T) => ReactNode) & PropsValid<P, T>;
|
|
39
|
+
type Overlap<P extends object, V extends object> = Extract<keyof P, keyof V>;
|
|
40
|
+
type PropsConflicting<P extends object, V extends object> = { [K in Overlap<P, V>]: [P[K]] extends [V[K]] ? never : K }[Overlap<P, V>];
|
|
41
|
+
type PropsValid<P extends object, T extends State> = [PropsConflicting<P, Props<T>>] extends [never] ? unknown : never;
|
|
42
|
+
type ComponentProps<T extends State> = Readonly<Props<T> & {
|
|
43
|
+
/**
|
|
44
|
+
* Callback for newly created instance. Only called once.
|
|
45
|
+
* @returns Callback to run when instance is destroyed.
|
|
46
|
+
*/
|
|
47
|
+
is?: (instance: T) => void;
|
|
48
|
+
/**
|
|
49
|
+
* A fallback react tree to show when suspended.
|
|
50
|
+
* If not provided, `fallback` property of the State will be used.
|
|
51
|
+
*/
|
|
52
|
+
fallback?: React.ReactNode;
|
|
53
|
+
/**
|
|
54
|
+
* Children to render within component. Will be passed as `children` prop.
|
|
55
|
+
*/
|
|
56
|
+
children?: React.ReactNode;
|
|
57
|
+
}>;
|
|
58
|
+
interface Component<P = {}> extends ReactState {
|
|
59
|
+
readonly props: ComponentProps<this> & P;
|
|
60
|
+
context: Context;
|
|
61
|
+
state: State.Values<this>;
|
|
62
|
+
fallback?: ReactNode;
|
|
63
|
+
render(): ReactNode;
|
|
64
|
+
/** @deprecated Only for React JSX compatibility in typescript and nonfunctional. */
|
|
65
|
+
setState: (state: any, callback?: () => void) => void;
|
|
66
|
+
/** @deprecated Only for React JSX compatibility in typescript and nonfunctional. */
|
|
67
|
+
forceUpdate: (callback?: () => void) => void;
|
|
68
|
+
}
|
|
69
|
+
type ComponentType<T, P = {}> = State.Type<T & Component<P>>;
|
|
38
70
|
declare namespace ReactState {
|
|
39
71
|
export import Extends = State.Extends;
|
|
40
72
|
export import Type = State.Type;
|
|
@@ -53,7 +85,7 @@ declare namespace ReactState {
|
|
|
53
85
|
export import Ref = State.Ref;
|
|
54
86
|
export import Effect = State.Effect;
|
|
55
87
|
export import EffectCallback = State.EffectCallback;
|
|
56
|
-
export { GetFactory, GetEffect, UseArgs,
|
|
88
|
+
export { GetFactory, GetEffect, UseArgs, Props, Component };
|
|
57
89
|
}
|
|
58
90
|
declare abstract class ReactState extends State {
|
|
59
91
|
/**
|
|
@@ -80,9 +112,11 @@ declare abstract class ReactState extends State {
|
|
|
80
112
|
static get<T extends State>(this: State.Extends<T>, requireValues: true): Required<T>;
|
|
81
113
|
static get<T extends State, R>(this: State.Extends<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;
|
|
82
114
|
static get<T extends State>(this: State.Extends<T>, factory: GetEffect<T>): null;
|
|
83
|
-
static as<T extends
|
|
115
|
+
static as<T extends State, P extends object = {}>(this: State.Type<T>, render: Render<T, P>): ComponentType<T, P>;
|
|
116
|
+
static as<T extends State, P extends object = {}>(this: ComponentType<T, P>, withProps: Props<T>): ComponentType<T, P>;
|
|
117
|
+
static as<T extends State>(this: State.Type<T>, withProps: Props<T>): ComponentType<T, {}>;
|
|
84
118
|
}
|
|
85
|
-
declare function Render<T extends
|
|
119
|
+
declare function Render<T extends Component, P extends State.Assign<T>>(this: T, render: (props: P, self: T) => ReactNode): ReactNode;
|
|
86
120
|
//#endregion
|
|
87
|
-
export {
|
|
121
|
+
export { Pragma, ReactState };
|
|
88
122
|
//# sourceMappingURL=state.d.ts.map
|
package/dist/state.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_chunk = require('./chunk-BE-pF4vm.js');
|
|
3
|
+
const require_context = require('./context-DEag_HVs.js');
|
|
4
|
+
let _expressive_state = require("@expressive/state");
|
|
4
5
|
|
|
5
6
|
//#region src/state.ts
|
|
6
7
|
const Pragma = {};
|
|
7
|
-
|
|
8
|
+
const OUTER = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
const PROPS = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
var ReactState = class ReactState extends _expressive_state.State {
|
|
8
11
|
/**
|
|
9
12
|
* Create and manage instance of this State within React component.
|
|
10
13
|
*
|
|
@@ -12,7 +15,7 @@ var ReactState = class ReactState extends _expressive_mvc.State {
|
|
|
12
15
|
* @returns Managed instance of this State.
|
|
13
16
|
*/
|
|
14
17
|
static use(...args) {
|
|
15
|
-
const ambient =
|
|
18
|
+
const ambient = _expressive_state.Context.use();
|
|
16
19
|
const state = Pragma.useState(() => {
|
|
17
20
|
let ready;
|
|
18
21
|
let active;
|
|
@@ -24,7 +27,7 @@ var ReactState = class ReactState extends _expressive_mvc.State {
|
|
|
24
27
|
} else return args;
|
|
25
28
|
});
|
|
26
29
|
const context = ambient.push(instance);
|
|
27
|
-
(0,
|
|
30
|
+
(0, _expressive_state.watch)(instance, (current) => {
|
|
28
31
|
active = current;
|
|
29
32
|
if (ready) state[1]((x) => x.bind(null));
|
|
30
33
|
});
|
|
@@ -47,7 +50,7 @@ var ReactState = class ReactState extends _expressive_mvc.State {
|
|
|
47
50
|
return state[0](...args);
|
|
48
51
|
}
|
|
49
52
|
static get(argument) {
|
|
50
|
-
const context =
|
|
53
|
+
const context = _expressive_state.Context.use();
|
|
51
54
|
const state = Pragma.useState(() => {
|
|
52
55
|
const instance = context.get(this);
|
|
53
56
|
if (!instance) if (argument === false) return () => void 0;
|
|
@@ -62,7 +65,7 @@ var ReactState = class ReactState extends _expressive_mvc.State {
|
|
|
62
65
|
render();
|
|
63
66
|
if (action) return action.finally(render);
|
|
64
67
|
}
|
|
65
|
-
const unwatch = (0,
|
|
68
|
+
const unwatch = (0, _expressive_state.watch)(instance, (current) => {
|
|
66
69
|
if (typeof argument === "function") {
|
|
67
70
|
const next = argument.call(current, current, refresh);
|
|
68
71
|
if (next === value) return;
|
|
@@ -95,54 +98,79 @@ var ReactState = class ReactState extends _expressive_mvc.State {
|
|
|
95
98
|
});
|
|
96
99
|
return state[0]();
|
|
97
100
|
}
|
|
98
|
-
static as(
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
static as(argument) {
|
|
102
|
+
const Type = this;
|
|
103
|
+
const render = typeof argument === "function" ? argument : void 0;
|
|
104
|
+
class ReactType extends Type {
|
|
105
|
+
static {
|
|
106
|
+
this.contextType = require_context.Layers;
|
|
107
|
+
}
|
|
108
|
+
get props() {
|
|
109
|
+
return PROPS.get(this);
|
|
110
|
+
}
|
|
111
|
+
set props(props) {
|
|
112
|
+
PROPS.set(this, props);
|
|
113
|
+
this.set(props);
|
|
114
|
+
}
|
|
115
|
+
get context() {
|
|
116
|
+
return _expressive_state.Context.get(this);
|
|
117
|
+
}
|
|
118
|
+
set context(context) {
|
|
119
|
+
if (OUTER.get(this) === context) return;
|
|
120
|
+
OUTER.set(this, context);
|
|
121
|
+
context.push(this);
|
|
122
|
+
}
|
|
123
|
+
get state() {
|
|
124
|
+
return this.get();
|
|
125
|
+
}
|
|
126
|
+
set state(_state) {}
|
|
127
|
+
constructor(nextProps, ...rest) {
|
|
128
|
+
const { is } = nextProps;
|
|
129
|
+
const defaults = typeof argument === "object" ? argument : {};
|
|
130
|
+
if (rest[0] instanceof _expressive_state.Context) rest.shift();
|
|
131
|
+
super(nextProps, defaults, is, rest);
|
|
132
|
+
this.fallback = void 0;
|
|
133
|
+
PROPS.set(this, nextProps);
|
|
134
|
+
if (render) {
|
|
135
|
+
const Self = Render.bind(this, render);
|
|
136
|
+
this.render = () => Pragma.createElement(Self);
|
|
137
|
+
} else if (!this.render) this.render = () => require_context.provide(this.context, this.props.children || null, this.props.fallback, String(this));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
Object.defineProperty(ReactType, "name", { value: "React" + this.name });
|
|
141
|
+
Object.defineProperty(ReactType.prototype, "isReactComponent", { get: () => true });
|
|
142
|
+
return ReactType;
|
|
104
143
|
}
|
|
105
144
|
};
|
|
106
|
-
function Render(
|
|
107
|
-
const { is, ...rest } = {
|
|
108
|
-
...props,
|
|
109
|
-
...props2
|
|
110
|
-
};
|
|
111
|
-
const ambient = _expressive_mvc.Context.use();
|
|
145
|
+
function Render(render) {
|
|
112
146
|
const state = Pragma.useState(() => {
|
|
113
|
-
|
|
114
|
-
const context =
|
|
147
|
+
(0, _expressive_state.event)(this);
|
|
148
|
+
const { context } = this;
|
|
115
149
|
let ready;
|
|
116
150
|
let active;
|
|
117
|
-
(0,
|
|
151
|
+
(0, _expressive_state.watch)(this, (current) => {
|
|
118
152
|
active = current;
|
|
119
153
|
if (ready) state[1]((x) => x.bind(null));
|
|
120
154
|
});
|
|
121
|
-
|
|
155
|
+
const didMount = () => {
|
|
122
156
|
ready = true;
|
|
123
157
|
return () => {
|
|
124
158
|
context.pop();
|
|
125
|
-
|
|
159
|
+
this.set(null);
|
|
126
160
|
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return render ? render.call(active, props, active) : props.children;
|
|
131
|
-
}
|
|
132
|
-
return (props) => {
|
|
161
|
+
};
|
|
162
|
+
const View = () => render.call(active, this.props, active);
|
|
163
|
+
return () => {
|
|
133
164
|
ready = false;
|
|
134
165
|
Pragma.useEffect(didMount, []);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
});
|
|
138
|
-
return require_context.provide(context, Pragma.createElement(Render, props), props.fallback || active.fallback, String(instance));
|
|
166
|
+
setTimeout(() => ready = true, 0);
|
|
167
|
+
return require_context.provide(context, Pragma.createElement(View), this.props.fallback, String(this));
|
|
139
168
|
};
|
|
140
169
|
});
|
|
141
|
-
return state[0](
|
|
170
|
+
return state[0]();
|
|
142
171
|
}
|
|
143
172
|
|
|
144
173
|
//#endregion
|
|
145
174
|
exports.Pragma = Pragma;
|
|
146
175
|
exports.ReactState = ReactState;
|
|
147
|
-
exports.Render = Render;
|
|
148
176
|
//# sourceMappingURL=state.js.map
|
package/dist/state.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","names":["State","Context","METHOD","provide"],"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":";;;;;AAKA,MAAa,SAAS,EAAE;AAyExB,IAAe,aAAf,MAAe,mBAAmBA,sBAAM;;;;;;;CAiBtC,OAAO,IAEL,GAAG,MACA;EACH,MAAM,UAAUC,wBAAQ,KAAK;EAC7B,MAAM,QAAQ,OAAO,eAAe;GAClC,IAAI;GACJ,IAAI;GAEJ,IAAI,OAAO,GAAG,SACZ,QAAQ,IACN,KACG,MAAM,CACN,KACE,QACC,OAAO,OAAO,YAAY,SAAS,IAAI,IAAuB,CACjE,CACJ;GAGH,MAAM,WAAW,IAAI,MAAM,MAAM;AAC/B,QAAI,aAAa,cAAc,EAAE,KAAK;AACpC,WAAM,EAAE,KAAK,KAAK,EAAE;AACpB,SAAI,GAAG,KAAK;UACP,QAAO;KACd;GAEF,MAAM,UAAU,QAAQ,KAAK,SAAS;AAEtC,8BAAM,WAAW,YAAY;AAC3B,aAAS;AAET,QAAI,MAAO,OAAM,IAAI,MAAM,EAAE,KAAK,KAAK,CAAC;KACxC;GAEF,SAAS,WAAW;AAClB,YAAQ;AACR,iBAAa;AACX,aAAQ,KAAK;AACb,cAAS,IAAI,KAAK;;;AAItB,WAAQ,GAAG,SAAwB;AACjC,WAAO,UAAU,UAAU,EAAE,CAAC;AAE9B,QAAI,OAAO;AACT,aAAQ;AAER,aAAQ,QAAQ,IAAI,GAAG,KAAK,CAAC,CAAC,cAAe,QAAQ,KAAM;;AAG7D,WAAO;;IAET;AAEF,SAAO,MAAM,GAAG,GAAG,KAAK;;CA6B1B,OAAO,IAEL,UACA;EACA,MAAM,UAAUA,wBAAQ,KAAK;EAC7B,MAAM,QAAQ,OAAO,eAAe;GAClC,MAA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AAK;CAC7B,MAAM,QAAQ,OAAO,eAAoC;EACvD,MAAM,WAAW,KAAK,IAAI,MAAY,QAAQ,MAAM,KAAK,GAAG,EAAE,EAAE;EAChE,MAAM,UAAU,QAAQ,KAAK,SAAS;EAEtC,IAAI;EACJ,IAAI;AAEJ,6BAAM,WAAW,YAAY;AAC3B,YAAS;AAET,OAAI,MAAO,OAAM,IAAI,MAAM,EAAE,KAAK,KAAK,CAAC;IACxC;EAEF,SAAS,WAAW;AAClB,WAAQ;AACR,gBAAa;AACX,YAAQ,KAAK;AACb,aAAS,IAAI,KAAK;;;EAItB,SAAS,OAAO,OAAiB;GAC/B,MAAM,SAASC,uBAAO,IAAI,OAAO,OAAO,IAAI,MAAM,UAAU,OAAO;AAEnE,UAAO,SACH,OAAO,KAAK,QAAQ,OAAsB,OAAO,GACjD,MAAM;;AAGZ,UAAQ,UAA0B;AAChC,WAAQ;AAER,UAAO,UAAU,UAAU,EAAE,CAAC;AAC9B,WAAQ,QAAQ,SAAS,IAAI,MAAY,CAAC,CAAC,cAAc;AACvD,YAAQ;KACR;AAEF,UAAOC,wBACL,SACA,OAAO,cAAc,QAAQ,MAAa,EAC1C,MAAM,YAAY,OAAO,UACzB,OAAO,SAAS,CACjB;;GAEH;AAEF,QAAO,MAAM,GAAG,KAAK"}
|
|
1
|
+
{"version":3,"file":"state.js","names":["State","Context","Layers","provide"],"sources":["../src/state.ts"],"sourcesContent":["import { State, Context, watch, event } from '@expressive/state';\nimport { ReactNode } from 'react';\nimport { provide, Layers } from './context';\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\nconst OUTER = new WeakMap<object, Context>();\nconst PROPS = new WeakMap<object, ComponentProps<any>>();\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\ntype Props<T extends State> = {\n [K in Exclude<keyof T, keyof Component>]?: T[K];\n};\n\ntype Render<T extends State, P extends object> = ((\n props: P & Props<T>,\n self: T\n) => ReactNode) &\n PropsValid<P, T>;\n\ntype Overlap<P extends object, V extends object> = Extract<keyof P, keyof V>;\n\ntype PropsConflicting<P extends object, V extends object> = {\n [K in Overlap<P, V>]: [P[K]] extends [V[K]] ? never : K;\n}[Overlap<P, V>];\n\ntype PropsValid<P extends object, T extends State> = [\n PropsConflicting<P, Props<T>>\n] extends [never]\n ? unknown\n : never;\n\ntype ComponentProps<T extends State> = Readonly<\n Props<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 * Children to render within component. Will be passed as `children` prop.\n */\n children?: React.ReactNode;\n }\n>;\n\ninterface Component<P = {}> extends ReactState {\n readonly props: ComponentProps<this> & P;\n context: Context;\n state: State.Values<this>;\n fallback?: ReactNode;\n\n render(): ReactNode;\n\n /** @deprecated Only for React JSX compatibility in typescript and nonfunctional. */\n setState: (state: any, callback?: () => void) => void;\n\n /** @deprecated Only for React JSX compatibility in typescript and nonfunctional. */\n forceUpdate: (callback?: () => void) => void;\n}\n\ntype ComponentType<T, P = {}> = State.Type<T & Component<P>>;\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, Props, Component };\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 State, P extends object = {}>(\n this: State.Type<T>,\n render: Render<T, P>\n ): ComponentType<T, P>;\n\n static as<T extends State, P extends object = {}>(\n this: ComponentType<T, P>,\n withProps: Props<T>\n ): ComponentType<T, P>;\n\n static as<T extends State>(\n this: State.Type<T>,\n withProps: Props<T>\n ): ComponentType<T, {}>;\n\n static as<T extends State, P>(\n this: State.Type<T>,\n argument: ((props: P, self: T) => ReactNode) | Props<T>\n ) {\n const Type = this as unknown as State.Type<State>;\n const render = typeof argument === 'function' ? argument : undefined;\n\n class ReactType extends Type {\n static contextType = Layers;\n\n get props(): ComponentProps<this> {\n return PROPS.get(this)!;\n }\n\n private set props(props: ComponentProps<this>) {\n PROPS.set(this, props);\n this.set(props as {});\n }\n\n get context() {\n return Context.get(this)!;\n }\n\n private set context(context: Context) {\n if (OUTER.get(this) === context) return;\n\n OUTER.set(this, context);\n context.push(this);\n }\n\n get state() {\n return this.get();\n }\n\n private set state(_state: State.Values<this>) {}\n\n render!: () => ReactNode;\n fallback?: ReactNode = undefined;\n\n constructor(nextProps: any, ...rest: any[]) {\n const { is } = nextProps;\n const defaults = typeof argument === 'object' ? argument : {};\n\n if (rest[0] instanceof Context) rest.shift();\n\n super(nextProps, defaults, is, rest);\n PROPS.set(this, nextProps);\n\n if (render) {\n const Self = Render.bind(this, render as any);\n this.render = () => Pragma.createElement(Self);\n } else if (!this.render) {\n this.render = () =>\n provide(\n this.context,\n this.props.children || null,\n this.props.fallback,\n String(this)\n );\n }\n }\n\n /** @deprecated Only for React JSX compatibility in typescript and nonfunctional. */\n setState!: (state: any, callback?: () => void) => void;\n\n /** @deprecated Only for React JSX compatibility in typescript and nonfunctional. */\n forceUpdate!: (callback?: () => void) => void;\n }\n\n Object.defineProperty(ReactType, 'name', { value: 'React' + this.name });\n Object.defineProperty(ReactType.prototype, 'isReactComponent', {\n get: () => true\n });\n\n return ReactType;\n }\n}\n\nfunction Render<T extends Component, P extends State.Assign<T>>(\n this: T,\n render: (props: P, self: T) => ReactNode\n) {\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 as T;\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 View = () => render.call(active, this.props as any, 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(View),\n this.props.fallback,\n String(this)\n );\n };\n });\n\n return state[0]();\n}\n\nexport { ReactState };\n"],"mappings":";;;;;;AAIA,MAAa,SAAS,EAAE;AASxB,MAAM,wBAAQ,IAAI,SAA0B;AAC5C,MAAM,wBAAQ,IAAI,SAAsC;AA8HxD,IAAe,aAAf,MAAe,mBAAmBA,wBAAM;;;;;;;CAiBtC,OAAO,IAEL,GAAG,MACA;EACH,MAAM,UAAUC,0BAAQ,KAAK;EAC7B,MAAM,QAAQ,OAAO,eAAe;GAClC,IAAI;GACJ,IAAI;GAEJ,IAAI,OAAO,GAAG,SACZ,QAAQ,IACN,KACG,MAAM,CACN,KACE,QACC,OAAO,OAAO,YAAY,SAAS,IAAI,IAAuB,CACjE,CACJ;GAGH,MAAM,WAAW,IAAI,MAAM,MAAM;AAC/B,QAAI,aAAa,cAAc,EAAE,KAAK;AACpC,WAAM,EAAE,KAAK,KAAK,EAAE;AACpB,SAAI,GAAG,KAAK;UACP,QAAO;KACd;GAEF,MAAM,UAAU,QAAQ,KAAK,SAAS;AAEtC,gCAAM,WAAW,YAAY;AAC3B,aAAS;AAET,QAAI,MAAO,OAAM,IAAI,MAAM,EAAE,KAAK,KAAK,CAAC;KACxC;GAEF,SAAS,WAAW;AAClB,YAAQ;AACR,iBAAa;AACX,aAAQ,KAAK;AACb,cAAS,IAAI,KAAK;;;AAItB,WAAQ,GAAG,SAAwB;AACjC,WAAO,UAAU,UAAU,EAAE,CAAC;AAE9B,QAAI,OAAO;AACT,aAAQ;AAER,aA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K,KAAK,CAAC;IACxC;EAEF,MAAM,iBAAiB;AACrB,WAAQ;AACR,gBAAa;AACX,YAAQ,KAAK;AACb,SAAK,IAAI,KAAK;;;EAIlB,MAAM,aAAa,OAAO,KAAK,QAAQ,KAAK,OAAc,OAAO;AAEjE,eAAa;AACX,WAAQ;AAER,UAAO,UAAU,UAAU,EAAE,CAAC;AAC9B,oBAAkB,QAAQ,MAAO,EAAE;AAEnC,UAAOA,wBACL,SACA,OAAO,cAAc,KAAK,EAC1B,KAAK,MAAM,UACX,OAAO,KAAK,CACb;;GAEH;AAEF,QAAO,MAAM,IAAI"}
|
package/package.json
CHANGED
|
@@ -7,14 +7,17 @@
|
|
|
7
7
|
"url": "https://github.com/gabeklein"
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
|
-
"repository":
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/gabeklein/expressive-state.git"
|
|
13
|
+
},
|
|
11
14
|
"publishConfig": {
|
|
12
15
|
"access": "public"
|
|
13
16
|
},
|
|
14
17
|
"keywords": [
|
|
15
18
|
"expressive",
|
|
16
19
|
"react",
|
|
17
|
-
"
|
|
20
|
+
"state"
|
|
18
21
|
],
|
|
19
22
|
"main": "dist/index.js",
|
|
20
23
|
"types": "dist/index.d.ts",
|
|
@@ -46,11 +49,11 @@
|
|
|
46
49
|
],
|
|
47
50
|
"scripts": {
|
|
48
51
|
"build": "tsdown",
|
|
49
|
-
"test": "tsc --noEmit &&
|
|
52
|
+
"test": "tsc --noEmit && vitest run --coverage",
|
|
50
53
|
"preversion": "npm run test && npm run build"
|
|
51
54
|
},
|
|
52
55
|
"dependencies": {
|
|
53
|
-
"@expressive/
|
|
56
|
+
"@expressive/state": "^0.74.0"
|
|
54
57
|
},
|
|
55
58
|
"peerDependencies": {
|
|
56
59
|
"react": ">=16.8.0 <20.0.0"
|
|
@@ -61,15 +64,16 @@
|
|
|
61
64
|
}
|
|
62
65
|
},
|
|
63
66
|
"devDependencies": {
|
|
64
|
-
"@testing-library/jest-dom": "^6.8.0",
|
|
65
67
|
"@testing-library/react": "^16.3.0",
|
|
66
68
|
"@types/react": "^19",
|
|
67
69
|
"@types/react-dom": "^19",
|
|
68
|
-
"
|
|
70
|
+
"@vitest/browser": "^4.0.18",
|
|
69
71
|
"react": "^19",
|
|
70
72
|
"react-dom": "^19",
|
|
71
|
-
"tsdown": "^0.20.1"
|
|
73
|
+
"tsdown": "^0.20.1",
|
|
74
|
+
"tsup": "^8.0.2",
|
|
75
|
+
"vitest": "^4.0.18"
|
|
72
76
|
},
|
|
73
|
-
"version": "0.
|
|
74
|
-
"gitHead": "
|
|
77
|
+
"version": "0.74.1",
|
|
78
|
+
"gitHead": "050f3959c0294d4223a39b4f42b7831c1962d353"
|
|
75
79
|
}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/esm/component.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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.fall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