atomirx 0.0.1 → 0.0.4
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 +867 -160
- package/dist/core/atom.d.ts +83 -6
- package/dist/core/batch.d.ts +3 -3
- package/dist/core/derived.d.ts +55 -21
- package/dist/core/effect.d.ts +47 -51
- package/dist/core/getAtomState.d.ts +29 -0
- package/dist/core/promiseCache.d.ts +23 -32
- package/dist/core/select.d.ts +208 -29
- package/dist/core/types.d.ts +55 -19
- package/dist/core/withReady.d.ts +69 -0
- package/dist/index-CqO6BDwj.cjs +1 -0
- package/dist/index-D8RDOTB_.js +1319 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +9 -7
- package/dist/index.js +12 -10
- package/dist/react/index.cjs +10 -10
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +423 -379
- package/dist/react/rx.d.ts +114 -25
- package/dist/react/useAction.d.ts +5 -4
- package/dist/react/{useValue.d.ts → useSelector.d.ts} +56 -25
- package/dist/react/useSelector.test.d.ts +1 -0
- package/package.json +17 -1
- package/src/core/atom.test.ts +307 -43
- package/src/core/atom.ts +143 -21
- package/src/core/batch.test.ts +10 -10
- package/src/core/batch.ts +3 -3
- package/src/core/derived.test.ts +727 -72
- package/src/core/derived.ts +141 -73
- package/src/core/effect.test.ts +259 -39
- package/src/core/effect.ts +62 -85
- package/src/core/getAtomState.ts +69 -0
- package/src/core/promiseCache.test.ts +5 -3
- package/src/core/promiseCache.ts +76 -71
- package/src/core/select.ts +405 -130
- package/src/core/selector.test.ts +574 -32
- package/src/core/types.ts +54 -26
- package/src/core/withReady.test.ts +360 -0
- package/src/core/withReady.ts +127 -0
- package/src/core/withUse.ts +1 -1
- package/src/index.test.ts +4 -4
- package/src/index.ts +11 -6
- package/src/react/index.ts +2 -1
- package/src/react/rx.test.tsx +173 -18
- package/src/react/rx.tsx +274 -43
- package/src/react/useAction.test.ts +12 -14
- package/src/react/useAction.ts +11 -9
- package/src/react/{useValue.test.ts → useSelector.test.ts} +16 -16
- package/src/react/{useValue.ts → useSelector.ts} +64 -33
- package/v2.md +44 -44
- package/dist/index-2ok7ilik.js +0 -1217
- package/dist/index-B_5SFzfl.cjs +0 -1
- /package/dist/{react/useValue.test.d.ts → core/withReady.test.d.ts} +0 -0
|
@@ -7,9 +7,9 @@ import {
|
|
|
7
7
|
isFulfilled,
|
|
8
8
|
isRejected,
|
|
9
9
|
unwrap,
|
|
10
|
-
getAtomState,
|
|
11
10
|
isDerived,
|
|
12
11
|
} from "./promiseCache";
|
|
12
|
+
import { getAtomState } from "./getAtomState";
|
|
13
13
|
import { atom } from "./atom";
|
|
14
14
|
import { derived } from "./derived";
|
|
15
15
|
|
|
@@ -187,7 +187,7 @@ describe("promiseCache", () => {
|
|
|
187
187
|
|
|
188
188
|
it("should return true for derived atom", () => {
|
|
189
189
|
const a$ = atom(0);
|
|
190
|
-
const d$ = derived(({
|
|
190
|
+
const d$ = derived(({ read }) => read(a$) * 2);
|
|
191
191
|
expect(isDerived(d$)).toBe(true);
|
|
192
192
|
});
|
|
193
193
|
|
|
@@ -227,7 +227,9 @@ describe("promiseCache", () => {
|
|
|
227
227
|
|
|
228
228
|
it("should return loading state for derived with fallback during loading", async () => {
|
|
229
229
|
const asyncValue$ = atom(new Promise<number>(() => {}));
|
|
230
|
-
const derived$ = derived(({
|
|
230
|
+
const derived$ = derived(({ read }) => read(asyncValue$), {
|
|
231
|
+
fallback: 0,
|
|
232
|
+
});
|
|
231
233
|
|
|
232
234
|
// Derived atoms return their state directly via state()
|
|
233
235
|
// State is loading, but staleValue provides the fallback
|
package/src/core/promiseCache.ts
CHANGED
|
@@ -1,5 +1,80 @@
|
|
|
1
|
+
import { shallow2Equal } from "./equality";
|
|
1
2
|
import { isPromiseLike } from "./isPromiseLike";
|
|
2
|
-
import {
|
|
3
|
+
import { AnyFunc, DerivedAtom, SYMBOL_DERIVED } from "./types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Metadata attached to combined promises for comparison.
|
|
7
|
+
*/
|
|
8
|
+
export interface CombinedPromiseMeta {
|
|
9
|
+
type: "all" | "race" | "allSettled";
|
|
10
|
+
promises: Promise<unknown>[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* WeakMap cache for combined promise metadata.
|
|
15
|
+
* Using WeakMap allows promises to be garbage collected when no longer referenced.
|
|
16
|
+
*/
|
|
17
|
+
const combinedPromiseCache = new WeakMap<
|
|
18
|
+
PromiseLike<unknown>,
|
|
19
|
+
CombinedPromiseMeta
|
|
20
|
+
>();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Gets the metadata for a combined promise, if any.
|
|
24
|
+
* Used internally by promisesEqual for comparison.
|
|
25
|
+
*/
|
|
26
|
+
export function getCombinedPromiseMetadata(
|
|
27
|
+
promise: PromiseLike<unknown>
|
|
28
|
+
): CombinedPromiseMeta | undefined {
|
|
29
|
+
return combinedPromiseCache.get(promise);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create a combined promise with metadata for comparison.
|
|
34
|
+
* If only one promise, returns it directly (no metadata needed).
|
|
35
|
+
*/
|
|
36
|
+
export function createCombinedPromise(
|
|
37
|
+
type: "all" | "race" | "allSettled",
|
|
38
|
+
promises: Promise<unknown>[]
|
|
39
|
+
): PromiseLike<unknown> {
|
|
40
|
+
if (promises.length === 1) {
|
|
41
|
+
// Single promise - no need for metadata, just return it
|
|
42
|
+
// For allSettled, we still need to wrap to prevent rejection propagation
|
|
43
|
+
if (type === "allSettled") {
|
|
44
|
+
const combined = Promise.allSettled(promises).then(() => undefined);
|
|
45
|
+
combinedPromiseCache.set(combined, { type, promises });
|
|
46
|
+
return combined;
|
|
47
|
+
}
|
|
48
|
+
return promises[0];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const combined = (Promise[type] as AnyFunc)(promises);
|
|
52
|
+
// Attach no-op catch to prevent unhandled rejection warnings
|
|
53
|
+
combined.catch(() => {});
|
|
54
|
+
combinedPromiseCache.set(combined, { type, promises });
|
|
55
|
+
return combined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Compare two promises, considering combined promise metadata.
|
|
60
|
+
* Returns true if promises are considered equal.
|
|
61
|
+
*/
|
|
62
|
+
export function promisesEqual(
|
|
63
|
+
a: PromiseLike<unknown> | undefined,
|
|
64
|
+
b: PromiseLike<unknown> | undefined
|
|
65
|
+
): boolean {
|
|
66
|
+
// Same reference
|
|
67
|
+
if (a === b) return true;
|
|
68
|
+
|
|
69
|
+
// One is undefined
|
|
70
|
+
if (!a || !b) return false;
|
|
71
|
+
|
|
72
|
+
// Compare by metadata (type + source promises array)
|
|
73
|
+
const metaA = getCombinedPromiseMetadata(a);
|
|
74
|
+
const metaB = getCombinedPromiseMetadata(b);
|
|
75
|
+
|
|
76
|
+
return !!metaA && !!metaB && shallow2Equal(metaA, metaB);
|
|
77
|
+
}
|
|
3
78
|
|
|
4
79
|
/**
|
|
5
80
|
* Represents the state of a tracked Promise.
|
|
@@ -98,76 +173,6 @@ export function isDerived<T>(value: unknown): value is DerivedAtom<T, boolean> {
|
|
|
98
173
|
);
|
|
99
174
|
}
|
|
100
175
|
|
|
101
|
-
/**
|
|
102
|
-
* Returns the current state of an atom as a discriminated union.
|
|
103
|
-
*
|
|
104
|
-
* For DerivedAtom:
|
|
105
|
-
* - Returns atom.state() directly (derived atoms track their own state)
|
|
106
|
-
*
|
|
107
|
-
* For MutableAtom:
|
|
108
|
-
* - If value is not a Promise: returns ready state
|
|
109
|
-
* - If value is a Promise: tracks and returns its state (ready/error/loading)
|
|
110
|
-
*
|
|
111
|
-
* @param atom - The atom to get state from
|
|
112
|
-
* @returns AtomState discriminated union (ready | error | loading)
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* ```ts
|
|
116
|
-
* const state = getAtomState(myAtom$);
|
|
117
|
-
*
|
|
118
|
-
* switch (state.status) {
|
|
119
|
-
* case "ready":
|
|
120
|
-
* console.log(state.value); // T
|
|
121
|
-
* break;
|
|
122
|
-
* case "error":
|
|
123
|
-
* console.log(state.error);
|
|
124
|
-
* break;
|
|
125
|
-
* case "loading":
|
|
126
|
-
* console.log(state.promise);
|
|
127
|
-
* break;
|
|
128
|
-
* }
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
export function getAtomState<T>(atom: Atom<T>): AtomState<Awaited<T>> {
|
|
132
|
-
// For derived atoms, use their own state method
|
|
133
|
-
if (isDerived<T>(atom)) {
|
|
134
|
-
return atom.state() as AtomState<Awaited<T>>;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const value = atom.value;
|
|
138
|
-
|
|
139
|
-
// 1. Sync value - ready
|
|
140
|
-
if (!isPromiseLike(value)) {
|
|
141
|
-
return {
|
|
142
|
-
status: "ready",
|
|
143
|
-
value: value as Awaited<T>,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// 2. Promise value - check state via promiseCache
|
|
148
|
-
const state = trackPromise(value);
|
|
149
|
-
|
|
150
|
-
switch (state.status) {
|
|
151
|
-
case "fulfilled":
|
|
152
|
-
return {
|
|
153
|
-
status: "ready",
|
|
154
|
-
value: state.value as Awaited<T>,
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
case "rejected":
|
|
158
|
-
return {
|
|
159
|
-
status: "error",
|
|
160
|
-
error: state.error,
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
case "pending":
|
|
164
|
-
return {
|
|
165
|
-
status: "loading",
|
|
166
|
-
promise: state.promise as Promise<Awaited<T>>,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
176
|
/**
|
|
172
177
|
* Unwraps a value that may be a Promise.
|
|
173
178
|
* - If not a Promise, returns the value directly.
|