@expressive/react 0.58.4 → 0.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/adapter.d.ts CHANGED
@@ -1,18 +1,18 @@
1
1
  import { Context } from '@expressive/mvc';
2
+ import react__default, { FunctionComponent } from 'react';
2
3
 
3
4
  declare module '@expressive/mvc' {
4
5
  namespace Model {
5
- interface Component<T extends Model, P extends Model.Assign<T>> {
6
- (props: P): JSX.Element | null;
6
+ interface Component<T extends Model, P extends Model.Assign<T>> extends FunctionComponent<P & Component.Props<T>> {
7
7
  displayName?: string;
8
8
  Model: Model.Type<T>;
9
9
  }
10
- /**
11
- * Creates a component which reflects this Model. All managed properties may be assigned using props.
12
- *
13
- * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.
14
- */
15
- function as<T extends Model, P extends Model.Assign<T>>(this: Model.Init<T>, render: (using: P) => JSX.Element | null): Component<T, P & Model.Assign<T>>;
10
+ namespace Component {
11
+ type Props<T extends Model> = Partial<Pick<T, Exclude<keyof T, keyof Model>>> & {
12
+ is?: (instance: T) => void | (() => void);
13
+ };
14
+ }
15
+ function as<T extends Model, P extends Model.Assign<T>>(this: Model.Init<T>, render: (props: P, self: T) => react__default.ReactNode): Component<T, P>;
16
16
  }
17
17
  }
18
18
 
package/dist/adapter.js CHANGED
@@ -25,16 +25,48 @@ __export(adapter_exports, {
25
25
  module.exports = __toCommonJS(adapter_exports);
26
26
 
27
27
  // src/model.as.ts
28
+ var import_mvc2 = require("@expressive/mvc");
29
+ var import_react2 = require("react");
30
+
31
+ // src/context.ts
28
32
  var import_mvc = require("@expressive/mvc");
29
- import_mvc.Model.as = function(render) {
30
- if (this === import_mvc.Model)
33
+ var import_react = require("react");
34
+ var Lookup = (0, import_react.createContext)(new import_mvc.Context());
35
+ function Provider(props) {
36
+ const ambient = (0, import_react.useContext)(Lookup);
37
+ const context = (0, import_react.useMemo)(() => ambient.push(), [ambient]);
38
+ (0, import_react.useEffect)(() => () => context.pop(), [ambient]);
39
+ context.include(props.for, (model) => {
40
+ if ("set" in props)
41
+ throw new Error(`The \`set\` prop is deprecated. Use \`forEach\` or <${model.constructor} {...props} /> instead.`);
42
+ if (props.forEach) {
43
+ const cleanup = props.forEach(model);
44
+ if (cleanup)
45
+ model.set(null, cleanup);
46
+ }
47
+ });
48
+ return (0, import_react.createElement)(Lookup.Provider, {
49
+ key: context.id,
50
+ value: context
51
+ }, props.children);
52
+ }
53
+
54
+ // src/model.as.ts
55
+ import_mvc2.Model.as = function(render) {
56
+ if (this === import_mvc2.Model)
31
57
  throw new Error("Cannot create component from base Model.");
32
- const Component = (inputProps) => {
33
- const props = this.use(inputProps, true);
34
- for (const key in inputProps)
35
- if (!(key in props))
36
- Object.defineProperty(props, key, { value: inputProps[key] });
37
- return render(props);
58
+ const Component = (props) => {
59
+ const { is, ...rest } = props;
60
+ const self = (0, import_react2.useState)(() => this.new(rest, is))[0];
61
+ Object.assign(Object.create(self), rest);
62
+ return (0, import_react2.createElement)(
63
+ Provider,
64
+ { for: self },
65
+ (0, import_react2.createElement)(() => render(
66
+ props,
67
+ render.length > 1 ? this.get() : void 0
68
+ ), {})
69
+ );
38
70
  };
39
71
  Component.Model = this;
40
72
  Component.displayName = this.name;
@@ -42,15 +74,15 @@ import_mvc.Model.as = function(render) {
42
74
  };
43
75
 
44
76
  // src/model.use.ts
45
- var import_mvc2 = require("@expressive/mvc");
77
+ var import_mvc3 = require("@expressive/mvc");
46
78
  var import_adapter = require("./adapter");
47
- import_mvc2.Model.use = function(argument, repeat) {
79
+ import_mvc3.Model.use = function(argument, repeat) {
48
80
  const ambient = import_adapter.Pragma.useContext();
49
81
  const render = import_adapter.Pragma.useFactory((refresh) => {
50
82
  let enabled;
51
83
  let local;
52
84
  const instance = new this(argument);
53
- const context = ambient.push();
85
+ const context = ambient.push({ instance });
54
86
  const unwatch = instance.get((current) => {
55
87
  local = current;
56
88
  if (enabled)
@@ -66,8 +98,7 @@ import_mvc2.Model.use = function(argument, repeat) {
66
98
  }
67
99
  return (props) => {
68
100
  import_adapter.Pragma.useLifecycle(didMount);
69
- context.include({ instance });
70
- if (repeat && enabled && props) {
101
+ if (enabled && repeat && props) {
71
102
  enabled = false;
72
103
  if (typeof props == "function")
73
104
  props.call(instance, instance);
@@ -86,9 +117,9 @@ import_mvc2.Model.use = function(argument, repeat) {
86
117
  };
87
118
 
88
119
  // src/model.get.ts
89
- var import_mvc3 = require("@expressive/mvc");
120
+ var import_mvc4 = require("@expressive/mvc");
90
121
  var import_adapter2 = require("./adapter");
91
- import_mvc3.Model.get = function(argument) {
122
+ import_mvc4.Model.get = function(argument) {
92
123
  const context = import_adapter2.Pragma.useContext();
93
124
  const render = import_adapter2.Pragma.useFactory((refresh) => {
94
125
  function forceUpdate(action) {
@@ -106,7 +137,7 @@ import_mvc3.Model.get = function(argument) {
106
137
  throw new Error(`Could not find ${this} in context.`);
107
138
  let unwatch;
108
139
  let value;
109
- unwatch = (0, import_mvc3.createEffect)(instance, (current) => {
140
+ unwatch = (0, import_mvc4.createEffect)(instance, (current) => {
110
141
  if (typeof argument === "function") {
111
142
  const next = argument.call(current, current, forceUpdate);
112
143
  if (next === value)
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/adapter.ts","../src/model.as.ts","../src/model.use.ts","../src/model.get.ts"],"sourcesContent":["import { Context } from '@expressive/mvc';\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends () => unknown>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';","import { Model } from '@expressive/mvc';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> {\n (props: P): JSX.Element | null;\n\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n /**\n * Creates a component which reflects this Model. All managed properties may be assigned using props.\n * \n * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.\n */\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (using: P) => JSX.Element | null\n ): Component<T, P & Model.Assign<T>>;\n }\n}\n\nModel.as = function <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (using: P) => JSX.Element | null){\n\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component = ((inputProps: P) => {\n const props = this.use(inputProps, true) as T & P;\n\n for(const key in inputProps)\n if(!(key in props))\n Object.defineProperty(props, key, { value: inputProps[key] });\n\n return render(props);\n }) as Model.Component<T, P>;\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import { Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const ambient = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n const context = ambient.push();\n const unwatch = instance.get(current => {\n local = current;\n\n if(enabled)\n refresh();\n });\n\n function didMount(){\n enabled = true;\n return () => {\n unwatch();\n instance.set(null);\n context.pop()\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n context.include({ instance });\n\n if(repeat && enabled && props){\n enabled = false;\n\n if(typeof props == \"function\")\n props.call(instance, instance);\n else if(typeof props == \"object\")\n instance.set(props);\n\n const update = instance.set();\n\n if(update)\n update.then(() => enabled = true);\n else\n enabled = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n function forceUpdate(): void;\n function forceUpdate<T>(action: Promise<T> | (() => Promise<T>)): Promise<T>;\n function forceUpdate<T>(action?: Promise<T> | (() => Promise<T>)){\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let unwatch: (() => void) | undefined;\n let value: any;\n\n unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, forceUpdate);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(unwatch)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n // TODO: ignore update if resolves to undefined or null\n value.then(x => value = x).catch(e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useLifecycle(() => unwatch);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;;;ACAA,iBAAsB;AAsBtB,iBAAM,KAAK,SACY,QAAyC;AAE9D,MAAG,SAAS;AACV,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,YAAa,CAAC,eAAkB;AACpC,UAAM,QAAQ,KAAK,IAAI,YAAY,IAAI;AAEvC,eAAU,OAAO;AACf,UAAG,EAAE,OAAO;AACV,eAAO,eAAe,OAAO,KAAK,EAAE,OAAO,WAAW,GAAG,EAAE,CAAC;AAEhE,WAAO,OAAO,KAAK;AAAA,EACrB;AAEA,YAAU,QAAQ;AAClB,YAAU,cAAc,KAAK;AAE7B,SAAO;AACT;;;AC1CA,IAAAC,cAAsB;AAEtB,qBAAuB;AAkBvB,kBAAM,MAAM,SAEV,UACA,QAAiB;AAEjB,QAAM,UAAU,sBAAO,WAAW;AAClC,QAAM,SAAS,sBAAO,WAAW,CAAC,YAAY;AAC5C,QAAI;AACJ,QAAI;AAEJ,UAAM,WAAW,IAAI,KAAK,QAAQ;AAClC,UAAM,UAAU,QAAQ,KAAK;AAC7B,UAAM,UAAU,SAAS,IAAI,aAAW;AACtC,cAAQ;AAER,UAA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,gBAAQ;AAAA,IACZ,GAAG,aAAa,IAAI;AAEpB,QAAG,iBAAiB,SAAQ;AAC1B,UAAI;AAEJ,cAAQ;AAGR,YAAM,KAAK,OAAK,QAAQ,CAAC,EAAE,MAAM,OAAK,QAAQ,CAAC,EAAE,QAAQ,OAAO;AAChE,cAAQ;AAER,aAAO,MAAM;AACX,YAAG;AACD,gBAAM;AAER,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,6BAAO,aAAa,MAAM,OAAO;AACjC,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,OAAO;AAChB;;;AH/HA,IAAMC,UAAS,CAAC;","names":["Pragma","import_mvc","import_mvc","import_adapter","Pragma"]}
1
+ {"version":3,"sources":["../src/adapter.ts","../src/model.as.ts","../src/context.ts","../src/model.use.ts","../src/model.get.ts"],"sourcesContent":["import { Context } from '@expressive/mvc';\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends () => unknown>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';","import { Model } from '@expressive/mvc';\nimport React, { createElement, FunctionComponent, useState } from 'react';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> extends FunctionComponent<P & Component.Props<T>> {\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n namespace Component {\n type Props<T extends Model> = \n & Partial<Pick<T, Exclude<keyof T, keyof Model>>>\n & {\n is?: (instance: T) => void | (() => void);\n }\n }\n\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (props: P, self: T) => React.ReactNode\n ): Component<T, P>;\n }\n}\n\nModel.as = function <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>,\n render: (props: P, self: T) => React.ReactNode\n){\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component: Model.Component<T, P> = (props) => {\n const { is, ...rest } = props;\n const self = useState(() => this.new(rest as Model.Assign<T>, is))[0];\n\n Object.assign(Object.create(self), rest);\n\n return createElement(Provider, { for: self },\n createElement(() => render(\n props as P, render.length > 1 ? this.get() : undefined as never\n ), {})\n );\n }\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = createContext(new Context());\n\ndeclare namespace Consumer {\n type Props<T extends Model> = {\n /** Type of controller to fetch from context. */\n for: Model.Type<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `Model.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 Model>(props: Consumer.Props<T>){\n return props.for.get(i => props.children(i));\n}\n\ndeclare namespace Provider {\n interface Props<T extends Model> {\n for: Context.Accept<T>;\n forEach?: Context.Expect<T>;\n children?: ReactNode;\n }\n}\n\nfunction Provider<T extends Model>(props: Provider.Props<T>){\n const ambient = useContext(Lookup);\n const context = useMemo(() => ambient.push(), [ambient]);\n\n useEffect(() => () => context.pop(), [ambient]);\n \n context.include(props.for, (model) => {\n // TODO: remove next minor release\n if(\"set\" in props)\n throw new Error(`The \\`set\\` prop is deprecated. Use \\`forEach\\` or <${model.constructor} {...props} /> instead.`);\n\n if(props.forEach){\n const cleanup = props.forEach(model);\n\n if(cleanup)\n model.set(null, cleanup);\n }\n });\n\n return createElement(Lookup.Provider, {\n key: context.id,\n value: context\n }, props.children);\n}\n\nexport { Consumer, Provider, Lookup as Context }","import { Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const ambient = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n const context = ambient.push({ instance });\n const unwatch = instance.get(current => {\n local = current;\n\n if(enabled) \n refresh();\n });\n\n function didMount(){\n enabled = true;\n return () => {\n unwatch();\n instance.set(null);\n context.pop()\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n\n if(enabled && repeat && props){\n enabled = false;\n\n if(typeof props == \"function\")\n props.call(instance, instance);\n else if(typeof props == \"object\")\n instance.set(props);\n\n const update = instance.set();\n\n if(update)\n update.then(() => enabled = true);\n else\n enabled = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n function forceUpdate(): void;\n function forceUpdate<T>(action: Promise<T> | (() => Promise<T>)): Promise<T>;\n function forceUpdate<T>(action?: Promise<T> | (() => Promise<T>)){\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let unwatch: (() => void) | undefined;\n let value: any;\n\n unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, forceUpdate);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(unwatch)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n // TODO: ignore update if resolves to undefined or null\n value.then(x => value = x).catch(e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useLifecycle(() => unwatch);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;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CAAC;AACF,UAAG,aAAa;AACd,eAAO,MAAM;AAAA;AAEb,cAAM,IAAI,MAAM,kBAAkB,IAAI,cAAc;AAExD,QAAI;AACJ,QAAI;AAEJ,kBAAU,0BAAa,UAAU,aAAW;AAC1C,UAAG,OAAO,aAAa,YAAW;AAChC,cAAM,OAAO,SAAS,KAAK,SAAS,SAAS,WAAW;AAExD,YAAG,SAAS;AACV;AAEF,gBAAQ;AAAA,MACV;AAEE,gBAAQ;AAEV,UAAG;AACD,gBAAQ;AAAA,IACZ,GAAG,aAAa,IAAI;AAEpB,QAAG,iBAAiB,SAAQ;AAC1B,UAAI;AAEJ,cAAQ;AAGR,YAAM,KAAK,OAAK,QAAQ,CAAC,EAAE,MAAM,OAAK,QAAQ,CAAC,EAAE,QAAQ,OAAO;AAChE,cAAQ;AAER,aAAO,MAAM;AACX,YAAG;AACD,gBAAM;AAER,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,6BAAO,aAAa,MAAM,OAAO;AACjC,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,OAAO;AAChB;;;AJ/HA,IAAMC,UAAS,CAAC;","names":["Pragma","import_mvc","import_react","import_mvc","import_mvc","import_adapter","Pragma"]}
@@ -1,14 +1,22 @@
1
1
  // src/model.as.ts
2
2
  import { Model } from "@expressive/mvc";
3
+ import { createElement, useState } from "react";
4
+ import { Provider } from "./context";
3
5
  Model.as = function(render) {
4
6
  if (this === Model)
5
7
  throw new Error("Cannot create component from base Model.");
6
- const Component = (inputProps) => {
7
- const props = this.use(inputProps, true);
8
- for (const key in inputProps)
9
- if (!(key in props))
10
- Object.defineProperty(props, key, { value: inputProps[key] });
11
- return render(props);
8
+ const Component = (props) => {
9
+ const { is, ...rest } = props;
10
+ const self = useState(() => this.new(rest, is))[0];
11
+ Object.assign(Object.create(self), rest);
12
+ return createElement(
13
+ Provider,
14
+ { for: self },
15
+ createElement(() => render(
16
+ props,
17
+ render.length > 1 ? this.get() : void 0
18
+ ), {})
19
+ );
12
20
  };
13
21
  Component.Model = this;
14
22
  Component.displayName = this.name;
@@ -24,7 +32,7 @@ Model2.use = function(argument, repeat) {
24
32
  let enabled;
25
33
  let local;
26
34
  const instance = new this(argument);
27
- const context = ambient.push();
35
+ const context = ambient.push({ instance });
28
36
  const unwatch = instance.get((current) => {
29
37
  local = current;
30
38
  if (enabled)
@@ -40,8 +48,7 @@ Model2.use = function(argument, repeat) {
40
48
  }
41
49
  return (props) => {
42
50
  Pragma.useLifecycle(didMount);
43
- context.include({ instance });
44
- if (repeat && enabled && props) {
51
+ if (enabled && repeat && props) {
45
52
  enabled = false;
46
53
  if (typeof props == "function")
47
54
  props.call(instance, instance);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model.as.ts","../../src/model.use.ts","../../src/model.get.ts","../../src/adapter.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> {\n (props: P): JSX.Element | null;\n\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n /**\n * Creates a component which reflects this Model. All managed properties may be assigned using props.\n * \n * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.\n */\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (using: P) => JSX.Element | null\n ): Component<T, P & Model.Assign<T>>;\n }\n}\n\nModel.as = function <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (using: P) => JSX.Element | null){\n\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component = ((inputProps: P) => {\n const props = this.use(inputProps, true) as T & P;\n\n for(const key in inputProps)\n if(!(key in props))\n Object.defineProperty(props, key, { value: inputProps[key] });\n\n return render(props);\n }) as Model.Component<T, P>;\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import { Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const ambient = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n const context = ambient.push();\n const unwatch = instance.get(current => {\n local = current;\n\n if(enabled)\n refresh();\n });\n\n function didMount(){\n enabled = true;\n return () => {\n unwatch();\n instance.set(null);\n context.pop()\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n context.include({ instance });\n\n if(repeat && enabled && props){\n enabled = false;\n\n if(typeof props == \"function\")\n props.call(instance, instance);\n else if(typeof props == \"object\")\n instance.set(props);\n\n const update = instance.set();\n\n if(update)\n update.then(() => enabled = true);\n else\n enabled = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n function forceUpdate(): void;\n function forceUpdate<T>(action: Promise<T> | (() => Promise<T>)): Promise<T>;\n function forceUpdate<T>(action?: Promise<T> | (() => Promise<T>)){\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let unwatch: (() => void) | undefined;\n let value: any;\n\n unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, forceUpdate);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(unwatch)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n // TODO: ignore update if resolves to undefined or null\n value.then(x => value = x).catch(e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useLifecycle(() => unwatch);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}","import { Context } from '@expressive/mvc';\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends () => unknown>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';"],"mappings":";AAAA,SAAS,aAAa;AAsBtB,MAAM,KAAK,SACY,QAAyC;AAE9D,MAAG,SAAS;AACV,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,YAAa,CAAC,eAAkB;AACpC,UAAM,QAAQ,KAAK,IAAI,YAAY,IAAI;AAEvC,eAAU,OAAO;AACf,UAAG,EAAE,OAAO;AACV,eAAO,eAAe,OAAO,KAAK,EAAE,OAAO,WAAW,GAAG,EAAE,CAAC;AAEhE,WAAO,OAAO,KAAK;AAAA,EACrB;AAEA,YAAU,QAAQ;AAClB,YAAU,cAAc,KAAK;AAE7B,SAAO;AACT;;;AC1CA,SAAS,SAAAA,cAAa;AAEtB,SAAS,cAAc;AAkBvBA,OAAM,MAAM,SAEV,UACA,QAAiB;AAEjB,QAAM,UAAU,OAAO,WAAW;AAClC,QAAM,SAAS,OAAO,WAAW,CAAC,YAAY;AAC5C,QAAI;AACJ,QAAI;AAEJ,UAAM,WAAW,IAAI,KAAK,QAAQ;AAClC,UAAM,UAAU,QAAQ,KAAK;AAC7B,UAAM,UAAU,SAAS,IAAI,aAAW;AACtC,cAAQ;AAER,UAAG;AACD,gBAAQ;AAAA,IACZ,CAAC;AAED,aAAS,WAAU;AACjB,gBAAU;AACV,aAAO,MAAM;AACX,gBAAQ;AACR,iBAAS,IAAI,IAAI;AACjB,gBAAQ,IAAI;AAAA,MACd;AAAA,IACF;AAEA,WAAO,CAAC,UAAgD;AACtD,aAAO,aAAa,QAAQ;AAC5B,cAAQ,QAAQ,EAAE,SAAS,CAAC;AAE5B,UAA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U,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,MAAAA,QAAO,aAAa,MAAM,OAAO;AACjC,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,OAAO;AAChB;;;AC/HA,IAAMC,UAAS,CAAC;","names":["Model","Model","Pragma","Pragma"]}
1
+ {"version":3,"sources":["../../src/model.as.ts","../../src/model.use.ts","../../src/model.get.ts","../../src/adapter.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport React, { createElement, FunctionComponent, useState } from 'react';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> extends FunctionComponent<P & Component.Props<T>> {\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n namespace Component {\n type Props<T extends Model> = \n & Partial<Pick<T, Exclude<keyof T, keyof Model>>>\n & {\n is?: (instance: T) => void | (() => void);\n }\n }\n\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (props: P, self: T) => React.ReactNode\n ): Component<T, P>;\n }\n}\n\nModel.as = function <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>,\n render: (props: P, self: T) => React.ReactNode\n){\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component: Model.Component<T, P> = (props) => {\n const { is, ...rest } = props;\n const self = useState(() => this.new(rest as Model.Assign<T>, is))[0];\n\n Object.assign(Object.create(self), rest);\n\n return createElement(Provider, { for: self },\n createElement(() => render(\n props as P, render.length > 1 ? this.get() : undefined as never\n ), {})\n );\n }\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import { Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const ambient = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n const context = ambient.push({ instance });\n const unwatch = instance.get(current => {\n local = current;\n\n if(enabled) \n refresh();\n });\n\n function didMount(){\n enabled = true;\n return () => {\n unwatch();\n instance.set(null);\n context.pop()\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n\n if(enabled && repeat && props){\n enabled = false;\n\n if(typeof props == \"function\")\n props.call(instance, instance);\n else if(typeof props == \"object\")\n instance.set(props);\n\n const update = instance.set();\n\n if(update)\n update.then(() => enabled = true);\n else\n enabled = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n function forceUpdate(): void;\n function forceUpdate<T>(action: Promise<T> | (() => Promise<T>)): Promise<T>;\n function forceUpdate<T>(action?: Promise<T> | (() => Promise<T>)){\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let unwatch: (() => void) | undefined;\n let value: any;\n\n unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, forceUpdate);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(unwatch)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n // TODO: ignore update if resolves to undefined or null\n value.then(x => value = x).catch(e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useLifecycle(() => unwatch);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}","import { Context } from '@expressive/mvc';\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends () => unknown>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';"],"mappings":";AAAA,SAAS,aAAa;AACtB,SAAgB,eAAkC,gBAAgB;AAElE,SAAS,gBAAgB;AAuBzB,MAAM,KAAK,SAET,QACD;AACC,MAAG,SAAS;AACV,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,YAAmC,CAAC,UAAU;AAClD,UAAM,EAAE,IAAI,GAAG,KAAK,IAAI;AACxB,UAAM,OAAO,SAAS,MAAM,KAAK,IAAI,MAAyB,EAAE,CAAC,EAAE,CAAC;AAEpE,WAAO,OAAO,OAAO,OAAO,IAAI,GAAG,IAAI;AAEvC,WAAO;AAAA,MAAc;AAAA,MAAU,EAAE,KAAK,KAAK;AAAA,MACzC,cAAc,MAAM;AAAA,QAClB;AAAA,QAAY,OAAO,SAAS,IAAI,KAAK,IAAI,IAAI;AAAA,MAC/C,GAAG,CAAC,CAAC;AAAA,IACP;AAAA,EACF;AAEA,YAAU,QAAQ;AAClB,YAAU,cAAc,KAAK;AAE7B,SAAO;AACT;;;AClDA,SAAS,SAAAA,cAAa;AAEtB,SAAS,cAAc;AAkBvBA,OAAM,MAAM,SAEV,UACA,QAAiB;AAEjB,QAAM,UAAU,OAAO,WAAW;AAClC,QAAM,SAAS,OAAO,WAAW,CAAC,YAAY;AAC5C,QAAI;AACJ,QAAI;AAEJ,UAAM,WAAW,IAAI,KAAK,QAAQ;AAClC,UAAM,UAAU,QAAQ,KAAK,EAAE,SAAS,CAAC;AACzC,UAAM,UAAU,SAAS,IAAI,aAAW;AACtC,cAAQ;AAER,UAAG;AACD,gBAAQ;AAAA,IACZ,CAAC;AAED,aAAS,WAAU;AACjB,gBAAU;AACV,aAAO,MAAM;AACX,gBAAQ;AACR,iBAAS,IAAI,IAAI;AACjB,gBAAQ,IAAI;AAAA,MACd;AAAA,IACF;AAEA,WAAO,CAAC,UAAgD;AACtD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AAG;AACD,gBAAM;AAER,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,MAAAA,QAAO,aAAa,MAAM,OAAO;AACjC,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,OAAO;AAChB;;;AC/HA,IAAMC,UAAS,CAAC;","names":["Model","Model","Pragma","Pragma"]}
@@ -1,8 +1,10 @@
1
1
  // src/jsx-dev-runtime.ts
2
- import * as Runtime from "react/jsx-dev-runtime";
3
- import { create } from "./jsx-runtime";
2
+ import { jsxDEV } from "react/jsx-dev-runtime";
3
+ import { compat } from "./jsx-runtime";
4
4
  import { Fragment } from "react";
5
- var jsxDEV2 = create.bind(Runtime.jsxDEV);
5
+ function jsxDEV2(type, props, key, isStatic, source, self) {
6
+ return jsxDEV(compat(type), props, key, isStatic, source, self);
7
+ }
6
8
  export {
7
9
  Fragment,
8
10
  jsxDEV2 as jsxDEV
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/jsx-dev-runtime.ts"],"sourcesContent":["import * as Runtime from 'react/jsx-dev-runtime';\nimport { create } from './jsx-runtime';\n\nexport const jsxDEV = create.bind(Runtime.jsxDEV);\n\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from \"react\";"],"mappings":";AAAA,YAAY,aAAa;AACzB,SAAS,cAAc;AAKvB,SAAS,gBAAgB;AAHlB,IAAMA,UAAS,OAAO,KAAa,cAAM;","names":["jsxDEV"]}
1
+ {"version":3,"sources":["../../src/jsx-dev-runtime.ts"],"sourcesContent":["import { jsxDEV, JSXSource } from 'react/jsx-dev-runtime';\n\nimport Model from '.';\nimport { compat } from './jsx-runtime';\n\nfunction jsxDEV2(\n type: React.ElementType | Model.Init,\n props: Record<string, any>,\n key: React.Key,\n isStatic: boolean,\n source?: JSXSource,\n self?: unknown) {\n\n return jsxDEV(compat(type), props, key, isStatic, source, self);\n}\n\nexport { jsxDEV2 as jsxDEV };\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from \"react\";\n"],"mappings":";AAAA,SAAS,cAAyB;AAGlC,SAAS,cAAc;AAevB,SAAS,gBAAgB;AAbzB,SAAS,QACP,MACA,OACA,KACA,UACA,QACA,MAAgB;AAEhB,SAAO,OAAO,OAAO,IAAI,GAAG,OAAO,KAAK,UAAU,QAAQ,IAAI;AAChE;","names":[]}
@@ -3,51 +3,38 @@ import { Model } from "@expressive/mvc";
3
3
  import { jsx, jsxs } from "react/jsx-runtime";
4
4
  import { Provider } from "./context";
5
5
  import { Fragment } from "react";
6
- function Render(props) {
6
+ function Component(props) {
7
7
  const { is, ...rest } = props;
8
8
  return jsx(Provider, {
9
9
  for: this,
10
10
  forEach: is,
11
11
  children: jsx(() => {
12
12
  const self = this.get();
13
- const { render } = self;
14
- for (const key in rest)
15
- if (key in self)
16
- self[key] = rest[key];
13
+ Object.assign(Object.create(self), rest);
14
+ const render = props.render || self.render;
17
15
  if (render)
18
- return jsx(() => {
19
- const self2 = this.get();
20
- return render.call(self2, props, self2);
21
- }, {});
22
- const { children } = self;
23
- if (children)
24
- return children;
25
- const fn = props.children;
26
- if (typeof fn != "function")
27
- return fn;
28
- return jsx(() => {
29
- const self2 = this.get();
30
- return fn.call(self2, self2);
31
- }, {});
16
+ return jsx(() => render(props, render.length > 1 ? this.get() : void 0), {});
17
+ return props.children;
32
18
  }, {})
33
19
  });
34
20
  }
35
21
  var RENDER = /* @__PURE__ */ new WeakMap();
36
- function create(type, props, key, ...args) {
37
- if (Model.is(type)) {
38
- const bound = RENDER.get(type);
39
- if (bound)
40
- type = bound;
41
- else
42
- RENDER.set(type, type = Render.bind(type));
43
- }
44
- return this(type, props, key, ...args);
22
+ function compat(type) {
23
+ const bound = RENDER.get(type);
24
+ if (bound) return bound;
25
+ if (Model.is(type))
26
+ RENDER.set(type, type = Component.bind(type));
27
+ return type;
28
+ }
29
+ function jsx2(type, props, key) {
30
+ return jsx(compat(type), props, key);
31
+ }
32
+ function jsxs2(type, props, key) {
33
+ return jsxs(compat(type), props, key);
45
34
  }
46
- var jsx2 = create.bind(jsx);
47
- var jsxs2 = create.bind(jsxs);
48
35
  export {
49
36
  Fragment,
50
- create,
37
+ compat,
51
38
  jsx2 as jsx,
52
39
  jsxs2 as jsxs
53
40
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/jsx-runtime.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport { jsx, jsxs } from 'react/jsx-runtime';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type Render<T extends Model> = (this: T, self: T) => React.ReactNode;\n\n namespace FC {\n type Extends <T extends Model, P extends {} = {}> = Props<T> & P;\n\n export { Extends as Props };\n }\n\n /**\n * Use this to define a component which wraps a Model, intended to forward props.\n * This is to be used explicity, in JSDoc types or type annotations.\n * The second type parameter is additional props the component expects to receive.\n * \n * @example\n * ```tsx\n * import { Model } from '@expressive/react';\n * \n * class MyModel extends Model {\n * foo: string;\n * bar: number;\n * }\n * \n * const MyComponent: Model.FC<MyModel, { label: string }> =\n * ({ label, ...rest }) => (\n * <div>\n * <span>{label}</span>\n * <MyModel {...rest} />\n * </div>\n * );\n * ```\n */\n type FC<T extends Model, P extends {} = {}> = React.FC<FC.Props<T, P>>;\n\n /** Model which is not incompatable as Component in React. */\n interface Compat extends Model {\n render?(props: Model.Assign<this>, self: this): React.ReactNode;\n children?: React.ReactNode;\n }\n\n type Props<T extends Model> = \n & T extends { render(props: infer P, self: any): any }\n ? Partial<Pick<T, Exclude<keyof T, keyof Model | \"render\">>> & P\n : Partial<Pick<T, Exclude<keyof T, keyof Model>>> & {\n children?: React.ReactNode | Render<T>;\n }\n & {\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 | (() => void);\n };\n }\n}\n\nexport declare namespace JSX {\n type ElementType = Model.Type<Model.Compat> | React.JSX.ElementType;\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 } ? U :\n C extends Model.Type<infer U> ? Model.Props<U> :\n React.JSX.LibraryManagedAttributes<C, P>;\n\n interface Element extends React.JSX.Element {}\n interface ElementClass extends React.JSX.ElementClass {}\n // This is a hack to make TypeScript happy - React one insists on `props` property existing.\n // I await the \"Find Out\" phase of this experiment in git issues.\n interface ElementAttributesProperty {}\n interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}\n\n interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}\n interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}\n interface IntrinsicElements extends React.JSX.IntrinsicElements {}\n}\n\nfunction Render<T extends Model.Compat>(\n this: Model.Init<T>,\n props: Model.Assign<T>\n){\n const { is, ...rest } = props;\n\n return jsx(Provider, {\n for: this,\n forEach: is,\n children: jsx(() => {\n const self = this.get();\n const { render } = self;\n \n for(const key in rest)\n if(key in self)\n (self as any)[key] = rest[key];\n\n if(render)\n return jsx(() => {\n const self = this.get();\n return render.call(self, props, self);\n }, {});\n\n const { children } = self;\n\n if(children)\n return children;\n\n const fn = props.children as React.ReactNode | Model.Render<T>;\n\n if(typeof fn != \"function\")\n return fn;\n\n return jsx(() => {\n const self = this.get();\n return fn.call(self, self);\n }, {});\n }, {})\n });\n}\n\nconst RENDER = new WeakMap<typeof Model, React.FC>();\n\nexport function create(\n this: (\n type: React.ElementType,\n props: Record<string, any>,\n key?: React.Key,\n ...rest: any[]\n ) => React.ReactElement,\n type: React.ElementType,\n props: Record<string, any>,\n key?: React.Key,\n ...args: any[]){\n\n if(Model.is(type)){\n const bound = RENDER.get(type);\n\n if(bound)\n type = bound;\n else\n RENDER.set(type, type = Render.bind(type as Model.Init));\n }\n \n return this(type, props, key, ...args);\n}\n\nconst jsx2 = create.bind(jsx);\nconst jsxs2 = create.bind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,QAAQ,OAAO,KAAK,IAAI;","names":["self"]}
1
+ {"version":3,"sources":["../../src/jsx-runtime.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport { jsx, jsxs } from 'react/jsx-runtime';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n namespace FC {\n type Extends <T extends Model, P extends {} = {}> = Props<T> & P;\n\n export { Extends as Props };\n }\n\n /**\n * Use this to define a component which wraps a Model, intended to forward props.\n * This is to be used explicity, in JSDoc types or type annotations.\n * The second type parameter is additional props the component expects to receive.\n * \n * @example\n * ```tsx\n * import { Model } from '@expressive/react';\n * \n * class MyModel extends Model {\n * foo: string;\n * bar: number;\n * }\n * \n * const MyComponent: Model.FC<MyModel, { label: string }> =\n * ({ label, ...rest }) => (\n * <div>\n * <span>{label}</span>\n * <MyModel {...rest} />\n * </div>\n * );\n * ```\n */\n type FC<T extends Model, P extends {} = {}> = React.FC<FC.Props<T, P>>;\n\n /** Model which is not incompatable as Component in React. */\n interface Compat extends Model {\n render?(props: HasProps<this>, self: this): React.ReactNode;\n }\n\n interface BaseProps<T extends Model> {\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 | (() => void);\n\n render?(props: HasProps<T>, self: T): React.ReactNode;\n }\n\n type HasProps<T extends Model> = Partial<Pick<T, Exclude<keyof T, keyof Model>>>;\n\n type Props<T extends Model> = \n T extends { render(props: infer P, self: any): any }\n ? BaseProps<T> & HasProps<T> & P\n : BaseProps<T> & HasProps<T> & { children?: React.ReactNode };\n }\n}\n\nexport declare namespace JSX {\n type ElementType = Model.Type<Model.Compat> | React.JSX.ElementType;\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 } ? U :\n C extends Model.Type<infer U> ? Model.Props<U> :\n React.JSX.LibraryManagedAttributes<C, P>;\n\n interface Element extends React.JSX.Element {}\n interface ElementClass extends React.JSX.ElementClass {}\n // This is a hack to make TypeScript happy - React one insists on `props` property existing.\n // I await the \"Find Out\" phase of this experiment in git issues.\n interface ElementAttributesProperty {}\n interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}\n\n interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}\n interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}\n interface IntrinsicElements extends React.JSX.IntrinsicElements {}\n}\n\nfunction Component<T extends Model.Compat>(\n this: Model.Init<T>,\n props: Model.Props<T>\n){\n const { is, ...rest } = props;\n\n return jsx(Provider, {\n for: this,\n forEach: is,\n children: jsx(() => {\n const self = this.get();\n \n Object.assign(Object.create(self), rest);\n\n const render = props.render || self.render;\n\n if(render)\n return jsx(() => render(props, render.length > 1 ? this.get() : undefined as never), {});\n\n return props.children;\n }, {})\n });\n}\n\nconst RENDER = new WeakMap<typeof Model, React.FC>();\n\nexport function compat(type: React.ElementType | Model.Init){\n const bound = RENDER.get(type as any);\n\n if(bound) return bound;\n\n if(Model.is(type))\n RENDER.set(type, type = Component.bind(type));\n\n return type;\n}\n\nfunction jsx2(\n type: React.ElementType | Model.Init,\n props: Record<string, any>,\n key?: React.Key) {\n \n return jsx(compat(type), props, key);\n}\n\nfunction jsxs2(\n type: React.ElementType | Model.Init,\n props: Record<string, any>,\n key?: React.Key) {\n \n return jsxs(compat(type), props, key);\n}\n\nexport { Fragment } from \"react\";\nexport {\n jsx2 as jsx,\n jsxs2 as jsxs\n}"],"mappings":";AAAA,SAAS,aAAa;AACtB,SAAS,KAAK,YAAY;AAE1B,SAAS,gBAAgB;AAqIzB,SAAS,gBAAgB;AArDzB,SAAS,UAEP,OACD;AACC,QAAM,EAAE,IAAI,GAAG,KAAK,IAAI;AAExB,SAAO,IAAI,UAAU;AAAA,IACnB,KAAK;AAAA,IACL,SAAS;AAAA,IACT,UAAU,IAAI,MAAM;AAClB,YAAM,OAAO,KAAK,IAAI;AAEtB,aAAO,OAAO,OAAO,OAAO,IAAI,GAAG,IAAI;AAEvC,YAAM,SAAS,MAAM,UAAU,KAAK;AAEpC,UAAG;AACD,eAAO,IAAI,MAAM,OAAO,OAAO,OAAO,SAAS,IAAI,KAAK,IAAI,IAAI,MAAkB,GAAG,CAAC,CAAC;AAEzF,aAAO,MAAM;AAAA,IACf,GAAG,CAAC,CAAC;AAAA,EACP,CAAC;AACH;AAEA,IAAM,SAAS,oBAAI,QAAgC;AAE5C,SAAS,OAAO,MAAqC;AAC1D,QAAM,QAAQ,OAAO,IAAI,IAAW;AAEpC,MAAG,MAAO,QAAO;AAEjB,MAAG,MAAM,GAAG,IAAI;AACd,WAAO,IAAI,MAAM,OAAO,UAAU,KAAK,IAAI,CAAC;AAE9C,SAAO;AACT;AAEA,SAAS,KACP,MACA,OACA,KAAiB;AAEjB,SAAO,IAAI,OAAO,IAAI,GAAG,OAAO,GAAG;AACrC;AAEA,SAAS,MACP,MACA,OACA,KAAiB;AAEjB,SAAO,KAAK,OAAO,IAAI,GAAG,OAAO,GAAG;AACtC;","names":[]}
@@ -1,7 +1,9 @@
1
1
  import * as react from 'react';
2
2
  export { Fragment } from 'react';
3
+ import { JSXSource } from 'react/jsx-dev-runtime';
4
+ import { Model } from '@expressive/mvc';
3
5
  export { JSX } from './jsx-runtime';
4
6
 
5
- declare const jsxDEV: (type: react.ElementType<any, keyof react.JSX.IntrinsicElements>, props: Record<string, any>, key?: react.Key | undefined, ...args: any[]) => react.ReactElement<any, string | react.JSXElementConstructor<any>>;
7
+ declare function jsxDEV2(type: React.ElementType | Model.Init, props: Record<string, any>, key: React.Key, isStatic: boolean, source?: JSXSource, self?: unknown): react.ReactElement<any, string | react.JSXElementConstructor<any>>;
6
8
 
7
- export { jsxDEV };
9
+ export { jsxDEV2 as jsxDEV };
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/jsx-dev-runtime.ts
@@ -34,10 +24,12 @@ __export(jsx_dev_runtime_exports, {
34
24
  jsxDEV: () => jsxDEV2
35
25
  });
36
26
  module.exports = __toCommonJS(jsx_dev_runtime_exports);
37
- var Runtime = __toESM(require("react/jsx-dev-runtime"));
27
+ var import_jsx_dev_runtime = require("react/jsx-dev-runtime");
38
28
  var import_jsx_runtime = require("./jsx-runtime");
39
29
  var import_react = require("react");
40
- var jsxDEV2 = import_jsx_runtime.create.bind(Runtime.jsxDEV);
30
+ function jsxDEV2(type, props, key, isStatic, source, self) {
31
+ return (0, import_jsx_dev_runtime.jsxDEV)((0, import_jsx_runtime.compat)(type), props, key, isStatic, source, self);
32
+ }
41
33
  // Annotate the CommonJS export names for ESM import in node:
42
34
  0 && (module.exports = {
43
35
  Fragment,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/jsx-dev-runtime.ts"],"sourcesContent":["import * as Runtime from 'react/jsx-dev-runtime';\nimport { create } from './jsx-runtime';\n\nexport const jsxDEV = create.bind(Runtime.jsxDEV);\n\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from \"react\";"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;AAAA,cAAyB;AACzB,yBAAuB;AAKvB,mBAAyB;AAHlB,IAAMA,UAAS,0BAAO,KAAa,cAAM;","names":["jsxDEV"]}
1
+ {"version":3,"sources":["../src/jsx-dev-runtime.ts"],"sourcesContent":["import { jsxDEV, JSXSource } from 'react/jsx-dev-runtime';\n\nimport Model from '.';\nimport { compat } from './jsx-runtime';\n\nfunction jsxDEV2(\n type: React.ElementType | Model.Init,\n props: Record<string, any>,\n key: React.Key,\n isStatic: boolean,\n source?: JSXSource,\n self?: unknown) {\n\n return jsxDEV(compat(type), props, key, isStatic, source, self);\n}\n\nexport { jsxDEV2 as jsxDEV };\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from \"react\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAAkC;AAGlC,yBAAuB;AAevB,mBAAyB;AAbzB,SAAS,QACP,MACA,OACA,KACA,UACA,QACA,MAAgB;AAEhB,aAAO,mCAAO,2BAAO,IAAI,GAAG,OAAO,KAAK,UAAU,QAAQ,IAAI;AAChE;","names":[]}
@@ -4,7 +4,6 @@ import { Model } from '@expressive/mvc';
4
4
 
5
5
  declare module '@expressive/mvc' {
6
6
  namespace Model {
7
- type Render<T extends Model> = (this: T, self: T) => React.ReactNode;
8
7
  namespace FC {
9
8
  type Extends<T extends Model, P extends {} = {}> = Props<T> & P;
10
9
  export type { Extends as Props };
@@ -35,19 +34,21 @@ declare module '@expressive/mvc' {
35
34
  type FC<T extends Model, P extends {} = {}> = React.FC<FC.Props<T, P>>;
36
35
  /** Model which is not incompatable as Component in React. */
37
36
  interface Compat extends Model {
38
- render?(props: Model.Assign<this>, self: this): React.ReactNode;
39
- children?: React.ReactNode;
37
+ render?(props: HasProps<this>, self: this): React.ReactNode;
40
38
  }
41
- type Props<T extends Model> = T extends {
42
- render(props: infer P, self: any): any;
43
- } ? Partial<Pick<T, Exclude<keyof T, keyof Model | "render">>> & P : Partial<Pick<T, Exclude<keyof T, keyof Model>>> & {
44
- children?: React.ReactNode | Render<T>;
45
- } & {
39
+ interface BaseProps<T extends Model> {
46
40
  /**
47
41
  * Callback for newly created instance. Only called once.
48
42
  * @returns Callback to run when instance is destroyed.
49
43
  */
50
44
  is?: (instance: T) => void | (() => void);
45
+ render?(props: HasProps<T>, self: T): React.ReactNode;
46
+ }
47
+ type HasProps<T extends Model> = Partial<Pick<T, Exclude<keyof T, keyof Model>>>;
48
+ type Props<T extends Model> = T extends {
49
+ render(props: infer P, self: any): any;
50
+ } ? BaseProps<T> & HasProps<T> & P : BaseProps<T> & HasProps<T> & {
51
+ children?: React.ReactNode;
51
52
  };
52
53
  }
53
54
  }
@@ -71,8 +72,8 @@ declare namespace JSX {
71
72
  interface IntrinsicElements extends React.JSX.IntrinsicElements {
72
73
  }
73
74
  }
74
- declare function create(this: (type: React.ElementType, props: Record<string, any>, key?: React.Key, ...rest: any[]) => React.ReactElement, type: React.ElementType, props: Record<string, any>, key?: React.Key, ...args: any[]): react.ReactElement<any, string | react.JSXElementConstructor<any>>;
75
- declare const jsx2: (type: react.ElementType<any, keyof react.JSX.IntrinsicElements>, props: Record<string, any>, key?: react.Key | undefined, ...args: any[]) => react.ReactElement<any, string | react.JSXElementConstructor<any>>;
76
- declare const jsxs2: (type: react.ElementType<any, keyof react.JSX.IntrinsicElements>, props: Record<string, any>, key?: react.Key | undefined, ...args: any[]) => react.ReactElement<any, string | react.JSXElementConstructor<any>>;
75
+ declare function compat(type: React.ElementType | Model.Init): "symbol" | "object" | "set" | "use" | "search" | "big" | "link" | "small" | "sub" | "sup" | "map" | "filter" | "input" | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | "b" | "base" | "bdi" | "bdo" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "cite" | "code" | "col" | "colgroup" | "data" | "datalist" | "dd" | "del" | "details" | "dfn" | "dialog" | "div" | "dl" | "dt" | "em" | "embed" | "fieldset" | "figcaption" | "figure" | "footer" | "form" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "img" | "ins" | "kbd" | "keygen" | "label" | "legend" | "li" | "main" | "mark" | "menu" | "menuitem" | "meta" | "meter" | "nav" | "noscript" | "ol" | "optgroup" | "option" | "output" | "p" | "param" | "picture" | "pre" | "progress" | "q" | "rp" | "rt" | "ruby" | "s" | "samp" | "slot" | "script" | "section" | "select" | "source" | "span" | "strong" | "style" | "summary" | "table" | "template" | "tbody" | "td" | "textarea" | "tfoot" | "th" | "thead" | "time" | "title" | "tr" | "track" | "u" | "ul" | "var" | "video" | "wbr" | "webview" | "animate" | "circle" | "clipPath" | "defs" | "desc" | "ellipse" | "feBlend" | "feColorMatrix" | "feComponentTransfer" | "feComposite" | "feConvolveMatrix" | "feDiffuseLighting" | "feDisplacementMap" | "feDistantLight" | "feDropShadow" | "feFlood" | "feFuncA" | "feFuncB" | "feFuncG" | "feFuncR" | "feGaussianBlur" | "feImage" | "feMerge" | "feMergeNode" | "feMorphology" | "feOffset" | "fePointLight" | "feSpecularLighting" | "feSpotLight" | "feTile" | "feTurbulence" | "foreignObject" | "g" | "image" | "line" | "linearGradient" | "marker" | "mask" | "metadata" | "path" | "pattern" | "polygon" | "polyline" | "radialGradient" | "rect" | "stop" | "svg" | "switch" | "text" | "textPath" | "tspan" | "view" | react.ComponentClass<any, any> | "noindex" | "animateMotion" | "animateTransform" | "mpath" | react.FC<{}>;
76
+ declare function jsx2(type: React.ElementType | Model.Init, props: Record<string, any>, key?: React.Key): react.ReactElement<any, string | react.JSXElementConstructor<any>>;
77
+ declare function jsxs2(type: React.ElementType | Model.Init, props: Record<string, any>, key?: React.Key): react.ReactElement<any, string | react.JSXElementConstructor<any>>;
77
78
 
78
- export { JSX, create, jsx2 as jsx, jsxs2 as jsxs };
79
+ export { JSX, compat, jsx2 as jsx, jsxs2 as jsxs };
@@ -21,7 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var jsx_runtime_exports = {};
22
22
  __export(jsx_runtime_exports, {
23
23
  Fragment: () => import_react2.Fragment,
24
- create: () => create,
24
+ compat: () => compat,
25
25
  jsx: () => jsx2,
26
26
  jsxs: () => jsxs2
27
27
  });
@@ -54,52 +54,39 @@ function Provider(props) {
54
54
 
55
55
  // src/jsx-runtime.ts
56
56
  var import_react2 = require("react");
57
- function Render(props) {
57
+ function Component(props) {
58
58
  const { is, ...rest } = props;
59
59
  return (0, import_jsx_runtime.jsx)(Provider, {
60
60
  for: this,
61
61
  forEach: is,
62
62
  children: (0, import_jsx_runtime.jsx)(() => {
63
63
  const self = this.get();
64
- const { render } = self;
65
- for (const key in rest)
66
- if (key in self)
67
- self[key] = rest[key];
64
+ Object.assign(Object.create(self), rest);
65
+ const render = props.render || self.render;
68
66
  if (render)
69
- return (0, import_jsx_runtime.jsx)(() => {
70
- const self2 = this.get();
71
- return render.call(self2, props, self2);
72
- }, {});
73
- const { children } = self;
74
- if (children)
75
- return children;
76
- const fn = props.children;
77
- if (typeof fn != "function")
78
- return fn;
79
- return (0, import_jsx_runtime.jsx)(() => {
80
- const self2 = this.get();
81
- return fn.call(self2, self2);
82
- }, {});
67
+ return (0, import_jsx_runtime.jsx)(() => render(props, render.length > 1 ? this.get() : void 0), {});
68
+ return props.children;
83
69
  }, {})
84
70
  });
85
71
  }
86
72
  var RENDER = /* @__PURE__ */ new WeakMap();
87
- function create(type, props, key, ...args) {
88
- if (import_mvc2.Model.is(type)) {
89
- const bound = RENDER.get(type);
90
- if (bound)
91
- type = bound;
92
- else
93
- RENDER.set(type, type = Render.bind(type));
94
- }
95
- return this(type, props, key, ...args);
73
+ function compat(type) {
74
+ const bound = RENDER.get(type);
75
+ if (bound) return bound;
76
+ if (import_mvc2.Model.is(type))
77
+ RENDER.set(type, type = Component.bind(type));
78
+ return type;
79
+ }
80
+ function jsx2(type, props, key) {
81
+ return (0, import_jsx_runtime.jsx)(compat(type), props, key);
82
+ }
83
+ function jsxs2(type, props, key) {
84
+ return (0, import_jsx_runtime.jsxs)(compat(type), props, key);
96
85
  }
97
- var jsx2 = create.bind(import_jsx_runtime.jsx);
98
- var jsxs2 = create.bind(import_jsx_runtime.jsxs);
99
86
  // Annotate the CommonJS export names for ESM import in node:
100
87
  0 && (module.exports = {
101
88
  Fragment,
102
- create,
89
+ compat,
103
90
  jsx,
104
91
  jsxs
105
92
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/jsx-runtime.ts","../src/context.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport { jsx, jsxs } from 'react/jsx-runtime';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type Render<T extends Model> = (this: T, self: T) => React.ReactNode;\n\n namespace FC {\n type Extends <T extends Model, P extends {} = {}> = Props<T> & P;\n\n export { Extends as Props };\n }\n\n /**\n * Use this to define a component which wraps a Model, intended to forward props.\n * This is to be used explicity, in JSDoc types or type annotations.\n * The second type parameter is additional props the component expects to receive.\n * \n * @example\n * ```tsx\n * import { Model } from '@expressive/react';\n * \n * class MyModel extends Model {\n * foo: string;\n * bar: number;\n * }\n * \n * const MyComponent: Model.FC<MyModel, { label: string }> =\n * ({ label, ...rest }) => (\n * <div>\n * <span>{label}</span>\n * <MyModel {...rest} />\n * </div>\n * );\n * ```\n */\n type FC<T extends Model, P extends {} = {}> = React.FC<FC.Props<T, P>>;\n\n /** Model which is not incompatable as Component in React. */\n interface Compat extends Model {\n render?(props: Model.Assign<this>, self: this): React.ReactNode;\n children?: React.ReactNode;\n }\n\n type Props<T extends Model> = \n & T extends { render(props: infer P, self: any): any }\n ? Partial<Pick<T, Exclude<keyof T, keyof Model | \"render\">>> & P\n : Partial<Pick<T, Exclude<keyof T, keyof Model>>> & {\n children?: React.ReactNode | Render<T>;\n }\n & {\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 | (() => void);\n };\n }\n}\n\nexport declare namespace JSX {\n type ElementType = Model.Type<Model.Compat> | React.JSX.ElementType;\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 } ? U :\n C extends Model.Type<infer U> ? Model.Props<U> :\n React.JSX.LibraryManagedAttributes<C, P>;\n\n interface Element extends React.JSX.Element {}\n interface ElementClass extends React.JSX.ElementClass {}\n // This is a hack to make TypeScript happy - React one insists on `props` property existing.\n // I await the \"Find Out\" phase of this experiment in git issues.\n interface ElementAttributesProperty {}\n interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}\n\n interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}\n interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}\n interface IntrinsicElements extends React.JSX.IntrinsicElements {}\n}\n\nfunction Render<T extends Model.Compat>(\n this: Model.Init<T>,\n props: Model.Assign<T>\n){\n const { is, ...rest } = props;\n\n return jsx(Provider, {\n for: this,\n forEach: is,\n children: jsx(() => {\n const self = this.get();\n const { render } = self;\n \n for(const key in rest)\n if(key in self)\n (self as any)[key] = rest[key];\n\n if(render)\n return jsx(() => {\n const self = this.get();\n return render.call(self, props, self);\n }, {});\n\n const { children } = self;\n\n if(children)\n return children;\n\n const fn = props.children as React.ReactNode | Model.Render<T>;\n\n if(typeof fn != \"function\")\n return fn;\n\n return jsx(() => {\n const self = this.get();\n return fn.call(self, self);\n }, {});\n }, {})\n });\n}\n\nconst RENDER = new WeakMap<typeof Model, React.FC>();\n\nexport function create(\n this: (\n type: React.ElementType,\n props: Record<string, any>,\n key?: React.Key,\n ...rest: any[]\n ) => React.ReactElement,\n type: React.ElementType,\n props: Record<string, any>,\n key?: React.Key,\n ...args: any[]){\n\n if(Model.is(type)){\n const bound = RENDER.get(type);\n\n if(bound)\n type = bound;\n else\n RENDER.set(type, type = Render.bind(type as Model.Init));\n }\n \n return this(type, props, key, ...args);\n}\n\nconst jsx2 = create.bind(jsx);\nconst jsxs2 = create.bind(jsxs);\n\nexport { Fragment } from \"react\";\nexport {\n jsx2 as jsx,\n jsxs2 as jsxs\n}","import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = createContext(new Context());\n\ndeclare namespace Consumer {\n type Props<T extends Model> = {\n /** Type of controller to fetch from context. */\n for: Model.Type<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `Model.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 Model>(props: Consumer.Props<T>){\n return props.for.get(i => props.children(i));\n}\n\ndeclare namespace Provider {\n interface Props<T extends Model> {\n for: Context.Accept<T>;\n forEach?: Context.Expect<T>;\n children?: ReactNode;\n }\n}\n\nfunction Provider<T extends Model>(props: Provider.Props<T>){\n const ambient = useContext(Lookup);\n const context = useMemo(() => ambient.push(), [ambient]);\n\n useEffect(() => () => context.pop(), [ambient]);\n \n context.include(props.for, (model) => {\n // TODO: remove next minor release\n if(\"set\" in props)\n throw new Error(`The \\`set\\` prop is deprecated. Use \\`forEach\\` or <${model.constructor} {...props} /> instead.`);\n\n if(props.forEach){\n const cleanup = props.forEach(model);\n\n if(cleanup)\n model.set(null, cleanup);\n }\n });\n\n return createElement(Lookup.Provider, {\n key: context.id,\n value: context\n }, props.children);\n}\n\nexport { Consumer, Provider, Lookup as Context }"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,cAAsB;AACtB,yBAA0B;;;ACD1B,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AA8B1C,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AAG,CAAC,CAAC;AAAA,IACP,GAAG,CAAC,CAAC;AAAA,EACP,CAAC;AACH;AAEA,IAAM,SAAS,oBAAI,QAAgC;AAE5C,SAAS,OAOd,MACA,OACA,QACG,MAAY;AAEf,MAAG,kBAAM,GAAG,IAAI,GAAE;AAChB,UAAM,QAAQ,OAAO,IAAI,IAAI;AAE7B,QAAG;AACD,aAAO;AAAA;AAEP,aAAO,IAAI,MAAM,OAAO,OAAO,KAAK,IAAkB,CAAC;AAAA,EAC3D;AAEA,SAAO,KAAK,MAAM,OAAO,KAAK,GAAG,IAAI;AACvC;AAEA,IAAM,OAAO,OAAO,KAAK,sBAAG;AAC5B,IAAM,QAAQ,OAAO,KAAK,uBAAI;","names":["import_mvc","import_react","self"]}
1
+ {"version":3,"sources":["../src/jsx-runtime.ts","../src/context.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport { jsx, jsxs } from 'react/jsx-runtime';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n namespace FC {\n type Extends <T extends Model, P extends {} = {}> = Props<T> & P;\n\n export { Extends as Props };\n }\n\n /**\n * Use this to define a component which wraps a Model, intended to forward props.\n * This is to be used explicity, in JSDoc types or type annotations.\n * The second type parameter is additional props the component expects to receive.\n * \n * @example\n * ```tsx\n * import { Model } from '@expressive/react';\n * \n * class MyModel extends Model {\n * foo: string;\n * bar: number;\n * }\n * \n * const MyComponent: Model.FC<MyModel, { label: string }> =\n * ({ label, ...rest }) => (\n * <div>\n * <span>{label}</span>\n * <MyModel {...rest} />\n * </div>\n * );\n * ```\n */\n type FC<T extends Model, P extends {} = {}> = React.FC<FC.Props<T, P>>;\n\n /** Model which is not incompatable as Component in React. */\n interface Compat extends Model {\n render?(props: HasProps<this>, self: this): React.ReactNode;\n }\n\n interface BaseProps<T extends Model> {\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 | (() => void);\n\n render?(props: HasProps<T>, self: T): React.ReactNode;\n }\n\n type HasProps<T extends Model> = Partial<Pick<T, Exclude<keyof T, keyof Model>>>;\n\n type Props<T extends Model> = \n T extends { render(props: infer P, self: any): any }\n ? BaseProps<T> & HasProps<T> & P\n : BaseProps<T> & HasProps<T> & { children?: React.ReactNode };\n }\n}\n\nexport declare namespace JSX {\n type ElementType = Model.Type<Model.Compat> | React.JSX.ElementType;\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 } ? U :\n C extends Model.Type<infer U> ? Model.Props<U> :\n React.JSX.LibraryManagedAttributes<C, P>;\n\n interface Element extends React.JSX.Element {}\n interface ElementClass extends React.JSX.ElementClass {}\n // This is a hack to make TypeScript happy - React one insists on `props` property existing.\n // I await the \"Find Out\" phase of this experiment in git issues.\n interface ElementAttributesProperty {}\n interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}\n\n interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}\n interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}\n interface IntrinsicElements extends React.JSX.IntrinsicElements {}\n}\n\nfunction Component<T extends Model.Compat>(\n this: Model.Init<T>,\n props: Model.Props<T>\n){\n const { is, ...rest } = props;\n\n return jsx(Provider, {\n for: this,\n forEach: is,\n children: jsx(() => {\n const self = this.get();\n \n Object.assign(Object.create(self), rest);\n\n const render = props.render || self.render;\n\n if(render)\n return jsx(() => render(props, render.length > 1 ? this.get() : undefined as never), {});\n\n return props.children;\n }, {})\n });\n}\n\nconst RENDER = new WeakMap<typeof Model, React.FC>();\n\nexport function compat(type: React.ElementType | Model.Init){\n const bound = RENDER.get(type as any);\n\n if(bound) return bound;\n\n if(Model.is(type))\n RENDER.set(type, type = Component.bind(type));\n\n return type;\n}\n\nfunction jsx2(\n type: React.ElementType | Model.Init,\n props: Record<string, any>,\n key?: React.Key) {\n \n return jsx(compat(type), props, key);\n}\n\nfunction jsxs2(\n type: React.ElementType | Model.Init,\n props: Record<string, any>,\n key?: React.Key) {\n \n return jsxs(compat(type), props, key);\n}\n\nexport { Fragment } from \"react\";\nexport {\n jsx2 as jsx,\n jsxs2 as jsxs\n}","import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = createContext(new Context());\n\ndeclare namespace Consumer {\n type Props<T extends Model> = {\n /** Type of controller to fetch from context. */\n for: Model.Type<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `Model.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 Model>(props: Consumer.Props<T>){\n return props.for.get(i => props.children(i));\n}\n\ndeclare namespace Provider {\n interface Props<T extends Model> {\n for: Context.Accept<T>;\n forEach?: Context.Expect<T>;\n children?: ReactNode;\n }\n}\n\nfunction Provider<T extends Model>(props: Provider.Props<T>){\n const ambient = useContext(Lookup);\n const context = useMemo(() => ambient.push(), [ambient]);\n\n useEffect(() => () => context.pop(), [ambient]);\n \n context.include(props.for, (model) => {\n // TODO: remove next minor release\n if(\"set\" in props)\n throw new Error(`The \\`set\\` prop is deprecated. Use \\`forEach\\` or <${model.constructor} {...props} /> instead.`);\n\n if(props.forEach){\n const cleanup = props.forEach(model);\n\n if(cleanup)\n model.set(null, cleanup);\n }\n });\n\n return createElement(Lookup.Provider, {\n key: context.id,\n value: context\n }, props.children);\n}\n\nexport { Consumer, Provider, Lookup as Context }"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,cAAsB;AACtB,yBAA0B;;;ACD1B,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AA8B1C,SAAS,SAA0B,OAAyB;AAC1D,QAAM,cAAU,yBAAW,MAAM;AACjC,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,aAAO,wBAAI,OAAO,IAAI,GAAG,OAAO,GAAG;AACrC;AAEA,SAAS,MACP,MACA,OACA,KAAiB;AAEjB,aAAO,yBAAK,OAAO,IAAI,GAAG,OAAO,GAAG;AACtC;","names":["import_mvc","import_react"]}
package/package.json CHANGED
@@ -49,7 +49,7 @@
49
49
  "preversion": "npm run test && npm run build"
50
50
  },
51
51
  "dependencies": {
52
- "@expressive/mvc": "^0.58.0"
52
+ "@expressive/mvc": "^0.59.0"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "react": ">=16.8.0 <19.0.0"
@@ -67,6 +67,6 @@
67
67
  "react-test-renderer": "^18.2.0",
68
68
  "tsup": "^8.0.2"
69
69
  },
70
- "version": "0.58.4",
71
- "gitHead": "614b3b4980892b0776e420e68b30d97ddae61ae7"
70
+ "version": "0.59.0",
71
+ "gitHead": "28857ebb709033e57505fbdfaa236b3ff55565cf"
72
72
  }