@systemfsoftware/effect-atom 0.5.3 → 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.
@@ -1,480 +0,0 @@
1
- import * as Cause from "effect/Cause";
2
- import * as Exit from "effect/Exit";
3
- import { LazyArg } from "effect/Function";
4
- import * as Option from "effect/Option";
5
- import * as Schema_ from "effect/Schema";
6
- import { Pipeable } from "effect/Pipeable";
7
- import { Predicate, Refinement } from "effect/Predicate";
8
- import * as Types from "effect/Types";
9
- //#region src/internal/result-schema.d.ts
10
- /**
11
- * Schema interface for `Result` values, retaining the schemas used for success values and failure errors.
12
- *
13
- * @category schemas
14
- * @since 4.0.0
15
- */
16
- interface Schema<Success extends Schema_.Constraint, Error extends Schema_.Constraint> extends Schema_.declareConstructor<Result<Success['Type'], Error['Type']>, Result<Success['Encoded'], Error['Encoded']>, readonly [Success, Schema_.Cause<Error, Schema_.Defect>]> {
17
- readonly success: Success;
18
- readonly error: Error;
19
- }
20
- /**
21
- * Creates a schema for `Result` values using optional schemas for success values and failure errors.
22
- *
23
- * @category schemas
24
- * @since 4.0.0
25
- */
26
- declare const Schema: <A extends Schema_.Constraint = Schema_.Never, E extends Schema_.Constraint = Schema_.Never>(options: {
27
- readonly success?: A | undefined;
28
- readonly error?: E | undefined;
29
- }) => Schema<A, E>;
30
- declare namespace Result_d_exports {
31
- export { Builder, Defect, Failure, Initial, Interrupt, Result, Schema, Success, TypeId, With, all, builder, cause, error, fail, failWithPrevious, failure, failureWithPrevious, flatMap, fromExit, fromExitWithPrevious, getOrElse, getOrThrow, initial, isResult as isAsyncResult, isFailure, isInitial, isInterrupted, isNotInitial, isResult, isSuccess, isWaiting, map, match, matchWithError, matchWithWaiting, replacePrevious, success, toExit, touch, value, waiting, waitingFrom };
32
- }
33
- /**
34
- * Type-level identifier used to recognize `Result` values.
35
- *
36
- * @category type IDs
37
- * @since 4.0.0
38
- */
39
- type TypeId = '~effect-atom/atom/Result';
40
- /**
41
- * Runtime identifier attached to `Result` values and used by `isResult`.
42
- *
43
- * @category type IDs
44
- * @since 4.0.0
45
- */
46
- declare const TypeId: TypeId;
47
- /**
48
- * Represents the state of an asynchronous value as `Initial`, `Success`, or `Failure`, with a `waiting` flag for in-flight refreshes.
49
- *
50
- * @category models
51
- * @since 4.0.0
52
- */
53
- type Result<A, E = never> = Initial<A, E> | Success<A, E> | Failure<A, E>;
54
- /**
55
- * Returns `true` when a value is an `Result`.
56
- *
57
- * @category guards
58
- * @since 4.0.0
59
- */
60
- declare const isResult: (u: unknown) => u is Result<unknown, unknown>;
61
- /**
62
- * Namespace containing type-level helpers and the shared prototype shape for `Result` values.
63
- *
64
- * @since 4.0.0
65
- */
66
- declare namespace Result {
67
- /**
68
- * Common prototype fields implemented by every `Result` variant, including pipeability, the type marker, phantom type members, and the `waiting` flag.
69
- *
70
- * @category models
71
- * @since 4.0.0
72
- */
73
- interface Proto<A, E> extends Pipeable {
74
- readonly [TypeId]: {
75
- readonly E: (_: never) => E;
76
- readonly A: (_: never) => A;
77
- };
78
- readonly waiting: boolean;
79
- }
80
- /**
81
- * Extracts the success value type from an `Result`.
82
- *
83
- * @category utility types
84
- * @since 4.0.0
85
- */
86
- type Success<R> = R extends Result<infer A, infer _> ? A : never;
87
- /**
88
- * Extracts the failure error type from an `Result`.
89
- *
90
- * @category utility types
91
- * @since 4.0.0
92
- */
93
- type Failure<R> = R extends Result<infer _, infer E> ? E : never;
94
- }
95
- /**
96
- * Rebuilds an `Result` with new success and failure types while preserving the variant of another result.
97
- *
98
- * @category utility types
99
- * @since 4.0.0
100
- */
101
- type With<R extends Result<any, any>, A, E> = R extends Initial<infer _A, infer _E> ? Initial<A, E> : R extends Success<infer _A, infer _E> ? Success<A, E> : R extends Failure<infer _A, infer _E> ? Failure<A, E> : never;
102
- /**
103
- * Returns whether an `Result` is currently waiting for an asynchronous computation or refresh to finish.
104
- *
105
- * @category predicates
106
- * @since 4.0.0
107
- */
108
- declare const isWaiting: <A, E>(result: Result<A, E>) => boolean;
109
- /**
110
- * Initial `Result` state before a success value or failure cause is available.
111
- *
112
- * @category models
113
- * @since 4.0.0
114
- */
115
- interface Initial<A, E = never> extends Result.Proto<A, E> {
116
- readonly _tag: 'Initial';
117
- }
118
- /**
119
- * Converts an `Exit` into a `Success` when it succeeds or a `Failure` carrying the exit cause when it fails.
120
- *
121
- * @category constructors
122
- * @since 4.0.0
123
- */
124
- declare const fromExit: <A, E>(exit: Exit.Exit<A, E>) => Success<A, E> | Failure<A, E>;
125
- /**
126
- * Converts an `Exit` to a result, preserving the latest previous success when the exit is a failure.
127
- *
128
- * @category constructors
129
- * @since 4.0.0
130
- */
131
- declare const fromExitWithPrevious: <A, E>(exit: Exit.Exit<A, E>, previous: Option.Option<Result<A, E>>) => Success<A, E> | Failure<A, E>;
132
- /**
133
- * Creates a waiting result from an optional previous result, using `Initial(true)` when no previous result exists.
134
- *
135
- * @category constructors
136
- * @since 4.0.0
137
- */
138
- declare const waitingFrom: <A, E>(previous: Option.Option<Result<A, E>>) => Result<A, E>;
139
- /**
140
- * Returns `true` when an `Result` is in the `Initial` state.
141
- *
142
- * @category guards
143
- * @since 4.0.0
144
- */
145
- declare const isInitial: <A, E>(result: Result<A, E>) => result is Initial<A, E>;
146
- /**
147
- * Returns `true` when an `Result` is either `Success` or `Failure`.
148
- *
149
- * @category guards
150
- * @since 4.0.0
151
- */
152
- declare const isNotInitial: <A, E>(result: Result<A, E>) => result is Success<A, E> | Failure<A, E>;
153
- /**
154
- * Creates an `Initial` result, optionally marking it as waiting.
155
- *
156
- * @category constructors
157
- * @since 4.0.0
158
- */
159
- declare const initial: <A = never, E = never>(waiting?: boolean) => Initial<A, E>;
160
- /**
161
- * Successful `Result` containing the current value, its timestamp, and the shared waiting flag.
162
- *
163
- * @category models
164
- * @since 4.0.0
165
- */
166
- interface Success<A, E = never> extends Result.Proto<A, E> {
167
- readonly _tag: 'Success';
168
- readonly value: A;
169
- readonly timestamp: number;
170
- }
171
- /**
172
- * Returns `true` when an `Result` is a `Success`.
173
- *
174
- * @category guards
175
- * @since 4.0.0
176
- */
177
- declare const isSuccess: <A, E>(result: Result<A, E>) => result is Success<A, E>;
178
- /**
179
- * Creates a `Success` result with a value and optional `waiting` flag or timestamp override.
180
- *
181
- * @category constructors
182
- * @since 4.0.0
183
- */
184
- declare const success: <A, E = never>(value: A, options?: {
185
- readonly waiting?: boolean | undefined;
186
- readonly timestamp?: number | undefined;
187
- }) => Success<A, E>;
188
- /**
189
- * Failed `Result` containing a failure cause and the latest previous success when one is available.
190
- *
191
- * @category models
192
- * @since 4.0.0
193
- */
194
- interface Failure<A, E = never> extends Result.Proto<A, E> {
195
- readonly _tag: 'Failure';
196
- readonly cause: Cause.Cause<E>;
197
- readonly previousSuccess: Option.Option<Success<A, E>>;
198
- }
199
- /**
200
- * Returns `true` when an `Result` is a `Failure`.
201
- *
202
- * @category guards
203
- * @since 4.0.0
204
- */
205
- declare const isFailure: <A, E>(result: Result<A, E>) => result is Failure<A, E>;
206
- /**
207
- * Returns `true` when an `Result` is a `Failure` whose cause contains only interruptions.
208
- *
209
- * @category guards
210
- * @since 4.0.0
211
- */
212
- declare const isInterrupted: <A, E>(result: Result<A, E>) => result is Failure<A, E>;
213
- /**
214
- * Creates a `Failure` result from a `Cause`, optionally preserving a previous success and marking the result as waiting.
215
- *
216
- * @category constructors
217
- * @since 4.0.0
218
- */
219
- declare const failure: <A, E = never>(cause: Cause.Cause<E>, options?: {
220
- readonly previousSuccess?: Option.Option<Success<A, E>> | undefined;
221
- readonly waiting?: boolean | undefined;
222
- }) => Failure<A, E>;
223
- /**
224
- * Creates a `Failure` result from a `Cause`, carrying forward the latest success stored in a previous result.
225
- *
226
- * @category constructors
227
- * @since 4.0.0
228
- */
229
- declare const failureWithPrevious: <A, E>(cause: Cause.Cause<E>, options: {
230
- readonly previous: Option.Option<Result<A, E>>;
231
- readonly waiting?: boolean | undefined;
232
- }) => Failure<A, E>;
233
- /**
234
- * Creates a `Failure` result from a typed error, wrapping it in `Cause.fail`.
235
- *
236
- * @category constructors
237
- * @since 4.0.0
238
- */
239
- declare const fail: <E, A = never>(error: E, options?: {
240
- readonly previousSuccess?: Option.Option<Success<A, E>> | undefined;
241
- readonly waiting?: boolean | undefined;
242
- }) => Failure<A, E>;
243
- /**
244
- * Creates a `Failure` result from a typed error while carrying forward the latest success stored in a previous result.
245
- *
246
- * @category constructors
247
- * @since 4.0.0
248
- */
249
- declare const failWithPrevious: <A, E>(error: E, options: {
250
- readonly previous: Option.Option<Result<A, E>>;
251
- readonly waiting?: boolean | undefined;
252
- }) => Failure<A, E>;
253
- /**
254
- * Marks an `Result` as waiting, optionally touching the timestamp when the result is a `Success`.
255
- *
256
- * @category constructors
257
- * @since 4.0.0
258
- */
259
- declare const waiting: <R extends Result<any, any>>(self: R, options?: {
260
- readonly touch?: boolean | undefined;
261
- }) => R;
262
- /**
263
- * Refreshes the timestamp of a `Success` result while preserving its value and waiting flag; non-success results are returned unchanged.
264
- *
265
- * @category combinators
266
- * @since 4.0.0
267
- */
268
- declare const touch: <A extends Result<any, any>>(result: A) => A;
269
- /**
270
- * Replaces a `Failure` value's stored previous success with the latest success
271
- * found in another result.
272
- *
273
- * @category combinators
274
- * @since 4.0.0
275
- */
276
- declare const replacePrevious: <R extends Result<any, any>, XE, A>(self: R, previous: Option.Option<Result<A, XE>>) => With<R, A, Result.Failure<R>>;
277
- /**
278
- * Returns the current success value, or the previous success value stored in a failure, as an `Option`.
279
- *
280
- * @category accessors
281
- * @since 4.0.0
282
- */
283
- declare const value: <A, E>(self: Result<A, E>) => Option.Option<A>;
284
- /**
285
- * Returns the available value from `value`, or evaluates the fallback when no current or previous success exists.
286
- *
287
- * @category accessors
288
- * @since 4.0.0
289
- */
290
- declare const getOrElse: {
291
- <B>(orElse: LazyArg<B>): <A, E>(self: Result<A, E>) => A | B;
292
- <A, E, B>(self: Result<A, E>, orElse: LazyArg<B>): A | B;
293
- };
294
- /**
295
- * Returns the available value from `value`, or throws `NoSuchElementError` when no current or previous success exists.
296
- *
297
- * @category accessors
298
- * @since 4.0.0
299
- */
300
- declare const getOrThrow: <A, E>(self: Result<A, E>) => A;
301
- /**
302
- * Returns the failure cause when the result is a `Failure`, otherwise `None`.
303
- *
304
- * @category accessors
305
- * @since 4.0.0
306
- */
307
- declare const cause: <A, E>(self: Result<A, E>) => Option.Option<Cause.Cause<E>>;
308
- /**
309
- * Returns the first typed error from a failure cause, or `None` for successes, initial results, defects, and interrupt-only causes.
310
- *
311
- * @category accessors
312
- * @since 4.0.0
313
- */
314
- declare const error: <A, E>(self: Result<A, E>) => Option.Option<E>;
315
- /**
316
- * Converts a result to an `Exit`, succeeding with a success value, failing with a failure cause, or failing with `NoSuchElementError` for `Initial`.
317
- *
318
- * @category combinators
319
- * @since 4.0.0
320
- */
321
- declare const toExit: {
322
- <A, E>(self: Success<A, E> | Failure<A, E>): Exit.Exit<A, E>;
323
- <A, E>(self: Result<A, E>): Exit.Exit<A, E | Cause.NoSuchElementError>;
324
- };
325
- /**
326
- * Maps the success value of an `Result`, also mapping any previous success stored in a failure while leaving initial results unchanged.
327
- *
328
- * @category combinators
329
- * @since 4.0.0
330
- */
331
- declare const map: {
332
- <A, B>(f: (a: A) => B): <E>(self: Result<A, E>) => Result<B, E>;
333
- <E, A, B>(self: Result<A, E>, f: (a: A) => B): Result<B, E>;
334
- };
335
- /**
336
- * Maps the success value of an `Result` and flattens the result.
337
- *
338
- * **When to use**
339
- *
340
- * Use to sequence computations that may return another `Result` while
341
- * preserving initial and failure states.
342
- *
343
- * **Details**
344
- *
345
- * Initial results are left unchanged. Failures preserve their cause and remap
346
- * the stored previous success when the mapping function returns a success.
347
- *
348
- * @category combinators
349
- * @since 4.0.0
350
- */
351
- declare const flatMap: {
352
- <A, E, B, E2>(f: (a: A, prev: Success<A, E>) => Result<B, E2>): (self: Result<A, E>) => Result<B, E | E2>;
353
- <E, A, B, E2>(self: Result<A, E>, f: (a: A, prev: Success<A, E>) => Result<B, E2>): Result<B, E | E2>;
354
- };
355
- /**
356
- * Pattern matches an `Result` by calling the handler for `Initial`, `Failure`, or `Success`.
357
- *
358
- * @category combinators
359
- * @since 4.0.0
360
- */
361
- declare const match: {
362
- <A, E, X, Y, Z>(options: {
363
- readonly onInitial: (_: Initial<A, E>) => X;
364
- readonly onFailure: (_: Failure<A, E>) => Y;
365
- readonly onSuccess: (_: Success<A, E>) => Z;
366
- }): (self: Result<A, E>) => X | Y | Z;
367
- <A, E, X, Y, Z>(self: Result<A, E>, options: {
368
- readonly onInitial: (_: Initial<A, E>) => X;
369
- readonly onFailure: (_: Failure<A, E>) => Y;
370
- readonly onSuccess: (_: Success<A, E>) => Z;
371
- }): X | Y | Z;
372
- };
373
- /**
374
- * Pattern matches a result, handling successes and initials directly while splitting failures into typed errors or squashed non-error causes passed to `onDefect`.
375
- *
376
- * @category combinators
377
- * @since 4.0.0
378
- */
379
- declare const matchWithError: {
380
- <A, E, W, X, Y, Z>(options: {
381
- readonly onInitial: (_: Initial<A, E>) => W;
382
- readonly onError: (error: E, _: Failure<A, E>) => X;
383
- readonly onDefect: (defect: unknown, _: Failure<A, E>) => Y;
384
- readonly onSuccess: (_: Success<A, E>) => Z;
385
- }): (self: Result<A, E>) => W | X | Y | Z;
386
- <A, E, W, X, Y, Z>(self: Result<A, E>, options: {
387
- readonly onInitial: (_: Initial<A, E>) => W;
388
- readonly onError: (error: E, _: Failure<A, E>) => X;
389
- readonly onDefect: (defect: unknown, _: Failure<A, E>) => Y;
390
- readonly onSuccess: (_: Success<A, E>) => Z;
391
- }): W | X | Y | Z;
392
- };
393
- /**
394
- * Pattern matches a result by calling `onWaiting` for waiting or initial states, otherwise handling successes and splitting failures into typed errors or squashed non-error causes.
395
- *
396
- * @category combinators
397
- * @since 4.0.0
398
- */
399
- declare const matchWithWaiting: {
400
- <A, E, W, X, Y, Z>(options: {
401
- readonly onWaiting: (_: Result<A, E>) => W;
402
- readonly onError: (error: E, _: Failure<A, E>) => X;
403
- readonly onDefect: (defect: unknown, _: Failure<A, E>) => Y;
404
- readonly onSuccess: (_: Success<A, E>) => Z;
405
- }): (self: Result<A, E>) => W | X | Y | Z;
406
- <A, E, W, X, Y, Z>(self: Result<A, E>, options: {
407
- readonly onWaiting: (_: Result<A, E>) => W;
408
- readonly onError: (error: E, _: Failure<A, E>) => X;
409
- readonly onDefect: (defect: unknown, _: Failure<A, E>) => Y;
410
- readonly onSuccess: (_: Success<A, E>) => Z;
411
- }): W | X | Y | Z;
412
- };
413
- /**
414
- * Combines an iterable or record of `Result` and plain values into one `Result`, returning the first non-success result or a success of the collected values marked waiting when any input success is waiting.
415
- *
416
- * @category combinators
417
- * @since 4.0.0
418
- */
419
- type AllSuccess<Arg> = [Arg] extends [readonly any[]] ? { -readonly [K in keyof Arg]: [Arg[K]] extends [Result<infer _A, infer _E>] ? _A : Arg[K]; } : [Arg] extends [Iterable<infer _A>] ? _A extends Result<infer _AA, infer _E> ? _AA : _A : [Arg] extends [Record<string, any>] ? { -readonly [K in keyof Arg]: [Arg[K]] extends [Result<infer _A, infer _E>] ? _A : Arg[K]; } : never;
420
- type AllError<Arg> = [Arg] extends [readonly any[]] ? Result.Failure<Arg[number]> : [Arg] extends [Iterable<infer _A>] ? Result.Failure<_A> : [Arg] extends [Record<string, any>] ? Result.Failure<Arg[keyof Arg]> : never;
421
- declare const all: <const Arg extends Iterable<any> | Record<string, any>>(results: Arg) => Result<AllSuccess<Arg>, AllError<Arg>>;
422
- /**
423
- * Creates a typed builder for rendering an `Result` by handling waiting, initial, success, error, defect, interrupt, and failure cases.
424
- *
425
- * @category constructors
426
- * @since 4.0.0
427
- */
428
- type BuilderFor<A extends Result<any, any>> = Builder<never, A extends Success<infer _A, infer _E> ? _A : never, A extends Failure<infer _A, infer _E> ? _E : never, A extends Initial<infer _A, infer _E> ? true : never, A extends Failure<infer _A, infer _E> ? Defect | Interrupt : never>;
429
- declare const builder: <A extends Result<any, any>>(self: A) => BuilderFor<A>;
430
- /**
431
- * Type marker used by `Builder` to track whether defect failures still need to be handled.
432
- *
433
- * @category utility types
434
- * @since 4.0.0
435
- */
436
- interface Defect {
437
- readonly _: unique symbol;
438
- }
439
- /**
440
- * Type marker used by `Builder` to track whether interrupt failures still need to be handled.
441
- *
442
- * @category utility types
443
- * @since 4.0.0
444
- */
445
- interface Interrupt {
446
- readonly _: unique symbol;
447
- }
448
- /**
449
- * Fluent renderer for `Result` values that tracks unhandled cases at the type level and exposes `exhaustive` only after all possible cases are handled.
450
- *
451
- * @category models
452
- * @since 4.0.0
453
- */
454
- type Builder<Out, A, E, I, F> = Pipeable & {
455
- onWaiting<B>(f: (result: Result<A, E>) => B): Builder<Out | B, A, E, I, F>;
456
- orElse<B>(orElse: LazyArg<B>): Out | B;
457
- orNull(): Out | null;
458
- render(): [A | I] extends [never] ? Out : Out | null;
459
- } & ([A | E | I | F] extends [never] ? {
460
- exhaustive(): Out;
461
- } : unknown) & ([I] extends [never] ? unknown : {
462
- onInitial<B>(f: (result: Initial<A, E>) => B): Builder<Out | B, A, E, never, F>;
463
- onInitialOrWaiting<B>(f: (result: Result<A, E>) => B): Builder<Out | B, A, E, never, F>;
464
- }) & ([A] extends [never] ? unknown : {
465
- onSuccess<B>(f: (value: A, result: Success<A, E>) => B): Builder<Out | B, never, E, I, F>;
466
- }) & ([E] extends [never] ? unknown : {
467
- onError<B>(f: (error: E, result: Failure<A, E>) => B): Builder<Out | B, A, never, I, F>;
468
- onErrorIf<B extends E, C>(refinement: Refinement<E, B>, f: (error: B, result: Failure<A, E>) => C): Builder<Out | C, A, Types.EqualsWith<E, B, E, Exclude<E, B>>, I, F>;
469
- onErrorIf<C>(predicate: Predicate<E>, f: (error: E, result: Failure<A, E>) => C): Builder<Out | C, A, E, I, F>;
470
- onErrorTag<const Tags extends readonly Types.Tags<E>[], B>(tags: Tags, f: (error: Types.ExtractTag<E, Tags[number]>, result: Failure<A, E>) => B): Builder<Out | B, A, Types.ExcludeTag<E, Tags[number]>, I, F>;
471
- onErrorTag<const Tag extends Types.Tags<E>, B>(tag: Tag, f: (error: Types.ExtractTag<E, Tag>, result: Failure<A, E>) => B): Builder<Out | B, A, Types.ExcludeTag<E, Tag>, I, F>;
472
- }) & ([E | F] extends [never] ? unknown : {
473
- onFailure<B>(f: (cause: Cause.Cause<E>, result: Failure<A, E>) => B): Builder<Out | B, A, never, I, never>;
474
- }) & (Interrupt extends F ? {
475
- onInterrupt<B>(f: (interruptors: ReadonlySet<number>, result: Failure<A, E>) => B): Builder<Out | B, A, E, I, Exclude<F, Interrupt>>;
476
- } : unknown) & (Defect extends F ? {
477
- onDefect<B>(f: (defect: unknown, result: Failure<A, E>) => B): Builder<Out | B, A, E, I, Exclude<F, Defect>>;
478
- } : unknown);
479
- //#endregion
480
- export { isSuccess as A, value as B, getOrThrow as C, isInterrupted as D, isInitial as E, matchWithWaiting as F, waitingFrom as H, replacePrevious as I, success as L, map as M, match as N, isNotInitial as O, matchWithError as P, toExit as R, getOrElse as S, isFailure as T, Schema as U, waiting as V, failure as _, Interrupt as a, fromExit as b, Success as c, all as d, builder as f, failWithPrevious as g, fail as h, Initial as i, isWaiting as j, isResult as k, TypeId as l, error as m, Defect as n, Result as o, cause as p, Failure as r, Result_d_exports as s, Builder as t, With as u, failureWithPrevious as v, initial as w, fromExitWithPrevious as x, flatMap as y, touch as z };