@expressive/react 0.77.0 → 0.78.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/README.md +228 -0
- package/dist/context-B-HNZeaN.d.ts +49 -0
- package/dist/context-BfeYQmAX.js +59 -0
- package/dist/context-BfeYQmAX.js.map +1 -0
- package/dist/index.d.ts +10 -131
- package/dist/index.js +73 -188
- package/dist/index.js.map +1 -1
- package/dist/{state.d.ts → runtime.d.ts} +74 -34
- package/dist/runtime.js +74 -0
- package/dist/runtime.js.map +1 -0
- package/package.json +17 -13
- package/LICENSE +0 -21
- package/dist/state.js +0 -105
- package/dist/state.js.map +0 -1
- package/dist/use-AIA25P7A.js +0 -63
- package/dist/use-AIA25P7A.js.map +0 -1
- package/dist/use-_Es6cU25.d.ts +0 -5
package/dist/state.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { a as useReady, i as useHook, n as Runtime, r as useFactory, t as use } from "./use-AIA25P7A.js";
|
|
2
|
-
import { Context, State, watch } from "@expressive/state";
|
|
3
|
-
|
|
4
|
-
//#region src/state.ts
|
|
5
|
-
State.use = function use(...args) {
|
|
6
|
-
const outer = Context.get();
|
|
7
|
-
return useFactory(() => {
|
|
8
|
-
const add = (arg) => typeof arg == "object" && instance.set(arg);
|
|
9
|
-
let use = (...args) => Promise.all(args.flat().map(add));
|
|
10
|
-
const instance = new this((x) => {
|
|
11
|
-
if ("use" in x && typeof x.use == "function") {
|
|
12
|
-
use = x.use.bind(x);
|
|
13
|
-
use(...args);
|
|
14
|
-
} else return args;
|
|
15
|
-
});
|
|
16
|
-
const context = outer.push(instance);
|
|
17
|
-
let ready = false;
|
|
18
|
-
return (args) => {
|
|
19
|
-
if (ready) {
|
|
20
|
-
ready = false;
|
|
21
|
-
Promise.resolve(use(...args)).finally(() => {
|
|
22
|
-
ready = true;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
useReady(() => ready = true);
|
|
26
|
-
return useHook((update) => {
|
|
27
|
-
watch(instance, update);
|
|
28
|
-
return () => {
|
|
29
|
-
context.pop();
|
|
30
|
-
instance.set(null);
|
|
31
|
-
};
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
})(args);
|
|
35
|
-
};
|
|
36
|
-
State.get = function get(argument) {
|
|
37
|
-
const next = Runtime.useState(0)[1];
|
|
38
|
-
const local = Context.get();
|
|
39
|
-
return useFactory(() => {
|
|
40
|
-
let unwatch;
|
|
41
|
-
let mounted = false;
|
|
42
|
-
let pending = false;
|
|
43
|
-
let value;
|
|
44
|
-
function update() {
|
|
45
|
-
pending = false;
|
|
46
|
-
next((x) => x + 1);
|
|
47
|
-
}
|
|
48
|
-
function refresh(action) {
|
|
49
|
-
if (typeof action == "function") action = action();
|
|
50
|
-
update();
|
|
51
|
-
if (action instanceof Promise) return action.finally(update);
|
|
52
|
-
}
|
|
53
|
-
function release() {
|
|
54
|
-
unsubscribe();
|
|
55
|
-
unwatch?.();
|
|
56
|
-
unwatch = void 0;
|
|
57
|
-
}
|
|
58
|
-
function attach(next) {
|
|
59
|
-
unwatch?.();
|
|
60
|
-
unwatch = watch(next, (current, changed) => {
|
|
61
|
-
if (typeof argument === "function") {
|
|
62
|
-
const next = argument.call(current, current, refresh);
|
|
63
|
-
if (next === value) return;
|
|
64
|
-
value = next;
|
|
65
|
-
} else value = current;
|
|
66
|
-
if (changed.length) update();
|
|
67
|
-
}, argument === true);
|
|
68
|
-
if (mounted) {
|
|
69
|
-
pending = true;
|
|
70
|
-
queueMicrotask(() => pending && update());
|
|
71
|
-
}
|
|
72
|
-
return release;
|
|
73
|
-
}
|
|
74
|
-
const unsubscribe = local.get(this, attach);
|
|
75
|
-
if (!unwatch) {
|
|
76
|
-
unsubscribe();
|
|
77
|
-
if (argument === false) return () => void 0;
|
|
78
|
-
throw new Error(`Could not find ${this} in context.`);
|
|
79
|
-
}
|
|
80
|
-
if (value === null) {
|
|
81
|
-
release();
|
|
82
|
-
return () => null;
|
|
83
|
-
}
|
|
84
|
-
if (value instanceof Promise) {
|
|
85
|
-
let error;
|
|
86
|
-
release();
|
|
87
|
-
value.then((x) => value = x, (e) => error = e).finally(update);
|
|
88
|
-
value = null;
|
|
89
|
-
return () => {
|
|
90
|
-
if (error) throw error;
|
|
91
|
-
return value === void 0 ? null : value;
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
return () => {
|
|
95
|
-
pending = false;
|
|
96
|
-
useReady(() => mounted = true);
|
|
97
|
-
useHook(() => release);
|
|
98
|
-
return value === void 0 ? null : value;
|
|
99
|
-
};
|
|
100
|
-
})();
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
//#endregion
|
|
104
|
-
export { Runtime, State, use };
|
|
105
|
-
//# sourceMappingURL=state.js.map
|
package/dist/state.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","names":[],"sources":["../src/state.ts"],"sourcesContent":["import { State, Context, watch } from '@expressive/state';\nimport { Runtime, useFactory, useHook, useReady } from './runtime';\n\nexport { Runtime };\nexport { use } from './use';\n\n/** Type may not be undefined - instead will be null. */\ntype NoVoid<T> = T extends undefined | void ? null : T;\n\ndeclare module '@expressive/state' {\n interface UseState extends State {\n /**\n * Optional hook called when State.use() is invoked within a React component.\n *\n * This is called *every render* of the component, and will intercept\n * arguments which would otherwise be passed into the State constructor.\n *\n * @param props Arguments passed to State.use().\n */\n use?(...props: any[]): Promise<void> | void;\n }\n\n namespace State {\n type 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\n type GetFactory<T extends State, R> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => R;\n\n type GetEffect<T extends State> = (\n this: T,\n current: T,\n refresh: ForceRefresh\n ) => null;\n\n type UseArgs<T extends State> = T extends {\n use(...props: infer P): any;\n }\n ? P\n : State.Args<T>;\n\n /**\n * Create and manage instance of this State within React component.\n *\n * @param args Arguments to pass to constructor or `use` method (if defined).\n * @returns Managed instance of this State.\n */\n function use<T extends UseState>(\n this: State.Type<T>,\n ...args: UseArgs<T>\n ): T;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(this: State.Extends<T>): T;\n\n /** Fetch instance of this class optionally. */\n function get<T extends State>(\n this: State.Extends<T>,\n required: false\n ): T | undefined;\n\n /** Fetch instance of this class from context. */\n function get<T extends State>(\n this: State.Extends<T>,\n requireValues: true\n ): Required<T>;\n\n function get<T extends State, R>(\n this: State.Extends<T>,\n factory: GetFactory<T, Promise<R> | R>\n ): NoVoid<R>;\n\n function get<T extends State>(\n this: State.Extends<T>,\n factory: GetEffect<T>\n ): null;\n }\n}\n\nState.use = function use<T extends State>(\n this: State.Type<T>,\n ...args: State.UseArgs<T>\n) {\n const outer = Context.get();\n const render = useFactory(() => {\n const add = (arg: unknown) =>\n typeof arg == 'object' && instance.set(arg as State.Assign<T>);\n\n let use = (...args: State.Args<T>) => Promise.all(args.flat().map(add));\n\n const instance = new this((x) => {\n if ('use' in x && typeof x.use == 'function') {\n use = x.use.bind(x);\n use(...args);\n }\n else return args;\n });\n\n const context = outer.push(instance);\n\n let ready = false;\n\n return (args: State.Args<T>) => {\n if (ready) {\n ready = false;\n Promise.resolve(use(...args)).finally(() => {\n ready = true;\n });\n }\n\n useReady(() => ready = true);\n\n return useHook<T>((update) => {\n watch(instance, update);\n return () => {\n context.pop();\n instance.set(null);\n };\n });\n };\n });\n\n return render(args);\n};\n\nState.get = function get<T extends State>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const next = Runtime.useState(0)[1];\n const local = Context.get();\n const render = useFactory(() => {\n let unwatch: (() => void) | undefined;\n let mounted = false;\n let pending = false;\n let value: any;\n\n function update() {\n pending = false;\n next((x) => x + 1);\n }\n\n function refresh<T>(action?: Promise<T> | (() => Promise<T>)): any {\n if (typeof action == 'function') action = action();\n update();\n if (action instanceof Promise) return action.finally(update);\n }\n\n function release() {\n unsubscribe();\n unwatch?.();\n unwatch = undefined;\n }\n\n function attach(next: T) {\n unwatch?.();\n unwatch = watch(\n next,\n (current, changed) => {\n if (typeof argument === 'function') {\n const next = argument.call(current, current, refresh);\n if (next === value) return;\n value = next;\n } else {\n value = current;\n }\n\n if (changed.length) update();\n },\n argument === true\n );\n\n if (mounted) {\n pending = true;\n queueMicrotask(() => pending && update());\n }\n\n return release;\n }\n\n const unsubscribe = local.get(this, attach);\n\n if (!unwatch) {\n unsubscribe();\n if (argument === false) return () => undefined;\n throw new Error(`Could not find ${this} in context.`);\n }\n\n if (value === null) {\n release();\n return () => null;\n }\n\n if (value instanceof Promise) {\n let error: Error | undefined;\n\n release();\n\n value\n .then(\n (x) => (value = x),\n (e) => (error = e)\n )\n .finally(update);\n\n value = null;\n\n return () => {\n if (error) throw error;\n return value === undefined ? null : value;\n };\n }\n\n return () => {\n pending = false;\n useReady(() => mounted = true);\n useHook(() => release);\n return value === undefined ? null : value;\n };\n });\n\n return render();\n};\n\nexport { State };\n"],"mappings":";;;;AAuGA,MAAM,MAAM,SAAS,IAEnB,GAAG,MACH;CACA,MAAM,QAAQ,QAAQ,KAAK;AAuC3B,QAtCe,iBAAiB;EAC9B,MAAM,OAAO,QACX,OAAO,OAAO,YAAY,SAAS,IAAI,IAAuB;EAEhE,IAAI,OAAO,GAAG,SAAwB,QAAQ,IAAI,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC;EAEvE,MAAM,WAAW,IAAI,MAAM,MAAM;AAC/B,OAAI,SAAS,KAAK,OAAO,EAAE,OAAO,YAAY;AAC5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AAO;AAE3C,MAAI,CAAC,SAAS;AACZ,gBAAa;AACb,OAAI,aAAa,MAAO,cAAa;AACrC,SAAM,IAAI,MAAM,kBAAkB,KAAK,cAAc;;AAGvD,MAAI,UAAU,MAAM;AAClB,YAAS;AACT,gBAAa;;AAGf,MAAI,iBAAiB,SAAS;GAC5B,IAAI;AAEJ,YAAS;AAET,SACG,MACE,MAAO,QAAQ,IACf,MAAO,QAAQ,EACjB,CACA,QAAQ,OAAO;AAElB,WAAQ;AAER,gBAAa;AACX,QAAI,MAAO,OAAM;AACjB,WAAO,UAAU,SAAY,OAAO;;;AAIxC,eAAa;AACX,aAAU;AACV,kBAAe,UAAU,KAAK;AAC9B,iBAAc,QAAQ;AACtB,UAAO,UAAU,SAAY,OAAO;;GAEtC,EAEa"}
|
package/dist/use-AIA25P7A.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { event, observer, watch } from "@expressive/state";
|
|
2
|
-
|
|
3
|
-
//#region src/runtime.ts
|
|
4
|
-
const Runtime = {};
|
|
5
|
-
function useFactory(factory) {
|
|
6
|
-
const ref = Runtime.useRef(null);
|
|
7
|
-
return ref.current || (ref.current = factory());
|
|
8
|
-
}
|
|
9
|
-
function useReady(callback) {
|
|
10
|
-
return Runtime.useEffect(() => void callback(), []);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Mount-effect with a refreshable return value, safe under React StrictMode.
|
|
14
|
-
*
|
|
15
|
-
* @param callback Setup handler; receives a setter, must return a cleanup.
|
|
16
|
-
* @returns Latest value published via the setter (`undefined` until set).
|
|
17
|
-
*/
|
|
18
|
-
function useHook(callback) {
|
|
19
|
-
const { current } = Runtime.useRef({ mounted: 0 });
|
|
20
|
-
current.update = Runtime.useState(() => {
|
|
21
|
-
if (!current.mounted) current.unmount = callback((next) => {
|
|
22
|
-
current.output = next;
|
|
23
|
-
current.update?.((x) => x + 1);
|
|
24
|
-
});
|
|
25
|
-
return current.mounted++;
|
|
26
|
-
})[1];
|
|
27
|
-
Runtime.useEffect(() => () => {
|
|
28
|
-
if (--current.mounted < 1) current.unmount();
|
|
29
|
-
}, []);
|
|
30
|
-
return current.output;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/use.ts
|
|
35
|
-
function use(subject) {
|
|
36
|
-
const { current } = Runtime.useRef({
|
|
37
|
-
mounted: 0,
|
|
38
|
-
proxy: subject
|
|
39
|
-
});
|
|
40
|
-
const update = Runtime.useState(() => current.mounted++)[1];
|
|
41
|
-
if (current.source !== subject) {
|
|
42
|
-
const status = observer(subject);
|
|
43
|
-
if (status === null) throw new Error("Provided object is no longer observable.");
|
|
44
|
-
if (!status) throw new Error("Provided object is not observable.");
|
|
45
|
-
if (!status.ready) event(subject);
|
|
46
|
-
current.unwatch?.();
|
|
47
|
-
current.source = subject;
|
|
48
|
-
let init = true;
|
|
49
|
-
current.unwatch = watch(subject, (next, changed) => {
|
|
50
|
-
current.proxy = next;
|
|
51
|
-
if (changed.length && !init) update((x) => x + 1);
|
|
52
|
-
});
|
|
53
|
-
init = false;
|
|
54
|
-
}
|
|
55
|
-
Runtime.useEffect(() => () => {
|
|
56
|
-
if (--current.mounted < 1) current.unwatch?.();
|
|
57
|
-
}, []);
|
|
58
|
-
return current.proxy;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
//#endregion
|
|
62
|
-
export { useReady as a, useHook as i, Runtime as n, useFactory as r, use as t };
|
|
63
|
-
//# sourceMappingURL=use-AIA25P7A.js.map
|
package/dist/use-AIA25P7A.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-AIA25P7A.js","names":[],"sources":["../src/runtime.ts","../src/use.ts"],"sourcesContent":["export const Runtime = {} as {\n createElement(type: any, props?: any, ...children: any[]): any;\n useState<S>(initial: S | (() => S)): [S, (next: (previous: S) => S) => void];\n useEffect(effect: () => (() => void) | void, deps?: any[]): void;\n useRef<T>(initial: T): { current: T };\n};\n\nexport function useFactory<T extends Function>(factory: () => T) {\n const ref = Runtime.useRef<T | null>(null);\n return ref.current || (ref.current = factory());\n}\n\nexport function useReady<T>(callback: () => void) {\n return Runtime.useEffect(() => void callback(), []);\n}\n\n/**\n * Mount-effect with a refreshable return value, safe under React StrictMode.\n *\n * @param callback Setup handler; receives a setter, must return a cleanup.\n * @returns Latest value published via the setter (`undefined` until set).\n */\nexport function useHook<T = void>(\n callback: (refresh: (next: T) => void) => () => void\n) {\n const { current } = Runtime.useRef(\n { mounted: 0 } as {\n mounted: number;\n unmount: () => void;\n update: (next: (previous: number) => number) => void;\n output: T;\n }\n );\n\n current.update = Runtime.useState(() => {\n if (!current.mounted)\n current.unmount = callback((next) => {\n current.output = next;\n current.update?.((x) => x + 1);\n });\n\n return current.mounted++;\n })[1];\n\n Runtime.useEffect(() => () => {\n if (--current.mounted < 1) current.unmount();\n }, []);\n\n return current.output;\n}","import { event, observer, watch } from '@expressive/state';\nimport { Runtime } from './runtime';\n\nexport function use<T extends object>(subject: T) {\n const { current } = Runtime.useRef<{\n proxy: T;\n source?: T;\n mounted: number;\n unwatch?: () => void;\n }>({ mounted: 0, proxy: subject });\n\n const update = Runtime.useState(() => current.mounted++)[1];\n\n if (current.source !== subject) {\n const status = observer(subject);\n\n if (status === null)\n throw new Error('Provided object is no longer observable.');\n\n if (!status)\n throw new Error('Provided object is not observable.');\n\n if (!status.ready) event(subject);\n\n current.unwatch?.();\n current.source = subject;\n\n let init = true;\n\n current.unwatch = watch(subject, (next, changed) => {\n current.proxy = next;\n if (changed.length && !init)\n update((x) => x + 1);\n });\n\n init = false;\n }\n\n Runtime.useEffect(() => () => {\n if (--current.mounted < 1) current.unwatch?.();\n }, []);\n\n return current.proxy;\n}\n"],"mappings":";;;AAAA,MAAa,UAAU,EAAE;AAOzB,SAAgB,WAA+B,SAAkB;CAC/D,MAAM,MAAM,QAAQ,OAAiB,KAAK;AAC1C,QAAO,IAAI,YAAY,IAAI,UAAU,SAAS;;AAGhD,S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