@expressive/react 0.58.4 → 0.60.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
 
@@ -63,7 +63,7 @@ declare module '@expressive/mvc' {
63
63
 
64
64
  declare const Pragma: {
65
65
  useContext(): Context;
66
- useFactory<T extends () => unknown>(factory: (refresh: () => void) => T): T;
66
+ useFactory<T extends Function>(factory: (refresh: () => void) => T): T;
67
67
  useLifecycle(callback: () => () => void): void;
68
68
  };
69
69
 
package/dist/adapter.js CHANGED
@@ -25,16 +25,31 @@ __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
+ import_mvc.Context.use = (create) => {
36
+ const ambient = (0, import_react.useContext)(Lookup);
37
+ return create ? (0, import_react.useMemo)(() => ambient.push(), []) : ambient;
38
+ };
39
+
40
+ // src/model.as.ts
41
+ import_mvc2.Model.as = function(render) {
42
+ if (this === import_mvc2.Model)
31
43
  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);
44
+ const Component = (props) => {
45
+ const local = this.use(props.is);
46
+ for (const key in local)
47
+ if (key in props)
48
+ local[key] = props[key];
49
+ return (0, import_react2.createElement)(Lookup.Provider, {
50
+ value: import_mvc.Context.get(local),
51
+ children: render(props, local)
52
+ });
38
53
  };
39
54
  Component.Model = this;
40
55
  Component.displayName = this.name;
@@ -42,15 +57,15 @@ import_mvc.Model.as = function(render) {
42
57
  };
43
58
 
44
59
  // src/model.use.ts
45
- var import_mvc2 = require("@expressive/mvc");
60
+ var import_mvc3 = require("@expressive/mvc");
46
61
  var import_adapter = require("./adapter");
47
- import_mvc2.Model.use = function(argument, repeat) {
48
- const ambient = import_adapter.Pragma.useContext();
62
+ import_mvc3.Model.use = function(argument, repeat) {
63
+ const context = import_mvc3.Context.use(true);
49
64
  const render = import_adapter.Pragma.useFactory((refresh) => {
50
65
  let enabled;
51
66
  let local;
52
67
  const instance = new this(argument);
53
- const context = ambient.push();
68
+ context.include(instance);
54
69
  const unwatch = instance.get((current) => {
55
70
  local = current;
56
71
  if (enabled)
@@ -60,20 +75,19 @@ import_mvc2.Model.use = function(argument, repeat) {
60
75
  enabled = true;
61
76
  return () => {
62
77
  unwatch();
63
- instance.set(null);
64
78
  context.pop();
79
+ instance.set(null);
65
80
  };
66
81
  }
67
82
  return (props) => {
68
83
  import_adapter.Pragma.useLifecycle(didMount);
69
- context.include({ instance });
70
- if (repeat && enabled && props) {
84
+ if (enabled && repeat && props) {
71
85
  enabled = false;
72
- if (typeof props == "function")
86
+ if (typeof props == "function") {
73
87
  props.call(instance, instance);
74
- else if (typeof props == "object")
75
- instance.set(props);
76
- const update = instance.set();
88
+ props = void 0;
89
+ }
90
+ const update = instance.set(props);
77
91
  if (update)
78
92
  update.then(() => enabled = true);
79
93
  else
@@ -86,9 +100,9 @@ import_mvc2.Model.use = function(argument, repeat) {
86
100
  };
87
101
 
88
102
  // src/model.get.ts
89
- var import_mvc3 = require("@expressive/mvc");
103
+ var import_mvc4 = require("@expressive/mvc");
90
104
  var import_adapter2 = require("./adapter");
91
- import_mvc3.Model.get = function(argument) {
105
+ import_mvc4.Model.get = function(argument) {
92
106
  const context = import_adapter2.Pragma.useContext();
93
107
  const render = import_adapter2.Pragma.useFactory((refresh) => {
94
108
  function forceUpdate(action) {
@@ -106,7 +120,7 @@ import_mvc3.Model.get = function(argument) {
106
120
  throw new Error(`Could not find ${this} in context.`);
107
121
  let unwatch;
108
122
  let value;
109
- unwatch = (0, import_mvc3.createEffect)(instance, (current) => {
123
+ unwatch = (0, import_mvc4.createEffect)(instance, (current) => {
110
124
  if (typeof argument === "function") {
111
125
  const next = argument.call(current, current, forceUpdate);
112
126
  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,KA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AAK,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;;;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 Function>(\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 } from 'react';\n\nimport { Context, Lookup } 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 local = this.use(props.is);\n\n for(const key in local)\n if(key in props)\n local[key] = (props as any)[key];\n\n return createElement(Lookup.Provider, {\n value: Context.get(local)!,\n children: render(props, local)\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 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(Lookup);\n\n return create ?\n useMemo(() => ambient.push(), []) :\n ambient;\n}\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 context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.include(props.for, (model) => {\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, Context }\n","import { Model, Context } 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 context = Context.use(true);\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n\n context.include(instance);\n \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 context.pop()\n instance.set(null);\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 props = undefined\n }\n\n const update = instance.set(props);\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);\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AAQ,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,19 @@
1
1
  // src/model.as.ts
2
2
  import { Model } from "@expressive/mvc";
3
+ import { createElement } from "react";
4
+ import { Context, Lookup } 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 local = this.use(props.is);
10
+ for (const key in local)
11
+ if (key in props)
12
+ local[key] = props[key];
13
+ return createElement(Lookup.Provider, {
14
+ value: Context.get(local),
15
+ children: render(props, local)
16
+ });
12
17
  };
13
18
  Component.Model = this;
14
19
  Component.displayName = this.name;
@@ -16,15 +21,15 @@ Model.as = function(render) {
16
21
  };
17
22
 
18
23
  // src/model.use.ts
19
- import { Model as Model2 } from "@expressive/mvc";
24
+ import { Model as Model2, Context as Context2 } from "@expressive/mvc";
20
25
  import { Pragma } from "./adapter";
21
26
  Model2.use = function(argument, repeat) {
22
- const ambient = Pragma.useContext();
27
+ const context = Context2.use(true);
23
28
  const render = Pragma.useFactory((refresh) => {
24
29
  let enabled;
25
30
  let local;
26
31
  const instance = new this(argument);
27
- const context = ambient.push();
32
+ context.include(instance);
28
33
  const unwatch = instance.get((current) => {
29
34
  local = current;
30
35
  if (enabled)
@@ -34,20 +39,19 @@ Model2.use = function(argument, repeat) {
34
39
  enabled = true;
35
40
  return () => {
36
41
  unwatch();
37
- instance.set(null);
38
42
  context.pop();
43
+ instance.set(null);
39
44
  };
40
45
  }
41
46
  return (props) => {
42
47
  Pragma.useLifecycle(didMount);
43
- context.include({ instance });
44
- if (repeat && enabled && props) {
48
+ if (enabled && repeat && props) {
45
49
  enabled = false;
46
- if (typeof props == "function")
50
+ if (typeof props == "function") {
47
51
  props.call(instance, instance);
48
- else if (typeof props == "object")
49
- instance.set(props);
50
- const update = instance.set();
52
+ props = void 0;
53
+ }
54
+ const update = instance.set(props);
51
55
  if (update)
52
56
  update.then(() => enabled = true);
53
57
  else
@@ -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,UAAG,UAAU,WAAW,OAAM;AAC5B,kBAAU;AAEV,YAAG,OAAO,SAAS;AACjB,gBAAM,KAAK,UAAU,Q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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 } from 'react';\n\nimport { Context, Lookup } 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 local = this.use(props.is);\n\n for(const key in local)\n if(key in props)\n local[key] = (props as any)[key];\n\n return createElement(Lookup.Provider, {\n value: Context.get(local)!,\n children: render(props, local)\n });\n }\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import { Model, Context } 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 context = Context.use(true);\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n\n context.include(instance);\n \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 context.pop()\n instance.set(null);\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 props = undefined\n }\n\n const update = instance.set(props);\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 Function>(\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,qB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;;AC/HA,IAAMC,UAAS,CAAC;","names":["Model","Context","Model","Pragma","Pragma"]}
@@ -2,16 +2,17 @@
2
2
  import { Context } from "@expressive/mvc";
3
3
  import { createContext, createElement, useContext, useEffect, useMemo } from "react";
4
4
  var Lookup = createContext(new Context());
5
+ Context.use = (create) => {
6
+ const ambient = useContext(Lookup);
7
+ return create ? useMemo(() => ambient.push(), []) : ambient;
8
+ };
5
9
  function Consumer(props) {
6
10
  return props.for.get((i) => props.children(i));
7
11
  }
8
12
  function Provider(props) {
9
- const ambient = useContext(Lookup);
10
- const context = useMemo(() => ambient.push(), [ambient]);
11
- useEffect(() => () => context.pop(), [ambient]);
13
+ const context = Context.use(true);
14
+ useEffect(() => () => context.pop(), [context]);
12
15
  context.include(props.for, (model) => {
13
- if ("set" in props)
14
- throw new Error(`The \`set\` prop is deprecated. Use \`forEach\` or <${model.constructor} {...props} /> instead.`);
15
16
  if (props.forEach) {
16
17
  const cleanup = props.forEach(model);
17
18
  if (cleanup)
@@ -25,7 +26,8 @@ function Provider(props) {
25
26
  }
26
27
  export {
27
28
  Consumer,
28
- Lookup as Context,
29
+ Context,
30
+ Lookup,
29
31
  Provider
30
32
  };
31
33
  //# sourceMappingURL=context.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/context.ts"],"sourcesContent":["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,SAAgB,eAAe;AAC/B,SAAoB,eAAe,eAAe,YAAY,WAAW,eAAe;AAExF,IAAM,SAAS,cAAc,IAAI,QAAQ,CAAC;AAkB1C,SAAS,SAA0B,OAAyB;AAC1D,SAAO,MAAM,IAAI,IAAI,OAAK,MAAM,SAAS,CAAC,CAAC;AAC7C;AAUA,SAAS,SAA0B,OAAyB;AAC1D,QAAM,UAAU,WAAW,MAAM;AACjC,QAAM,UAAU,QAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,OAAO,CAAC;AAEvD,YAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,QAAQ,MAAM,KAAK,CAAC,UAAU;AAEpC,QAAG,SAAS;AACV,YAAM,IAAI,MAAM,uDAAuD,MAAM,WAAW,yBAAyB;AAEnH,QAAG,MAAM,SAAQ;AACf,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAG;AACD,cAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACF,CAAC;AAED,SAAO,cAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;AAAA,EACT,GAAG,MAAM,QAAQ;AACnB;","names":[]}
1
+ {"version":3,"sources":["../../src/context.ts"],"sourcesContent":["import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = 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(Lookup);\n\n return create ?\n useMemo(() => ambient.push(), []) :\n ambient;\n}\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 context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.include(props.for, (model) => {\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, Context }\n"],"mappings":";AAAA,SAAgB,eAAe;AAC/B,SAAoB,eAAe,eAAe,YAAY,WAAW,eAAe;AAExF,IAAM,SAAS,cAAc,IAAI,QAAQ,CAAC;AAS1C,QAAQ,MAAM,CAAC,WAAqB;AAClC,QAAM,UAAU,WAAW,MAAM;AAEjC,SAAO,SACL,QAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,CAAC,IAChC;AACJ;AAkBA,SAAS,SAA0B,OAAyB;AAC1D,SAAO,MAAM,IAAI,IAAI,OAAK,MAAM,SAAS,CAAC,CAAC;AAC7C;AAUA,SAAS,SAA0B,OAAyB;AAC1D,QAAM,UAAU,QAAQ,IAAI,IAAI;AAEhC,YAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,QAAQ,MAAM,KAAK,CAAC,UAAU;AACpC,QAAG,MAAM,SAAQ;AACf,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAG;AACD,cAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACF,CAAC;AAED,SAAO,cAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;AAAA,EACT,GAAG,MAAM,QAAQ;AACnB;","names":[]}
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { useContext, useEffect, useState } from "react";
2
+ import { useEffect, useState } from "react";
3
3
  import { Pragma } from "./adapter";
4
4
  import { Context } from "./context";
5
5
  import {
@@ -11,9 +11,9 @@ import {
11
11
  set,
12
12
  has
13
13
  } from "@expressive/mvc";
14
- import { Consumer, Provider } from "./context";
14
+ import { Consumer, Context as Context2, Provider } from "./context";
15
15
  import { Fragment, createElement } from "react";
16
- Pragma.useContext = () => useContext(Context);
16
+ Pragma.useContext = () => Context.use();
17
17
  Pragma.useLifecycle = (callback) => useEffect(() => callback(), []);
18
18
  Pragma.useFactory = (factory) => {
19
19
  const state = useState(() => factory(() => {
@@ -23,6 +23,7 @@ Pragma.useFactory = (factory) => {
23
23
  };
24
24
  export {
25
25
  Consumer,
26
+ Context2 as Context,
26
27
  Fragment,
27
28
  Model,
28
29
  Provider,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { useContext, useEffect, useState } from 'react';\n\nimport { Pragma } from './adapter';\nimport { Context } from './context';\n\nPragma.useContext = () => useContext(Context);\n\nPragma.useLifecycle = (callback) => useEffect(() => callback(), []);\n\nPragma.useFactory = (factory) => {\n const state = useState(() => factory(() => {\n state[1](x => x.bind(null) as any);\n }));\n\n return state[0];\n}\n\nexport {\n Model, Model as default,\n get, use, ref, set, has\n} from '@expressive/mvc';\n\nexport { Consumer, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { type Pragma };\n"],"mappings":";AAAA,SAAS,YAAY,WAAW,gBAAgB;AAEhD,SAAS,cAAc;AACvB,SAAS,eAAe;AAcxB;AAAA,EACE;AAAA,EAAgB,SAATA;AAAA,EACP;AAAA,EAAK;AAAA,EAAK;AAAA,EAAK;AAAA,EAAK;AAAA,OACf;AAEP,SAAS,UAAU,gBAAgB;AACnC,SAAS,UAAU,qBAAqB;AAlBxC,OAAO,aAAa,MAAM,WAAW,OAAO;AAE5C,OAAO,eAAe,CAAC,aAAa,UAAU,MAAM,SAAS,GAAG,CAAC,CAAC;AAElE,OAAO,aAAa,CAAC,YAAY;AAC/B,QAAM,QAAQ,SAAS,MAAM,QAAQ,MAAM;AACzC,UAAM,CAAC,EAAE,OAAK,EAAE,KAAK,IAAI,CAAQ;AAAA,EACnC,CAAC,CAAC;AAEF,SAAO,MAAM,CAAC;AAChB;","names":["Model"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { Pragma } from './adapter';\nimport { Context } from './context';\n\nPragma.useContext = () => Context.use();\n\nPragma.useLifecycle = (callback) => useEffect(() => callback(), []);\n\nPragma.useFactory = (factory) => {\n const state = useState(() => factory(() => {\n state[1](x => x.bind(null) as any);\n }));\n\n return state[0];\n}\n\nexport {\n Model, Model as default,\n get, use, ref, set, has\n} from '@expressive/mvc';\n\nexport { Consumer, Context, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { type Pragma };\n"],"mappings":";AAAA,SAAS,WAAW,gBAAgB;AAEpC,SAAS,cAAc;AACvB,SAAS,eAAe;AAcxB;AAAA,EACE;AAAA,EAAgB,SAATA;AAAA,EACP;AAAA,EAAK;AAAA,EAAK;AAAA,EAAK;AAAA,EAAK;AAAA,OACf;AAEP,SAAS,UAAU,WAAAC,UAAS,gBAAgB;AAC5C,SAAS,UAAU,qBAAqB;AAlBxC,OAAO,aAAa,MAAM,QAAQ,IAAI;AAEtC,OAAO,eAAe,CAAC,aAAa,UAAU,MAAM,SAAS,GAAG,CAAC,CAAC;AAElE,OAAO,aAAa,CAAC,YAAY;AAC/B,QAAM,QAAQ,SAAS,MAAM,QAAQ,MAAM;AACzC,UAAM,CAAC,EAAE,OAAK,EAAE,KAAK,IAAI,CAAQ;AAAA,EACnC,CAAC,CAAC;AAEF,SAAO,MAAM,CAAC;AAChB;","names":["Model","Context"]}
@@ -1,10 +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 Runtime 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
+ var jsxDEV = compat.bind(Runtime.jsxDEV);
6
6
  export {
7
7
  Fragment,
8
- jsxDEV2 as jsxDEV
8
+ jsxDEV
9
9
  };
10
10
  //# sourceMappingURL=jsx-dev-runtime.js.map
@@ -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 Runtime from 'react/jsx-dev-runtime';\n\nimport { compat } from './jsx-runtime';\n\nexport const jsxDEV = compat.bind(Runtime.jsxDEV);\n\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from \"react\";\n"],"mappings":";AAAA,OAAO,aAAa;AAEpB,SAAS,cAAc;AAKvB,SAAS,gBAAgB;AAHlB,IAAM,SAAS,OAAO,KAAK,QAAQ,MAAM;","names":[]}
@@ -1,54 +1,54 @@
1
1
  // src/jsx-runtime.ts
2
2
  import { Model } from "@expressive/mvc";
3
- import { jsx, jsxs } from "react/jsx-runtime";
4
- import { Provider } from "./context";
3
+ import React, { isValidElement } from "react";
4
+ import Runtime from "react/jsx-runtime";
5
+ import { Context, Lookup } from "./context";
5
6
  import { Fragment } from "react";
6
- function Render(props) {
7
- const { is, ...rest } = props;
8
- return jsx(Provider, {
9
- for: this,
10
- forEach: is,
11
- children: jsx(() => {
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];
17
- 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
- }, {});
32
- }, {})
33
- });
7
+ var Ambient = void 0;
8
+ var useContext = Context.use;
9
+ Context.use = (create) => {
10
+ if (Ambient || create === false)
11
+ return Ambient;
12
+ const context = useContext(create);
13
+ if (Ambient === null)
14
+ Ambient = context;
15
+ return context;
16
+ };
17
+ function provider(children) {
18
+ if (Ambient && (Array.isArray(children) || isValidElement(children) && children.type !== Lookup.Provider))
19
+ children = jsx(Lookup.Provider, { value: Ambient, children });
20
+ Ambient = void 0;
21
+ return children;
22
+ }
23
+ function Component(props) {
24
+ Ambient = null;
25
+ const local = this.use(props.is);
26
+ const render = props.render || local.render;
27
+ for (const key in local)
28
+ if (key in props)
29
+ local[key] = props[key];
30
+ return provider(render ? render(props, local) : props.children);
31
+ }
32
+ function FC(props, ref) {
33
+ Ambient = null;
34
+ return provider(this(props, ref));
34
35
  }
35
36
  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));
37
+ function compat(type, ...args) {
38
+ if (typeof type == "function") {
39
+ if (RENDER.has(type))
40
+ type = RENDER.get(type);
41
+ else if (typeof type == "function")
42
+ RENDER.set(type, type = type.prototype instanceof Model ? Component.bind(type) : type.prototype instanceof React.Component ? type : FC.bind(type));
43
43
  }
44
- return this(type, props, key, ...args);
44
+ return this(type, ...args);
45
45
  }
46
- var jsx2 = create.bind(jsx);
47
- var jsxs2 = create.bind(jsxs);
46
+ var jsx = compat.bind(Runtime.jsx);
47
+ var jsxs = compat.bind(Runtime.jsxs);
48
48
  export {
49
49
  Fragment,
50
- create,
51
- jsx2 as jsx,
52
- jsxs2 as jsxs
50
+ compat,
51
+ jsx,
52
+ jsxs
53
53
  };
54
54
  //# sourceMappingURL=jsx-runtime.js.map
@@ -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(jsxs);\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;AAsJzB,SAAS,gBAAgB;AAtEzB,SAAS,OAEP,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;AACtB,YAAM,EAAE,OAAO,IAAI;AAEnB,iBAAU,OAAO;AACf,YAAG,OAAO;AACR,UAAC,KAAa,GAAG,IAAI,KAAK,GAAG;AAEjC,UAAG;AACD,eAAO,IAAI,MAAM;AACf,gBAAMA,QAAO,KAAK,IAAI;AACtB,iBAAO,OAAO,KAAKA,OAAM,OAAOA,KAAI;AAAA,QACtC,GAAG,CAAC,CAAC;AAEP,YAAM,EAAE,SAAS,IAAI;AAErB,UAAG;AACD,eAAO;AAET,YAAM,KAAK,MAAM;AAEjB,UAAG,OAAO,MAAM;AACd,eAAO;AAET,aAAO,IAAI,MAAM;AACf,cAAMA,QAAO,KAAK,IAAI;AACtB,eAAO,GAAG,KAAKA,OAAMA,KAAI;AAAA,MAC3B,GAAG,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,MAAM,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,GAAG;AAC5B,IAAM,QAAQ,OAAO,KAAK,IAAI;","names":["self"]}
1
+ {"version":3,"sources":["../../src/jsx-runtime.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport React, { isValidElement } from 'react';\nimport Runtime from 'react/jsx-runtime';\n\nimport { Context, Lookup } 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\nlet Ambient: Context | null | undefined = undefined;\n\nconst useContext = Context.use;\n\nContext.use = (create?: boolean): any => {\n if(Ambient || create === false)\n return Ambient;\n\n const context = useContext(create);\n\n if(Ambient === null)\n Ambient = context;\n\n return context;\n}\n\nfunction provider(children: React.ReactNode) {\n if(Ambient && (Array.isArray(children) || isValidElement(children) && children.type !== Lookup.Provider))\n children = jsx(Lookup.Provider, { value: Ambient, children });\n\n Ambient = undefined;\n\n return children;\n}\n\nfunction Component<T extends Model.Compat>(\n this: Model.Init<T>,\n props: Model.Props<T>\n) {\n Ambient = null;\n const local = this.use(props.is);\n const render = props.render || local.render;\n\n for(const key in local)\n if(key in props)\n local[key] = (props as any)[key];\n\n return provider(render ? render(props, local) : props.children);\n}\n\nfunction FC<T extends {}>(this: React.FC<T>, props: T, ref: any) {\n Ambient = null;\n return provider(this(props, ref));\n}\n\nconst RENDER = new WeakMap<Function, React.ComponentType>();\n\nexport function compat(\n this: (type: React.ElementType, ...args: any[]) => React.ReactElement,\n type: React.ElementType | Model.Init, ...args: any[]): React.ReactElement {\n\n if(typeof type == \"function\")\n if(RENDER.has(type))\n type = RENDER.get(type)!;\n else if(typeof type == \"function\")\n RENDER.set(type, type = (\n type.prototype instanceof Model ?\n Component.bind(type as Model.Init) :\n type.prototype instanceof React.Component ? \n type as React.ComponentClass :\n FC.bind(type as React.FC)\n ));\n\n return this(type, ...args);\n}\n\nexport const jsx = compat.bind(Runtime.jsx);\nexport const jsxs = compat.bind(Runtime.jsxs);\n\nexport { Fragment } from \"react\";"],"mappings":";AAAA,SAAS,aAAa;AACtB,OAAO,SAAS,sBAAsB;AACtC,OAAO,aAAa;AAEpB,SAAS,SAAS,cAAc;AAqJhC,SAAS,gBAAgB;AArEzB,IAAI,UAAsC;AAE1C,IAAM,aAAa,QAAQ;AAE3B,QAAQ,MAAM,CAAC,WAA0B;AACvC,MAAG,WAAW,WAAW;AACvB,WAAO;AAET,QAAM,UAAU,WAAW,MAAM;AAEjC,MAAG,YAAY;AACb,cAAU;AAEZ,SAAO;AACT;AAEA,SAAS,SAAS,UAA2B;AAC3C,MAAG,YAAY,MAAM,QAAQ,QAAQ,KAAK,eAAe,QAAQ,KAAK,SAAS,SAAS,OAAO;AAC7F,eAAW,IAAI,OAAO,UAAU,EAAE,OAAO,SAAS,SAAS,CAAC;AAE9D,YAAU;AAEV,SAAO;AACT;AAEA,SAAS,UAEP,OACA;AACA,YAAU;AACV,QAAM,QAAQ,KAAK,IAAI,MAAM,EAAE;AAC/B,QAAM,SAAS,MAAM,UAAU,MAAM;AAErC,aAAU,OAAO;AACf,QAAG,OAAO;AACR,YAAM,GAAG,IAAK,MAAc,GAAG;AAEnC,SAAO,SAAS,SAAS,OAAO,OAAO,KAAK,IAAI,MAAM,QAAQ;AAChE;AAEA,SAAS,GAAoC,OAAU,KAAU;AAC/D,YAAU;AACV,SAAO,SAAS,KAAK,OAAO,GAAG,CAAC;AAClC;AAEA,IAAM,SAAS,oBAAI,QAAuC;AAEnD,SAAS,OAEd,SAAyC,MAAiC;AAE1E,MAAG,OAAO,QAAQ;AAChB,QAAG,OAAO,IAAI,IAAI;AAChB,aAAO,OAAO,IAAI,IAAI;AAAA,aAChB,OAAO,QAAQ;AACrB,aAAO,IAAI,MAAM,OACf,KAAK,qBAAqB,QACxB,UAAU,KAAK,IAAkB,IACnC,KAAK,qBAAqB,MAAM,YAC9B,OACA,GAAG,KAAK,IAAgB,CAC3B;AAAA;AAEL,SAAO,KAAK,MAAM,GAAG,IAAI;AAC3B;AAEO,IAAM,MAAM,OAAO,KAAK,QAAQ,GAAG;AACnC,IAAM,OAAO,OAAO,KAAK,QAAQ,IAAI;","names":[]}
package/dist/index.d.ts CHANGED
@@ -1,14 +1,20 @@
1
1
  export { Pragma } from './adapter';
2
- import Model, { Context } from '@expressive/mvc';
3
- export { Model, Model as default, get, has, ref, set, use } from '@expressive/mvc';
4
- import * as react from 'react';
2
+ import Model__default, { Context } from '@expressive/mvc';
3
+ export { Context, Model, Model as default, get, has, ref, set, use } from '@expressive/mvc';
4
+ import * as React from 'react';
5
5
  import { ReactNode } from 'react';
6
6
  export { Fragment, createElement } from 'react';
7
7
 
8
+ declare module '@expressive/mvc' {
9
+ namespace Context {
10
+ function use(create?: true): Context;
11
+ function use(create: boolean): Context | null | undefined;
12
+ }
13
+ }
8
14
  declare namespace Consumer {
9
- type Props<T extends Model> = {
15
+ type Props<T extends Model__default> = {
10
16
  /** Type of controller to fetch from context. */
11
- for: Model.Type<T>;
17
+ for: Model__default.Type<T>;
12
18
  /**
13
19
  * Render function, will receive instance of desired controller.
14
20
  *
@@ -19,14 +25,14 @@ declare namespace Consumer {
19
25
  children: (value: T) => ReactNode | void;
20
26
  };
21
27
  }
22
- declare function Consumer<T extends Model>(props: Consumer.Props<T>): string | number | boolean | react.ReactElement<any, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | react.ReactPortal | null;
28
+ declare function Consumer<T extends Model__default>(props: Consumer.Props<T>): string | number | boolean | React.ReactElement<any, string | React.JSXElementConstructor<any>> | Iterable<ReactNode> | React.ReactPortal | null;
23
29
  declare namespace Provider {
24
- interface Props<T extends Model> {
30
+ interface Props<T extends Model__default> {
25
31
  for: Context.Accept<T>;
26
32
  forEach?: Context.Expect<T>;
27
33
  children?: ReactNode;
28
34
  }
29
35
  }
30
- declare function Provider<T extends Model>(props: Provider.Props<T>): react.FunctionComponentElement<react.ProviderProps<Context>>;
36
+ declare function Provider<T extends Model__default>(props: Provider.Props<T>): React.FunctionComponentElement<React.ProviderProps<Context>>;
31
37
 
32
38
  export { Consumer, Provider };
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  Consumer: () => Consumer,
24
+ Context: () => import_mvc.Context,
24
25
  Fragment: () => import_react3.Fragment,
25
26
  Model: () => import_mvc2.Model,
26
27
  Provider: () => Provider,
@@ -40,16 +41,17 @@ var import_adapter = require("./adapter");
40
41
  var import_mvc = require("@expressive/mvc");
41
42
  var import_react = require("react");
42
43
  var Lookup = (0, import_react.createContext)(new import_mvc.Context());
44
+ import_mvc.Context.use = (create) => {
45
+ const ambient = (0, import_react.useContext)(Lookup);
46
+ return create ? (0, import_react.useMemo)(() => ambient.push(), []) : ambient;
47
+ };
43
48
  function Consumer(props) {
44
49
  return props.for.get((i) => props.children(i));
45
50
  }
46
51
  function Provider(props) {
47
- const ambient = (0, import_react.useContext)(Lookup);
48
- const context = (0, import_react.useMemo)(() => ambient.push(), [ambient]);
49
- (0, import_react.useEffect)(() => () => context.pop(), [ambient]);
52
+ const context = import_mvc.Context.use(true);
53
+ (0, import_react.useEffect)(() => () => context.pop(), [context]);
50
54
  context.include(props.for, (model) => {
51
- if ("set" in props)
52
- throw new Error(`The \`set\` prop is deprecated. Use \`forEach\` or <${model.constructor} {...props} /> instead.`);
53
55
  if (props.forEach) {
54
56
  const cleanup = props.forEach(model);
55
57
  if (cleanup)
@@ -65,7 +67,7 @@ function Provider(props) {
65
67
  // src/index.ts
66
68
  var import_mvc2 = require("@expressive/mvc");
67
69
  var import_react3 = require("react");
68
- import_adapter.Pragma.useContext = () => (0, import_react2.useContext)(Lookup);
70
+ import_adapter.Pragma.useContext = () => import_mvc.Context.use();
69
71
  import_adapter.Pragma.useLifecycle = (callback) => (0, import_react2.useEffect)(() => callback(), []);
70
72
  import_adapter.Pragma.useFactory = (factory) => {
71
73
  const state = (0, import_react2.useState)(() => factory(() => {
@@ -76,6 +78,7 @@ import_adapter.Pragma.useFactory = (factory) => {
76
78
  // Annotate the CommonJS export names for ESM import in node:
77
79
  0 && (module.exports = {
78
80
  Consumer,
81
+ Context,
79
82
  Fragment,
80
83
  Model,
81
84
  Provider,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/context.ts"],"sourcesContent":["import { useContext, useEffect, useState } from 'react';\n\nimport { Pragma } from './adapter';\nimport { Context } from './context';\n\nPragma.useContext = () => useContext(Context);\n\nPragma.useLifecycle = (callback) => useEffect(() => callback(), []);\n\nPragma.useFactory = (factory) => {\n const state = useState(() => factory(() => {\n state[1](x => x.bind(null) as any);\n }));\n\n return state[0];\n}\n\nexport {\n Model, Model as default,\n get, use, ref, set, has\n} from '@expressive/mvc';\n\nexport { Consumer, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { type Pragma };\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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,gBAAgD;AAEhD,qBAAuB;;;ACFvB,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AAkB1C,SAAS,SAA0B,OAAyB;AAC1D,SAAO,MAAM,IAAI,IAAI,OAAK,MAAM,SAAS,CAAC,CAAC;AAC7C;AAUA,SAAS,SAA0B,OAAyB;AAC1D,QAAM,cAAU,yBAAW,MAAM;AACjC,QAAM,cAAU,sBAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,OAAO,CAAC;AAEvD,8BAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,QAAQ,MAAM,KAAK,CAAC,UAAU;AAEpC,QAAG,SAAS;AACV,YAAM,IAAI,MAAM,uDAAuD,MAAM,WAAW,yBAAyB;AAEnH,QAAG,MAAM,SAAQ;AACf,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAG;AACD,cAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACF,CAAC;AAED,aAAO,4BAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;AAAA,EACT,GAAG,MAAM,QAAQ;AACnB;;;ADvCA,IAAAC,cAGO;AAGP,IAAAC,gBAAwC;AAlBxC,sBAAO,aAAa,UAAM,0BAAW,MAAO;AAE5C,sBAAO,eAAe,CAAC,iBAAa,yBAAU,MAAM,SAAS,GAAG,CAAC,CAAC;AAElE,sBAAO,aAAa,CAAC,YAAY;AAC/B,QAAM,YAAQ,wBAAS,MAAM,QAAQ,MAAM;AACzC,UAAM,CAAC,EAAE,OAAK,EAAE,KAAK,IAAI,CAAQ;AAAA,EACnC,CAAC,CAAC;AAEF,SAAO,MAAM,CAAC;AAChB;","names":["import_react","import_mvc","import_react"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/context.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { Pragma } from './adapter';\nimport { Context } from './context';\n\nPragma.useContext = () => Context.use();\n\nPragma.useLifecycle = (callback) => useEffect(() => callback(), []);\n\nPragma.useFactory = (factory) => {\n const state = useState(() => factory(() => {\n state[1](x => x.bind(null) as any);\n }));\n\n return state[0];\n}\n\nexport {\n Model, Model as default,\n get, use, ref, set, has\n} from '@expressive/mvc';\n\nexport { Consumer, Context, Provider } from './context';\nexport { Fragment, createElement } from 'react';\nexport { type Pragma };\n","import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = 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(Lookup);\n\n return create ?\n useMemo(() => ambient.push(), []) :\n ambient;\n}\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 context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.include(props.for, (model) => {\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, Context }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,gBAAoC;AAEpC,qBAAuB;;;ACFvB,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AAS1C,mBAAQ,MAAM,CAAC,WAAqB;AAClC,QAAM,cAAU,yBAAW,MAAM;AAEjC,SAAO,aACL,sBAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,CAAC,IAChC;AACJ;AAkBA,SAAS,SAA0B,OAAyB;AAC1D,SAAO,MAAM,IAAI,IAAI,OAAK,MAAM,SAAS,CAAC,CAAC;AAC7C;AAUA,SAAS,SAA0B,OAAyB;AAC1D,QAAM,UAAU,mBAAQ,IAAI,IAAI;AAEhC,8BAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,QAAQ,MAAM,KAAK,CAAC,UAAU;AACpC,QAAG,MAAM,SAAQ;AACf,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAG;AACD,cAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACF,CAAC;AAED,aAAO,4BAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;AAAA,EACT,GAAG,MAAM,QAAQ;AACnB;;;ADjDA,IAAAC,cAGO;AAGP,IAAAC,gBAAwC;AAlBxC,sBAAO,aAAa,MAAM,mBAAQ,IAAI;AAEtC,sBAAO,eAAe,CAAC,iBAAa,yBAAU,MAAM,SAAS,GAAG,CAAC,CAAC;AAElE,sBAAO,aAAa,CAAC,YAAY;AAC/B,QAAM,YAAQ,wBAAS,MAAM,QAAQ,MAAM;AACzC,UAAM,CAAC,EAAE,OAAK,EAAE,KAAK,IAAI,CAAQ;AAAA,EACnC,CAAC,CAAC;AAEF,SAAO,MAAM,CAAC;AAChB;","names":["import_react","import_mvc","import_react"]}
@@ -1,7 +1,8 @@
1
- import * as react from 'react';
1
+ import * as Model from '@expressive/mvc';
2
+ import * as React from 'react';
2
3
  export { Fragment } from 'react';
3
4
  export { JSX } from './jsx-runtime';
4
5
 
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>>;
6
+ declare const jsxDEV: (type: React.ElementType<any, keyof React.JSX.IntrinsicElements> | Model.Model.Init<Model.Model>, ...args: any[]) => React.ReactElement<any, string | React.JSXElementConstructor<any>>;
6
7
 
7
8
  export { jsxDEV };
@@ -31,13 +31,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var jsx_dev_runtime_exports = {};
32
32
  __export(jsx_dev_runtime_exports, {
33
33
  Fragment: () => import_react.Fragment,
34
- jsxDEV: () => jsxDEV2
34
+ jsxDEV: () => jsxDEV
35
35
  });
36
36
  module.exports = __toCommonJS(jsx_dev_runtime_exports);
37
- var Runtime = __toESM(require("react/jsx-dev-runtime"));
37
+ var import_jsx_dev_runtime = __toESM(require("react/jsx-dev-runtime"));
38
38
  var import_jsx_runtime = require("./jsx-runtime");
39
39
  var import_react = require("react");
40
- var jsxDEV2 = import_jsx_runtime.create.bind(Runtime.jsxDEV);
40
+ var jsxDEV = import_jsx_runtime.compat.bind(import_jsx_dev_runtime.default.jsxDEV);
41
41
  // Annotate the CommonJS export names for ESM import in node:
42
42
  0 && (module.exports = {
43
43
  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 Runtime from 'react/jsx-dev-runtime';\n\nimport { compat } from './jsx-runtime';\n\nexport const jsxDEV = compat.bind(Runtime.jsxDEV);\n\nexport { type JSX } from './jsx-runtime';\nexport { Fragment } from \"react\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAAoB;AAEpB,yBAAuB;AAKvB,mBAAyB;AAHlB,IAAM,SAAS,0BAAO,KAAK,uBAAAA,QAAQ,MAAM;","names":["Runtime"]}
@@ -1,10 +1,9 @@
1
- import * as react from 'react';
2
- export { Fragment } from 'react';
3
1
  import { Model } from '@expressive/mvc';
2
+ import React__default from 'react';
3
+ export { Fragment } from 'react';
4
4
 
5
- declare module '@expressive/mvc' {
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 };
@@ -32,47 +31,49 @@ declare module '@expressive/mvc' {
32
31
  * );
33
32
  * ```
34
33
  */
35
- type FC<T extends Model, P extends {} = {}> = React.FC<FC.Props<T, P>>;
34
+ type FC<T extends Model, P extends {} = {}> = React__default.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__default.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__default.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__default.ReactNode;
51
52
  };
52
53
  }
53
54
  }
54
55
  declare namespace JSX {
55
- type ElementType = Model.Type<Model.Compat> | React.JSX.ElementType;
56
+ type ElementType = Model.Type<Model.Compat> | React__default.JSX.ElementType;
56
57
  type LibraryManagedAttributes<C, P> = C extends new (...args: any[]) => {
57
58
  props: infer U;
58
- } ? U : C extends Model.Type<infer U> ? Model.Props<U> : React.JSX.LibraryManagedAttributes<C, P>;
59
- interface Element extends React.JSX.Element {
59
+ } ? U : C extends Model.Type<infer U> ? Model.Props<U> : React__default.JSX.LibraryManagedAttributes<C, P>;
60
+ interface Element extends React__default.JSX.Element {
60
61
  }
61
- interface ElementClass extends React.JSX.ElementClass {
62
+ interface ElementClass extends React__default.JSX.ElementClass {
62
63
  }
63
64
  interface ElementAttributesProperty {
64
65
  }
65
- interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {
66
+ interface ElementChildrenAttribute extends React__default.JSX.ElementChildrenAttribute {
66
67
  }
67
- interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {
68
+ interface IntrinsicAttributes extends React__default.JSX.IntrinsicAttributes {
68
69
  }
69
- interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {
70
+ interface IntrinsicClassAttributes<T> extends React__default.JSX.IntrinsicClassAttributes<T> {
70
71
  }
71
- interface IntrinsicElements extends React.JSX.IntrinsicElements {
72
+ interface IntrinsicElements extends React__default.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(this: (type: React__default.ElementType, ...args: any[]) => React__default.ReactElement, type: React__default.ElementType | Model.Init, ...args: any[]): React__default.ReactElement;
76
+ declare const jsx: (type: React__default.ElementType<any, keyof React__default.JSX.IntrinsicElements> | Model.Init<Model>, ...args: any[]) => React__default.ReactElement<any, string | React__default.JSXElementConstructor<any>>;
77
+ declare const jsxs: (type: React__default.ElementType<any, keyof React__default.JSX.IntrinsicElements> | Model.Init<Model>, ...args: any[]) => React__default.ReactElement<any, string | React__default.JSXElementConstructor<any>>;
77
78
 
78
- export { JSX, create, jsx2 as jsx, jsxs2 as jsxs };
79
+ export { JSX, compat, jsx, jsxs };
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,91 +17,85 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
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
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/jsx-runtime.ts
21
31
  var jsx_runtime_exports = {};
22
32
  __export(jsx_runtime_exports, {
23
- Fragment: () => import_react2.Fragment,
24
- create: () => create,
25
- jsx: () => jsx2,
26
- jsxs: () => jsxs2
33
+ Fragment: () => import_react3.Fragment,
34
+ compat: () => compat,
35
+ jsx: () => jsx,
36
+ jsxs: () => jsxs
27
37
  });
28
38
  module.exports = __toCommonJS(jsx_runtime_exports);
29
39
  var import_mvc2 = require("@expressive/mvc");
30
- var import_jsx_runtime = require("react/jsx-runtime");
40
+ var import_react2 = __toESM(require("react"));
41
+ var import_jsx_runtime = __toESM(require("react/jsx-runtime"));
31
42
 
32
43
  // src/context.ts
33
44
  var import_mvc = require("@expressive/mvc");
34
45
  var import_react = require("react");
35
46
  var Lookup = (0, import_react.createContext)(new import_mvc.Context());
36
- function Provider(props) {
47
+ import_mvc.Context.use = (create) => {
37
48
  const ambient = (0, import_react.useContext)(Lookup);
38
- const context = (0, import_react.useMemo)(() => ambient.push(), [ambient]);
39
- (0, import_react.useEffect)(() => () => context.pop(), [ambient]);
40
- context.include(props.for, (model) => {
41
- if ("set" in props)
42
- throw new Error(`The \`set\` prop is deprecated. Use \`forEach\` or <${model.constructor} {...props} /> instead.`);
43
- if (props.forEach) {
44
- const cleanup = props.forEach(model);
45
- if (cleanup)
46
- model.set(null, cleanup);
47
- }
48
- });
49
- return (0, import_react.createElement)(Lookup.Provider, {
50
- key: context.id,
51
- value: context
52
- }, props.children);
53
- }
49
+ return create ? (0, import_react.useMemo)(() => ambient.push(), []) : ambient;
50
+ };
54
51
 
55
52
  // src/jsx-runtime.ts
56
- var import_react2 = require("react");
57
- function Render(props) {
58
- const { is, ...rest } = props;
59
- return (0, import_jsx_runtime.jsx)(Provider, {
60
- for: this,
61
- forEach: is,
62
- children: (0, import_jsx_runtime.jsx)(() => {
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];
68
- 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
- }, {});
83
- }, {})
84
- });
53
+ var import_react3 = require("react");
54
+ var Ambient = void 0;
55
+ var useContext2 = import_mvc.Context.use;
56
+ import_mvc.Context.use = (create) => {
57
+ if (Ambient || create === false)
58
+ return Ambient;
59
+ const context = useContext2(create);
60
+ if (Ambient === null)
61
+ Ambient = context;
62
+ return context;
63
+ };
64
+ function provider(children) {
65
+ if (Ambient && (Array.isArray(children) || (0, import_react2.isValidElement)(children) && children.type !== Lookup.Provider))
66
+ children = jsx(Lookup.Provider, { value: Ambient, children });
67
+ Ambient = void 0;
68
+ return children;
69
+ }
70
+ function Component(props) {
71
+ Ambient = null;
72
+ const local = this.use(props.is);
73
+ const render = props.render || local.render;
74
+ for (const key in local)
75
+ if (key in props)
76
+ local[key] = props[key];
77
+ return provider(render ? render(props, local) : props.children);
78
+ }
79
+ function FC(props, ref) {
80
+ Ambient = null;
81
+ return provider(this(props, ref));
85
82
  }
86
83
  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));
84
+ function compat(type, ...args) {
85
+ if (typeof type == "function") {
86
+ if (RENDER.has(type))
87
+ type = RENDER.get(type);
88
+ else if (typeof type == "function")
89
+ RENDER.set(type, type = type.prototype instanceof import_mvc2.Model ? Component.bind(type) : type.prototype instanceof import_react2.default.Component ? type : FC.bind(type));
94
90
  }
95
- return this(type, props, key, ...args);
91
+ return this(type, ...args);
96
92
  }
97
- var jsx2 = create.bind(import_jsx_runtime.jsx);
98
- var jsxs2 = create.bind(import_jsx_runtime.jsxs);
93
+ var jsx = compat.bind(import_jsx_runtime.default.jsx);
94
+ var jsxs = compat.bind(import_jsx_runtime.default.jsxs);
99
95
  // Annotate the CommonJS export names for ESM import in node:
100
96
  0 && (module.exports = {
101
97
  Fragment,
102
- create,
98
+ compat,
103
99
  jsx,
104
100
  jsxs
105
101
  });
@@ -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,SAAS,SAA0B,OAAyB;AAC1D,QAAM,cAAU,yBAAW,MAAM;AACjC,QAAM,cAAU,sBAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,OAAO,CAAC;AAEvD,8BAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,QAAQ,MAAM,KAAK,CAAC,UAAU;AAEpC,QAAG,SAAS;AACV,YAAM,IAAI,MAAM,uDAAuD,MAAM,WAAW,yBAAyB;AAEnH,QAAG,MAAM,SAAQ;AACf,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAG;AACD,cAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACF,CAAC;AAED,aAAO,4BAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;A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],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport React, { isValidElement } from 'react';\nimport Runtime from 'react/jsx-runtime';\n\nimport { Context, Lookup } 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\nlet Ambient: Context | null | undefined = undefined;\n\nconst useContext = Context.use;\n\nContext.use = (create?: boolean): any => {\n if(Ambient || create === false)\n return Ambient;\n\n const context = useContext(create);\n\n if(Ambient === null)\n Ambient = context;\n\n return context;\n}\n\nfunction provider(children: React.ReactNode) {\n if(Ambient && (Array.isArray(children) || isValidElement(children) && children.type !== Lookup.Provider))\n children = jsx(Lookup.Provider, { value: Ambient, children });\n\n Ambient = undefined;\n\n return children;\n}\n\nfunction Component<T extends Model.Compat>(\n this: Model.Init<T>,\n props: Model.Props<T>\n) {\n Ambient = null;\n const local = this.use(props.is);\n const render = props.render || local.render;\n\n for(const key in local)\n if(key in props)\n local[key] = (props as any)[key];\n\n return provider(render ? render(props, local) : props.children);\n}\n\nfunction FC<T extends {}>(this: React.FC<T>, props: T, ref: any) {\n Ambient = null;\n return provider(this(props, ref));\n}\n\nconst RENDER = new WeakMap<Function, React.ComponentType>();\n\nexport function compat(\n this: (type: React.ElementType, ...args: any[]) => React.ReactElement,\n type: React.ElementType | Model.Init, ...args: any[]): React.ReactElement {\n\n if(typeof type == \"function\")\n if(RENDER.has(type))\n type = RENDER.get(type)!;\n else if(typeof type == \"function\")\n RENDER.set(type, type = (\n type.prototype instanceof Model ?\n Component.bind(type as Model.Init) :\n type.prototype instanceof React.Component ? \n type as React.ComponentClass :\n FC.bind(type as React.FC)\n ));\n\n return this(type, ...args);\n}\n\nexport const jsx = compat.bind(Runtime.jsx);\nexport const jsxs = compat.bind(Runtime.jsxs);\n\nexport { Fragment } from \"react\";","import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = 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(Lookup);\n\n return create ?\n useMemo(() => ambient.push(), []) :\n ambient;\n}\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 context = Context.use(true);\n\n useEffect(() => () => context.pop(), [context]);\n\n context.include(props.for, (model) => {\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, Context }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,cAAsB;AACtB,IAAAC,gBAAsC;AACtC,yBAAoB;;;ACFpB,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AAS1C,mBAAQ,MAAM,CAAC,WAAqB;AAClC,QAAM,cAAU,yBAAW,MAAM;AAEjC,SAAO,aACL,sBAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,CAAC,IAChC;AACJ;;;ADuIA,IAAAC,gBAAyB;AArEzB,IAAI,UAAsC;AA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/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.60.0"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "react": ">=16.8.0 <19.0.0"
@@ -60,13 +60,12 @@
60
60
  }
61
61
  },
62
62
  "devDependencies": {
63
+ "@testing-library/react": "^16.3.0",
63
64
  "@types/react": "^18.2.14",
64
- "@types/react-test-renderer": "^18.0.0",
65
- "jest": "^27.0.6",
65
+ "jest": "^27.5.1",
66
66
  "react": "^18.2.0",
67
- "react-test-renderer": "^18.2.0",
68
67
  "tsup": "^8.0.2"
69
68
  },
70
- "version": "0.58.4",
71
- "gitHead": "614b3b4980892b0776e420e68b30d97ddae61ae7"
69
+ "version": "0.60.0",
70
+ "gitHead": "a147241b0028524bc6807f233185bda26ae8f969"
72
71
  }