@expressive/react 0.75.3 → 0.77.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/esm/state.js DELETED
@@ -1,138 +0,0 @@
1
- import { Context, State, watch } from "@expressive/state";
2
-
3
- //#region src/state.ts
4
- const Pragma = {};
5
- State.use = function(...args) {
6
- const outer = Context.get();
7
- const ref = Pragma.useRef(null);
8
- return (ref.current || (ref.current = init(this)))(args);
9
- function init(Type) {
10
- const add = (arg) => typeof arg == "object" && instance.set(arg);
11
- let use = (...args) => Promise.all(args.flat().map(add));
12
- const instance = new Type((x) => {
13
- if ("use" in x && typeof x.use == "function") (use = x.use.bind(x))(...args);
14
- else return args;
15
- });
16
- const context = outer.push(instance);
17
- let ready = false;
18
- let active;
19
- return (args) => {
20
- Pragma.useEffect(() => {
21
- ready = true;
22
- }, []);
23
- useMount((refresh) => {
24
- watch(instance, (current) => {
25
- active = current;
26
- if (ready) refresh();
27
- });
28
- return () => {
29
- context.pop();
30
- instance.set(null);
31
- };
32
- });
33
- if (ready) {
34
- ready = false;
35
- Promise.resolve(use(...args)).finally(() => {
36
- ready = true;
37
- });
38
- }
39
- return active;
40
- };
41
- }
42
- };
43
- State.get = function(argument) {
44
- const ref = Pragma.useRef(null);
45
- const next = Pragma.useState(0)[1];
46
- const context = Context.get();
47
- return (ref.current || (ref.current = init(this)))();
48
- function init(Type) {
49
- let instance;
50
- let unwatch;
51
- let mounted = false;
52
- let pending = false;
53
- let value;
54
- function update() {
55
- pending = false;
56
- next((x) => x + 1);
57
- }
58
- function refresh(action) {
59
- if (typeof action == "function") action = action();
60
- update();
61
- if (action instanceof Promise) return action.finally(update);
62
- }
63
- function release() {
64
- unwatch?.();
65
- unsubscribe();
66
- unwatch = void 0;
67
- instance = void 0;
68
- }
69
- const unsubscribe = context.get(Type, (next) => {
70
- unwatch?.();
71
- unwatch = watch(instance = next, (current, changed) => {
72
- if (typeof argument === "function") {
73
- const next = argument.call(current, current, refresh);
74
- if (next === value) return;
75
- value = next;
76
- } else value = current;
77
- if (changed.length) update();
78
- }, argument === true);
79
- if (mounted) {
80
- pending = true;
81
- queueMicrotask(() => pending && update());
82
- }
83
- return release;
84
- });
85
- if (!instance) {
86
- release();
87
- if (argument === false) return () => void 0;
88
- throw new Error(`Could not find ${Type} in context.`);
89
- }
90
- if (value instanceof Promise) {
91
- let error;
92
- release();
93
- value.then((x) => value = x, (e) => error = e).finally(update);
94
- value = null;
95
- return () => {
96
- if (error) throw error;
97
- return value === void 0 ? null : value;
98
- };
99
- }
100
- if (value === null) {
101
- release();
102
- return () => null;
103
- }
104
- return () => {
105
- pending = false;
106
- Pragma.useEffect(() => {
107
- mounted = true;
108
- }, []);
109
- useMount(() => release);
110
- return value === void 0 ? null : value;
111
- };
112
- }
113
- };
114
- /**
115
- * useEffect which tolerates react StrictMode double-invoking effects on mount.
116
- */
117
- function useMount(callback) {
118
- const { current } = Pragma.useRef({});
119
- current.update = Pragma.useState(() => {
120
- if (!current.mounts) {
121
- current.mounts = 0;
122
- current.unmount = callback(() => {
123
- if (current.ready) current.update((x) => x + 1);
124
- });
125
- }
126
- return current.mounts++;
127
- })[1];
128
- Pragma.useEffect(() => {
129
- current.ready = true;
130
- return () => {
131
- if (!--current.mounts) current.unmount?.();
132
- };
133
- }, []);
134
- }
135
-
136
- //#endregion
137
- export { Pragma, State, useMount };
138
- //# sourceMappingURL=state.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"state.js","names":[],"sources":["../../src/state.ts"],"sourcesContent":["import { State, Context, watch } from '@expressive/state';\n\nexport const Pragma = {} 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\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 <T extends State>(\n this: State.Type<T>,\n ...args: State.UseArgs<T>\n) {\n const outer = Context.get();\n const ref = Pragma.useRef<((args: State.Args<T>) => T) | null>(null);\n const render = ref.current || (ref.current = init(this));\n\n return render(args);\n\n function init(Type: State.Type<T>) {\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 Type((x) => {\n if ('use' in x && typeof x.use == 'function') {\n (use = x.use.bind(x))(...args);\n } else {\n return args;\n }\n });\n\n const context = outer.push(instance);\n\n let ready = false;\n let active: T;\n\n return (args: State.Args<T>) => {\n Pragma.useEffect(() => {\n ready = true;\n }, []);\n\n useMount((refresh) => {\n watch(instance, (current) => {\n active = current;\n\n if (ready) refresh();\n });\n\n return () => {\n context.pop();\n instance.set(null);\n };\n });\n\n if (ready) {\n ready = false;\n Promise.resolve(use(...args)).finally(() => {\n ready = true;\n });\n }\n\n return active;\n };\n }\n};\n\nState.get = function <T extends State>(\n this: State.Extends<T>,\n argument?: boolean | State.GetFactory<T, unknown>\n) {\n const ref = Pragma.useRef<(() => any) | null>(null);\n const next = Pragma.useState(0)[1];\n const context = Context.get();\n const render = ref.current || (ref.current = init(this));\n\n return render();\n\n function init(Type: State.Extends<T>) {\n let instance: T | undefined;\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 unwatch?.();\n unsubscribe();\n unwatch = undefined;\n instance = undefined;\n }\n\n const unsubscribe = context.get(Type, (next) => {\n unwatch?.();\n unwatch = watch(\n (instance = 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 if (!instance) {\n release();\n if (argument === false) return () => undefined;\n throw new Error(`Could not find ${Type} in context.`);\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 if (value === null) {\n release();\n return () => null;\n }\n\n return () => {\n pending = false;\n Pragma.useEffect(() => {\n mounted = true;\n }, []);\n useMount(() => release);\n return value === undefined ? null : value;\n };\n }\n};\n\n/**\n * useEffect which tolerates react StrictMode double-invoking effects on mount.\n */\nexport function useMount(\n callback: (refresh: () => void) => (() => void) | void\n) {\n const { current } = Pragma.useRef(\n {} as {\n ready?: boolean;\n mounts: number;\n unmount: (() => void) | void;\n update: (next: (previous: number) => number) => void;\n }\n );\n\n current.update = Pragma.useState(() => {\n if (!current.mounts) {\n current.mounts = 0;\n current.unmount = callback(() => {\n if (current.ready) current.update((x) => x + 1);\n });\n }\n return current.mounts++;\n })[1];\n\n Pragma.useEffect(() => {\n current.ready = true;\n return () => {\n if (!--current.mounts) current.unmount?.();\n };\n }, []);\n}\n\nexport { State };\n"],"mappings":";;;AAEA,MAAa,SAAS,EAAE;AAwGxB,MAAM,MAAM,SAEV,GAAG,MACH;CACA,MAAM,QAAQ,QAAQ,KAAK;CAC3B,MAAM,MAAM,OAAO,OAA4C,KAAK;AAGpE,SAFe,IAAI,YAAY,IAAI,UAAU,KAAK,KAAK,GAEzC,KAAK;CAEnB,SAAS,KAAK,MAAqB;EACjC,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,WAChC,EAAC,MAAM,EAAE,IAAI,KAAK,EAAE,EAA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