atomirx 0.0.8 → 0.1.1

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.
Files changed (138) hide show
  1. package/README.md +198 -2234
  2. package/bin/cli.js +90 -0
  3. package/dist/core/derived.d.ts +2 -2
  4. package/dist/core/effect.d.ts +3 -2
  5. package/dist/core/onCreateHook.d.ts +15 -2
  6. package/dist/core/onErrorHook.d.ts +4 -1
  7. package/dist/core/pool.d.ts +78 -0
  8. package/dist/core/pool.test.d.ts +1 -0
  9. package/dist/core/select-boolean.test.d.ts +1 -0
  10. package/dist/core/select-pool.test.d.ts +1 -0
  11. package/dist/core/select.d.ts +278 -86
  12. package/dist/core/types.d.ts +233 -1
  13. package/dist/core/withAbort.d.ts +95 -0
  14. package/dist/core/withReady.d.ts +3 -3
  15. package/dist/devtools/constants.d.ts +41 -0
  16. package/dist/devtools/index.cjs +1 -0
  17. package/dist/devtools/index.d.ts +29 -0
  18. package/dist/devtools/index.js +429 -0
  19. package/dist/devtools/registry.d.ts +98 -0
  20. package/dist/devtools/registry.test.d.ts +1 -0
  21. package/dist/devtools/setup.d.ts +61 -0
  22. package/dist/devtools/types.d.ts +311 -0
  23. package/dist/index-BZEnfIcB.cjs +1 -0
  24. package/dist/index-BbPZhsDl.js +1653 -0
  25. package/dist/index.cjs +1 -1
  26. package/dist/index.d.ts +4 -3
  27. package/dist/index.js +18 -14
  28. package/dist/onDispatchHook-C8yLzr-o.cjs +1 -0
  29. package/dist/onDispatchHook-SKbiIUaJ.js +5 -0
  30. package/dist/onErrorHook-BGGy3tqK.js +38 -0
  31. package/dist/onErrorHook-DHBASmYw.cjs +1 -0
  32. package/dist/react/index.cjs +1 -1
  33. package/dist/react/index.js +191 -151
  34. package/dist/react/onDispatchHook.d.ts +106 -0
  35. package/dist/react/useAction.d.ts +4 -1
  36. package/dist/react-devtools/DevToolsPanel.d.ts +93 -0
  37. package/dist/react-devtools/EntityDetails.d.ts +10 -0
  38. package/dist/react-devtools/EntityList.d.ts +15 -0
  39. package/dist/react-devtools/LogList.d.ts +12 -0
  40. package/dist/react-devtools/hooks.d.ts +50 -0
  41. package/dist/react-devtools/index.cjs +1 -0
  42. package/dist/react-devtools/index.d.ts +31 -0
  43. package/dist/react-devtools/index.js +1589 -0
  44. package/dist/react-devtools/styles.d.ts +148 -0
  45. package/package.json +26 -2
  46. package/skills/atomirx/SKILL.md +456 -0
  47. package/skills/atomirx/references/async-patterns.md +188 -0
  48. package/skills/atomirx/references/atom-patterns.md +238 -0
  49. package/skills/atomirx/references/deferred-loading.md +191 -0
  50. package/skills/atomirx/references/derived-patterns.md +428 -0
  51. package/skills/atomirx/references/effect-patterns.md +426 -0
  52. package/skills/atomirx/references/error-handling.md +140 -0
  53. package/skills/atomirx/references/hooks.md +322 -0
  54. package/skills/atomirx/references/pool-patterns.md +229 -0
  55. package/skills/atomirx/references/react-integration.md +411 -0
  56. package/skills/atomirx/references/rules.md +407 -0
  57. package/skills/atomirx/references/select-context.md +309 -0
  58. package/skills/atomirx/references/service-template.md +172 -0
  59. package/skills/atomirx/references/store-template.md +205 -0
  60. package/skills/atomirx/references/testing-patterns.md +431 -0
  61. package/coverage/base.css +0 -224
  62. package/coverage/block-navigation.js +0 -87
  63. package/coverage/clover.xml +0 -1440
  64. package/coverage/coverage-final.json +0 -14
  65. package/coverage/favicon.png +0 -0
  66. package/coverage/index.html +0 -131
  67. package/coverage/prettify.css +0 -1
  68. package/coverage/prettify.js +0 -2
  69. package/coverage/sort-arrow-sprite.png +0 -0
  70. package/coverage/sorter.js +0 -210
  71. package/coverage/src/core/atom.ts.html +0 -889
  72. package/coverage/src/core/batch.ts.html +0 -223
  73. package/coverage/src/core/define.ts.html +0 -805
  74. package/coverage/src/core/emitter.ts.html +0 -919
  75. package/coverage/src/core/equality.ts.html +0 -631
  76. package/coverage/src/core/hook.ts.html +0 -460
  77. package/coverage/src/core/index.html +0 -281
  78. package/coverage/src/core/isAtom.ts.html +0 -100
  79. package/coverage/src/core/isPromiseLike.ts.html +0 -133
  80. package/coverage/src/core/onCreateHook.ts.html +0 -138
  81. package/coverage/src/core/scheduleNotifyHook.ts.html +0 -94
  82. package/coverage/src/core/types.ts.html +0 -523
  83. package/coverage/src/core/withUse.ts.html +0 -253
  84. package/coverage/src/index.html +0 -116
  85. package/coverage/src/index.ts.html +0 -106
  86. package/dist/index-CBVj1kSj.js +0 -1350
  87. package/dist/index-Cxk9v0um.cjs +0 -1
  88. package/scripts/publish.js +0 -198
  89. package/src/core/atom.test.ts +0 -633
  90. package/src/core/atom.ts +0 -311
  91. package/src/core/atomState.test.ts +0 -342
  92. package/src/core/atomState.ts +0 -256
  93. package/src/core/batch.test.ts +0 -257
  94. package/src/core/batch.ts +0 -172
  95. package/src/core/define.test.ts +0 -343
  96. package/src/core/define.ts +0 -243
  97. package/src/core/derived.test.ts +0 -1215
  98. package/src/core/derived.ts +0 -450
  99. package/src/core/effect.test.ts +0 -802
  100. package/src/core/effect.ts +0 -188
  101. package/src/core/emitter.test.ts +0 -364
  102. package/src/core/emitter.ts +0 -392
  103. package/src/core/equality.test.ts +0 -392
  104. package/src/core/equality.ts +0 -182
  105. package/src/core/getAtomState.ts +0 -69
  106. package/src/core/hook.test.ts +0 -227
  107. package/src/core/hook.ts +0 -177
  108. package/src/core/isAtom.ts +0 -27
  109. package/src/core/isPromiseLike.test.ts +0 -72
  110. package/src/core/isPromiseLike.ts +0 -16
  111. package/src/core/onCreateHook.ts +0 -107
  112. package/src/core/onErrorHook.test.ts +0 -350
  113. package/src/core/onErrorHook.ts +0 -52
  114. package/src/core/promiseCache.test.ts +0 -241
  115. package/src/core/promiseCache.ts +0 -284
  116. package/src/core/scheduleNotifyHook.ts +0 -53
  117. package/src/core/select.ts +0 -729
  118. package/src/core/selector.test.ts +0 -799
  119. package/src/core/types.ts +0 -389
  120. package/src/core/withReady.test.ts +0 -534
  121. package/src/core/withReady.ts +0 -191
  122. package/src/core/withUse.test.ts +0 -249
  123. package/src/core/withUse.ts +0 -56
  124. package/src/index.test.ts +0 -80
  125. package/src/index.ts +0 -65
  126. package/src/react/index.ts +0 -21
  127. package/src/react/rx.test.tsx +0 -571
  128. package/src/react/rx.tsx +0 -531
  129. package/src/react/strictModeTest.tsx +0 -71
  130. package/src/react/useAction.test.ts +0 -987
  131. package/src/react/useAction.ts +0 -607
  132. package/src/react/useSelector.test.ts +0 -182
  133. package/src/react/useSelector.ts +0 -292
  134. package/src/react/useStable.test.ts +0 -553
  135. package/src/react/useStable.ts +0 -288
  136. package/tsconfig.json +0 -9
  137. package/v2.md +0 -725
  138. package/vite.config.ts +0 -42
