@expressive/react 0.59.0 → 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 +3 -3
- package/dist/adapter.js +18 -35
- package/dist/adapter.js.map +1 -1
- package/dist/esm/adapter.js +18 -21
- package/dist/esm/adapter.js.map +1 -1
- package/dist/esm/context.js +8 -6
- package/dist/esm/context.js.map +1 -1
- package/dist/esm/index.js +4 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jsx-dev-runtime.js +3 -5
- package/dist/esm/jsx-dev-runtime.js.map +1 -1
- package/dist/esm/jsx-runtime.js +42 -29
- package/dist/esm/jsx-runtime.js.map +1 -1
- package/dist/index.d.ts +14 -8
- package/dist/index.js +9 -6
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +4 -5
- package/dist/jsx-dev-runtime.js +13 -5
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +19 -19
- package/dist/jsx-runtime.js +56 -47
- package/dist/jsx-runtime.js.map +1 -1
- package/package.json +5 -6
package/dist/adapter.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context } from '@expressive/mvc';
|
|
2
|
-
import
|
|
2
|
+
import React__default, { FunctionComponent } from 'react';
|
|
3
3
|
|
|
4
4
|
declare module '@expressive/mvc' {
|
|
5
5
|
namespace Model {
|
|
@@ -12,7 +12,7 @@ declare module '@expressive/mvc' {
|
|
|
12
12
|
is?: (instance: T) => void | (() => void);
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
function as<T extends Model, P extends Model.Assign<T>>(this: Model.Init<T>, render: (props: P, self: T) =>
|
|
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
|
|
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
|
@@ -32,41 +32,24 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_mvc = require("@expressive/mvc");
|
|
33
33
|
var import_react = require("react");
|
|
34
34
|
var Lookup = (0, import_react.createContext)(new import_mvc.Context());
|
|
35
|
-
|
|
35
|
+
import_mvc.Context.use = (create) => {
|
|
36
36
|
const ambient = (0, import_react.useContext)(Lookup);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
context.include(props.for, (model) => {
|
|
40
|
-
if ("set" in props)
|
|
41
|
-
throw new Error(`The \`set\` prop is deprecated. Use \`forEach\` or <${model.constructor} {...props} /> instead.`);
|
|
42
|
-
if (props.forEach) {
|
|
43
|
-
const cleanup = props.forEach(model);
|
|
44
|
-
if (cleanup)
|
|
45
|
-
model.set(null, cleanup);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
return (0, import_react.createElement)(Lookup.Provider, {
|
|
49
|
-
key: context.id,
|
|
50
|
-
value: context
|
|
51
|
-
}, props.children);
|
|
52
|
-
}
|
|
37
|
+
return create ? (0, import_react.useMemo)(() => ambient.push(), []) : ambient;
|
|
38
|
+
};
|
|
53
39
|
|
|
54
40
|
// src/model.as.ts
|
|
55
41
|
import_mvc2.Model.as = function(render) {
|
|
56
42
|
if (this === import_mvc2.Model)
|
|
57
43
|
throw new Error("Cannot create component from base Model.");
|
|
58
44
|
const Component = (props) => {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
(
|
|
66
|
-
|
|
67
|
-
render.length > 1 ? this.get() : void 0
|
|
68
|
-
), {})
|
|
69
|
-
);
|
|
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
|
+
});
|
|
70
53
|
};
|
|
71
54
|
Component.Model = this;
|
|
72
55
|
Component.displayName = this.name;
|
|
@@ -77,12 +60,12 @@ import_mvc2.Model.as = function(render) {
|
|
|
77
60
|
var import_mvc3 = require("@expressive/mvc");
|
|
78
61
|
var import_adapter = require("./adapter");
|
|
79
62
|
import_mvc3.Model.use = function(argument, repeat) {
|
|
80
|
-
const
|
|
63
|
+
const context = import_mvc3.Context.use(true);
|
|
81
64
|
const render = import_adapter.Pragma.useFactory((refresh) => {
|
|
82
65
|
let enabled;
|
|
83
66
|
let local;
|
|
84
67
|
const instance = new this(argument);
|
|
85
|
-
|
|
68
|
+
context.include(instance);
|
|
86
69
|
const unwatch = instance.get((current) => {
|
|
87
70
|
local = current;
|
|
88
71
|
if (enabled)
|
|
@@ -92,19 +75,19 @@ import_mvc3.Model.use = function(argument, repeat) {
|
|
|
92
75
|
enabled = true;
|
|
93
76
|
return () => {
|
|
94
77
|
unwatch();
|
|
95
|
-
instance.set(null);
|
|
96
78
|
context.pop();
|
|
79
|
+
instance.set(null);
|
|
97
80
|
};
|
|
98
81
|
}
|
|
99
82
|
return (props) => {
|
|
100
83
|
import_adapter.Pragma.useLifecycle(didMount);
|
|
101
84
|
if (enabled && repeat && props) {
|
|
102
85
|
enabled = false;
|
|
103
|
-
if (typeof props == "function")
|
|
86
|
+
if (typeof props == "function") {
|
|
104
87
|
props.call(instance, instance);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const update = instance.set();
|
|
88
|
+
props = void 0;
|
|
89
|
+
}
|
|
90
|
+
const update = instance.set(props);
|
|
108
91
|
if (update)
|
|
109
92
|
update.then(() => enabled = true);
|
|
110
93
|
else
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/adapter.ts","../src/model.as.ts","../src/context.ts","../src/model.use.ts","../src/model.get.ts"],"sourcesContent":["import { Context } from '@expressive/mvc';\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends () => unknown>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';","import { Model } from '@expressive/mvc';\nimport React, { createElement, FunctionComponent, useState } from 'react';\n\nimport { Provider } from './context';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> extends FunctionComponent<P & Component.Props<T>> {\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n namespace Component {\n type Props<T extends Model> = \n & Partial<Pick<T, Exclude<keyof T, keyof Model>>>\n & {\n is?: (instance: T) => void | (() => void);\n }\n }\n\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (props: P, self: T) => React.ReactNode\n ): Component<T, P>;\n }\n}\n\nModel.as = function <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>,\n render: (props: P, self: T) => React.ReactNode\n){\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component: Model.Component<T, P> = (props) => {\n const { is, ...rest } = props;\n const self = useState(() => this.new(rest as Model.Assign<T>, is))[0];\n\n Object.assign(Object.create(self), rest);\n\n return createElement(Provider, { for: self },\n createElement(() => render(\n props as P, render.length > 1 ? this.get() : undefined as never\n ), {})\n );\n }\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import Model, { Context } from '@expressive/mvc';\nimport { ReactNode, createContext, createElement, useContext, useEffect, useMemo } from 'react';\n\nconst Lookup = createContext(new Context());\n\ndeclare namespace Consumer {\n type Props<T extends Model> = {\n /** Type of controller to fetch from context. */\n for: Model.Type<T>;\n\n /**\n * Render function, will receive instance of desired controller.\n *\n * Called every render of parent component.\n * Similar to `Model.get()`, updates to properties accessed in\n * this function will cause a refresh when they change.\n */\n children: (value: T) => ReactNode | void;\n }\n}\n\nfunction Consumer<T extends Model>(props: Consumer.Props<T>){\n return props.for.get(i => props.children(i));\n}\n\ndeclare namespace Provider {\n interface Props<T extends Model> {\n for: Context.Accept<T>;\n forEach?: Context.Expect<T>;\n children?: ReactNode;\n }\n}\n\nfunction Provider<T extends Model>(props: Provider.Props<T>){\n const ambient = useContext(Lookup);\n const context = useMemo(() => ambient.push(), [ambient]);\n\n useEffect(() => () => context.pop(), [ambient]);\n \n context.include(props.for, (model) => {\n // TODO: remove next minor release\n if(\"set\" in props)\n throw new Error(`The \\`set\\` prop is deprecated. Use \\`forEach\\` or <${model.constructor} {...props} /> instead.`);\n\n if(props.forEach){\n const cleanup = props.forEach(model);\n\n if(cleanup)\n model.set(null, cleanup);\n }\n });\n\n return createElement(Lookup.Provider, {\n key: context.id,\n value: context\n }, props.children);\n}\n\nexport { Consumer, Provider, Lookup as Context }","import { Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const ambient = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n const context = ambient.push({ instance });\n const unwatch = instance.get(current => {\n local = current;\n\n if(enabled) \n refresh();\n });\n\n function didMount(){\n enabled = true;\n return () => {\n unwatch();\n instance.set(null);\n context.pop()\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n\n if(enabled && repeat && props){\n enabled = false;\n\n if(typeof props == \"function\")\n props.call(instance, instance);\n else if(typeof props == \"object\")\n instance.set(props);\n\n const update = instance.set();\n\n if(update)\n update.then(() => enabled = true);\n else\n enabled = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n function forceUpdate(): void;\n function forceUpdate<T>(action: Promise<T> | (() => Promise<T>)): Promise<T>;\n function forceUpdate<T>(action?: Promise<T> | (() => Promise<T>)){\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let unwatch: (() => void) | undefined;\n let value: any;\n\n unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, forceUpdate);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(unwatch)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n // TODO: ignore update if resolves to undefined or null\n value.then(x => value = x).catch(e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useLifecycle(() => unwatch);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;;;ACAA,IAAAC,cAAsB;AACtB,IAAAC,gBAAkE;;;ACDlE,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,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,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
|
+
{"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);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;;;ACAA,IAAAC,cAAsB;AACtB,IAAAC,gBAAwD;;;ACDxD,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AAS1C,mBAAQ,MAAM,CAAC,WAAqB;A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C;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"]}
|
package/dist/esm/adapter.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
// src/model.as.ts
|
|
2
2
|
import { Model } from "@expressive/mvc";
|
|
3
|
-
import { createElement
|
|
4
|
-
import {
|
|
3
|
+
import { createElement } from "react";
|
|
4
|
+
import { Context, Lookup } from "./context";
|
|
5
5
|
Model.as = function(render) {
|
|
6
6
|
if (this === Model)
|
|
7
7
|
throw new Error("Cannot create component from base Model.");
|
|
8
8
|
const Component = (props) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
render.length > 1 ? this.get() : void 0
|
|
18
|
-
), {})
|
|
19
|
-
);
|
|
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
|
+
});
|
|
20
17
|
};
|
|
21
18
|
Component.Model = this;
|
|
22
19
|
Component.displayName = this.name;
|
|
@@ -24,15 +21,15 @@ Model.as = function(render) {
|
|
|
24
21
|
};
|
|
25
22
|
|
|
26
23
|
// src/model.use.ts
|
|
27
|
-
import { Model as Model2 } from "@expressive/mvc";
|
|
24
|
+
import { Model as Model2, Context as Context2 } from "@expressive/mvc";
|
|
28
25
|
import { Pragma } from "./adapter";
|
|
29
26
|
Model2.use = function(argument, repeat) {
|
|
30
|
-
const
|
|
27
|
+
const context = Context2.use(true);
|
|
31
28
|
const render = Pragma.useFactory((refresh) => {
|
|
32
29
|
let enabled;
|
|
33
30
|
let local;
|
|
34
31
|
const instance = new this(argument);
|
|
35
|
-
|
|
32
|
+
context.include(instance);
|
|
36
33
|
const unwatch = instance.get((current) => {
|
|
37
34
|
local = current;
|
|
38
35
|
if (enabled)
|
|
@@ -42,19 +39,19 @@ Model2.use = function(argument, repeat) {
|
|
|
42
39
|
enabled = true;
|
|
43
40
|
return () => {
|
|
44
41
|
unwatch();
|
|
45
|
-
instance.set(null);
|
|
46
42
|
context.pop();
|
|
43
|
+
instance.set(null);
|
|
47
44
|
};
|
|
48
45
|
}
|
|
49
46
|
return (props) => {
|
|
50
47
|
Pragma.useLifecycle(didMount);
|
|
51
48
|
if (enabled && repeat && props) {
|
|
52
49
|
enabled = false;
|
|
53
|
-
if (typeof props == "function")
|
|
50
|
+
if (typeof props == "function") {
|
|
54
51
|
props.call(instance, instance);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const update = instance.set();
|
|
52
|
+
props = void 0;
|
|
53
|
+
}
|
|
54
|
+
const update = instance.set(props);
|
|
58
55
|
if (update)
|
|
59
56
|
update.then(() => enabled = true);
|
|
60
57
|
else
|
package/dist/esm/adapter.js.map
CHANGED
|
@@ -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';\nimport React, { createElement, FunctionComponent
|
|
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,qBAAwC;AAExD,SAAS,SAAS,cAAc;AAuBhC,MAAM,KAAK,SAET,QACD;AACC,MAAG,SAAS;AACV,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,YAAmC,CAAC,UAAU;AAClD,UAAM,QAAQ,KAAK,IAAI,MAAM,EAAE;AAE/B,eAAU,OAAO;AACf,UAAG,OAAO;AACR,cAAM,GAAG,IAAK,MAAc,GAAG;AAEnC,WAAO,cAAc,OAAO,UAAU;AAAA,MACpC,OAAO,QAAQ,IAAI,KAAK;AAAA,MACxB,UAAU,OAAO,OAAO,KAAK;AAAA,IAC/B,CAAC;AAAA,EACH;AAEA,YAAU,QAAQ;AAClB,YAAU,cAAc,KAAK;AAE7B,SAAO;AACT;;;AClDA,SAAS,SAAAA,QAAO,WAAAC,gBAAe;AAE/B,SAAS,cAAc;AAkBvBD,OAAM,MAAM,SAEV,UACA,QAAiB;AAEjB,QAAM,UAAUC,SAAQ,IAAI,IAAI;AAChC,QAAM,SAAS,OAAO,WAA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C,cAAM,OAAO,SAAS,KAAK,SAAS,SAAS,WAAW;AAExD,YAAG,SAAS;AACV;AAEF,gBAAQ;AAAA,MACV;AAEE,gBAAQ;AAEV,UAAG;AACD,gBAAQ;AAAA,IACZ,GAAG,aAAa,IAAI;AAEpB,QAAG,iBAAiB,SAAQ;AAC1B,UAAI;AAEJ,cAAQ;AAGR,YAAM,KAAK,OAAK,QAAQ,CAAC,EAAE,MAAM,OAAK,QAAQ,CAAC,EAAE,QAAQ,OAAO;AAChE,cAAQ;AAER,aAAO,MAAM;AACX,YAAG;AACD,gBAAM;AAER,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,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","Context","Model","Pragma","Pragma"]}
|
package/dist/esm/context.js
CHANGED
|
@@ -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
|
|
10
|
-
|
|
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
|
-
|
|
29
|
+
Context,
|
|
30
|
+
Lookup,
|
|
29
31
|
Provider
|
|
30
32
|
};
|
|
31
33
|
//# sourceMappingURL=context.js.map
|
package/dist/esm/context.js.map
CHANGED
|
@@ -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
|
|
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 {
|
|
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 = () =>
|
|
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,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import {
|
|
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,12 +1,10 @@
|
|
|
1
1
|
// src/jsx-dev-runtime.ts
|
|
2
|
-
import
|
|
2
|
+
import Runtime from "react/jsx-dev-runtime";
|
|
3
3
|
import { compat } from "./jsx-runtime";
|
|
4
4
|
import { Fragment } from "react";
|
|
5
|
-
|
|
6
|
-
return jsxDEV(compat(type), props, key, isStatic, source, self);
|
|
7
|
-
}
|
|
5
|
+
var jsxDEV = compat.bind(Runtime.jsxDEV);
|
|
8
6
|
export {
|
|
9
7
|
Fragment,
|
|
10
|
-
|
|
8
|
+
jsxDEV
|
|
11
9
|
};
|
|
12
10
|
//# sourceMappingURL=jsx-dev-runtime.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/jsx-dev-runtime.ts"],"sourcesContent":["import
|
|
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":[]}
|
package/dist/esm/jsx-runtime.js
CHANGED
|
@@ -1,41 +1,54 @@
|
|
|
1
1
|
// src/jsx-runtime.ts
|
|
2
2
|
import { Model } from "@expressive/mvc";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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);
|
|
28
31
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
32
|
+
function FC(props, ref) {
|
|
33
|
+
Ambient = null;
|
|
34
|
+
return provider(this(props, ref));
|
|
31
35
|
}
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
var RENDER = /* @__PURE__ */ new WeakMap();
|
|
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
|
+
}
|
|
44
|
+
return this(type, ...args);
|
|
34
45
|
}
|
|
46
|
+
var jsx = compat.bind(Runtime.jsx);
|
|
47
|
+
var jsxs = compat.bind(Runtime.jsxs);
|
|
35
48
|
export {
|
|
36
49
|
Fragment,
|
|
37
50
|
compat,
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
jsx,
|
|
52
|
+
jsxs
|
|
40
53
|
};
|
|
41
54
|
//# sourceMappingURL=jsx-runtime.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/jsx-runtime.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport {
|
|
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
|
|
3
|
-
export { Model, Model as default, get, has, ref, set, use } from '@expressive/mvc';
|
|
4
|
-
import * as
|
|
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
|
|
15
|
+
type Props<T extends Model__default> = {
|
|
10
16
|
/** Type of controller to fetch from context. */
|
|
11
|
-
for:
|
|
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
|
|
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
|
|
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
|
|
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
|
|
48
|
-
|
|
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 = () =>
|
|
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 {
|
|
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,9 +1,8 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as Model from '@expressive/mvc';
|
|
2
|
+
import * as React from 'react';
|
|
2
3
|
export { Fragment } from 'react';
|
|
3
|
-
import { JSXSource } from 'react/jsx-dev-runtime';
|
|
4
|
-
import { Model } from '@expressive/mvc';
|
|
5
4
|
export { JSX } from './jsx-runtime';
|
|
6
5
|
|
|
7
|
-
declare
|
|
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>>;
|
|
8
7
|
|
|
9
|
-
export {
|
|
8
|
+
export { jsxDEV };
|
package/dist/jsx-dev-runtime.js
CHANGED
|
@@ -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,21 +17,27 @@ 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-dev-runtime.ts
|
|
21
31
|
var jsx_dev_runtime_exports = {};
|
|
22
32
|
__export(jsx_dev_runtime_exports, {
|
|
23
33
|
Fragment: () => import_react.Fragment,
|
|
24
|
-
jsxDEV: () =>
|
|
34
|
+
jsxDEV: () => jsxDEV
|
|
25
35
|
});
|
|
26
36
|
module.exports = __toCommonJS(jsx_dev_runtime_exports);
|
|
27
|
-
var import_jsx_dev_runtime = require("react/jsx-dev-runtime");
|
|
37
|
+
var import_jsx_dev_runtime = __toESM(require("react/jsx-dev-runtime"));
|
|
28
38
|
var import_jsx_runtime = require("./jsx-runtime");
|
|
29
39
|
var import_react = require("react");
|
|
30
|
-
|
|
31
|
-
return (0, import_jsx_dev_runtime.jsxDEV)((0, import_jsx_runtime.compat)(type), props, key, isStatic, source, self);
|
|
32
|
-
}
|
|
40
|
+
var jsxDEV = import_jsx_runtime.compat.bind(import_jsx_dev_runtime.default.jsxDEV);
|
|
33
41
|
// Annotate the CommonJS export names for ESM import in node:
|
|
34
42
|
0 && (module.exports = {
|
|
35
43
|
Fragment,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/jsx-dev-runtime.ts"],"sourcesContent":["import
|
|
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"]}
|
package/dist/jsx-runtime.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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
|
|
5
|
+
declare module "@expressive/mvc" {
|
|
6
6
|
namespace Model {
|
|
7
7
|
namespace FC {
|
|
8
8
|
type Extends<T extends Model, P extends {} = {}> = Props<T> & P;
|
|
@@ -31,10 +31,10 @@ declare module '@expressive/mvc' {
|
|
|
31
31
|
* );
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
|
-
type FC<T extends Model, P extends {} = {}> =
|
|
34
|
+
type FC<T extends Model, P extends {} = {}> = React__default.FC<FC.Props<T, P>>;
|
|
35
35
|
/** Model which is not incompatable as Component in React. */
|
|
36
36
|
interface Compat extends Model {
|
|
37
|
-
render?(props: HasProps<this>, self: this):
|
|
37
|
+
render?(props: HasProps<this>, self: this): React__default.ReactNode;
|
|
38
38
|
}
|
|
39
39
|
interface BaseProps<T extends Model> {
|
|
40
40
|
/**
|
|
@@ -42,38 +42,38 @@ declare module '@expressive/mvc' {
|
|
|
42
42
|
* @returns Callback to run when instance is destroyed.
|
|
43
43
|
*/
|
|
44
44
|
is?: (instance: T) => void | (() => void);
|
|
45
|
-
render?(props: HasProps<T>, self: T):
|
|
45
|
+
render?(props: HasProps<T>, self: T): React__default.ReactNode;
|
|
46
46
|
}
|
|
47
47
|
type HasProps<T extends Model> = Partial<Pick<T, Exclude<keyof T, keyof Model>>>;
|
|
48
48
|
type Props<T extends Model> = T extends {
|
|
49
49
|
render(props: infer P, self: any): any;
|
|
50
50
|
} ? BaseProps<T> & HasProps<T> & P : BaseProps<T> & HasProps<T> & {
|
|
51
|
-
children?:
|
|
51
|
+
children?: React__default.ReactNode;
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
declare namespace JSX {
|
|
56
|
-
type ElementType = Model.Type<Model.Compat> |
|
|
56
|
+
type ElementType = Model.Type<Model.Compat> | React__default.JSX.ElementType;
|
|
57
57
|
type LibraryManagedAttributes<C, P> = C extends new (...args: any[]) => {
|
|
58
58
|
props: infer U;
|
|
59
|
-
} ? U : C extends Model.Type<infer U> ? Model.Props<U> :
|
|
60
|
-
interface Element extends
|
|
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 {
|
|
61
61
|
}
|
|
62
|
-
interface ElementClass extends
|
|
62
|
+
interface ElementClass extends React__default.JSX.ElementClass {
|
|
63
63
|
}
|
|
64
64
|
interface ElementAttributesProperty {
|
|
65
65
|
}
|
|
66
|
-
interface ElementChildrenAttribute extends
|
|
66
|
+
interface ElementChildrenAttribute extends React__default.JSX.ElementChildrenAttribute {
|
|
67
67
|
}
|
|
68
|
-
interface IntrinsicAttributes extends
|
|
68
|
+
interface IntrinsicAttributes extends React__default.JSX.IntrinsicAttributes {
|
|
69
69
|
}
|
|
70
|
-
interface IntrinsicClassAttributes<T> extends
|
|
70
|
+
interface IntrinsicClassAttributes<T> extends React__default.JSX.IntrinsicClassAttributes<T> {
|
|
71
71
|
}
|
|
72
|
-
interface IntrinsicElements extends
|
|
72
|
+
interface IntrinsicElements extends React__default.JSX.IntrinsicElements {
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
declare function compat(type:
|
|
76
|
-
declare
|
|
77
|
-
declare
|
|
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>>;
|
|
78
78
|
|
|
79
|
-
export { JSX, compat,
|
|
79
|
+
export { JSX, compat, jsx, jsxs };
|
package/dist/jsx-runtime.js
CHANGED
|
@@ -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,74 +17,81 @@ 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: () =>
|
|
33
|
+
Fragment: () => import_react3.Fragment,
|
|
24
34
|
compat: () => compat,
|
|
25
|
-
jsx: () =>
|
|
26
|
-
jsxs: () =>
|
|
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
|
|
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
|
-
|
|
47
|
+
import_mvc.Context.use = (create) => {
|
|
37
48
|
const ambient = (0, import_react.useContext)(Lookup);
|
|
38
|
-
|
|
39
|
-
|
|
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
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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;
|
|
71
69
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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);
|
|
79
78
|
}
|
|
80
|
-
function
|
|
81
|
-
|
|
79
|
+
function FC(props, ref) {
|
|
80
|
+
Ambient = null;
|
|
81
|
+
return provider(this(props, ref));
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
var RENDER = /* @__PURE__ */ new WeakMap();
|
|
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));
|
|
90
|
+
}
|
|
91
|
+
return this(type, ...args);
|
|
85
92
|
}
|
|
93
|
+
var jsx = compat.bind(import_jsx_runtime.default.jsx);
|
|
94
|
+
var jsxs = compat.bind(import_jsx_runtime.default.jsxs);
|
|
86
95
|
// Annotate the CommonJS export names for ESM import in node:
|
|
87
96
|
0 && (module.exports = {
|
|
88
97
|
Fragment,
|
package/dist/jsx-runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/jsx-runtime.ts","../src/context.ts"],"sourcesContent":["import { Model } from '@expressive/mvc';\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/jsx-runtime.ts","../src/context.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\";","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;AAE1C,IAAMC,cAAa,mBAAQ;AAE3B,mBAAQ,MAAM,CAAC,WAA0B;AACvC,MAAG,WAAW,WAAW;AACvB,WAAO;AAET,QAAM,UAAUA,YAAW,MAAM;AAEjC,MAAG,YAAY;AACb,cAAU;AAEZ,SAAO;AACT;AAEA,SAAS,SAAS,UAA2B;AAC3C,MAAG,YAAY,MAAM,QAAQ,QAAQ,SAAK,8BAAe,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,oBACxB,UAAU,KAAK,IAAkB,IACnC,KAAK,qBAAqB,cAAAC,QAAM,YAC9B,OACA,GAAG,KAAK,IAAgB,CAC3B;AAAA;AAEL,SAAO,KAAK,MAAM,GAAG,IAAI;AAC3B;AAEO,IAAM,MAAM,OAAO,KAAK,mBAAAC,QAAQ,GAAG;AACnC,IAAM,OAAO,OAAO,KAAK,mBAAAA,QAAQ,IAAI;","names":["import_mvc","import_react","import_react","useContext","React","Runtime"]}
|
package/package.json
CHANGED
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"preversion": "npm run test && npm run build"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@expressive/mvc": "^0.
|
|
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
|
-
"
|
|
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.
|
|
71
|
-
"gitHead": "
|
|
69
|
+
"version": "0.60.0",
|
|
70
|
+
"gitHead": "a147241b0028524bc6807f233185bda26ae8f969"
|
|
72
71
|
}
|