awai-react 0.0.1-alpha-9 → 0.0.1-alpha-11

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/index.cjs ADDED
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var awai = require('awai');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
24
+
25
+ function createGetSnapshot(readable) {
26
+ if (!awai.isReadableAsyncState(readable)) {
27
+ return () => readable.get();
28
+ }
29
+ let previousSnapshot = void 0;
30
+ return () => {
31
+ const snapshot = readable.getAsync();
32
+ const isEqual = snapshot.error === previousSnapshot?.error && snapshot.isLoading === previousSnapshot?.isLoading && snapshot.value === previousSnapshot?.value;
33
+ if (isEqual) {
34
+ return previousSnapshot;
35
+ }
36
+ previousSnapshot = snapshot;
37
+ return snapshot;
38
+ };
39
+ }
40
+
41
+ const createSubscribe = (readable) => {
42
+ return (onStoreChange) => {
43
+ const abortController = new AbortController();
44
+ awai.scenario(
45
+ () => awai.isReadableAsyncState(readable) ? awai.race(
46
+ [readable.events.requested, readable.events.fulfilled, readable.events.rejected],
47
+ abortController.signal
48
+ ) : readable.events.changed.abortable(abortController.signal),
49
+ onStoreChange,
50
+ { until: abortController.signal }
51
+ );
52
+ return () => {
53
+ abortController.abort();
54
+ };
55
+ };
56
+ };
57
+
58
+ const useAwaiAsyncValue = (readable) => {
59
+ const subscribe = React.useMemo(() => createSubscribe(readable), [readable]);
60
+ const getSnapshot = React.useMemo(() => createGetSnapshot(readable), [readable]);
61
+ return React.useSyncExternalStore(subscribe, getSnapshot);
62
+ };
63
+
64
+ const useAwaiSetState = (writable) => {
65
+ return writable.set;
66
+ };
67
+
68
+ const { useMemo, useSyncExternalStore } = React__namespace;
69
+ const { use } = React__namespace;
70
+ const suspend = (promise) => {
71
+ if (use) {
72
+ use(promise);
73
+ return;
74
+ }
75
+ throw promise;
76
+ };
77
+ const useAwaiStateValue = (readable) => {
78
+ const isAsync = awai.isReadableAsyncState(readable);
79
+ const getSnapshot = useMemo(() => createGetSnapshot(readable), [readable]);
80
+ const subscribe = useMemo(() => createSubscribe(readable), [readable]);
81
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot);
82
+ const isLoading = isAsync && snapshot.isLoading;
83
+ const isSettledPromise = useMemo(
84
+ () => isLoading ? readable.getPromise() : void 0,
85
+ [readable, isLoading]
86
+ );
87
+ if (!isAsync) {
88
+ return snapshot;
89
+ }
90
+ const asyncSnapshot = snapshot;
91
+ if (asyncSnapshot.error) {
92
+ throw asyncSnapshot.error;
93
+ }
94
+ if (isSettledPromise) {
95
+ suspend(isSettledPromise);
96
+ }
97
+ return asyncSnapshot.value;
98
+ };
99
+
100
+ function useAwaiState(state) {
101
+ const value = useAwaiStateValue(state);
102
+ const setValue = useAwaiSetState(state);
103
+ return [value, setValue];
104
+ }
105
+
106
+ exports.useAwaiAsyncValue = useAwaiAsyncValue;
107
+ exports.useAwaiSetState = useAwaiSetState;
108
+ exports.useAwaiState = useAwaiState;
109
+ exports.useAwaiStateValue = useAwaiStateValue;
110
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/lib/createGetSnapshot.ts","../src/lib/createSubscribe.ts","../src/useAwaiAsyncValue.ts","../src/useAwaiSetState.ts","../src/useAwaiStateValue.ts","../src/useAwaiState.ts"],"sourcesContent":["import { AsyncValue, isReadableAsyncState, ReadableAsyncState, ReadableState } from 'awai';\n\ntype SnapshotValue<R> = R extends ReadableAsyncState<infer T>\n ? AsyncValue<T>\n : R extends ReadableState<infer T>\n ? T\n : never;\n\ntype ReadableInput = ReadableState<any> | ReadableAsyncState<any>;\n\nfunction createGetSnapshot<const R extends ReadableInput>(readable: R): () => SnapshotValue<R> {\n if (!isReadableAsyncState(readable)) {\n return () => readable.get();\n }\n\n let previousSnapshot: AsyncValue<unknown> | undefined = undefined;\n\n return () => {\n const snapshot = readable.getAsync();\n const isEqual =\n snapshot.error === previousSnapshot?.error &&\n snapshot.isLoading === previousSnapshot?.isLoading &&\n snapshot.value === previousSnapshot?.value;\n\n if (isEqual) {\n return previousSnapshot as SnapshotValue<R>;\n }\n\n previousSnapshot = snapshot;\n return snapshot as SnapshotValue<R>;\n };\n}\n\nexport default createGetSnapshot;\n","import { isReadableAsyncState, ReadableAsyncState, ReadableState, race, scenario } from 'awai';\n\nconst createSubscribe = <T extends ReadableState<any> | ReadableAsyncState<any>>(readable: T) => {\n return (onStoreChange: () => void) => {\n const abortController = new AbortController();\n\n scenario(\n () =>\n isReadableAsyncState(readable)\n ? race(\n [readable.events.requested, readable.events.fulfilled, readable.events.rejected],\n abortController.signal,\n )\n : readable.events.changed.abortable(abortController.signal),\n onStoreChange,\n { until: abortController.signal },\n );\n\n return () => {\n abortController.abort();\n };\n };\n};\n\nexport default createSubscribe;\n","import type { ReadableAsyncState, AsyncValue } from 'awai';\nimport { useMemo, useSyncExternalStore } from 'react';\nimport createGetSnapshot from './lib/createGetSnapshot';\nimport createSubscribe from './lib/createSubscribe';\n\nconst useAwaiAsyncValue = <T>(readable: ReadableAsyncState<T>): AsyncValue<T> => {\n const subscribe = useMemo(() => createSubscribe(readable), [readable]);\n const getSnapshot = useMemo(() => createGetSnapshot(readable), [readable]);\n\n return useSyncExternalStore(subscribe, getSnapshot);\n};\n\nexport default useAwaiAsyncValue;\n","import type { WritableAsyncState, WritableState } from 'awai';\n\nconst useAwaiSetState = <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(\n writable: Q,\n): Q['set'] => {\n return writable.set;\n};\n\nexport default useAwaiSetState;\n","import {\n type AsyncValue,\n type ReadableAsyncState,\n type ReadableState,\n isReadableAsyncState,\n} from 'awai';\nimport * as React from 'react';\nimport createSubscribe from './lib/createSubscribe';\nimport createGetSnapshot from './lib/createGetSnapshot';\n\nconst { useMemo, useSyncExternalStore } = React;\nconst { use } = React as { use?: (promise: Promise<unknown>) => unknown };\nconst suspend = (promise: Promise<unknown>) => {\n if (use) {\n use(promise);\n return;\n }\n\n throw promise;\n};\n\nconst useAwaiStateValue = <T>(readable: ReadableState<T> | ReadableAsyncState<T>): T => {\n const isAsync = isReadableAsyncState(readable);\n const getSnapshot = useMemo(() => createGetSnapshot(readable), [readable]);\n const subscribe = useMemo(() => createSubscribe(readable), [readable]);\n const snapshot = useSyncExternalStore(subscribe, getSnapshot);\n const isLoading = isAsync && (snapshot as AsyncValue<T>).isLoading;\n\n const isSettledPromise = useMemo(\n () => (isLoading ? readable.getPromise() : undefined),\n [readable, isLoading],\n );\n\n if (!isAsync) {\n return snapshot as T;\n }\n\n const asyncSnapshot = snapshot as AsyncValue<T>;\n\n if (asyncSnapshot.error) {\n throw asyncSnapshot.error;\n }\n\n if (isSettledPromise) {\n suspend(isSettledPromise);\n }\n\n return asyncSnapshot.value!;\n};\n\nexport default useAwaiStateValue;\n","import type { AsyncSetter, AsyncState, Setter, State } from 'awai';\n\nimport useAwaiSetState from './useAwaiSetState';\nimport useAwaiStateValue from './useAwaiStateValue';\n\ntype Return<T> = T extends State<infer U>\n ? [U, Setter<U>]\n : T extends AsyncState<infer V>\n ? [V, AsyncSetter<V>]\n : never;\n\nfunction useAwaiState<T>(state: State<T>): Return<State<T>>;\nfunction useAwaiState<T>(state: AsyncState<T>): Return<AsyncState<T>>;\nfunction useAwaiState<T>(state: State<T> | AsyncState<T>) {\n const value = useAwaiStateValue(state);\n const setValue = useAwaiSetState(state);\n\n return [value, setValue];\n}\n\nexport default useAwaiState;\n"],"names":["isReadableAsyncState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AAC5D,EAAM,MAAA,SAAA,GAAY,WAAY,QAA2B,CAAA,SAAA,CAAA;AAEzD,EAAA,MAAM,gBAAmB,GAAA,OAAA;AAAA,IACvB,MAAO,SAAA,GAAY,QAAS,CAAA,UAAA,EAAe,GAAA,KAAA,CAAA;AAAA,IAC3C,CAAC,UAAU,SAAS,CAAA;AAAA,GACtB,CAAA;AAEA,EAAA,IAAI,CAAC,OAAS,EAAA;AACZ,IAAO,OAAA,QAAA,CAAA;AAAA,GACT;AAEA,EAAA,MAAM,aAAgB,GAAA,QAAA,CAAA;AAEtB,EAAA,IAAI,cAAc,KAAO,EAAA;AACvB,IAAA,MAAM,aAAc,CAAA,KAAA,CAAA;AAAA,GACtB;AAEA,EAAA,IAAI,gBAAkB,EAAA;AACpB,IAAA,OAAA,CAAQ,gBAAgB,CAAA,CAAA;AAAA,GAC1B;AAEA,EAAA,OAAO,aAAc,CAAA,KAAA,CAAA;AACvB;;ACnCA,SAAS,aAAgB,KAAiC,EAAA;AACxD,EAAM,MAAA,KAAA,GAAQ,kBAAkB,KAAK,CAAA,CAAA;AACrC,EAAM,MAAA,QAAA,GAAW,gBAAgB,KAAK,CAAA,CAAA;AAEtC,EAAO,OAAA,CAAC,OAAO,QAAQ,CAAA,CAAA;AACzB;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- import { ReadableAsyncState, InferReadableType, AsyncValue, WritableState, WritableAsyncState, State, AsyncState, Setter, AsyncSetter, ReadableState } from 'awai';
1
+ import { ReadableAsyncState, AsyncValue, WritableState, WritableAsyncState, State, AsyncState, Setter, AsyncSetter, ReadableState } from 'awai';
2
2
 
