@expressive/react 0.52.0 → 0.52.2-alpha.74
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 +70 -0
- package/dist/adapter.js +148 -0
- package/dist/adapter.js.map +1 -0
- package/dist/index.d.ts +12 -97
- package/dist/index.js +30 -167
- package/dist/index.js.map +1 -0
- package/package.json +22 -12
- package/dist/index.mjs +0 -181
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Context } from '@expressive/mvc';
|
|
2
|
+
|
|
3
|
+
declare module '@expressive/mvc' {
|
|
4
|
+
namespace Model {
|
|
5
|
+
interface Component<T extends Model, P extends Model.Assign<T>> {
|
|
6
|
+
(props: P): JSX.Element | null;
|
|
7
|
+
displayName?: string;
|
|
8
|
+
Model: Model.Type<T>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a component which reflects this Model. All managed properties may be assigned using props.
|
|
12
|
+
*
|
|
13
|
+
* @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.
|
|
14
|
+
*/
|
|
15
|
+
function as<T extends Model, P extends Model.Assign<T>>(this: Model.Init<T>, render: (using: P) => JSX.Element | null): Component<T, P & Model.Assign<T>>;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module '@expressive/mvc' {
|
|
20
|
+
namespace Model {
|
|
21
|
+
function use<T extends Model>(this: Model.Init<T>, apply?: Model.Assign<T>, repeat?: boolean): T;
|
|
22
|
+
function use<T extends Model>(this: Model.Init<T>, callback?: Model.Callback<T>, repeat?: boolean): T;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Type may not be undefined - instead will be null. */
|
|
27
|
+
type NoVoid<T> = T extends undefined | void ? null : T;
|
|
28
|
+
type ForceRefresh = {
|
|
29
|
+
/** Request a refresh for current component. */
|
|
30
|
+
(): void;
|
|
31
|
+
/**
|
|
32
|
+
* Request a refresh and again after promise either resolves or rejects.
|
|
33
|
+
*
|
|
34
|
+
* @param waitFor Promise to wait for.
|
|
35
|
+
* @returns Promise which resolves, after refresh, to same value as `waitFor`.
|
|
36
|
+
*/
|
|
37
|
+
<T = void>(waitFor: Promise<T>): Promise<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Request refresh before and after async function.
|
|
40
|
+
* A refresh will occur both before and after the given function.
|
|
41
|
+
*
|
|
42
|
+
* **Note:** Any actions performed before first `await` will occur prior to refresh.
|
|
43
|
+
*
|
|
44
|
+
* @param invoke Async function to invoke.
|
|
45
|
+
* @returns Promise which resolves returned value after refresh.
|
|
46
|
+
*/
|
|
47
|
+
<T = void>(invoke: () => Promise<T>): Promise<T>;
|
|
48
|
+
};
|
|
49
|
+
declare module '@expressive/mvc' {
|
|
50
|
+
namespace Model {
|
|
51
|
+
type GetFactory<T extends Model, R> = (this: T, current: T, refresh: ForceRefresh) => R;
|
|
52
|
+
type GetEffect<T extends Model> = (this: T, current: T, refresh: ForceRefresh) => null;
|
|
53
|
+
/** Fetch instance of this class from context. */
|
|
54
|
+
function get<T extends Model>(this: Model.Type<T>): T;
|
|
55
|
+
/** Fetch instance of this class optionally. */
|
|
56
|
+
function get<T extends Model>(this: Model.Type<T>, required: false): T | undefined;
|
|
57
|
+
/** Fetch instance of this class from context. */
|
|
58
|
+
function get<T extends Model>(this: Model.Type<T>, requireValues: true): Required<T>;
|
|
59
|
+
function get<T extends Model, R>(this: Model.Type<T>, factory: GetFactory<T, Promise<R> | R>): NoVoid<R>;
|
|
60
|
+
function get<T extends Model>(this: Model.Type<T>, factory: GetEffect<T>): null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
declare const Pragma: {
|
|
65
|
+
useContext(): Context;
|
|
66
|
+
useFactory<T extends () => unknown>(factory: (refresh: () => void) => T): T;
|
|
67
|
+
useLifecycle(callback: () => () => void): void;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export { Pragma };
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/adapter.ts
|
|
21
|
+
var adapter_exports = {};
|
|
22
|
+
__export(adapter_exports, {
|
|
23
|
+
Pragma: () => Pragma3
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(adapter_exports);
|
|
26
|
+
|
|
27
|
+
// src/model.as.ts
|
|
28
|
+
var import_mvc = require("@expressive/mvc");
|
|
29
|
+
import_mvc.Model.as = function(render) {
|
|
30
|
+
if (this === import_mvc.Model)
|
|
31
|
+
throw new Error("Cannot create component from base Model.");
|
|
32
|
+
const Component = (inputProps) => {
|
|
33
|
+
const props = this.use(inputProps, true);
|
|
34
|
+
for (const key in inputProps)
|
|
35
|
+
if (!(key in props))
|
|
36
|
+
Object.defineProperty(props, key, { value: inputProps[key] });
|
|
37
|
+
return render(props);
|
|
38
|
+
};
|
|
39
|
+
Component.Model = this;
|
|
40
|
+
Component.displayName = this.name;
|
|
41
|
+
return Component;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/model.use.ts
|
|
45
|
+
var import_mvc2 = require("@expressive/mvc");
|
|
46
|
+
var import_adapter = require("./adapter");
|
|
47
|
+
import_mvc2.Model.use = function(argument, repeat) {
|
|
48
|
+
const ambient = import_adapter.Pragma.useContext();
|
|
49
|
+
const render = import_adapter.Pragma.useFactory((refresh) => {
|
|
50
|
+
let enabled;
|
|
51
|
+
let local;
|
|
52
|
+
const instance = new this(argument);
|
|
53
|
+
const context = ambient.push({ instance });
|
|
54
|
+
const unwatch = instance.get((current) => {
|
|
55
|
+
local = current;
|
|
56
|
+
if (enabled)
|
|
57
|
+
refresh();
|
|
58
|
+
});
|
|
59
|
+
function didMount() {
|
|
60
|
+
enabled = true;
|
|
61
|
+
return () => {
|
|
62
|
+
unwatch();
|
|
63
|
+
instance.set(null);
|
|
64
|
+
context.pop();
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return (props) => {
|
|
68
|
+
import_adapter.Pragma.useLifecycle(didMount);
|
|
69
|
+
if (repeat && enabled && props) {
|
|
70
|
+
enabled = false;
|
|
71
|
+
if (typeof props == "function")
|
|
72
|
+
props.call(instance, instance);
|
|
73
|
+
else if (typeof props == "object")
|
|
74
|
+
instance.set(props);
|
|
75
|
+
const update = instance.set();
|
|
76
|
+
if (update)
|
|
77
|
+
update.then(() => enabled = true);
|
|
78
|
+
else
|
|
79
|
+
enabled = true;
|
|
80
|
+
}
|
|
81
|
+
return local;
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
return render(argument);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// src/model.get.ts
|
|
88
|
+
var import_mvc3 = require("@expressive/mvc");
|
|
89
|
+
var import_adapter2 = require("./adapter");
|
|
90
|
+
import_mvc3.Model.get = function(argument) {
|
|
91
|
+
const context = import_adapter2.Pragma.useContext();
|
|
92
|
+
const render = import_adapter2.Pragma.useFactory((refresh) => {
|
|
93
|
+
function forceUpdate(action) {
|
|
94
|
+
if (typeof action == "function")
|
|
95
|
+
action = action();
|
|
96
|
+
refresh();
|
|
97
|
+
if (action)
|
|
98
|
+
return action.finally(refresh);
|
|
99
|
+
}
|
|
100
|
+
const instance = context.get(this);
|
|
101
|
+
if (!instance)
|
|
102
|
+
if (argument === false)
|
|
103
|
+
return () => void 0;
|
|
104
|
+
else
|
|
105
|
+
throw new Error(`Could not find ${this} in context.`);
|
|
106
|
+
let unwatch;
|
|
107
|
+
let value;
|
|
108
|
+
unwatch = (0, import_mvc3.createEffect)(instance, (current) => {
|
|
109
|
+
if (typeof argument === "function") {
|
|
110
|
+
const next = argument.call(current, current, forceUpdate);
|
|
111
|
+
if (next === value)
|
|
112
|
+
return;
|
|
113
|
+
value = next;
|
|
114
|
+
} else
|
|
115
|
+
value = current;
|
|
116
|
+
if (unwatch)
|
|
117
|
+
refresh();
|
|
118
|
+
}, argument === true);
|
|
119
|
+
if (value instanceof Promise) {
|
|
120
|
+
let error;
|
|
121
|
+
unwatch();
|
|
122
|
+
value.then((x) => value = x).catch((e) => error = e).finally(refresh);
|
|
123
|
+
value = null;
|
|
124
|
+
return () => {
|
|
125
|
+
if (error)
|
|
126
|
+
throw error;
|
|
127
|
+
return value === void 0 ? null : value;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (value === null) {
|
|
131
|
+
unwatch();
|
|
132
|
+
return () => null;
|
|
133
|
+
}
|
|
134
|
+
return () => {
|
|
135
|
+
import_adapter2.Pragma.useLifecycle(() => unwatch);
|
|
136
|
+
return value === void 0 ? null : value;
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
return render();
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/adapter.ts
|
|
143
|
+
var Pragma3 = {};
|
|
144
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
145
|
+
0 && (module.exports = {
|
|
146
|
+
Pragma
|
|
147
|
+
});
|
|
148
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +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({ 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(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,EAAE,SAAS,CAAC;AACzC,UAAM,UAAU,SAAS,IAAI,aAAW;AACtC,cAAQ;AAER,UAAG;AACD,gBAAQ;AAAA,IACZ,CAAC;AAED,aAAS,WAAU;AACjB,gBAAU;AACV,aAAO,MAAM;AACX,gBAAQ;AACR,iBAAS,IAAI,IAAI;AACjB,gBAAQ,IAAI;AAAA,MACd;AAAA,IACF;AAEA,WAAO,CAAC,UAAgD;AACtD,4BAAO,aAAa,QAAQ;AAE5B,UAAG,UAAU,WAAW,OAAM;AAC5B,kBAAU;AAEV,YAAG,OAAO,SAAS;AACjB,gBAAM,KAAK,UAAU,QAAQ;AAAA,iBACvB,OAAO,SAAS;AACtB,mBAAS,IAAI,KAAK;AAEpB,cAAM,SAAS,SAAS,IAAI;AAE5B,YAAG;AACD,iBAAO,KAAK,MAAM,UAAU,IAAI;AAAA;AAEhC,oBAAU;AAAA,MACd;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AAED,SAAO,OAAO,QAAQ;AACxB;;;ACxEA,IAAAC,cAAoC;AAEpC,IAAAC,kBAAuB;AAqDvB,kBAAM,MAAM,SAEV,UACD;AACC,QAAM,UAAU,uBAAO,WAAW;AAClC,QAAM,SAAS,uBAAO,WAAW,CAAC,YAAY;AAG5C,aAAS,YAAe,QAAyC;AAC/D,UAAG,OAAO,UAAU;AAClB,iBAAS,OAAO;AAElB,cAAQ;AAER,UAAG;AACD,eAAO,OAAO,QAAQ,OAAO;AAAA,IACjC;AAEA,UAAM,WAAW,QAAQ,IAAI,IAAI;AAEjC,QAAG,CAAC;AACF,UAAG,aAAa;AACd,eAAO,MAAM;AAAA;AAEb,cAAM,IAAI,MAAM,kBAAkB,IAAI,cAAc;AAExD,QAAI;AACJ,QAAI;AAEJ,kBAAU,0BAAa,UAAU,aAAW;AAC1C,UAAG,OAAO,aAAa,YAAW;AAChC,cAAM,OAAO,SAAS,KAAK,SAAS,SAAS,WAAW;AAExD,YAAG,SAAS;AACV;AAEF,gBAAQ;AAAA,MACV;AAEE,gBAAQ;AAEV,UAAG;AACD,gBAAQ;AAAA,IACZ,GAAG,aAAa,IAAI;AAEpB,QAAG,iBAAiB,SAAQ;AAC1B,UAAI;AAEJ,cAAQ;AAGR,YAAM,KAAK,OAAK,QAAQ,CAAC,EAAE,MAAM,OAAK,QAAQ,CAAC,EAAE,QAAQ,OAAO;AAChE,cAAQ;AAER,aAAO,MAAM;AACX,YAAG;AACD,gBAAM;AAER,eAAO,UAAU,SAAY,OAAO;AAAA,MACtC;AAAA,IACF;AAEA,QAAG,UAAU,MAAK;AAChB,cAAQ;AACR,aAAO,MAAM;AAAA,IACf;AAEA,WAAO,MAAM;AACX,6BAAO,aAAa,MAAM,OAAO;AACjC,aAAO,UAAU,SAAY,OAAO;AAAA,IACtC;AAAA,EACF,CAAC;AAED,SAAO,OAAO;AAChB;;;AH/HA,IAAMC,UAAS,CAAC;","names":["Pragma","import_mvc","import_mvc","import_adapter","Pragma"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,115 +1,30 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Model, { Context } from '@expressive/mvc';
|
|
2
2
|
export { Model, Model as default, get, has, ref, set, use } from '@expressive/mvc';
|
|
3
|
-
import
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
4
5
|
|
|
5
6
|
declare namespace Consumer {
|
|
6
|
-
type
|
|
7
|
-
/** Type of controller to fetch from context. */
|
|
8
|
-
for: Model.Type<T>;
|
|
9
|
-
/**
|
|
10
|
-
* If boolean, will assert controller of type `for` is present in context.
|
|
11
|
-
*
|
|
12
|
-
* If function, will called on every natural render of this component.
|
|
13
|
-
* Will throw if usable instance cannot be found in context.
|
|
14
|
-
*/
|
|
15
|
-
has: ((value: T) => void) | boolean;
|
|
16
|
-
};
|
|
17
|
-
type GetProps<T extends Model> = {
|
|
18
|
-
/** Type of controller to fetch from context. */
|
|
19
|
-
for: Model.Type<T>;
|
|
20
|
-
/** Function called on every natural render of this component. */
|
|
21
|
-
get: (value: T | undefined) => void;
|
|
22
|
-
};
|
|
23
|
-
type RenderProps<T extends Model> = {
|
|
7
|
+
type Props<T extends Model> = {
|
|
24
8
|
/** Type of controller to fetch from context. */
|
|
25
9
|
for: Model.Type<T>;
|
|
26
10
|
/**
|
|
27
11
|
* Render function, will receive instance of desired controller.
|
|
28
12
|
*
|
|
29
|
-
*
|
|
13
|
+
* Called every render of parent component.
|
|
14
|
+
* Similar to `Model.get()`, updates to properties accessed in
|
|
30
15
|
* this function will cause a refresh when they change.
|
|
31
16
|
*/
|
|
32
|
-
children: (value: T) =>
|
|
17
|
+
children: (value: T) => ReactNode | void;
|
|
33
18
|
};
|
|
34
|
-
type Props<T extends Model> = HasProps<T> | GetProps<T> | RenderProps<T>;
|
|
35
19
|
}
|
|
36
|
-
declare function Consumer<T extends Model>(props: Consumer.Props<T>):
|
|
37
|
-
|
|
38
|
-
type Node = JSX.Element | Iterable<Node> | string | number | boolean | null | undefined;
|
|
20
|
+
declare function Consumer<T extends Model>(props: Consumer.Props<T>): string | number | boolean | react.ReactElement<any, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | react.ReactPortal | null;
|
|
39
21
|
declare namespace Provider {
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
interface Props<T extends Model> {
|
|
23
|
+
for: Context.Accept<T>;
|
|
24
|
+
set?: Model.Assign<T>;
|
|
42
25
|
children?: ReactNode;
|
|
43
|
-
}>;
|
|
44
|
-
type Item = Model | Model.Init;
|
|
45
|
-
type Multiple<T extends Item = Item> = {
|
|
46
|
-
[key: string | number]: T;
|
|
47
|
-
};
|
|
48
|
-
type Instance<E> = E extends (new () => any) ? InstanceType<E> : E extends Model ? E : never;
|
|
49
|
-
type Props<T extends Item = Item> = MultipleProps<T> | NormalProps<T>;
|
|
50
|
-
type NormalProps<E, I = Instance<E>> = {
|
|
51
|
-
for: E;
|
|
52
|
-
children?: Node;
|
|
53
|
-
use?: Model.Assign<I>;
|
|
54
|
-
};
|
|
55
|
-
type MultipleProps<T extends Item> = {
|
|
56
|
-
for: Multiple<T>;
|
|
57
|
-
children?: Node;
|
|
58
|
-
use?: Model.Assign<Instance<T>>;
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): Provider.Element;
|
|
62
|
-
|
|
63
|
-
/** Type may not be undefined - instead will be null. */
|
|
64
|
-
type NoVoid<T> = T extends undefined | void ? null : T;
|
|
65
|
-
declare module '@expressive/mvc' {
|
|
66
|
-
namespace Model {
|
|
67
|
-
namespace get {
|
|
68
|
-
type Factory<T extends Model, R> = (this: T, current: T, refresh: ForceRefresh) => R;
|
|
69
|
-
type ForceRefresh = {
|
|
70
|
-
/** Request a refresh for current component. */
|
|
71
|
-
(): void;
|
|
72
|
-
/**
|
|
73
|
-
* Request a refresh and again after promise either resolves or rejects.
|
|
74
|
-
*
|
|
75
|
-
* @param waitFor Promise to wait for.
|
|
76
|
-
* @returns Promise which resolves, after refresh, to same value as `waitFor`.
|
|
77
|
-
*/
|
|
78
|
-
<T = void>(waitFor: Promise<T>): Promise<T>;
|
|
79
|
-
/**
|
|
80
|
-
* Request refresh before and after async function.
|
|
81
|
-
* A refresh will occur both before and after the given function.
|
|
82
|
-
*
|
|
83
|
-
* **Note:** Any actions performed before first `await` will occur prior to refresh.
|
|
84
|
-
*
|
|
85
|
-
* @param invoke Async function to invoke.
|
|
86
|
-
* @returns Promise which resolves returned value after refresh.
|
|
87
|
-
*/
|
|
88
|
-
<T = void>(invoke: () => Promise<T>): Promise<T>;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
interface Component<T extends Model, P extends Model.Assign<T>> {
|
|
92
|
-
(props: P): JSX.Element;
|
|
93
|
-
Model: Model.Type<T>;
|
|
94
|
-
displayName: string;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Creates a component which reflects this Model. All managed properties may be assigned using props.
|
|
98
|
-
*
|
|
99
|
-
* @param render Function which renders component. This function receives all Model state merged with props. Normal subscription behavior still applies.
|
|
100
|
-
*/
|
|
101
|
-
function as<T extends Model, P = {}>(this: Model.Init<T>, render: (using: T & P) => React.ReactNode): Component<T, P & Model.Assign<T>>;
|
|
102
|
-
/** Fetch instance of this class from context. */
|
|
103
|
-
function get<T extends Model>(this: Model.Type<T>): T;
|
|
104
|
-
/** Fetch instance of this class optionally. */
|
|
105
|
-
function get<T extends Model>(this: Model.Type<T>, expect: false): T | undefined;
|
|
106
|
-
/** Fetch instance of this class from context. */
|
|
107
|
-
function get<T extends Model>(this: Model.Type<T>, expectValues: true): Required<T>;
|
|
108
|
-
function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, Promise<R> | R>): NoVoid<R>;
|
|
109
|
-
function get<T extends Model, R>(this: Model.Type<T>, factory: get.Factory<T, null>): NoVoid<R> | null;
|
|
110
|
-
function use<T extends Model>(this: Model.Init<T>, apply?: Model.Assign<T>, repeat?: boolean): T;
|
|
111
|
-
function use<T extends Model>(this: Model.Init<T>, callback?: Model.Callback<T>, repeat?: boolean): T;
|
|
112
26
|
}
|
|
113
27
|
}
|
|
28
|
+
declare function Provider<T extends Model>(props: Provider.Props<T>): react.FunctionComponentElement<react.ProviderProps<Context>>;
|
|
114
29
|
|
|
115
30
|
export { Consumer, Provider };
|
package/dist/index.js
CHANGED
|
@@ -21,185 +21,47 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
Consumer: () => Consumer,
|
|
24
|
-
Model: () =>
|
|
24
|
+
Model: () => import_mvc2.Model,
|
|
25
25
|
Provider: () => Provider,
|
|
26
|
-
default: () =>
|
|
27
|
-
get: () =>
|
|
28
|
-
has: () =>
|
|
29
|
-
ref: () =>
|
|
30
|
-
set: () =>
|
|
31
|
-
use: () =>
|
|
26
|
+
default: () => import_mvc2.Model,
|
|
27
|
+
get: () => import_mvc2.get,
|
|
28
|
+
has: () => import_mvc2.has,
|
|
29
|
+
ref: () => import_mvc2.ref,
|
|
30
|
+
set: () => import_mvc2.set,
|
|
31
|
+
use: () => import_mvc2.use
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(src_exports);
|
|
34
|
-
var
|
|
34
|
+
var import_react2 = require("react");
|
|
35
|
+
var import_adapter = require("./adapter");
|
|
35
36
|
|
|
36
|
-
// src/
|
|
37
|
+
// src/context.ts
|
|
37
38
|
var import_mvc = require("@expressive/mvc");
|
|
38
|
-
function createComponent(render) {
|
|
39
|
-
import_mvc.Model.is(this);
|
|
40
|
-
const Component = (inputProps) => {
|
|
41
|
-
const props = this.use(inputProps, true);
|
|
42
|
-
for (const key in inputProps)
|
|
43
|
-
if (!(key in props))
|
|
44
|
-
Object.defineProperty(props, key, { value: inputProps[key] });
|
|
45
|
-
return render(props);
|
|
46
|
-
};
|
|
47
|
-
Component.Model = this;
|
|
48
|
-
Component.displayName = this.name;
|
|
49
|
-
return Component;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/useLocal.ts
|
|
53
|
-
var import_mvc3 = require("@expressive/mvc");
|
|
54
|
-
|
|
55
|
-
// src/useContext.ts
|
|
56
|
-
var import_mvc2 = require("@expressive/mvc");
|
|
57
39
|
var import_react = require("react");
|
|
58
|
-
var
|
|
59
|
-
var Shared = (0, import_react.createContext)(new import_mvc2.Context());
|
|
60
|
-
var useShared = () => (0, import_react.useContext)(Shared);
|
|
61
|
-
var createProvider = (value, children) => (0, import_react.createElement)(Shared.Provider, { key: value.id, value }, children);
|
|
62
|
-
|
|
63
|
-
// src/useLocal.ts
|
|
64
|
-
function useLocal(argument, repeat) {
|
|
65
|
-
const outer = useShared();
|
|
66
|
-
const state = (0, import_react2.useState)(() => {
|
|
67
|
-
let enabled;
|
|
68
|
-
let context;
|
|
69
|
-
let local;
|
|
70
|
-
import_mvc3.Model.is(this);
|
|
71
|
-
const instance = new this(argument);
|
|
72
|
-
const release = instance.get((current) => {
|
|
73
|
-
local = current;
|
|
74
|
-
if (enabled)
|
|
75
|
-
state[1]((x) => x.bind(null));
|
|
76
|
-
});
|
|
77
|
-
return (props) => {
|
|
78
|
-
if (!context)
|
|
79
|
-
context = outer.push({ instance });
|
|
80
|
-
if (repeat && enabled) {
|
|
81
|
-
enabled = false;
|
|
82
|
-
if (typeof props == "function")
|
|
83
|
-
props.call(instance, instance);
|
|
84
|
-
else if (typeof props == "object")
|
|
85
|
-
instance.set(props);
|
|
86
|
-
const update = instance.set();
|
|
87
|
-
if (update)
|
|
88
|
-
update.then(() => enabled = true);
|
|
89
|
-
else
|
|
90
|
-
enabled = true;
|
|
91
|
-
}
|
|
92
|
-
(0, import_react2.useEffect)(() => {
|
|
93
|
-
enabled = true;
|
|
94
|
-
return () => {
|
|
95
|
-
release();
|
|
96
|
-
instance.set(null);
|
|
97
|
-
context.pop();
|
|
98
|
-
};
|
|
99
|
-
}, []);
|
|
100
|
-
return local;
|
|
101
|
-
};
|
|
102
|
-
});
|
|
103
|
-
return state[0](argument);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// src/useRemote.ts
|
|
107
|
-
var import_mvc4 = require("@expressive/mvc");
|
|
108
|
-
function useRemote(argument) {
|
|
109
|
-
const context = useShared();
|
|
110
|
-
const state = (0, import_react2.useState)(() => {
|
|
111
|
-
const instance = context.get(this);
|
|
112
|
-
const refresh = () => state[1]((x) => x.bind(null));
|
|
113
|
-
if (!instance)
|
|
114
|
-
if (argument === false)
|
|
115
|
-
return () => {
|
|
116
|
-
};
|
|
117
|
-
else
|
|
118
|
-
throw new Error(`Could not find ${this} in context.`);
|
|
119
|
-
function forceUpdate(action) {
|
|
120
|
-
if (typeof action == "function")
|
|
121
|
-
action = action();
|
|
122
|
-
refresh();
|
|
123
|
-
if (action)
|
|
124
|
-
return action.finally(refresh);
|
|
125
|
-
}
|
|
126
|
-
let release;
|
|
127
|
-
let value;
|
|
128
|
-
release = (0, import_mvc4.effect)(instance, (current) => {
|
|
129
|
-
if (typeof argument === "function") {
|
|
130
|
-
const next = argument.call(current, current, forceUpdate);
|
|
131
|
-
if (next === value)
|
|
132
|
-
return;
|
|
133
|
-
value = next;
|
|
134
|
-
} else
|
|
135
|
-
value = current;
|
|
136
|
-
if (release)
|
|
137
|
-
refresh();
|
|
138
|
-
}, argument === true);
|
|
139
|
-
if (value instanceof Promise) {
|
|
140
|
-
let error;
|
|
141
|
-
release();
|
|
142
|
-
value.then((x) => value = x).catch((e) => error = e).finally(refresh);
|
|
143
|
-
value = null;
|
|
144
|
-
return () => {
|
|
145
|
-
if (error)
|
|
146
|
-
throw error;
|
|
147
|
-
return value === void 0 ? null : value;
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
if (value === null) {
|
|
151
|
-
release();
|
|
152
|
-
return () => null;
|
|
153
|
-
}
|
|
154
|
-
return () => {
|
|
155
|
-
(0, import_react2.useEffect)(() => release, []);
|
|
156
|
-
return value === void 0 ? null : value;
|
|
157
|
-
};
|
|
158
|
-
});
|
|
159
|
-
return state[0]();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// src/index.ts
|
|
163
|
-
var import_mvc7 = require("@expressive/mvc");
|
|
164
|
-
|
|
165
|
-
// src/consumer.ts
|
|
40
|
+
var Lookup = (0, import_react.createContext)(new import_mvc.Context());
|
|
166
41
|
function Consumer(props) {
|
|
167
|
-
|
|
168
|
-
if (typeof children == "function")
|
|
169
|
-
return Type.get(children);
|
|
170
|
-
const context = useShared();
|
|
171
|
-
const instance = (0, import_react2.useMemo)(() => {
|
|
172
|
-
const instance2 = context.get(Type);
|
|
173
|
-
if (!instance2 && has2)
|
|
174
|
-
throw new Error(`Could not find ${Type} in context.`);
|
|
175
|
-
return instance2;
|
|
176
|
-
}, []);
|
|
177
|
-
const callback = has2 || get2;
|
|
178
|
-
if (typeof callback == "function")
|
|
179
|
-
callback(instance);
|
|
180
|
-
return null;
|
|
42
|
+
return props.for.get((i) => props.children(i));
|
|
181
43
|
}
|
|
182
|
-
|
|
183
|
-
// src/provider.ts
|
|
184
|
-
var import_mvc5 = require("@expressive/mvc");
|
|
185
44
|
function Provider(props) {
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
});
|
|
195
|
-
(0, import_react2.useEffect)(() => () => context.pop(), []);
|
|
196
|
-
return createProvider(context, props.children);
|
|
45
|
+
const ambient = (0, import_react.useContext)(Lookup);
|
|
46
|
+
const context = (0, import_react.useMemo)(() => ambient.push(), [ambient]);
|
|
47
|
+
(0, import_react.useEffect)(() => () => context.pop(), [ambient]);
|
|
48
|
+
context.include(props.for, props.set);
|
|
49
|
+
return (0, import_react.createElement)(Lookup.Provider, {
|
|
50
|
+
key: context.id,
|
|
51
|
+
value: context
|
|
52
|
+
}, props.children);
|
|
197
53
|
}
|
|
198
54
|
|
|
199
55
|
// src/index.ts
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
56
|
+
var import_mvc2 = require("@expressive/mvc");
|
|
57
|
+
import_adapter.Pragma.useContext = () => (0, import_react2.useContext)(Lookup);
|
|
58
|
+
import_adapter.Pragma.useLifecycle = (callback) => (0, import_react2.useEffect)(() => callback(), []);
|
|
59
|
+
import_adapter.Pragma.useFactory = (factory) => {
|
|
60
|
+
const state = (0, import_react2.useState)(() => factory(() => {
|
|
61
|
+
state[1]((x) => x.bind(null));
|
|
62
|
+
}));
|
|
63
|
+
return state[0];
|
|
64
|
+
};
|
|
203
65
|
// Annotate the CommonJS export names for ESM import in node:
|
|
204
66
|
0 && (module.exports = {
|
|
205
67
|
Consumer,
|
|
@@ -211,3 +73,4 @@ import_mvc6.Model.use = useLocal;
|
|
|
211
73
|
set,
|
|
212
74
|
use
|
|
213
75
|
});
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/context.ts"],"sourcesContent":["import { useContext, useEffect, useState } from 'react';\n\nimport { Pragma } from './adapter';\nimport { Context } from './context';\n\nPragma.useContext = () => useContext(Context);\n\nPragma.useLifecycle = (callback) => useEffect(() => callback(), []);\n\nPragma.useFactory = (factory) => {\n const state = useState(() => factory(() => {\n state[1](x => x.bind(null) as any);\n }));\n\n return state[0];\n}\n\nexport {\n Model, Model as default,\n get, use, ref, set, has\n} from '@expressive/mvc';\n\nexport { Consumer, Provider } from './context';","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 set?: Model.Assign<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, props.set);\n\n return createElement(Lookup.Provider, {\n key: context.id,\n value: context\n }, props.children);\n}\n\nexport { Consumer, Provider, Lookup as Context }"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,gBAAgD;AAEhD,qBAAuB;;;ACFvB,iBAA+B;AAC/B,mBAAwF;AAExF,IAAM,aAAS,4BAAc,IAAI,mBAAQ,CAAC;AAkB1C,SAAS,SAA0B,OAAyB;AAC1D,SAAO,MAAM,IAAI,IAAI,OAAK,MAAM,SAAS,CAAC,CAAC;AAC7C;AAUA,SAAS,SAA0B,OAAyB;AAC1D,QAAM,cAAU,yBAAW,MAAM;AACjC,QAAM,cAAU,sBAAQ,MAAM,QAAQ,KAAK,GAAG,CAAC,OAAO,CAAC;AAEvD,8BAAU,MAAM,MAAM,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC;AAE9C,UAAQ,QAAQ,MAAM,KAAK,MAAM,GAAG;AAEpC,aAAO,4BAAc,OAAO,UAAU;AAAA,IACpC,KAAK,QAAQ;AAAA,IACb,OAAO;AAAA,EACT,GAAG,MAAM,QAAQ;AACnB;;;AD5BA,IAAAC,cAGO;AAfP,sBAAO,aAAa,UAAM,0BAAW,MAAO;AAE5C,sBAAO,eAAe,CAAC,iBAAa,yBAAU,MAAM,SAAS,GAAG,CAAC,CAAC;AAElE,sBAAO,aAAa,CAAC,YAAY;AAC/B,QAAM,YAAQ,wBAAS,MAAM,QAAQ,MAAM;AACzC,UAAM,CAAC,EAAE,OAAK,EAAE,KAAK,IAAI,CAAQ;AAAA,EACnC,CAAC,CAAC;AAEF,SAAO,MAAM,CAAC;AAChB;","names":["import_react","import_mvc"]}
|
package/package.json
CHANGED
|
@@ -16,32 +16,42 @@
|
|
|
16
16
|
"react",
|
|
17
17
|
"mvc"
|
|
18
18
|
],
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"require": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./adapter": {
|
|
25
|
+
"types": "./dist/adapter.d.ts",
|
|
26
|
+
"require": "./dist/adapter.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
22
29
|
"files": [
|
|
23
|
-
"dist
|
|
24
|
-
"dist/index.mjs",
|
|
25
|
-
"dist/index.d.ts"
|
|
30
|
+
"dist"
|
|
26
31
|
],
|
|
27
32
|
"scripts": {
|
|
28
|
-
"build": "tsup
|
|
33
|
+
"build": "tsup",
|
|
29
34
|
"test": "tsc --noEmit && jest --collectCoverage",
|
|
30
|
-
"prebuild": "rm -rf dist",
|
|
31
35
|
"preversion": "npm run test && npm run build"
|
|
32
36
|
},
|
|
33
37
|
"dependencies": {
|
|
34
|
-
"@expressive/mvc": "^0.52.
|
|
38
|
+
"@expressive/mvc": "^0.52.2-alpha.74+088f4ba5"
|
|
35
39
|
},
|
|
36
40
|
"peerDependencies": {
|
|
37
41
|
"react": ">=16.8.0 <19.0.0"
|
|
38
42
|
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"react": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
39
48
|
"devDependencies": {
|
|
40
49
|
"@types/react": "^18.2.14",
|
|
41
50
|
"@types/react-test-renderer": "^18.0.0",
|
|
42
51
|
"react": "^18.2.0",
|
|
43
|
-
"react-test-renderer": "^18.2.0"
|
|
52
|
+
"react-test-renderer": "^18.2.0",
|
|
53
|
+
"tsup": "^8.0.2"
|
|
44
54
|
},
|
|
45
|
-
"version": "0.52.
|
|
46
|
-
"gitHead": "
|
|
55
|
+
"version": "0.52.2-alpha.74+088f4ba5",
|
|
56
|
+
"gitHead": "088f4ba53762f0b185871570c58f349084eb29af"
|
|
47
57
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { Model as Model5 } from "@expressive/mvc";
|
|
3
|
-
|
|
4
|
-
// src/component.ts
|
|
5
|
-
import { Model } from "@expressive/mvc";
|
|
6
|
-
function createComponent(render) {
|
|
7
|
-
Model.is(this);
|
|
8
|
-
const Component = (inputProps) => {
|
|
9
|
-
const props = this.use(inputProps, true);
|
|
10
|
-
for (const key in inputProps)
|
|
11
|
-
if (!(key in props))
|
|
12
|
-
Object.defineProperty(props, key, { value: inputProps[key] });
|
|
13
|
-
return render(props);
|
|
14
|
-
};
|
|
15
|
-
Component.Model = this;
|
|
16
|
-
Component.displayName = this.name;
|
|
17
|
-
return Component;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// src/useLocal.ts
|
|
21
|
-
import { Model as Model2 } from "@expressive/mvc";
|
|
22
|
-
|
|
23
|
-
// src/useContext.ts
|
|
24
|
-
import { Context } from "@expressive/mvc";
|
|
25
|
-
import { createContext, createElement, useContext } from "react";
|
|
26
|
-
import { useState, useEffect, useMemo } from "react";
|
|
27
|
-
var Shared = createContext(new Context());
|
|
28
|
-
var useShared = () => useContext(Shared);
|
|
29
|
-
var createProvider = (value, children) => createElement(Shared.Provider, { key: value.id, value }, children);
|
|
30
|
-
|
|
31
|
-
// src/useLocal.ts
|
|
32
|
-
function useLocal(argument, repeat) {
|
|
33
|
-
const outer = useShared();
|
|
34
|
-
const state = useState(() => {
|
|
35
|
-
let enabled;
|
|
36
|
-
let context;
|
|
37
|
-
let local;
|
|
38
|
-
Model2.is(this);
|
|
39
|
-
const instance = new this(argument);
|
|
40
|
-
const release = instance.get((current) => {
|
|
41
|
-
local = current;
|
|
42
|
-
if (enabled)
|
|
43
|
-
state[1]((x) => x.bind(null));
|
|
44
|
-
});
|
|
45
|
-
return (props) => {
|
|
46
|
-
if (!context)
|
|
47
|
-
context = outer.push({ instance });
|
|
48
|
-
if (repeat && enabled) {
|
|
49
|
-
enabled = false;
|
|
50
|
-
if (typeof props == "function")
|
|
51
|
-
props.call(instance, instance);
|
|
52
|
-
else if (typeof props == "object")
|
|
53
|
-
instance.set(props);
|
|
54
|
-
const update = instance.set();
|
|
55
|
-
if (update)
|
|
56
|
-
update.then(() => enabled = true);
|
|
57
|
-
else
|
|
58
|
-
enabled = true;
|
|
59
|
-
}
|
|
60
|
-
useEffect(() => {
|
|
61
|
-
enabled = true;
|
|
62
|
-
return () => {
|
|
63
|
-
release();
|
|
64
|
-
instance.set(null);
|
|
65
|
-
context.pop();
|
|
66
|
-
};
|
|
67
|
-
}, []);
|
|
68
|
-
return local;
|
|
69
|
-
};
|
|
70
|
-
});
|
|
71
|
-
return state[0](argument);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// src/useRemote.ts
|
|
75
|
-
import { effect } from "@expressive/mvc";
|
|
76
|
-
function useRemote(argument) {
|
|
77
|
-
const context = useShared();
|
|
78
|
-
const state = useState(() => {
|
|
79
|
-
const instance = context.get(this);
|
|
80
|
-
const refresh = () => state[1]((x) => x.bind(null));
|
|
81
|
-
if (!instance)
|
|
82
|
-
if (argument === false)
|
|
83
|
-
return () => {
|
|
84
|
-
};
|
|
85
|
-
else
|
|
86
|
-
throw new Error(`Could not find ${this} in context.`);
|
|
87
|
-
function forceUpdate(action) {
|
|
88
|
-
if (typeof action == "function")
|
|
89
|
-
action = action();
|
|
90
|
-
refresh();
|
|
91
|
-
if (action)
|
|
92
|
-
return action.finally(refresh);
|
|
93
|
-
}
|
|
94
|
-
let release;
|
|
95
|
-
let value;
|
|
96
|
-
release = effect(instance, (current) => {
|
|
97
|
-
if (typeof argument === "function") {
|
|
98
|
-
const next = argument.call(current, current, forceUpdate);
|
|
99
|
-
if (next === value)
|
|
100
|
-
return;
|
|
101
|
-
value = next;
|
|
102
|
-
} else
|
|
103
|
-
value = current;
|
|
104
|
-
if (release)
|
|
105
|
-
refresh();
|
|
106
|
-
}, argument === true);
|
|
107
|
-
if (value instanceof Promise) {
|
|
108
|
-
let error;
|
|
109
|
-
release();
|
|
110
|
-
value.then((x) => value = x).catch((e) => error = e).finally(refresh);
|
|
111
|
-
value = null;
|
|
112
|
-
return () => {
|
|
113
|
-
if (error)
|
|
114
|
-
throw error;
|
|
115
|
-
return value === void 0 ? null : value;
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
if (value === null) {
|
|
119
|
-
release();
|
|
120
|
-
return () => null;
|
|
121
|
-
}
|
|
122
|
-
return () => {
|
|
123
|
-
useEffect(() => release, []);
|
|
124
|
-
return value === void 0 ? null : value;
|
|
125
|
-
};
|
|
126
|
-
});
|
|
127
|
-
return state[0]();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// src/index.ts
|
|
131
|
-
import { get, use, ref, set, has } from "@expressive/mvc";
|
|
132
|
-
|
|
133
|
-
// src/consumer.ts
|
|
134
|
-
function Consumer(props) {
|
|
135
|
-
const { children, has: has2, get: get2, for: Type } = props;
|
|
136
|
-
if (typeof children == "function")
|
|
137
|
-
return Type.get(children);
|
|
138
|
-
const context = useShared();
|
|
139
|
-
const instance = useMemo(() => {
|
|
140
|
-
const instance2 = context.get(Type);
|
|
141
|
-
if (!instance2 && has2)
|
|
142
|
-
throw new Error(`Could not find ${Type} in context.`);
|
|
143
|
-
return instance2;
|
|
144
|
-
}, []);
|
|
145
|
-
const callback = has2 || get2;
|
|
146
|
-
if (typeof callback == "function")
|
|
147
|
-
callback(instance);
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/provider.ts
|
|
152
|
-
import { Model as Model4 } from "@expressive/mvc";
|
|
153
|
-
function Provider(props) {
|
|
154
|
-
let { for: include, use: assign } = props;
|
|
155
|
-
const ambient = useShared();
|
|
156
|
-
const context = useMemo(() => ambient.push(), []);
|
|
157
|
-
if (typeof include == "function" || include instanceof Model4)
|
|
158
|
-
include = { [""]: include };
|
|
159
|
-
context.include(include, (model, explicit) => {
|
|
160
|
-
if (assign && explicit)
|
|
161
|
-
model.set(assign);
|
|
162
|
-
});
|
|
163
|
-
useEffect(() => () => context.pop(), []);
|
|
164
|
-
return createProvider(context, props.children);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// src/index.ts
|
|
168
|
-
Model5.as = createComponent;
|
|
169
|
-
Model5.get = useRemote;
|
|
170
|
-
Model5.use = useLocal;
|
|
171
|
-
export {
|
|
172
|
-
Consumer,
|
|
173
|
-
Model5 as Model,
|
|
174
|
-
Provider,
|
|
175
|
-
Model5 as default,
|
|
176
|
-
get,
|
|
177
|
-
has,
|
|
178
|
-
ref,
|
|
179
|
-
set,
|
|
180
|
-
use
|
|
181
|
-
};
|