@@ -1,284 +0,0 @@
1
- import { shallow2Equal } from "./equality";
2
- import { isPromiseLike } from "./isPromiseLike";
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
- }
78
-
79
- /**
80
- * Represents the state of a tracked Promise.
81
- */
82
- export type PromiseState<T> =
83
- | { status: "pending"; promise: PromiseLike<T> }
84
- | { status: "fulfilled"; value: T }
85
- | { status: "rejected"; error: unknown };
86
-
87
- /**
88
- * WeakMap cache for Promise states.
89
- * Using WeakMap allows Promises to be garbage collected when no longer referenced.
90
- */
91
- const promiseCache = new WeakMap<PromiseLike<unknown>, PromiseState<unknown>>();
92
-
93
- /**
94
- * Tracks a Promise and caches its state.
95
- * If the Promise is already tracked, returns the existing state.
96
- * Otherwise, starts tracking and returns the initial pending state.
97
- *
98
- * @param promise - The Promise to track
99
- * @returns The current state of the Promise
100
- *
101
- * @example
102
- * ```ts
103
- * const promise = fetchData();
104
- * const state = trackPromise(promise);
105
- * // state.status === 'pending'
106
- *
107
- * await promise;
108
- * const state2 = trackPromise(promise);
109
- * // state2.status === 'fulfilled'
110
- * ```
111
- */
112
- export function trackPromise<T>(promise: PromiseLike<T>): PromiseState<T> {
113
- const existing = promiseCache.get(promise);
114
- if (existing) {
115
- return existing as PromiseState<T>;
116
- }
117
-
118
- const state: PromiseState<T> = { status: "pending", promise };
119
- promiseCache.set(promise, state as PromiseState<unknown>);
120
-
121
- promise.then(
122
- (value) => {
123
- // Only update if still pending (not replaced by another state)
124
- const current = promiseCache.get(promise);
125
- if (current?.status === "pending") {
126
- promiseCache.set(promise, { status: "fulfilled", value });
127
- }
128
- },
129
- (error) => {
130
- // Only update if still pending
131
- const current = promiseCache.get(promise);
132
- if (current?.status === "pending") {
133
- promiseCache.set(promise, { status: "rejected", error });
134
- }
135
- }
136
- );
137
-
138
- return state;
139
- }
140
-
141
- /**
142
- * Gets the current state of a Promise without tracking it.
143
- * Returns undefined if the Promise is not being tracked.
144
- *
145
- * @param promise - The Promise to check
146
- * @returns The current state or undefined if not tracked
147
- */
148
- export function getPromiseState<T>(
149
- promise: PromiseLike<T>
150
- ): PromiseState<T> | undefined {
151
- return promiseCache.get(promise) as PromiseState<T> | undefined;
152
- }
153
-
154
- /**
155
- * Checks if a Promise is being tracked.
156
- *
157
- * @param promise - The Promise to check
158
- * @returns true if the Promise is tracked
159
- */
160
- export function isTracked(promise: PromiseLike<unknown>): boolean {
161
- return promiseCache.has(promise);
162
- }
163
-
164
- /**
165
- * Type guard to check if a value is a DerivedAtom.
166
- */
167
- export function isDerived<T>(value: unknown): value is DerivedAtom<T, boolean> {
168
- return (
169
- value !== null &&
170
- typeof value === "object" &&
171
- SYMBOL_DERIVED in value &&
172
- (value as DerivedAtom<T, boolean>)[SYMBOL_DERIVED] === true
173
- );
174
- }
175
-
176
- /**
177
- * Unwraps a value that may be a Promise.
178
- * - If not a Promise, returns the value directly.
179
- * - If a resolved Promise, returns the resolved value.
180
- * - If a loading Promise, throws the Promise (for Suspense).
181
- * - If a rejected Promise, throws the error.
182
- *
183
- * This follows the React Suspense pattern where throwing a Promise
184
- * signals that the component should suspend until the Promise resolves.
185
- *
186
- * @param value - The value to unwrap (may be a Promise)
187
- * @returns The unwrapped value
188
- * @throws Promise if loading, Error if rejected
189
- */
190
- export function unwrap<T>(value: T | PromiseLike<T>): T {
191
- if (!isPromiseLike(value)) {
192
- return value;
193
- }
194
-
195
- const promise = value as PromiseLike<T>;
196
- const state = trackPromise(promise);
197
-
198
- switch (state.status) {
199
- case "pending":
200
- throw state.promise;
201
- case "rejected":
202
- throw state.error;
203
- case "fulfilled":
204
- return state.value;
205
- }
206
- }
207
-
208
- /**
209
- * Checks if a value is a pending Promise.
210
- *
211
- * @param value - The value to check
212
- * @returns true if value is a Promise in pending state
213
- */
214
- export function isPending<T>(value: T | PromiseLike<T>): boolean {
215
- if (!isPromiseLike(value)) {
216
- return false;
217
- }
218
- const state = trackPromise(value as PromiseLike<T>);
219
- return state.status === "pending";
220
- }
221
-
222
- /**
223
- * Checks if a value is a fulfilled Promise.
224
- *
225
- * @param value - The value to check
226
- * @returns true if value is a Promise in fulfilled state
227
- */
228
- export function isFulfilled<T>(value: T | PromiseLike<T>): boolean {
229
- if (!isPromiseLike(value)) {
230
- return false;
231
- }
232
- const state = trackPromise(value as PromiseLike<T>);
233
- return state.status === "fulfilled";
234
- }
235
-
236
- /**
237
- * Checks if a value is a rejected Promise.
238
- *
239
- * @param value - The value to check
240
- * @returns true if value is a Promise in rejected state
241
- */
242
- export function isRejected<T>(value: T | PromiseLike<T>): boolean {
243
- if (!isPromiseLike(value)) {
244
- return false;
245
- }
246
- const state = trackPromise(value as PromiseLike<T>);
247
- return state.status === "rejected";
248
- }
249
-
250
- /**
251
- * Gets the resolved value of a Promise if fulfilled, otherwise undefined.
252
- *
253
- * @param value - The value to check
254
- * @returns The resolved value or undefined
255
- */
256
- export function getResolvedValue<T>(value: T | PromiseLike<T>): T | undefined {
257
- if (!isPromiseLike(value)) {
258
- return value;
259
- }
260
- const state = getPromiseState(value as PromiseLike<T>);
261
- if (state?.status === "fulfilled") {
262
- return state.value;
263
- }
264
- return undefined;
265
- }
266
-
267
- /**
268
- * Gets the error of a Promise if rejected, otherwise undefined.
269
- *
270
- * @param value - The value to check
271
- * @returns The error or undefined
272
- */
273
- export function getRejectedError<T>(
274
- value: T | PromiseLike<T>
275
- ): unknown | undefined {
276
- if (!isPromiseLike(value)) {
277
- return undefined;
278
- }
279
- const state = getPromiseState(value as PromiseLike<T>);
280
- if (state?.status === "rejected") {
281
- return state.error;
282
- }
283
- return undefined;
284
- }
@@ -1,53 +0,0 @@
1
- import { hook } from "./hook";
2
-
3
- /**
4
- * Hook that controls how atom change notifications are scheduled.
5
- *
6
- * ## Default Behavior
7
- *
8
- * By default, notifications are **synchronous** - listeners are called immediately
9
- * when an atom's value changes:
10
- *
11
- * ```ts
12
- * // Default: (fn) => fn() - immediate execution
13
- * atom.set(newValue); // Listeners called immediately here
14
- * ```
15
- *
16
- * ## Used by `batch()`
17
- *
18
- * The `batch()` function temporarily overrides this hook to defer notifications
19
- * until all updates complete:
20
- *
21
- * ```ts
22
- * batch(() => {
23
- * a.set(1); // Notification deferred
24
- * b.set(2); // Notification deferred
25
- * });
26
- * // All listeners called here (deduped)
27
- * ```
28
- *
29
- * ## Custom Scheduling
30
- *
31
- * Can be overridden for custom scheduling strategies (e.g., microtask, RAF):
32
- *
33
- * ```ts
34
- * // Schedule notifications as microtasks
35
- * scheduleNotifyHook.override(() => (fn) => queueMicrotask(fn));
36
- *
37
- * // Schedule notifications on next animation frame
38
- * scheduleNotifyHook.override(() => (fn) => requestAnimationFrame(fn));
39
- *
40
- * // Reset to default synchronous behavior
41
- * scheduleNotifyHook.reset();
42
- * ```
43
- *
44
- * ## API
45
- *
46
- * - `scheduleNotifyHook.current` - Get/set the current scheduler function
47
- * - `scheduleNotifyHook.override(reducer)` - Override with custom scheduler (reducer receives previous)
48
- * - `scheduleNotifyHook.reset()` - Reset to default synchronous behavior
49
- * - `scheduleNotifyHook(reducer)` - Create a HookSetup for use with `hook.use()`
50
- *
51
- * @internal Used internally by atomState and batch. Not typically needed by users.
52
- */
53
- export const scheduleNotifyHook = hook((fn: VoidFunction) => fn());