3
- declare const useAsyncStateValue: <T extends ReadableAsyncState<any>, V = InferReadableType<T>>(readable: T) => AsyncValue<V>;
3
+ declare const useAwaiAsyncValue: <T>(readable: ReadableAsyncState<T>) => AsyncValue<T>;
4
4
 
5
- declare const useSetState: <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(writable: Q) => Q["set"];
5
+ declare const useAwaiSetState: <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(writable: Q) => Q["set"];
6
6
 
7
7
  type Return<T> = T extends State<infer U> ? [U, Setter<U>] : T extends AsyncState<infer V> ? [V, AsyncSetter<V>] : never;
8
- declare function useState<T>(state: State<T>): Return<State<T>>;
9
- declare function useState<T>(state: AsyncState<T>): Return<AsyncState<T>>;
8
+ declare function useAwaiState<T>(state: State<T>): Return<State<T>>;
9
+ declare function useAwaiState<T>(state: AsyncState<T>): Return<AsyncState<T>>;
10
10
 
11
- declare const useStateValue: <T extends ReadableAsyncState<any> | ReadableState<any>>(readable: T) => InferReadableType<T>;
11
+ declare const useAwaiStateValue: <T>(readable: ReadableState<T> | ReadableAsyncState<T>) => T;
12
12
 
