@effuse/store 1.0.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 +7 -0
- package/dist/index.cjs +1462 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +698 -0
- package/dist/index.d.ts +698 -0
- package/dist/index.js +1388 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
- package/src/actions/async.ts +386 -0
- package/src/actions/cancellation.ts +131 -0
- package/src/actions/index.ts +48 -0
- package/src/composition/compose.ts +194 -0
- package/src/composition/index.ts +31 -0
- package/src/config/constants.ts +110 -0
- package/src/config/index.ts +40 -0
- package/src/context/index.ts +39 -0
- package/src/context/scope.ts +136 -0
- package/src/core/index.ts +36 -0
- package/src/core/state.ts +48 -0
- package/src/core/store.ts +467 -0
- package/src/core/types.ts +107 -0
- package/src/devtools/connector.ts +115 -0
- package/src/devtools/index.ts +31 -0
- package/src/errors.ts +81 -0
- package/src/index.ts +157 -0
- package/src/middleware/index.ts +25 -0
- package/src/middleware/manager.ts +61 -0
- package/src/persistence/adapters.ts +96 -0
- package/src/persistence/index.ts +31 -0
- package/src/reactivity/derived.ts +170 -0
- package/src/reactivity/index.ts +42 -0
- package/src/reactivity/selectors.ts +180 -0
- package/src/reactivity/streams.ts +194 -0
- package/src/registry/index.ts +60 -0
- package/src/validation/index.ts +33 -0
- package/src/validation/schema.ts +142 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { Effect, Duration, Schedule } from 'effect';
|
|
26
|
+
import type { Store } from '../core/types.js';
|
|
27
|
+
import { createCancellationToken } from './cancellation.js';
|
|
28
|
+
import { runWithAbortSignal } from './cancellation.js';
|
|
29
|
+
import {
|
|
30
|
+
DEFAULT_RETRY_INITIAL_DELAY_MS,
|
|
31
|
+
DEFAULT_RETRY_MAX_DELAY_MS,
|
|
32
|
+
DEFAULT_RETRY_BACKOFF_FACTOR,
|
|
33
|
+
} from '../config/constants.js';
|
|
34
|
+
import {
|
|
35
|
+
ActionNotFoundError,
|
|
36
|
+
CancellationError,
|
|
37
|
+
TimeoutError,
|
|
38
|
+
} from '../errors.js';
|
|
39
|
+
|
|
40
|
+
// Asynchronous operation outcome
|
|
41
|
+
export interface ActionResult<T> {
|
|
42
|
+
data: T | null;
|
|
43
|
+
error: Error | null;
|
|
44
|
+
loading: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Asynchronous action with pending state
|
|
48
|
+
export interface AsyncAction<A extends unknown[], R> {
|
|
49
|
+
(...args: A): Promise<R>;
|
|
50
|
+
pending: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Cancellable asynchronous action
|
|
54
|
+
export interface CancellableAction<A extends unknown[], R> {
|
|
55
|
+
(...args: A): Promise<R>;
|
|
56
|
+
pending: boolean;
|
|
57
|
+
cancel: () => void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type ActionFn<A extends unknown[], R> = (...args: A) => Promise<R> | R;
|
|
61
|
+
|
|
62
|
+
// Build asynchronous action
|
|
63
|
+
export const createAsyncAction = <A extends unknown[], R>(
|
|
64
|
+
fn: ActionFn<A, R>
|
|
65
|
+
): AsyncAction<A, R> => {
|
|
66
|
+
let pending = false;
|
|
67
|
+
|
|
68
|
+
const action = async (...args: A): Promise<R> => {
|
|
69
|
+
pending = true;
|
|
70
|
+
const result = await Effect.runPromise(
|
|
71
|
+
Effect.tryPromise({
|
|
72
|
+
try: async () => fn(...args),
|
|
73
|
+
catch: (error) => error as Error,
|
|
74
|
+
}).pipe(
|
|
75
|
+
Effect.tapBoth({
|
|
76
|
+
onSuccess: () =>
|
|
77
|
+
Effect.sync(() => {
|
|
78
|
+
pending = false;
|
|
79
|
+
}),
|
|
80
|
+
onFailure: () =>
|
|
81
|
+
Effect.sync(() => {
|
|
82
|
+
pending = false;
|
|
83
|
+
}),
|
|
84
|
+
})
|
|
85
|
+
)
|
|
86
|
+
);
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
Object.defineProperty(action, 'pending', {
|
|
91
|
+
get: () => pending,
|
|
92
|
+
enumerable: true,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return action as AsyncAction<A, R>;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Build cancellable asynchronous action
|
|
99
|
+
export const createCancellableAction = <A extends unknown[], R>(
|
|
100
|
+
fn: ActionFn<A, R>
|
|
101
|
+
): CancellableAction<A, R> => {
|
|
102
|
+
let pending = false;
|
|
103
|
+
let currentController: AbortController | null = null;
|
|
104
|
+
|
|
105
|
+
const action = async (...args: A): Promise<R> => {
|
|
106
|
+
if (currentController) {
|
|
107
|
+
currentController.abort();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
currentController = new AbortController();
|
|
111
|
+
const signal = currentController.signal;
|
|
112
|
+
pending = true;
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const effect = Effect.tryPromise({
|
|
116
|
+
try: async () => fn(...args),
|
|
117
|
+
catch: (error) => error as Error,
|
|
118
|
+
});
|
|
119
|
+
const result = await Effect.runPromise(
|
|
120
|
+
runWithAbortSignal(effect, signal)
|
|
121
|
+
);
|
|
122
|
+
return result;
|
|
123
|
+
} finally {
|
|
124
|
+
pending = false;
|
|
125
|
+
currentController = null;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
Object.defineProperty(action, 'pending', {
|
|
130
|
+
get: () => pending,
|
|
131
|
+
enumerable: true,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
Object.defineProperty(action, 'cancel', {
|
|
135
|
+
value: () => {
|
|
136
|
+
if (currentController) {
|
|
137
|
+
currentController.abort();
|
|
138
|
+
currentController = null;
|
|
139
|
+
pending = false;
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
enumerable: true,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return action as CancellableAction<A, R>;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Enforce operation timeout
|
|
149
|
+
export const withTimeout = <A extends unknown[], R>(
|
|
150
|
+
fn: ActionFn<A, R>,
|
|
151
|
+
timeoutMs: number
|
|
152
|
+
): ((...args: A) => Promise<R>) => {
|
|
153
|
+
return async (...args: A): Promise<R> => {
|
|
154
|
+
const effect = Effect.tryPromise({
|
|
155
|
+
try: async () => fn(...args),
|
|
156
|
+
catch: (error) => error as Error,
|
|
157
|
+
}).pipe(
|
|
158
|
+
Effect.timeoutFail({
|
|
159
|
+
duration: Duration.millis(timeoutMs),
|
|
160
|
+
onTimeout: () => new TimeoutError({ ms: timeoutMs }),
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
return Effect.runPromise(effect);
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// Retry configuration
|
|
169
|
+
export interface RetryConfig {
|
|
170
|
+
maxRetries: number;
|
|
171
|
+
initialDelayMs?: number;
|
|
172
|
+
maxDelayMs?: number;
|
|
173
|
+
backoffFactor?: number;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Retry on failure
|
|
177
|
+
export const withRetry = <A extends unknown[], R>(
|
|
178
|
+
fn: ActionFn<A, R>,
|
|
179
|
+
config: RetryConfig
|
|
180
|
+
): ((...args: A) => Promise<R>) => {
|
|
181
|
+
const {
|
|
182
|
+
maxRetries,
|
|
183
|
+
initialDelayMs = DEFAULT_RETRY_INITIAL_DELAY_MS,
|
|
184
|
+
maxDelayMs = DEFAULT_RETRY_MAX_DELAY_MS,
|
|
185
|
+
backoffFactor = DEFAULT_RETRY_BACKOFF_FACTOR,
|
|
186
|
+
} = config;
|
|
187
|
+
|
|
188
|
+
return async (...args: A): Promise<R> => {
|
|
189
|
+
const schedule = Schedule.exponential(
|
|
190
|
+
Duration.millis(initialDelayMs),
|
|
191
|
+
backoffFactor
|
|
192
|
+
).pipe(
|
|
193
|
+
Schedule.either(Schedule.recurs(maxRetries)),
|
|
194
|
+
Schedule.upTo(Duration.millis(maxDelayMs))
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const effect = Effect.tryPromise({
|
|
198
|
+
try: async () => fn(...args),
|
|
199
|
+
catch: (error) => error as Error,
|
|
200
|
+
}).pipe(Effect.retry(schedule));
|
|
201
|
+
|
|
202
|
+
return Effect.runPromise(effect);
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// Execute only latest call
|
|
207
|
+
export const takeLatest = <A extends unknown[], R>(
|
|
208
|
+
fn: ActionFn<A, R>
|
|
209
|
+
): CancellableAction<A, R> => {
|
|
210
|
+
let pending = false;
|
|
211
|
+
let currentToken = createCancellationToken();
|
|
212
|
+
let callId = 0;
|
|
213
|
+
|
|
214
|
+
const action = async (...args: A): Promise<R> => {
|
|
215
|
+
currentToken.cancel();
|
|
216
|
+
currentToken = createCancellationToken();
|
|
217
|
+
const myToken = currentToken;
|
|
218
|
+
const myCallId = ++callId;
|
|
219
|
+
|
|
220
|
+
pending = true;
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
const result = await fn(...args);
|
|
224
|
+
|
|
225
|
+
if (myToken.isCancelled || myCallId !== callId) {
|
|
226
|
+
throw new CancellationError({ message: 'Superseded by newer call' });
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return result;
|
|
230
|
+
} finally {
|
|
231
|
+
if (myCallId === callId) {
|
|
232
|
+
pending = false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
Object.defineProperty(action, 'pending', {
|
|
238
|
+
get: () => pending,
|
|
239
|
+
enumerable: true,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
Object.defineProperty(action, 'cancel', {
|
|
243
|
+
value: () => {
|
|
244
|
+
currentToken.cancel();
|
|
245
|
+
pending = false;
|
|
246
|
+
},
|
|
247
|
+
enumerable: true,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
return action as CancellableAction<A, R>;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// Execute only first call
|
|
254
|
+
export const takeFirst = <A extends unknown[], R>(
|
|
255
|
+
fn: ActionFn<A, R>
|
|
256
|
+
): AsyncAction<A, R | undefined> => {
|
|
257
|
+
let pending = false;
|
|
258
|
+
|
|
259
|
+
const action = async (...args: A): Promise<R | undefined> => {
|
|
260
|
+
if (pending) {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
pending = true;
|
|
265
|
+
try {
|
|
266
|
+
return await fn(...args);
|
|
267
|
+
} finally {
|
|
268
|
+
pending = false;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
Object.defineProperty(action, 'pending', {
|
|
273
|
+
get: () => pending,
|
|
274
|
+
enumerable: true,
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
return action as AsyncAction<A, R | undefined>;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// Debounce action execution
|
|
281
|
+
export const debounceAction = <A extends unknown[], R>(
|
|
282
|
+
fn: ActionFn<A, R>,
|
|
283
|
+
delayMs: number
|
|
284
|
+
): ((...args: A) => Promise<R>) => {
|
|
285
|
+
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
286
|
+
let currentToken = createCancellationToken();
|
|
287
|
+
|
|
288
|
+
return (...args: A): Promise<R> => {
|
|
289
|
+
if (timeout) {
|
|
290
|
+
clearTimeout(timeout);
|
|
291
|
+
currentToken.cancel();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
currentToken = createCancellationToken();
|
|
295
|
+
const myToken = currentToken;
|
|
296
|
+
|
|
297
|
+
return new Promise((resolve, reject) => {
|
|
298
|
+
timeout = setTimeout(() => {
|
|
299
|
+
if (myToken.isCancelled) return;
|
|
300
|
+
|
|
301
|
+
Promise.resolve(fn(...args))
|
|
302
|
+
.then((result) => {
|
|
303
|
+
if (!myToken.isCancelled) resolve(result);
|
|
304
|
+
})
|
|
305
|
+
.catch((error: unknown) => {
|
|
306
|
+
if (!myToken.isCancelled) reject(error as Error);
|
|
307
|
+
});
|
|
308
|
+
}, delayMs);
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// Throttle action execution
|
|
314
|
+
export const throttleAction = <A extends unknown[], R>(
|
|
315
|
+
fn: ActionFn<A, R>,
|
|
316
|
+
intervalMs: number
|
|
317
|
+
): ((...args: A) => Promise<R | undefined>) => {
|
|
318
|
+
let lastCallTime = 0;
|
|
319
|
+
let pending = false;
|
|
320
|
+
|
|
321
|
+
return async (...args: A): Promise<R | undefined> => {
|
|
322
|
+
const now = Date.now();
|
|
323
|
+
if (now - lastCallTime < intervalMs || pending) {
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
lastCallTime = now;
|
|
328
|
+
pending = true;
|
|
329
|
+
|
|
330
|
+
try {
|
|
331
|
+
return await fn(...args);
|
|
332
|
+
} finally {
|
|
333
|
+
pending = false;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// Dispatch store action asynchronously
|
|
339
|
+
export const dispatch = <T>(
|
|
340
|
+
store: Store<T>,
|
|
341
|
+
actionName: keyof T,
|
|
342
|
+
...args: unknown[]
|
|
343
|
+
): Promise<unknown> => {
|
|
344
|
+
const storeRecord = store as unknown as Record<string, unknown>;
|
|
345
|
+
const action = storeRecord[actionName as string];
|
|
346
|
+
if (typeof action !== 'function') {
|
|
347
|
+
return Promise.reject(
|
|
348
|
+
new ActionNotFoundError({ actionName: String(actionName) })
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
const actionFn = action as (...a: unknown[]) => unknown;
|
|
352
|
+
return Effect.runPromise(
|
|
353
|
+
Effect.tryPromise({
|
|
354
|
+
try: () => Promise.resolve(actionFn(...args)),
|
|
355
|
+
catch: (error) => error as Error,
|
|
356
|
+
})
|
|
357
|
+
);
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// Dispatch store action synchronously
|
|
361
|
+
export const dispatchSync = <T>(
|
|
362
|
+
store: Store<T>,
|
|
363
|
+
actionName: keyof T,
|
|
364
|
+
...args: unknown[]
|
|
365
|
+
): unknown => {
|
|
366
|
+
const storeRecord = store as unknown as Record<string, unknown>;
|
|
367
|
+
const action = storeRecord[actionName as string];
|
|
368
|
+
if (typeof action !== 'function') {
|
|
369
|
+
throw new ActionNotFoundError({ actionName: String(actionName) });
|
|
370
|
+
}
|
|
371
|
+
const actionFn = action as (...a: unknown[]) => unknown;
|
|
372
|
+
return actionFn(...args);
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// Attach external abort signal
|
|
376
|
+
export const withAbortSignal = <A extends unknown[], R>(
|
|
377
|
+
fn: ActionFn<A, R>
|
|
378
|
+
): ((signal: AbortSignal, ...args: A) => Promise<R>) => {
|
|
379
|
+
return (signal: AbortSignal, ...args: A): Promise<R> => {
|
|
380
|
+
const effect = Effect.tryPromise({
|
|
381
|
+
try: async () => fn(...args),
|
|
382
|
+
catch: (error) => error as Error,
|
|
383
|
+
});
|
|
384
|
+
return Effect.runPromise(runWithAbortSignal(effect, signal));
|
|
385
|
+
};
|
|
386
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { Effect } from 'effect';
|
|
26
|
+
import { CancellationError } from '../errors.js';
|
|
27
|
+
|
|
28
|
+
// Cancellation tracking token
|
|
29
|
+
export interface CancellationToken {
|
|
30
|
+
readonly isCancelled: boolean;
|
|
31
|
+
cancel: () => void;
|
|
32
|
+
throwIfCancelled: () => void;
|
|
33
|
+
onCancel: (callback: () => void) => () => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Build cancellation token
|
|
37
|
+
export const createCancellationToken = (): CancellationToken => {
|
|
38
|
+
let cancelled = false;
|
|
39
|
+
const callbacks = new Set<() => void>();
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
get isCancelled() {
|
|
43
|
+
return cancelled;
|
|
44
|
+
},
|
|
45
|
+
cancel: () => {
|
|
46
|
+
if (!cancelled) {
|
|
47
|
+
cancelled = true;
|
|
48
|
+
for (const cb of callbacks) cb();
|
|
49
|
+
callbacks.clear();
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
throwIfCancelled: () => {
|
|
53
|
+
if (cancelled)
|
|
54
|
+
throw new CancellationError({ message: 'Operation was cancelled' });
|
|
55
|
+
},
|
|
56
|
+
onCancel: (callback: () => void) => {
|
|
57
|
+
if (cancelled) {
|
|
58
|
+
callback();
|
|
59
|
+
return () => {};
|
|
60
|
+
}
|
|
61
|
+
callbacks.add(callback);
|
|
62
|
+
return () => callbacks.delete(callback);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Nested cancellation scope
|
|
68
|
+
export interface CancellationScope {
|
|
69
|
+
readonly token: CancellationToken;
|
|
70
|
+
createChild: () => CancellationToken;
|
|
71
|
+
dispose: () => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Build cancellation scope
|
|
75
|
+
export const createCancellationScope = (): CancellationScope => {
|
|
76
|
+
const children = new Set<CancellationToken>();
|
|
77
|
+
const token = createCancellationToken();
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
token,
|
|
81
|
+
createChild: () => {
|
|
82
|
+
const child = createCancellationToken();
|
|
83
|
+
children.add(child);
|
|
84
|
+
token.onCancel(() => {
|
|
85
|
+
child.cancel();
|
|
86
|
+
});
|
|
87
|
+
return child;
|
|
88
|
+
},
|
|
89
|
+
dispose: () => {
|
|
90
|
+
token.cancel();
|
|
91
|
+
for (const child of children) child.cancel();
|
|
92
|
+
children.clear();
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Connect external abort signal
|
|
98
|
+
export const runWithAbortSignal = <A, E>(
|
|
99
|
+
effect: Effect.Effect<A, E>,
|
|
100
|
+
signal: AbortSignal
|
|
101
|
+
): Effect.Effect<A, E | CancellationError> => {
|
|
102
|
+
if (signal.aborted) {
|
|
103
|
+
return Effect.fail(
|
|
104
|
+
new CancellationError({ message: 'Operation was cancelled' }) as
|
|
105
|
+
| E
|
|
106
|
+
| CancellationError
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return Effect.async<A, E | CancellationError>((resume) => {
|
|
111
|
+
const onAbort = () => {
|
|
112
|
+
resume(
|
|
113
|
+
Effect.fail(
|
|
114
|
+
new CancellationError({ message: 'Operation was cancelled' })
|
|
115
|
+
)
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
120
|
+
|
|
121
|
+
Effect.runPromise(effect)
|
|
122
|
+
.then((result) => {
|
|
123
|
+
signal.removeEventListener('abort', onAbort);
|
|
124
|
+
resume(Effect.succeed(result));
|
|
125
|
+
})
|
|
126
|
+
.catch((error: unknown) => {
|
|
127
|
+
signal.removeEventListener('abort', onAbort);
|
|
128
|
+
resume(Effect.fail(error as E));
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
createAsyncAction,
|
|
27
|
+
createCancellableAction,
|
|
28
|
+
dispatch,
|
|
29
|
+
dispatchSync,
|
|
30
|
+
withTimeout,
|
|
31
|
+
withRetry,
|
|
32
|
+
withAbortSignal,
|
|
33
|
+
takeLatest,
|
|
34
|
+
takeFirst,
|
|
35
|
+
debounceAction,
|
|
36
|
+
throttleAction,
|
|
37
|
+
type ActionResult,
|
|
38
|
+
type AsyncAction,
|
|
39
|
+
type CancellableAction,
|
|
40
|
+
type RetryConfig,
|
|
41
|
+
} from './async.js';
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
createCancellationToken,
|
|
45
|
+
createCancellationScope,
|
|
46
|
+
type CancellationToken,
|
|
47
|
+
type CancellationScope,
|
|
48
|
+
} from './cancellation.js';
|