@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 +9 -9
- package/dist/adapter.js +36 -22
- package/dist/adapter.js.map +1 -1
- package/dist/esm/adapter.js +20 -16
- 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 +4 -4
- package/dist/esm/jsx-dev-runtime.js.map +1 -1
- package/dist/esm/jsx-runtime.js +43 -43
- 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 +3 -2
- package/dist/jsx-dev-runtime.js +3 -3
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +25 -24
- package/dist/jsx-runtime.js +58 -62
- package/dist/jsx-runtime.js.map +1 -1
- package/package.json +5 -6
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function as<T extends Model, P extends Model.Assign<T>>(this: Model.Init<T>, render: (
|
|
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
|
|
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
|
-
|
|
30
|
-
|
|
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 = (
|
|
33
|
-
const
|
|
34
|
-
for (const key in
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
return
|
|
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
|
|
60
|
+
var import_mvc3 = require("@expressive/mvc");
|
|
46
61
|
var import_adapter = require("./adapter");
|
|
47
|
-
|
|
48
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
75
|
-
|
|
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
|
|
103
|
+
var import_mvc4 = require("@expressive/mvc");
|
|
90
104
|
var import_adapter2 = require("./adapter");
|
|
91
|
-
|
|
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,
|
|
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)
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/adapter.ts","../src/model.as.ts","../src/model.use.ts","../src/model.get.ts"],"sourcesContent":["import { Context } from '@expressive/mvc';\n\nconst Pragma = {} as {\n useContext(): Context;\n\n useFactory<T extends () => unknown>(\n factory: (refresh: () => void) => T\n ): T;\n\n useLifecycle(callback: () => () => void): void;\n};\n\nexport { Pragma };\n\nimport \"./model.as\";\nimport './model.use';\nimport './model.get';","import { Model } from '@expressive/mvc';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>> {\n (props: P): JSX.Element | null;\n\n displayName?: string;\n Model: Model.Type<T>;\n }\n\n /**\n * Creates a component which reflects this Model. All managed properties may be assigned using props.\n * \n * @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.\n */\n function as <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (using: P) => JSX.Element | null\n ): Component<T, P & Model.Assign<T>>;\n }\n}\n\nModel.as = function <T extends Model, P extends Model.Assign<T>> (\n this: Model.Init<T>, render: (using: P) => JSX.Element | null){\n\n if(this === Model)\n throw new Error(\"Cannot create component from base Model.\");\n\n const Component = ((inputProps: P) => {\n const props = this.use(inputProps, true) as T & P;\n\n for(const key in inputProps)\n if(!(key in props))\n Object.defineProperty(props, key, { value: inputProps[key] });\n\n return render(props);\n }) as Model.Component<T, P>;\n\n Component.Model = this;\n Component.displayName = this.name;\n \n return Component;\n}","import { Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n function use <T extends Model> (\n this: Model.Init<T>,\n apply?: Model.Assign<T>,\n repeat?: boolean\n ): T;\n\n function use <T extends Model> (\n this: Model.Init<T>,\n callback?: Model.Callback<T>,\n repeat?: boolean\n ): T;\n }\n}\n\nModel.use = function <T extends Model> (\n this: Model.Init<T>,\n argument?: Model.Assign<T> | Model.Callback<T>,\n repeat?: boolean){\n\n const ambient = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n let enabled: boolean | undefined;\n let local: T;\n\n const instance = new this(argument);\n const context = ambient.push();\n const unwatch = instance.get(current => {\n local = current;\n\n if(enabled)\n refresh();\n });\n\n function didMount(){\n enabled = true;\n return () => {\n unwatch();\n instance.set(null);\n context.pop()\n }\n }\n\n return (props?: Model.Assign<T> | Model.Callback<T>) => {\n Pragma.useLifecycle(didMount);\n context.include({ instance });\n\n if(repeat && enabled && props){\n enabled = false;\n\n if(typeof props == \"function\")\n props.call(instance, instance);\n else if(typeof props == \"object\")\n instance.set(props);\n\n const update = instance.set();\n\n if(update)\n update.then(() => enabled = true);\n else\n enabled = true;\n }\n\n return local; \n };\n });\n\n return render(argument);\n}","import { createEffect, Model } from '@expressive/mvc';\n\nimport { Pragma } from './adapter';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ntype ForceRefresh = {\n /** Request a refresh for current component. */\n (): void;\n \n /**\n * Request a refresh and again after promise either resolves or rejects.\n * \n * @param waitFor Promise to wait for.\n * @returns Promise which resolves, after refresh, to same value as `waitFor`.\n */\n <T = void>(waitFor: Promise<T>): Promise<T>;\n\n /**\n * Request refresh before and after async function.\n * A refresh will occur both before and after the given function.\n * \n * **Note:** Any actions performed before first `await` will occur prior to refresh.\n * \n * @param invoke Async function to invoke.\n * @returns Promise which resolves returned value after refresh.\n */\n <T = void>(invoke: () => Promise<T>): Promise<T>;\n};\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n type GetFactory<T extends Model, R> =\n (this: T, current: T, refresh: ForceRefresh) => R;\n\n type GetEffect<T extends Model> =\n (this: T, current: T, refresh: ForceRefresh) => null;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>): T;\n \n /** Fetch instance of this class optionally. */\n function get <T extends Model> (this: Model.Type<T>, required: false): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get <T extends Model> (this: Model.Type<T>, requireValues: true): Required<T>\n \n function get <T extends Model, R> (this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;\n \n // TODO: eagerly metch this so any nulls are caught - would prevent updates.\n function get <T extends Model> (this: Model.Type<T>, factory: GetEffect<T>): null;\n }\n}\n\nModel.get = function <T extends Model, R> (\n this: Model.Type<T>,\n argument?: boolean | Model.GetFactory<T, unknown>\n){\n const context = Pragma.useContext();\n const render = Pragma.useFactory((refresh) => {\n function forceUpdate(): void;\n function forceUpdate<T>(action: Promise<T> | (() => Promise<T>)): Promise<T>;\n function forceUpdate<T>(action?: Promise<T> | (() => Promise<T>)){\n if(typeof action == \"function\")\n action = action();\n\n refresh();\n\n if(action)\n return action.finally(refresh);\n }\n\n const instance = context.get(this);\n\n if(!instance)\n if(argument === false)\n return () => undefined;\n else\n throw new Error(`Could not find ${this} in context.`);\n\n let unwatch: (() => void) | undefined;\n let value: any;\n\n unwatch = createEffect(instance, current => {\n if(typeof argument === \"function\"){\n const next = argument.call(current, current, forceUpdate);\n\n if(next === value)\n return;\n\n value = next;\n }\n else\n value = current;\n\n if(unwatch)\n refresh();\n }, argument === true);\n\n if(value instanceof Promise){\n let error: Error | undefined;\n\n unwatch();\n\n // TODO: ignore update if resolves to undefined or null\n value.then(x => value = x).catch(e => error = e).finally(refresh);\n value = null;\n\n return () => {\n if(error)\n throw error;\n\n return value === undefined ? null : value;\n }\n }\n\n if(value === null){\n unwatch();\n return () => null;\n }\n\n return () => {\n Pragma.useLifecycle(() => unwatch);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;;;ACAA,iBAAsB;AAsBtB,iBAAM,KAAK,SACY,QAAyC;AAE9D,MAAG,SAAS;AACV,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,YAAa,CAAC,eAAkB;AACpC,UAAM,QAAQ,KAAK,IAAI,YAAY,IAAI;AAEvC,eAAU,OAAO;AACf,UAAG,EAAE,OAAO;AACV,eAAO,eAAe,OAAO,KAAK,EAAE,OAAO,WAAW,GAAG,EAAE,CAAC;AAEhE,WAAO,OAAO,KAAK;AAAA,EACrB;AAEA,YAAU,QAAQ;AAClB,YAAU,cAAc,KAAK;AAE7B,SAAO;AACT;;;AC1CA,IAAAC,cAAsB;AAEtB,qBAAuB;AAkBvB,kBAAM,MAAM,SAEV,UACA,QAAiB;AAEjB,QAAM,UAAU,sBAAO,WAAW;AAClC,QAAM,SAAS,sBAAO,WAAW,CAAC,YAAY;AAC5C,QAAI;AACJ,QAAI;AAEJ,UAAM,WAAW,IAAI,KAAK,QAAQ;AAClC,UAAM,UAAU,QAAQ,KAAK;AAC7B,UAAM,UAAU,SAAS,I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V;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);\n return value === undefined ? null : value;\n }\n });\n\n return render() as R;\n}"],"mappings":";;;;;;;;;;;;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AM;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"]}
|
package/dist/esm/adapter.js
CHANGED
|
@@ -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 = (
|
|
7
|
-
const
|
|
8
|
-
for (const key in
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
return
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
49
|
-
|
|
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
|
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';\n\ndeclare module '@expressive/mvc' {\n namespace Model {\n interface Component<T extends Model, P extends Model.Assign<T>>
|
|
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,KAA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C,QAAG,CAAC;AACF,UAAG,aAAa;AACd,eAAO,MAAM;AAAA;AAEb,cAAM,IAAI,MAAM,kBAAkB,IAAI,cAAc;AAExD,QAAI;AACJ,QAAI;AAEJ,cAAU,aAAa,UAAU,aAAW;AAC1C,UAAG,OAAO,aAAa,YAAW;AAChC,cAAM,OAAO,SAAS,KAAK,SAAS,SAAS,WAAW;AAExD,YAAG,SAAS;AACV;AAEF,gBAAQ;AAAA,MACV;AAEE,gBAAQ;AAEV,UAAG;AACD,gBAAQ;AAAA,IACZ,GAAG,aAAa,IAAI;AAEpB,QAAG,iBAAiB,SAAQ;AAC1B,UAAI;AAEJ,cAAQ;AAGR,YAAM,KAAK,OAAK,QAAQ,CAAC,EAAE,MAAM,OAAK,QAAQ,CAAC,EAAE,QAAQ,OAAO;AAChE,cAAQ;AAER,aAAO,MAAM;AACX,YAAG;AACD,gBAAM;AAER,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,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,10 +1,10 @@
|
|
|
1
1
|
// src/jsx-dev-runtime.ts
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import Runtime from "react/jsx-dev-runtime";
|
|
3
|
+
import { compat } from "./jsx-runtime";
|
|
4
4
|
import { Fragment } from "react";
|
|
5
|
-
var
|
|
5
|
+
var jsxDEV = compat.bind(Runtime.jsxDEV);
|
|
6
6
|
export {
|
|
7
7
|
Fragment,
|
|
8
|
-
|
|
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
|
|
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,54 +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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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,
|
|
44
|
+
return this(type, ...args);
|
|
45
45
|
}
|
|
46
|
-
var
|
|
47
|
-
var
|
|
46
|
+
var jsx = compat.bind(Runtime.jsx);
|
|
47
|
+
var jsxs = compat.bind(Runtime.jsxs);
|
|
48
48
|
export {
|
|
49
49
|
Fragment,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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 {
|
|
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 =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,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,7 +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
4
|
export { JSX } from './jsx-runtime';
|
|
4
5
|
|
|
5
|
-
declare const jsxDEV: (type:
|
|
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 };
|
package/dist/jsx-dev-runtime.js
CHANGED
|
@@ -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: () =>
|
|
34
|
+
jsxDEV: () => jsxDEV
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(jsx_dev_runtime_exports);
|
|
37
|
-
var
|
|
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
|
|
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
|
|
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,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
|
|
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 {} = {}> =
|
|
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:
|
|
39
|
-
children?: React.ReactNode;
|
|
37
|
+
render?(props: HasProps<this>, self: this): React__default.ReactNode;
|
|
40
38
|
}
|
|
41
|
-
|
|
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> |
|
|
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> :
|
|
59
|
-
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 {
|
|
60
61
|
}
|
|
61
|
-
interface ElementClass extends
|
|
62
|
+
interface ElementClass extends React__default.JSX.ElementClass {
|
|
62
63
|
}
|
|
63
64
|
interface ElementAttributesProperty {
|
|
64
65
|
}
|
|
65
|
-
interface ElementChildrenAttribute extends
|
|
66
|
+
interface ElementChildrenAttribute extends React__default.JSX.ElementChildrenAttribute {
|
|
66
67
|
}
|
|
67
|
-
interface IntrinsicAttributes extends
|
|
68
|
+
interface IntrinsicAttributes extends React__default.JSX.IntrinsicAttributes {
|
|
68
69
|
}
|
|
69
|
-
interface IntrinsicClassAttributes<T> extends
|
|
70
|
+
interface IntrinsicClassAttributes<T> extends React__default.JSX.IntrinsicClassAttributes<T> {
|
|
70
71
|
}
|
|
71
|
-
interface IntrinsicElements extends
|
|
72
|
+
interface IntrinsicElements extends React__default.JSX.IntrinsicElements {
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
|
-
declare function
|
|
75
|
-
declare const
|
|
76
|
-
declare const
|
|
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,
|
|
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,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: () =>
|
|
24
|
-
|
|
25
|
-
jsx: () =>
|
|
26
|
-
jsxs: () =>
|
|
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
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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,
|
|
91
|
+
return this(type, ...args);
|
|
96
92
|
}
|
|
97
|
-
var
|
|
98
|
-
var
|
|
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
|
-
|
|
98
|
+
compat,
|
|
103
99
|
jsx,
|
|
104
100
|
jsxs
|
|
105
101
|
});
|
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 {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,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
|
}
|