@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.
@@ -1,4 +1,4 @@
1
- //#region rolldown:runtime
1
+ //#region \0rolldown/runtime.js
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -1,10 +1,10 @@
1
- const require_chunk = require('./chunk-kSYXY2_d.js');
2
- let _expressive_mvc = require("@expressive/mvc");
1
+ const require_chunk = require('./chunk-BE-pF4vm.js');
2
+ let _expressive_state = require("@expressive/state");
3
3
  let react = require("react");
4
4
 
5
5
  //#region src/context.ts
6
- const Layers = (0, react.createContext)(new _expressive_mvc.Context());
7
- _expressive_mvc.Context.use = (create) => {
6
+ const Layers = (0, react.createContext)(new _expressive_state.Context());
7
+ _expressive_state.Context.use = (create) => {
8
8
  const ambient = (0, react.useContext)(Layers);
9
9
  return create ? (0, react.useMemo)(() => ambient.push(), [ambient]) : ambient;
10
10
  };
@@ -12,7 +12,7 @@ function Consumer(props) {
12
12
  return props.for.get((i) => props.children(i));
13
13
  }
14
14
  function Provider(props) {
15
- const context = _expressive_mvc.Context.use(true);
15
+ const context = _expressive_state.Context.use(true);
16
16
  (0, react.useEffect)(() => () => context.pop(), [context]);
17
17
  context.use(props.for, (state) => {
18
18
  if (props.forEach) {
@@ -59,4 +59,4 @@ Object.defineProperty(exports, 'provide', {
59
59
  return provide;
60
60
  }
61
61
  });
62
- //# sourceMappingURL=context-DpbK9_2_.js.map
62
+ //# sourceMappingURL=context-DEag_HVs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-DEag_HVs.js","names":["Context","Suspense"],"sources":["../src/context.ts"],"sourcesContent":["import State, { Context } from '@expressive/state';\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/state' {\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,2BAAS,CAAC;AASlD,0BAAQ,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,0BAAQ,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"}
@@ -1,4 +1,4 @@
1
- import { Context } from "@expressive/mvc";
1
+ import { Context } from "@expressive/state";
2
2
  import { Suspense, createContext, createElement, useContext, useEffect, useMemo } from "react";
3
3
 
4
4
  //#region src/context.ts
@@ -1 +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"}
1
+ {"version":3,"file":"context.js","names":[],"sources":["../../src/context.ts"],"sourcesContent":["import State, { Context } from '@expressive/state';\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/state' {\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,6 +1,5 @@
1
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";
2
+ import { Context, Observable, State, get, ref, set, use } from "@expressive/state";
4
3
  import { Fragment, createElement, createElement as createElement$1, useEffect, useState } from "react";
5
4
  import { Pragma, ReactState } from "./state";
6
5
 
@@ -13,5 +12,5 @@ State.use = ReactState.use;
13
12
  State.as = ReactState.as;
14
13
 
15
14
  //#endregion
16
- export { Component, Consumer, Context, Fragment, Observable, Pragma, Provider, ReactState as State, ReactState as default, createElement, get, ref, set, use };
15
+ export { Consumer, Context, Fragment, Observable, Pragma, Provider, ReactState as State, ReactState as default, createElement, get, ref, set, use };
17
16
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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
+ {"version":3,"file":"index.js","names":["createElement"],"sources":["../../src/index.ts"],"sourcesContent":["import { State } from '@expressive/state';\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/state` directly.\n */\ndeclare module '@expressive/state' {\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/state';\nexport { Consumer, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { Pragma };\n"],"mappings":";;;;;;AAMA,OAAO,YAAY;AACnB,OAAO,WAAW;AAClB,OAAO,gBAAgBA;AAgBvB,MAAM,MAAM,WAAW;AACvB,MAAM,MAAM,WAAW;AACvB,MAAM,KAAK,WAAW"}
@@ -1,17 +1,55 @@
1
- import State$1 from "@expressive/mvc";
1
+ import { provide } from "./context.js";
2
+ import { Context, METHOD, State, watch } from "@expressive/state";
2
3
  import { Fragment } from "react";
3
- import { Render } from "./state";
4
+ import { Pragma } from "./state";
4
5
  import Runtime from "react/jsx-runtime";
5
6
 
6
7
  //#region src/jsx-runtime.ts
7
8
  const RENDER = /* @__PURE__ */ new WeakMap();
8
9
  function patch(type, ...args) {
9
- if (State$1.is(type)) if (RENDER.has(type)) type = RENDER.get(type);
10
+ if (State.is(type)) if (RENDER.has(type)) type = RENDER.get(type);
10
11
  else RENDER.set(type, type = Render.bind(type));
11
12
  return this(type, ...args);
12
13
  }
13
14
  const jsx = patch.bind(Runtime.jsx);
14
15
  const jsxs = patch.bind(Runtime.jsxs);
16
+ function Render(props, props2) {
17
+ const { is, ...rest } = {
18
+ ...props,
19
+ ...props2
20
+ };
21
+ const ambient = Context.use();
22
+ const state = Pragma.useState(() => {
23
+ const instance = this.new(rest, is && ((x) => void is(x)));
24
+ const context = ambient.push(instance);
25
+ let ready;
26
+ let active;
27
+ watch(instance, (current) => {
28
+ active = current;
29
+ if (ready) state[1]((x) => x.bind(null));
30
+ });
31
+ function didMount() {
32
+ ready = true;
33
+ return () => {
34
+ context.pop();
35
+ instance.set(null);
36
+ };
37
+ }
38
+ function Render(props) {
39
+ const render = METHOD.get(active.render) || active.render;
40
+ return render ? render.call(active, props, active) : props.children;
41
+ }
42
+ return (props) => {
43
+ ready = false;
44
+ Pragma.useEffect(didMount, []);
45
+ Promise.resolve(instance.set(props)).finally(() => {
46
+ ready = true;
47
+ });
48
+ return provide(context, Pragma.createElement(Render, props), props.fallback || active.fallback, String(instance));
49
+ };
50
+ });
51
+ return state[0](rest);
52
+ }
15
53
 
16
54
  //#endregion
17
55
  export { Fragment, jsx, jsxs, patch };
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"jsx-runtime.js","names":[],"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,KAAI,MAAM,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;AAE5C,SAAS,OAEP,OACA,QACA;CACA,MAAM,EAAE,IAAI,GAAG,SAAS;EAAE,GAAG;EAAO,GAAG;EAAQ;CAE/C,MAAM,UAAU,QAAQ,KAAK;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,QAAM,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,SAAS,OAAO,IAAI,OAAO,OAAO,IAAI,OAAO;AAEnD,UAAO,SACH,OAAO,KAAK,QAAQ,OAAwB,OAAO,GACnD,MAAM;;AAGZ,UAAQ,UAAoB;AAC1B,WAAQ;AAER,UAAO,UAAU,UAAU,EAAE,CAAC;AAC9B,WAAQ,QAAQ,SAAS,IAAI,MAAY,CAAC,CAAC,cAAc;AACvD,YAAQ;KACR;AAEF,UAAO,QACL,SACA,OAAO,cAAc,QAAQ,MAAa,EAC1C,MAAM,YAAY,OAAO,UACzB,OAAO,SAAS,CACjB;;GAEH;AAEF,QAAO,MAAM,GAAG,KAAK"}
package/dist/esm/state.js CHANGED
@@ -1,8 +1,10 @@
1
- import { provide } from "./context.js";
2
- import { Context, METHOD, State, watch } from "@expressive/mvc";
1
+ import { Layers, provide } from "./context.js";
2
+ import { Context, State, event, watch } from "@expressive/state";
3
3
 
4
4
  //#region src/state.ts
5
5
  const Pragma = {};
6
+ const OUTER = /* @__PURE__ */ new WeakMap();
7
+ const PROPS = /* @__PURE__ */ new WeakMap();
6
8
  var ReactState = class ReactState extends State {
7
9
  /**
8
10
  * Create and manage instance of this State within React component.
@@ -94,52 +96,78 @@ var ReactState = class ReactState extends State {
94
96
  });
95
97
  return state[0]();
96
98
  }
97
- static as(render) {
98
- const FC = Render.bind(this, { render });
99
- return Object.assign(FC, {
100
- displayName: this.name,
101
- State: this
102
- });
99
+ static as(argument) {
100
+ const Type = this;
101
+ const render = typeof argument === "function" ? argument : void 0;
102
+ class ReactType extends Type {
103
+ static {
104
+ this.contextType = Layers;
105
+ }
106
+ get props() {
107
+ return PROPS.get(this);
108
+ }
109
+ set props(props) {
110
+ PROPS.set(this, props);
111
+ this.set(props);
112
+ }
113
+ get context() {
114
+ return Context.get(this);
115
+ }
116
+ set context(context) {
117
+ if (OUTER.get(this) === context) return;
118
+ OUTER.set(this, context);
119
+ context.push(this);
120
+ }
121
+ get state() {
122
+ return this.get();
123
+ }
124
+ set state(_state) {}
125
+ constructor(nextProps, ...rest) {
126
+ const { is } = nextProps;
127
+ const defaults = typeof argument === "object" ? argument : {};
128
+ if (rest[0] instanceof Context) rest.shift();
129
+ super(nextProps, defaults, is, rest);
130
+ this.fallback = void 0;
131
+ PROPS.set(this, nextProps);
132
+ if (render) {
133
+ const Self = Render.bind(this, render);
134
+ this.render = () => Pragma.createElement(Self);
135
+ } else if (!this.render) this.render = () => provide(this.context, this.props.children || null, this.props.fallback, String(this));
136
+ }
137
+ }
138
+ Object.defineProperty(ReactType, "name", { value: "React" + this.name });
139
+ Object.defineProperty(ReactType.prototype, "isReactComponent", { get: () => true });
140
+ return ReactType;
103
141
  }
104
142
  };
105
- function Render(props, props2) {
106
- const { is, ...rest } = {
107
- ...props,
108
- ...props2
109
- };
110
- const ambient = Context.use();
143
+ function Render(render) {
111
144
  const state = Pragma.useState(() => {
112
- const instance = this.new(rest, is && ((x) => void is(x)));
113
- const context = ambient.push(instance);
145
+ event(this);
146
+ const { context } = this;
114
147
  let ready;
115
148
  let active;
116
- watch(instance, (current) => {
149
+ watch(this, (current) => {
117
150
  active = current;
118
151
  if (ready) state[1]((x) => x.bind(null));
119
152
  });
120
- function didMount() {
153
+ const didMount = () => {
121
154
  ready = true;
122
155
  return () => {
123
156
  context.pop();
124
- instance.set(null);
157
+ this.set(null);
125
158
  };
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) => {
159
+ };
160
+ const View = () => render.call(active, this.props, active);
161
+ return () => {
132
162
  ready = false;
133
163
  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));
164
+ setTimeout(() => ready = true, 0);
165
+ return provide(context, Pragma.createElement(View), this.props.fallback, String(this));
138
166
  };
139
167
  });
140
- return state[0](rest);
168
+ return state[0]();
141
169
  }
142
170
 
143
171
  //#endregion
144
- export { Pragma, ReactState, Render };
172
+ export { Pragma, ReactState };
145
173
  //# sourceMappingURL=state.js.map
@@ -1 +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":";;;;AAKA,MAAa,SAAS,EAAE;AAyExB,IAAe,aAAf,MAAe,mBAAmB,MAAM;;;;;;;CAiBtC,OAAO,IAEL,GAAG,MACA;EACH,MAAM,UAAU,QAAQ,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,SAAM,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;;A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KAAK;GAClB,OAAO;GACR,CAAC;;;AAIN,SAAgB,OAEd,OACA,QACA;CACA,MAAM,EAAE,IAAI,GAAG,SAAS;EAAE,GAAG;EAAO,GAAG;EAAQ;CAE/C,MAAM,UAAU,QAAQ,KAAK;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,QAAM,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,SAAS,OAAO,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,UAAO,QACL,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":[],"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,mBAAmB,MAAM;;;;;;;CAiBtC,OAAO,IAEL,GAAG,MACA;EACH,MAAM,UAAU,QAAQ,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,SAAM,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,c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AY;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,aAAa,OAAO,KAAK,QAAQ,KAAK,OAAc,OAAO;AAEjE,eAAa;AACX,WAAQ;AAER,UAAO,UAAU,UAAU,EAAE,CAAC;AAC9B,oBAAkB,QAAQ,MAAO,EAAE;AAEnC,UAAO,QACL,SACA,OAAO,cAAc,KAAK,EAC1B,KAAK,MAAM,UACX,OAAO,KAAK,CACb;;GAEH;AAEF,QAAO,MAAM,IAAI"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,66 @@
1
- import { a as ReactState, c as ref, d as Consumer, f as Provider, i as Pragma, l as set, m as Component, n as Fragment, o as createElement, r as Observable, s as get, t as Context, u as use } from "./index-zVvhW0uD.js";
2
- export { Component, Consumer, Context, Fragment, Observable, Pragma, Provider, ReactState as State, ReactState as default, createElement, get, ref, set, use };
1
+ import { Pragma, ReactState } from "./state";
2
+ import State$1, { Context, Context as Context$1, Observable, get, ref, set, use } from "@expressive/state";
3
+ import * as react from "react";
4
+ import { Fragment, ReactNode, createElement } from "react";
5
+
6
+ //#region src/context.d.ts
7
+ declare module '@expressive/state' {
8
+ namespace Context {
9
+ function use(create?: true): Context$1;
10
+ function use(create: boolean): Context$1 | null | undefined;
11
+ }
12
+ }
13
+ declare namespace Consumer {
14
+ type Props<T extends State$1> = {
15
+ /** Type of controller to fetch from context. */for: State$1.Extends<T>;
16
+ /**
17
+ * Render function, will receive instance of desired controller.
18
+ *
19
+ * Called every render of parent component.
20
+ * Similar to `State.get()`, updates to properties accessed in
21
+ * this function will cause a refresh when they change.
22
+ */
23
+ children: (value: T) => ReactNode | void;
24
+ };
25
+ }
26
+ declare function Consumer<T extends State$1>(props: Consumer.Props<T>): string | number | bigint | boolean | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | react.ReactPortal | null;
27
+ declare namespace Provider {
28
+ interface Props<T extends State$1> {
29
+ /** State or group of States to provide to descendant Consumers. */
30
+ for: Context$1.Accept<T>;
31
+ /**
32
+ * Callback to run for each provided State.
33
+ */
34
+ forEach?: Context$1.Expect<T>;
35
+ /**
36
+ * Children to render within this Provider.
37
+ */
38
+ children?: ReactNode;
39
+ /** A fallback tree to show when suspended. */
40
+ fallback?: ReactNode;
41
+ /**
42
+ * A name for this Suspense boundary for instrumentation purposes.
43
+ * The name will help identify this boundary in React DevTools.
44
+ */
45
+ name?: string | undefined;
46
+ }
47
+ }
48
+ declare function Provider<T extends State$1>(props: Provider.Props<T>): ReactNode;
49
+ //#endregion
50
+ //#region src/index.d.ts
51
+ /**
52
+ * Augmented State namespace to include ReactState hooks as static methods.
53
+ *
54
+ * This retroactively adds React support to agnostic State defined
55
+ * by component libraries which import `@expressive/state` directly.
56
+ */
57
+ declare module '@expressive/state' {
58
+ namespace State {
59
+ export import as = ReactState.as;
60
+ export import get = ReactState.get;
61
+ export import use = ReactState.use;
62
+ }
63
+ }
64
+ //#endregion
65
+ export { Consumer, Context, Fragment, Observable, Pragma, Provider, ReactState as State, ReactState as default, createElement, get, ref, set, use };
66
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,85 +1,24 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
- const require_chunk = require('./chunk-kSYXY2_d.js');
3
- const require_context = require('./context-DpbK9_2_.js');
4
- let _expressive_mvc = require("@expressive/mvc");
1
+ Object.defineProperties(exports, { __esModule: { value: true }, [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");
5
5
  let react = require("react");
6
6
  let src_state = require("./state");
7
7
 
8
- //#region src/component.ts
9
- const OUTER = /* @__PURE__ */ new WeakMap();
10
- var Component = class extends src_state.ReactState {
11
- static {
12
- this.contextType = require_context.Layers;
13
- }
14
- get props() {
15
- return this._props;
16
- }
17
- set props(props) {
18
- this._props = props;
19
- this.set(props);
20
- }
21
- get context() {
22
- return _expressive_mvc.Context.get(this);
23
- }
24
- set context(context) {
25
- if (OUTER.get(this) === context) return;
26
- OUTER.set(this, context);
27
- context.push(this);
28
- }
29
- constructor({ is, ...props }) {
30
- super(props, is);
31
- const self = Render.bind(this, _expressive_mvc.METHOD.get(this.render));
32
- this.render = () => src_state.Pragma.createElement(self);
33
- }
34
- render() {
35
- return typeof this.children === "function" ? this.children(this) : this.children;
36
- }
37
- };
38
- Object.defineProperty(Component.prototype, "isReactComponent", { get: () => true });
39
- function Render(render) {
40
- const state = src_state.Pragma.useState(() => {
41
- (0, _expressive_mvc.event)(this);
42
- const { context } = this;
43
- let ready;
44
- let active;
45
- (0, _expressive_mvc.watch)(this, (current) => {
46
- active = current;
47
- if (ready) state[1]((x) => x.bind(null));
48
- });
49
- const didMount = () => {
50
- ready = true;
51
- return () => {
52
- context.pop();
53
- this.set(null);
54
- };
55
- };
56
- const Render = () => render.call(active);
57
- return () => {
58
- ready = false;
59
- src_state.Pragma.useEffect(didMount, []);
60
- setTimeout(() => ready = true, 0);
61
- return require_context.provide(context, src_state.Pragma.createElement(Render), active.fallback, String(this));
62
- };
63
- });
64
- return state[0]();
65
- }
66
-
67
- //#endregion
68
8
  //#region src/index.ts
69
9
  src_state.Pragma.useEffect = react.useEffect;
70
10
  src_state.Pragma.useState = react.useState;
71
11
  src_state.Pragma.createElement = react.createElement;
72
- _expressive_mvc.State.get = src_state.ReactState.get;
73
- _expressive_mvc.State.use = src_state.ReactState.use;
74
- _expressive_mvc.State.as = src_state.ReactState.as;
12
+ _expressive_state.State.get = src_state.ReactState.get;
13
+ _expressive_state.State.use = src_state.ReactState.use;
14
+ _expressive_state.State.as = src_state.ReactState.as;
75
15
 
76
16
  //#endregion
77
- exports.Component = Component;
78
17
  exports.Consumer = require_context.Consumer;
79
18
  Object.defineProperty(exports, 'Context', {
80
19
  enumerable: true,
81
20
  get: function () {
82
- return _expressive_mvc.Context;
21
+ return _expressive_state.Context;
83
22
  }
84
23
  });
85
24
  Object.defineProperty(exports, 'Fragment', {
@@ -91,7 +30,7 @@ Object.defineProperty(exports, 'Fragment', {
91
30
  Object.defineProperty(exports, 'Observable', {
92
31
  enumerable: true,
93
32
  get: function () {
94
- return _expressive_mvc.Observable;
33
+ return _expressive_state.Observable;
95
34
  }
96
35
  });
97
36
  exports.Pragma = src_state.Pragma;
@@ -107,25 +46,25 @@ Object.defineProperty(exports, 'createElement', {
107
46
  Object.defineProperty(exports, 'get', {
108
47
  enumerable: true,
109
48
  get: function () {
110
- return _expressive_mvc.get;
49
+ return _expressive_state.get;
111
50
  }
112
51
  });
113
52
  Object.defineProperty(exports, 'ref', {
114
53
  enumerable: true,
115
54
  get: function () {
116
- return _expressive_mvc.ref;
55
+ return _expressive_state.ref;
117
56
  }
118
57
  });
119
58
  Object.defineProperty(exports, 'set', {
120
59
  enumerable: true,
121
60
  get: function () {
122
- return _expressive_mvc.set;
61
+ return _expressive_state.set;
123
62
  }
124
63
  });
125
64
  Object.defineProperty(exports, 'use', {
126
65
  enumerable: true,
127
66
  get: function () {
128
- return _expressive_mvc.use;
67
+ return _expressive_state.use;
129
68
  }
130
69
  });
131
70
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["State","Layers","Context","METHOD","Pragma","provide","useEffect","useState","createElement","ReactState"],"sources":["../src/component.ts","../src/index.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","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":";;;;;;;;AAOA,MAAM,wBAAQ,IAAI,SAA6B;AAyD/C,IAAM,YAAN,cAAwBA,qBAAM;;qBACPC;;CAIrB,IAAI,QAAoC;AACtC,SAAO,KAAK;;CAGd,IAAI,MAAM,OAAmC;AAC3C,OAAK,SAAS;AACd,OAAK,IAAI,MAAY;;CAGvB,IAAI,UAAmB;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Effect","useState","createElement","ReactState"],"sources":["../src/index.ts"],"sourcesContent":["import { State } from '@expressive/state';\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/state` directly.\n */\ndeclare module '@expressive/state' {\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/state';\nexport { Consumer, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { Pragma };\n"],"mappings":";;;;;;;;AAMA,iBAAO,YAAYA;AACnB,iBAAO,WAAWC;AAClB,iBAAO,gBAAgBC;AAgBvB,wBAAM,MAAMC,qBAAW;AACvB,wBAAM,MAAMA,qBAAW;AACvB,wBAAM,KAAKA,qBAAW"}
@@ -1,10 +1,10 @@
1
- import * as _expressive_mvc0 from "@expressive/mvc";
2
- import * as react0 from "react";
1
+ import * as _expressive_state0 from "@expressive/state";
2
+ import * as react from "react";
3
3
  import { Fragment } from "react";
4
4
  import { JSX } from "./jsx-runtime";
5
5
 
6
6
  //#region src/jsx-dev-runtime.d.ts
7
- declare const jsxDEV: (type: react0.ElementType<any, keyof react0.JSX.IntrinsicElements> | _expressive_mvc0.State.Type<_expressive_mvc0.State>, ...args: any[]) => react0.ReactElement<unknown, string | react0.JSXElementConstructor<any>>;
7
+ declare const jsxDEV: (type: react.ElementType<any, keyof react.JSX.IntrinsicElements> | _expressive_state0.State.Type<_expressive_state0.State>, ...args: any[]) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
8
8
  //#endregion
9
9
  export { Fragment, type JSX, jsxDEV };
10
10
  //# sourceMappingURL=jsx-dev-runtime.d.ts.map
@@ -1,4 +1,5 @@
1
- const require_chunk = require('./chunk-kSYXY2_d.js');
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_chunk = require('./chunk-BE-pF4vm.js');
2
3
  let react = require("react");
3
4
  let react_jsx_dev_runtime = require("react/jsx-dev-runtime");
4
5
  react_jsx_dev_runtime = require_chunk.__toESM(react_jsx_dev_runtime);
@@ -1 +1 @@
1
- {"version":3,"file":"jsx-dev-runtime.js","names":["patch","Runtime"],"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,SAASA,sBAAM,KAAKC,8BAAQ,OAAO"}
1
+ {"version":3,"file":"jsx-dev-runtime.js","names":["patch","Runtime"],"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,SAASA,sBAAM,KAAKC,8BAAQ,OAAO"}