13
- export { useAsyncStateValue, useSetState, useState, useStateValue };
13
+ export { useAwaiAsyncValue, useAwaiSetState, useAwaiState, useAwaiStateValue };
package/dist/index.mjs CHANGED
@@ -1,74 +1,87 @@
1
- import { useState as useState$1, useEffect, startTransition } from 'react';
2
- import { isReadableAsyncState } from 'awai';
1
+ import * as React from 'react';
2
+ import { useMemo as useMemo$1, useSyncExternalStore as useSyncExternalStore$1 } from 'react';
3
+ import { isReadableAsyncState, scenario, race } from 'awai';
3
4
 
4
- const useAsyncStateValue = (readable) => {
5
- const [state, setState] = useState$1(readable.getAsync);
6
- useEffect(() => {
7
- let mounted = true;
8
- let abortController;
9
- setState(readable.getAsync());
10
- (async () => {
11
- while (mounted) {
12
- abortController = new AbortController();
13
- try {
14
- await Promise.any([
15
- readable.events.fulfilled.abortable(abortController),
16
- readable.events.rejected.abortable(abortController),
17
- readable.events.requested.abortable(abortController)
18
- ]);
19
- const newAsync = readable.getAsync();
20
- const isChanged = newAsync.error !== state.error || newAsync.isLoading !== state.isLoading || newAsync.value !== state.value;
21
- if (isChanged && mounted) {
22
- setState(readable.getAsync());
23
- }
24
- } catch {
25
- } finally {
26
- abortController.abort();
27
- }
28
- }
29
- })();
5
+ function createGetSnapshot(readable) {
6
+ if (!isReadableAsyncState(readable)) {
7
+ return () => readable.get();
8
+ }
9
+ let previousSnapshot = void 0;
10
+ return () => {
11
+ const snapshot = readable.getAsync();
12
+ const isEqual = snapshot.error === previousSnapshot?.error && snapshot.isLoading === previousSnapshot?.isLoading && snapshot.value === previousSnapshot?.value;
13
+ if (isEqual) {
14
+ return previousSnapshot;
15
+ }
16
+ previousSnapshot = snapshot;
17
+ return snapshot;
18
+ };
19
+ }
20
+
21
+ const createSubscribe = (readable) => {
22
+ return (onStoreChange) => {
23
+ const abortController = new AbortController();
24
+ scenario(
25
+ () => isReadableAsyncState(readable) ? race(
26
+ [readable.events.requested, readable.events.fulfilled, readable.events.rejected],
27
+ abortController.signal
28
+ ) : readable.events.changed.abortable(abortController.signal),
29
+ onStoreChange,
30
+ { until: abortController.signal }
31
+ );
30
32
  return () => {
31
- mounted = false;
32
- abortController?.abort();
33
+ abortController.abort();
33
34
  };
34
- }, [readable]);
35
- return state;
35
+ };
36
36
  };
37
37
 
38
- const useSetState = (writable) => {
38
+ const useAwaiAsyncValue = (readable) => {
39
+ const subscribe = useMemo$1(() => createSubscribe(readable), [readable]);
40
+ const getSnapshot = useMemo$1(() => createGetSnapshot(readable), [readable]);
41
+ return useSyncExternalStore$1(subscribe, getSnapshot);
42
+ };
43
+
44
+ const useAwaiSetState = (writable) => {
39
45
  return writable.set;
40
46
  };
41
47
 
42
- const useStateValue = (readable) => {
43
- const [state, setState] = useState$1(readable.get);
44
- if (isReadableAsyncState(readable) && readable.get() === void 0) {
45
- startTransition(() => {
46
- throw new Promise((resolve) => readable.events.changed.then(resolve));
47
- });
48
+ const { useMemo, useSyncExternalStore } = React;
49
+ const { use } = React;
50
+ const suspend = (promise) => {
51
+ if (use) {
52
+ use(promise);
53
+ return;
48
54
  }
49
- useEffect(() => {
50
- let mounted = true;
51
- setState(readable.get());
52
- (async () => {
53
- while (mounted) {
54
- const newValue = await readable.events.changed;
55
- if (mounted) {
56
- setState(newValue);
57
- }
58
- }
59
- })();
60
- return () => {
61
- mounted = false;
62
- };
63
- }, [readable]);
64
- return state;
55
+ throw promise;
56
+ };
57
+ const useAwaiStateValue = (readable) => {
58
+ const isAsync = isReadableAsyncState(readable);
59
+ const getSnapshot = useMemo(() => createGetSnapshot(readable), [readable]);
60
+ const subscribe = useMemo(() => createSubscribe(readable), [readable]);
61
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot);
62
+ const isLoading = isAsync && snapshot.isLoading;
63
+ const isSettledPromise = useMemo(
64
+ () => isLoading ? readable.getPromise() : void 0,
65
+ [readable, isLoading]
66
+ );
67
+ if (!isAsync) {
68
+ return snapshot;
69
+ }
70
+ const asyncSnapshot = snapshot;
71
+ if (asyncSnapshot.error) {
72
+ throw asyncSnapshot.error;
73
+ }
74
+ if (isSettledPromise) {
75
+ suspend(isSettledPromise);
76
+ }
77
+ return asyncSnapshot.value;
65
78
  };
66
79
 
67
- function useState(state) {
68
- const value = useStateValue(state);
69
- const setValue = useSetState(state);
80
+ function useAwaiState(state) {
81
+ const value = useAwaiStateValue(state);
82
+ const setValue = useAwaiSetState(state);
70
83
  return [value, setValue];
71
84
  }
72
85
 
73
- export { useAsyncStateValue, useSetState, useState, useStateValue };
86
+ export { useAwaiAsyncValue, useAwaiSetState, useAwaiState, useAwaiStateValue };
74
87
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/useAsyncStateValue.ts","../src/useSetState.ts","../src/useStateValue.ts","../src/useState.ts"],"sourcesContent":["import type { InferReadableType, ReadableAsyncState, AsyncValue } from 'awai';\nimport { useEffect, useState } from 'react';\n\nconst useAsyncStateValue = <T extends ReadableAsyncState<any>, V = InferReadableType<T>>(\n readable: T,\n): AsyncValue<V> => {\n const [state, setState] = useState<AsyncValue<V>>(readable.getAsync);\n\n useEffect(() => {\n let mounted = true;\n let abortController: AbortController;\n\n setState(readable.getAsync());\n\n (async () => {\n while (mounted) {\n abortController = new AbortController();\n /**\n * @todo Cleanup on unmount\n * @url https://github.com/yuriyyakym/awai/issues/1\n */\n try {\n await Promise.any([\n readable.events.fulfilled.abortable(abortController),\n readable.events.rejected.abortable(abortController),\n readable.events.requested.abortable(abortController),\n ]);\n\n const newAsync = readable.getAsync();\n const isChanged =\n newAsync.error !== state.error ||\n newAsync.isLoading !== state.isLoading ||\n newAsync.value !== state.value;\n\n if (isChanged && mounted) {\n setState(readable.getAsync());\n }\n } catch {\n } finally {\n abortController.abort();\n }\n }\n })();\n\n return () => {\n mounted = false;\n abortController?.abort();\n };\n }, [readable]);\n\n return state;\n};\n\nexport default useAsyncStateValue;\n","import type { WritableAsyncState, WritableState } from 'awai';\n\nconst useSetState = <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(\n writable: Q,\n): Q['set'] => {\n return writable.set;\n};\n\nexport default useSetState;\n","import {\n type InferReadableType,\n type ReadableAsyncState,\n type ReadableState,\n isReadableAsyncState,\n} from 'awai';\nimport { startTransition, useEffect, useState } from 'react';\n\nconst useStateValue = <T extends ReadableState<any> | ReadableAsyncState<any>>(\n readable: T,\n): InferReadableType<T> => {\n const [state, setState] = useState<T | undefined>(readable.get);\n\n if (isReadableAsyncState(readable) && readable.get() === undefined) {\n startTransition(() => {\n throw new Promise((resolve) => readable.events.changed.then(resolve));\n });\n }\n\n useEffect(() => {\n let mounted = true;\n\n setState(readable.get());\n\n (async () => {\n while (mounted) {\n /**\n * @todo Cleanup on unmount\n * @url https://github.com/yuriyyakym/awai/issues/1\n */\n const newValue = await readable.events.changed;\n if (mounted) {\n setState(newValue);\n }\n }\n })();\n\n return () => {\n mounted = false;\n };\n }, [readable]);\n\n return state as InferReadableType<T>;\n};\n\nexport default useStateValue;\n","import type { AsyncSetter, AsyncState, Setter, State } from 'awai';\n\nimport useSetState from './useSetState';\nimport useStateValue from './useStateValue';\n\ntype Return<T> = T extends State<infer U>\n ? [U, Setter<U>]\n : T extends AsyncState<infer V>\n ? [V, AsyncSetter<V>]\n : never;\n\nfunction useState<T>(state: State<T>): Return<State<T>>;\nfunction useState<T>(state: AsyncState<T>): Return<AsyncState<T>>;\nfunction useState<T>(state: State<T> | AsyncState<T>) {\n const value = useStateValue(state);\n const setValue = useSetState(state);\n\n return [value, setValue];\n}\n\nexport default useState;\n"],"names":["useState"],"mappings":";;;AAGM,MAAA,kBAAA,GAAqB,CACzB,QACkB,KAAA;AAClB,EAAA,MAAM,CAAC,KAAO,EAAA,QAAQ,CAAI,GAAAA,UAAA,CAAwB,SAAS,QAAQ,CAAA,CAAA;AAEnE,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAU,GAAA,IAAA,CAAA;AACd,IAAI,IAAA,eAAA,CAAA;AAEJ,IAAS,QAAA,CAAA,QAAA,CAAS,UAAU,CAAA,CAAA;AAE5B,IAAA,CAAC,YAAY;AACX,MAAA,OAAO,OAAS,EAAA;AACd,QAAA,eAAA,GAAkB,IAAI,eAAgB,EAAA,CAAA;AAKtC,QAAI,IAAA;AACF,UAAA,MAAM,QAAQ,GAAI,CAAA;AAAA,YAChB,QAAS,CAAA,MAAA,CAAO,SAAU,CAAA,SAAA,CAAU,eAAe,CAAA;AAAA,YACnD,QAAS,CAAA,MAAA,CAAO,QAAS,CAAA,SAAA,CAAU,eAAe,CAAA;AAAA,YAClD,QAAS,CAAA,MAAA,CAAO,SAAU,CAAA,SAAA,CAAU,eAAe,CAAA;AAAA,WACpD,CAAA,CAAA;AAED,UAAM,MAAA,QAAA,GAAW,SAAS,QAAS,EAAA,CAAA;AACnC,UAAM,MAAA,SAAA,GACJ,QAAS,CAAA,KAAA,KAAU,KAAM,CAAA,KAAA,IACzB,QAAS,CAAA,SAAA,KAAc,KAAM,CAAA,SAAA,IAC7B,QAAS,CAAA,KAAA,KAAU,KAAM,CAAA,KAAA,CAAA;AAE3B,UAAA,IAAI,aAAa,OAAS,EAAA;AACxB,YAAS,QAAA,CAAA,QAAA,CAAS,UAAU,CAAA,CAAA;AAAA,WAC9B;AAAA,SACM,CAAA,M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A,CAAA;AAAA,KACZ,CAAA;AAAA,GACF,EAAG,CAAC,QAAQ,CAAC,CAAA,CAAA;AAEb,EAAO,OAAA,KAAA,CAAA;AACT;;AC9BA,SAAS,SAAY,KAAiC,EAAA;AACpD,EAAM,MAAA,KAAA,GAAQ,cAAc,KAAK,CAAA,CAAA;AACjC,EAAM,MAAA,QAAA,GAAW,YAAY,KAAK,CAAA,CAAA;AAElC,EAAO,OAAA,CAAC,OAAO,QAAQ,CAAA,CAAA;AACzB;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/lib/createGetSnapshot.ts","../src/lib/createSubscribe.ts","../src/useAwaiAsyncValue.ts","../src/useAwaiSetState.ts","../src/useAwaiStateValue.ts","../src/useAwaiState.ts"],"sourcesContent":["import { AsyncValue, isReadableAsyncState, ReadableAsyncState, ReadableState } from 'awai';\n\ntype SnapshotValue<R> = R extends ReadableAsyncState<infer T>\n ? AsyncValue<T>\n : R extends ReadableState<infer T>\n ? T\n : never;\n\ntype ReadableInput = ReadableState<any> | ReadableAsyncState<any>;\n\nfunction createGetSnapshot<const R extends ReadableInput>(readable: R): () => SnapshotValue<R> {\n if (!isReadableAsyncState(readable)) {\n return () => readable.get();\n }\n\n let previousSnapshot: AsyncValue<unknown> | undefined = undefined;\n\n return () => {\n const snapshot = readable.getAsync();\n const isEqual =\n snapshot.error === previousSnapshot?.error &&\n snapshot.isLoading === previousSnapshot?.isLoading &&\n snapshot.value === previousSnapshot?.value;\n\n if (isEqual) {\n return previousSnapshot as SnapshotValue<R>;\n }\n\n previousSnapshot = snapshot;\n return snapshot as SnapshotValue<R>;\n };\n}\n\nexport default createGetSnapshot;\n","import { isReadableAsyncState, ReadableAsyncState, ReadableState, race, scenario } from 'awai';\n\nconst createSubscribe = <T extends ReadableState<any> | ReadableAsyncState<any>>(readable: T) => {\n return (onStoreChange: () => void) => {\n const abortController = new AbortController();\n\n scenario(\n () =>\n isReadableAsyncState(readable)\n ? race(\n [readable.events.requested, readable.events.fulfilled, readable.events.rejected],\n abortController.signal,\n )\n : readable.events.changed.abortable(abortController.signal),\n onStoreChange,\n { until: abortController.signal },\n );\n\n return () => {\n abortController.abort();\n };\n };\n};\n\nexport default createSubscribe;\n","import type { ReadableAsyncState, AsyncValue } from 'awai';\nimport { useMemo, useSyncExternalStore } from 'react';\nimport createGetSnapshot from './lib/createGetSnapshot';\nimport createSubscribe from './lib/createSubscribe';\n\nconst useAwaiAsyncValue = <T>(readable: ReadableAsyncState<T>): AsyncValue<T> => {\n const subscribe = useMemo(() => createSubscribe(readable), [readable]);\n const getSnapshot = useMemo(() => createGetSnapshot(readable), [readable]);\n\n return useSyncExternalStore(subscribe, getSnapshot);\n};\n\nexport default useAwaiAsyncValue;\n","import type { WritableAsyncState, WritableState } from 'awai';\n\nconst useAwaiSetState = <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(\n writable: Q,\n): Q['set'] => {\n return writable.set;\n};\n\nexport default useAwaiSetState;\n","import {\n type AsyncValue,\n type ReadableAsyncState,\n type ReadableState,\n isReadableAsyncState,\n} from 'awai';\nimport * as React from 'react';\nimport createSubscribe from './lib/createSubscribe';\nimport createGetSnapshot from './lib/createGetSnapshot';\n\nconst { useMemo, useSyncExternalStore } = React;\nconst { use } = React as { use?: (promise: Promise<unknown>) => unknown };\nconst suspend = (promise: Promise<unknown>) => {\n if (use) {\n use(promise);\n return;\n }\n\n throw promise;\n};\n\nconst useAwaiStateValue = <T>(readable: ReadableState<T> | ReadableAsyncState<T>): T => {\n const isAsync = isReadableAsyncState(readable);\n const getSnapshot = useMemo(() => createGetSnapshot(readable), [readable]);\n const subscribe = useMemo(() => createSubscribe(readable), [readable]);\n const snapshot = useSyncExternalStore(subscribe, getSnapshot);\n const isLoading = isAsync && (snapshot as AsyncValue<T>).isLoading;\n\n const isSettledPromise = useMemo(\n () => (isLoading ? readable.getPromise() : undefined),\n [readable, isLoading],\n );\n\n if (!isAsync) {\n return snapshot as T;\n }\n\n const asyncSnapshot = snapshot as AsyncValue<T>;\n\n if (asyncSnapshot.error) {\n throw asyncSnapshot.error;\n }\n\n if (isSettledPromise) {\n suspend(isSettledPromise);\n }\n\n return asyncSnapshot.value!;\n};\n\nexport default useAwaiStateValue;\n","import type { AsyncSetter, AsyncState, Setter, State } from 'awai';\n\nimport useAwaiSetState from './useAwaiSetState';\nimport useAwaiStateValue from './useAwaiStateValue';\n\ntype Return<T> = T extends State<infer U>\n ? [U, Setter<U>]\n : T extends AsyncState<infer V>\n ? [V, AsyncSetter<V>]\n : never;\n\nfunction useAwaiState<T>(state: State<T>): Return<State<T>>;\nfunction useAwaiState<T>(state: AsyncState<T>): Return<AsyncState<T>>;\nfunction useAwaiState<T>(state: State<T> | AsyncState<T>) {\n const value = useAwaiStateValue(state);\n const setValue = useAwaiSetState(state);\n\n return [value, setValue];\n}\n\nexport default useAwaiState;\n"],"names":["useMemo","useSyncExternalStore"],"mappings":";;;;AAUA,SAAS,kBAAiD,QAAqC,EAAA;AAC7F,EAAI,IAAA,CAAC,oBAAqB,CAAA,QAAQ,CAAG,EAAA;AACnC,IAAO,OAAA,MAAM,SAAS,GAAI,EAAA,CAAA;AAAA,GAC5B;AAEA,EAAA,IAAI,gBAAoD,GAAA,KAAA,CAAA,CAAA;AAExD,EAAA,OAAO,MAAM;AACX,IAAM,MAAA,QAAA,GAAW,SAAS,QAAS,EAAA,CAAA;AACnC,IAAM,MAAA,OAAA,GACJ,QAAS,CAAA,KAAA,KAAU,gBAAkB,EAAA,KAAA,IACrC,QAAS,CAAA,SAAA,KAAc,gBAAkB,EAAA,SAAA,IACzC,QAAS,CAAA,KAAA,KAAU,gBAAkB,EAAA,KAAA,CAAA;AAEvC,IAAA,IAAI,OAAS,EAAA;AACX,MAAO,OAAA,gBAAA,CAAA;AAAA,KACT;AAEA,IAAmB,gBAAA,GAAA,QAAA,CAAA;AACnB,IAAO,OAAA,QAAA,CAAA;AAAA,GACT,CAAA;AACF;;AC7BA,MAAM,eAAA,GAAkB,CAAyD,QAAgB,KAAA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AA,CAAC,OAAO,QAAQ,CAAA,CAAA;AACzB;;;;"}
package/package.json CHANGED
@@ -1,20 +1,27 @@
1
1
  {
2
2
  "name": "awai-react",
3
- "version": "v0.0.1-alpha-9",
3
+ "version": "v0.0.1-alpha-11",
4
4
  "author": "Yuriy Yakym",
5
5
  "description": "React hooks for Awai integration",
6
6
  "license": "MIT",
7
- "main": "dist/index.js",
7
+ "main": "dist/index.cjs",
8
8
  "module": "dist/index.mjs",
9
9
  "typings": "dist/index.d.ts",
10
10
  "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
11
18
  "files": [
12
19
  "dist"
13
20
  ],
14
21
  "scripts": {
15
22
  "build": "rm -rf dist && rollup -c rollup.config.js",
16
- "test": "jest",
17
- "test:watch": "jest --watch"
23
+ "test": "vitest run",
24
+ "test:watch": "vitest"
18
25
  },
19
26
  "repository": {
20
27
  "type": "git",
@@ -33,21 +40,20 @@
33
40
  "async"
34
41
  ],
35
42
  "devDependencies": {
36
- "@testing-library/react": "^14.0.0",
37
- "@types/jest": "^29.5.3",
38
- "@types/react": "^18.2.16",
39
- "awai": "^1.0.0-alpha-2",
40
- "jest": "^29.6.2",
41
- "jest-environment-jsdom": "^29.6.2",
42
- "react": "^18.2.0",
43
+ "@types/react": "^19.0.0",
44
+ "@types/react-dom": "^19.2.3",
45
+ "awai": "^1.0.0-rc-7",
46
+ "jsdom": "^24.0.0",
47
+ "react": "^19.0.0",
48
+ "react-dom": "^19.2.3",
43
49
  "rollup": "^3.26.3",
44
50
  "rollup-plugin-dts": "^5.3.0",
45
51
  "rollup-plugin-esbuild": "^5.0.0",
46
- "ts-jest": "^29.1.1",
47
- "typescript": "^5.1.6"
52
+ "typescript": "^5.1.6",
53
+ "vitest": "^2.1.0"
48
54
  },
49
55
  "peerDependencies": {
50
56
  "awai": "*",
51
- "react": "*"
57
+ "react": "^18.2.0 || ^19.0.0"
52
58
  }
53
59
  }
package/dist/index.js DELETED
@@ -1,79 +0,0 @@
1
- 'use strict';
2
-
3
- var react = require('react');
4
- var awai = require('awai');
5
-
6
- const useAsyncStateValue = (readable) => {
7
- const [state, setState] = react.useState(readable.getAsync);
8
- react.useEffect(() => {
9
- let mounted = true;
10
- let abortController;
11
- setState(readable.getAsync());
12
- (async () => {
13
- while (mounted) {
14
- abortController = new AbortController();
15
- try {
16
- await Promise.any([
17
- readable.events.fulfilled.abortable(abortController),
18
- readable.events.rejected.abortable(abortController),
19
- readable.events.requested.abortable(abortController)
20
- ]);
21
- const newAsync = readable.getAsync();
22
- const isChanged = newAsync.error !== state.error || newAsync.isLoading !== state.isLoading || newAsync.value !== state.value;
23
- if (isChanged && mounted) {
24
- setState(readable.getAsync());
25
- }
26
- } catch {
27
- } finally {
28
- abortController.abort();
29
- }
30
- }
31
- })();
32
- return () => {
33
- mounted = false;
34
- abortController?.abort();
35
- };
36
- }, [readable]);
37
- return state;
38
- };
39
-
40
- const useSetState = (writable) => {
41
- return writable.set;
42
- };
43
-
44
- const useStateValue = (readable) => {
45
- const [state, setState] = react.useState(readable.get);
46
- if (awai.isReadableAsyncState(readable) && readable.get() === void 0) {
47
- react.startTransition(() => {
48
- throw new Promise((resolve) => readable.events.changed.then(resolve));
49
- });
50
- }
51
- react.useEffect(() => {
52
- let mounted = true;
53
- setState(readable.get());
54
- (async () => {
55
- while (mounted) {
56
- const newValue = await readable.events.changed;
57
- if (mounted) {
58
- setState(newValue);
59
- }
60
- }
61
- })();
62
- return () => {
63
- mounted = false;
64
- };
65
- }, [readable]);
66
- return state;
67
- };
68
-
69
- function useState(state) {
70
- const value = useStateValue(state);
71
- const setValue = useSetState(state);
72
- return [value, setValue];
73
- }
74
-
75
- exports.useAsyncStateValue = useAsyncStateValue;
76
- exports.useSetState = useSetState;
77
- exports.useState = useState;
78
- exports.useStateValue = useStateValue;
79
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/useAsyncStateValue.ts","../src/useSetState.ts","../src/useStateValue.ts","../src/useState.ts"],"sourcesContent":["import type { InferReadableType, ReadableAsyncState, AsyncValue } from 'awai';\nimport { useEffect, useState } from 'react';\n\nconst useAsyncStateValue = <T extends ReadableAsyncState<any>, V = InferReadableType<T>>(\n readable: T,\n): AsyncValue<V> => {\n const [state, setState] = useState<AsyncValue<V>>(readable.getAsync);\n\n useEffect(() => {\n let mounted = true;\n let abortController: AbortController;\n\n setState(readable.getAsync());\n\n (async () => {\n while (mounted) {\n abortController = new AbortController();\n /**\n * @todo Cleanup on unmount\n * @url https://github.com/yuriyyakym/awai/issues/1\n */\n try {\n await Promise.any([\n readable.events.fulfilled.abortable(abortController),\n readable.events.rejected.abortable(abortController),\n readable.events.requested.abortable(abortController),\n ]);\n\n const newAsync = readable.getAsync();\n const isChanged =\n newAsync.error !== state.error ||\n newAsync.isLoading !== state.isLoading ||\n newAsync.value !== state.value;\n\n if (isChanged && mounted) {\n setState(readable.getAsync());\n }\n } catch {\n } finally {\n abortController.abort();\n }\n }\n })();\n\n return () => {\n mounted = false;\n abortController?.abort();\n };\n }, [readable]);\n\n return state;\n};\n\nexport default useAsyncStateValue;\n","import type { WritableAsyncState, WritableState } from 'awai';\n\nconst useSetState = <Q extends WritableState<T> | WritableAsyncState<T>, T = any>(\n writable: Q,\n): Q['set'] => {\n return writable.set;\n};\n\nexport default useSetState;\n","import {\n type InferReadableType,\n type ReadableAsyncState,\n type ReadableState,\n isReadableAsyncState,\n} from 'awai';\nimport { startTransition, useEffect, useState } from 'react';\n\nconst useStateValue = <T extends ReadableState<any> | ReadableAsyncState<any>>(\n readable: T,\n): InferReadableType<T> => {\n const [state, setState] = useState<T | undefined>(readable.get);\n\n if (isReadableAsyncState(readable) && readable.get() === undefined) {\n startTransition(() => {\n throw new Promise((resolve) => readable.events.changed.then(resolve));\n });\n }\n\n useEffect(() => {\n let mounted = true;\n\n setState(readable.get());\n\n (async () => {\n while (mounted) {\n /**\n * @todo Cleanup on unmount\n * @url https://github.com/yuriyyakym/awai/issues/1\n */\n const newValue = await readable.events.changed;\n if (mounted) {\n setState(newValue);\n }\n }\n })();\n\n return () => {\n mounted = false;\n };\n }, [readable]);\n\n return state as InferReadableType<T>;\n};\n\nexport default useStateValue;\n","import type { AsyncSetter, AsyncState, Setter, State } from 'awai';\n\nimport useSetState from './useSetState';\nimport useStateValue from './useStateValue';\n\ntype Return<T> = T extends State<infer U>\n ? [U, Setter<U>]\n : T extends AsyncState<infer V>\n ? [V, AsyncSetter<V>]\n : never;\n\nfunction useState<T>(state: State<T>): Return<State<T>>;\nfunction useState<T>(state: AsyncState<T>): Return<AsyncState<T>>;\nfunction useState<T>(state: State<T> | AsyncState<T>) {\n const value = useStateValue(state);\n const setValue = useSetState(state);\n\n return [value, setValue];\n}\n\nexport default useState;\n"],"names":["useState","useEffect","isReadableAsyncState","startTransition"],"mappings":";;;;;AAGM,MAAA,kBAAA,GAAqB,CACzB,QACkB,KAAA;AAClB,EAAA,MAAM,CAAC,KAAO,EAAA,QAAQ,CAAI,GAAAA,cAAA,CAAwB,SAAS,QAAQ,CAAA,CAAA;AAEnE,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAU,GAAA,IAAA,CAAA;AACd,IAAI,IAAA,eAAA,CAAA;AAEJ,IAAS,QAAA,CAAA,QAAA,CAAS,UAAU,CAAA,CAAA;AAE5B,IAAA,CAAC,YAAY;AACX,MAAA,OAAO,OAAS,EAAA;AACd,QAAA,eAAA,GAAkB,IAAI,eAAgB,EAAA,CAAA;AAKtC,QAAI,IAAA;AACF,UAAA,MAAM,QAAQ,GAAI,CAAA;AAAA,YAChB,QAAS,CAAA,MAAA,CAAO,SAAU,CAAA,SAAA,CAAU,eAAe,CAAA;AAAA,YACnD,QAAS,CAAA,MAAA,CAAO,QAAS,CAAA,SAAA,CAAU,eAAe,CAAA;AAAA,YAClD,QAAS,CAAA,MA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