@systemfsoftware/effect-atom 0.5.3

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.
@@ -0,0 +1,663 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
+ import * as Cause from "effect/Cause";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Exit from "effect/Exit";
5
+ import { constTrue, dual, identity } from "effect/Function";
6
+ import * as Option from "effect/Option";
7
+ import * as Schema_ from "effect/Schema";
8
+ import { pipeArguments } from "effect/Pipeable";
9
+ import { hasProperty, isIterable } from "effect/Predicate";
10
+ import * as Either from "effect/Result";
11
+ import * as Equal$1 from "effect/Equal";
12
+ import * as Hash from "effect/Hash";
13
+ import * as SchemaIssue from "effect/SchemaIssue";
14
+ import * as SchemaParser from "effect/SchemaParser";
15
+ import * as SchemaTransformation from "effect/SchemaTransformation";
16
+ //#region src/internal/result-values.ts
17
+ /**
18
+ * Value-side declarations backing the `Result` module.
19
+ *
20
+ * These declarations were lifted out of `Result.ts` to break the import cycle
21
+ * between `Result.ts` and `internal/result-schema.ts`. Both modules now import
22
+ * from this leaf, so the dependency graph becomes:
23
+ *
24
+ * result-values.ts (this file) <-- Result.ts
25
+ * <-- internal/result-schema.ts
26
+ *
27
+ * The public surface is unchanged: every public name declared here is
28
+ * re-exported from `Result.ts`. `ResultProto` stays leaf-internal and is
29
+ * reached directly by `Result.ts` and by the constructors in this file.
30
+ */
31
+ /**
32
+ * Runtime identifier attached to `Result` values and used by `isResult`.
33
+ *
34
+ * @category type IDs
35
+ * @since 4.0.0
36
+ */
37
+ const TypeId = "~effect-atom/atom/Result";
38
+ /**
39
+ * Shared prototype every `Result` variant inherits from. The three
40
+ * constructors (`initial`, `success`, `failure`) use it; `waiting` in
41
+ * `Result.ts` also reaches it directly.
42
+ *
43
+ * The schema codec (`internal/result-schema.ts`) serializes only the tagged
44
+ * variant fields (`value`, `waiting`, `timestamp`, `cause`,
45
+ * `previousSuccess`) — nothing added to this prototype is wire-carried, so
46
+ * any future proto-private state must be mirrored in that encode/decode pair.
47
+ *
48
+ * @since 4.0.0
49
+ */
50
+ const ResultProto = {
51
+ [TypeId]: {
52
+ E: identity,
53
+ A: identity
54
+ },
55
+ pipe() {
56
+ return pipeArguments(this, arguments);
57
+ },
58
+ [Equal$1.symbol](that) {
59
+ if (this._tag !== that._tag || this.waiting !== that.waiting) return false;
60
+ switch (this._tag) {
61
+ case "Initial": return true;
62
+ case "Success": return Equal$1.equals(this.value, that.value);
63
+ case "Failure": return Equal$1.equals(this.cause, that.cause);
64
+ }
65
+ },
66
+ [Hash.symbol]() {
67
+ const tagHash = Hash.string(`${this._tag}:${this.waiting}`);
68
+ if (this._tag === "Initial") return tagHash;
69
+ return Hash.combine(tagHash)(this._tag === "Success" ? Hash.hash(this.value) : Hash.hash(this.cause));
70
+ }
71
+ };
72
+ /**
73
+ * Returns `true` when a value is an `Result`.
74
+ *
75
+ * @category guards
76
+ * @since 4.0.0
77
+ */
78
+ const isResult = (u) => hasProperty(u, TypeId);
79
+ /**
80
+ * Creates an `Initial` result, optionally marking it as waiting.
81
+ *
82
+ * @category constructors
83
+ * @since 4.0.0
84
+ */
85
+ const initial = (waiting = false) => {
86
+ const result = Object.create(ResultProto);
87
+ result._tag = "Initial";
88
+ result.waiting = waiting;
89
+ return result;
90
+ };
91
+ /**
92
+ * Creates a `Success` result with a value and optional `waiting` flag or
93
+ * timestamp override.
94
+ *
95
+ * @category constructors
96
+ * @since 4.0.0
97
+ */
98
+ const success = (value, options) => {
99
+ const result = Object.create(ResultProto);
100
+ result._tag = "Success";
101
+ result.value = value;
102
+ result.waiting = options?.waiting ?? false;
103
+ result.timestamp = options?.timestamp ?? Date.now();
104
+ return result;
105
+ };
106
+ /**
107
+ * Creates a `Failure` result from a `Cause`, optionally preserving a previous
108
+ * success and marking the result as waiting.
109
+ *
110
+ * @category constructors
111
+ * @since 4.0.0
112
+ */
113
+ const failure = (cause, options) => {
114
+ const result = Object.create(ResultProto);
115
+ result._tag = "Failure";
116
+ result.cause = cause;
117
+ result.previousSuccess = options?.previousSuccess ?? Option.none();
118
+ result.waiting = options?.waiting ?? false;
119
+ return result;
120
+ };
121
+ //#endregion
122
+ //#region src/internal/result-schema.ts
123
+ /**
124
+ * Creates a schema for `Result` values using optional schemas for success values and failure errors.
125
+ *
126
+ * @category schemas
127
+ * @since 4.0.0
128
+ */
129
+ const Schema = (options) => {
130
+ const success_ = options.success ?? Schema_.Never;
131
+ const error = options.error ?? Schema_.Never;
132
+ const schema = Schema_.declareConstructor()([success_, Schema_.Cause(error, Schema_.Defect())], ([value, cause]) => (input, ast, options) => {
133
+ if (!isResult(input)) return Effect.fail(new SchemaIssue.InvalidType(ast, input, options));
134
+ switch (input._tag) {
135
+ case "Initial": return Effect.succeed(input);
136
+ case "Success": return Effect.mapBothEager(SchemaParser.decodeUnknownEffect(value)(input.value, options), {
137
+ onSuccess: (value) => success(value, input),
138
+ onFailure: (issue) => new SchemaIssue.Composite(ast, [new SchemaIssue.Pointer(["value"], issue)], input, options)
139
+ });
140
+ case "Failure": {
141
+ const prevSuccessEffect = input.previousSuccess.pipe(Option.map((ps) => Effect.mapBothEager(SchemaParser.decodeUnknownEffect(value)(ps.value, options), {
142
+ onSuccess: (value) => Option.some(success(value, ps)),
143
+ onFailure: (issue) => new SchemaIssue.Composite(ast, [new SchemaIssue.Pointer(["previousSuccess", "value"], issue)], input, options)
144
+ })), Option.getOrElse(() => Effect.succeedNone));
145
+ const causeEffect = Effect.mapErrorEager(SchemaParser.decodeUnknownEffect(cause)(input.cause, options), (issue) => new SchemaIssue.Composite(ast, [new SchemaIssue.Pointer(["cause"], issue)], input, options));
146
+ return Effect.flatMapEager(prevSuccessEffect, (previousSuccess) => Effect.mapEager(causeEffect, (cause) => failure(cause, {
147
+ previousSuccess,
148
+ waiting: input.waiting
149
+ })));
150
+ }
151
+ }
152
+ }, {
153
+ expected: "Result",
154
+ toCodec([value, cause]) {
155
+ const Success = Schema_.TaggedStruct("Success", {
156
+ value,
157
+ waiting: Schema_.Boolean,
158
+ timestamp: Schema_.Number
159
+ });
160
+ return Schema_.link()(Schema_.Union([
161
+ Schema_.TaggedStruct("Initial", { waiting: Schema_.Boolean }),
162
+ Success,
163
+ Schema_.TaggedStruct("Failure", {
164
+ cause,
165
+ previousSuccess: Schema_.Option(Success),
166
+ waiting: Schema_.Boolean
167
+ })
168
+ ]), SchemaTransformation.transform({
169
+ decode: (encoded) => {
170
+ switch (encoded._tag) {
171
+ case "Initial": return initial(encoded.waiting);
172
+ case "Success": return success(encoded.value, {
173
+ waiting: encoded.waiting,
174
+ timestamp: encoded.timestamp
175
+ });
176
+ case "Failure": return failure(encoded.cause, {
177
+ previousSuccess: Option.map(encoded.previousSuccess, (ps) => success(ps.value, ps)),
178
+ waiting: encoded.waiting
179
+ });
180
+ }
181
+ },
182
+ encode(result) {
183
+ switch (result._tag) {
184
+ case "Initial": return {
185
+ _tag: "Initial",
186
+ waiting: result.waiting
187
+ };
188
+ case "Success": return {
189
+ _tag: "Success",
190
+ value: result.value,
191
+ waiting: result.waiting,
192
+ timestamp: result.timestamp
193
+ };
194
+ case "Failure": return {
195
+ _tag: "Failure",
196
+ cause: result.cause,
197
+ previousSuccess: result.previousSuccess,
198
+ waiting: result.waiting
199
+ };
200
+ }
201
+ }
202
+ }));
203
+ },
204
+ toEquivalence: Equal$1.asEquivalence,
205
+ toArbitrary: ([value]) => (fc) => fc.oneof(fc.record({
206
+ value: value.arbitrary,
207
+ waiting: fc.boolean(),
208
+ timestamp: fc.nat()
209
+ }).map((r) => success(r.value, {
210
+ waiting: r.waiting,
211
+ timestamp: r.timestamp
212
+ })), fc.record({
213
+ cause: fc.oneof(Schema_.toArbitrary(error)(fc).map((e) => Cause.fail(e)), fc.jsonValue().map((d) => Cause.die(d))),
214
+ waiting: fc.boolean(),
215
+ previous: fc.option(value.arbitrary, { nil: void 0 })
216
+ }).map((r) => failure(r.cause, {
217
+ previousSuccess: r.previous === void 0 ? Option.none() : Option.some(success(r.previous)),
218
+ waiting: r.waiting
219
+ })), fc.boolean().map((waiting) => initial(waiting))),
220
+ toFormatter: ([value, cause]) => (t) => {
221
+ switch (t._tag) {
222
+ case "Success": return `Result.Success(${value(t.value)}, ${t.waiting}, ${t.timestamp})`;
223
+ case "Failure": return `Result.Failure(${cause(t.cause)}, ${t.waiting})`;
224
+ case "Initial": return `Result.Initial(${t.waiting})`;
225
+ }
226
+ }
227
+ });
228
+ return Object.assign(schema, {
229
+ success: success_,
230
+ error
231
+ });
232
+ };
233
+ /**
234
+ * A `Schema.ConstraintCodec` for `Result<unknown, unknown>` built from the
235
+ * given success and error schemas.
236
+ *
237
+ * @internal
238
+ */
239
+ const schemaCodec = (success, error) => Schema({
240
+ success,
241
+ error
242
+ });
243
+ //#endregion
244
+ //#region src/Result.ts
245
+ /**
246
+ * Represents observable state for asynchronous values.
247
+ *
248
+ * `Result<A, E>` records whether asynchronous work has no value yet,
249
+ * succeeded with an `A`, or failed with an `E`. Every state also carries a
250
+ * `waiting` flag, so callers can keep showing the current value while newer
251
+ * work is loading, refreshing, retrying, or recovering. This module includes
252
+ * constructors, checks, accessors, mapping and matching helpers, ways to combine
253
+ * several results, and schemas for encoding or decoding results.
254
+ *
255
+ * @since 4.0.0
256
+ */
257
+ var Result_exports = /* @__PURE__ */ __exportAll({
258
+ Schema: () => Schema,
259
+ TypeId: () => TypeId,
260
+ all: () => all,
261
+ builder: () => builder,
262
+ cause: () => cause,
263
+ error: () => error,
264
+ fail: () => fail,
265
+ failWithPrevious: () => failWithPrevious,
266
+ failure: () => failure,
267
+ failureWithPrevious: () => failureWithPrevious,
268
+ flatMap: () => flatMap,
269
+ fromExit: () => fromExit,
270
+ fromExitWithPrevious: () => fromExitWithPrevious,
271
+ getOrElse: () => getOrElse,
272
+ getOrThrow: () => getOrThrow,
273
+ initial: () => initial,
274
+ isAsyncResult: () => isResult,
275
+ isFailure: () => isFailure,
276
+ isInitial: () => isInitial,
277
+ isInterrupted: () => isInterrupted,
278
+ isNotInitial: () => isNotInitial,
279
+ isResult: () => isResult,
280
+ isSuccess: () => isSuccess,
281
+ isWaiting: () => isWaiting,
282
+ map: () => map,
283
+ match: () => match,
284
+ matchWithError: () => matchWithError,
285
+ matchWithWaiting: () => matchWithWaiting,
286
+ replacePrevious: () => replacePrevious,
287
+ success: () => success,
288
+ toExit: () => toExit,
289
+ touch: () => touch,
290
+ value: () => value,
291
+ waiting: () => waiting,
292
+ waitingFrom: () => waitingFrom
293
+ });
294
+ /**
295
+ * Returns whether an `Result` is currently waiting for an asynchronous computation or refresh to finish.
296
+ *
297
+ * @category predicates
298
+ * @since 4.0.0
299
+ */
300
+ const isWaiting = (result) => result.waiting;
301
+ /**
302
+ * Converts an `Exit` into a `Success` when it succeeds or a `Failure` carrying the exit cause when it fails.
303
+ *
304
+ * @category constructors
305
+ * @since 4.0.0
306
+ */
307
+ const fromExit = (exit) => exit._tag === "Success" ? success(exit.value) : failure(exit.cause);
308
+ /**
309
+ * Converts an `Exit` to a result, preserving the latest previous success when the exit is a failure.
310
+ *
311
+ * @category constructors
312
+ * @since 4.0.0
313
+ */
314
+ const fromExitWithPrevious = (exit, previous) => exit._tag === "Success" ? success(exit.value) : failureWithPrevious(exit.cause, { previous });
315
+ /**
316
+ * Creates a waiting result from an optional previous result, using `Initial(true)` when no previous result exists.
317
+ *
318
+ * @category constructors
319
+ * @since 4.0.0
320
+ */
321
+ const waitingFrom = (previous) => {
322
+ if (previous._tag === "None") return initial(true);
323
+ return waiting(previous.value);
324
+ };
325
+ /**
326
+ * Returns `true` when an `Result` is in the `Initial` state.
327
+ *
328
+ * @category guards
329
+ * @since 4.0.0
330
+ */
331
+ const isInitial = (result) => result._tag === "Initial";
332
+ /**
333
+ * Returns `true` when an `Result` is either `Success` or `Failure`.
334
+ *
335
+ * @category guards
336
+ * @since 4.0.0
337
+ */
338
+ const isNotInitial = (result) => result._tag !== "Initial";
339
+ /**
340
+ * Returns `true` when an `Result` is a `Success`.
341
+ *
342
+ * @category guards
343
+ * @since 4.0.0
344
+ */
345
+ const isSuccess = (result) => result._tag === "Success";
346
+ /**
347
+ * Returns `true` when an `Result` is a `Failure`.
348
+ *
349
+ * @category guards
350
+ * @since 4.0.0
351
+ */
352
+ const isFailure = (result) => result._tag === "Failure";
353
+ /**
354
+ * Returns `true` when an `Result` is a `Failure` whose cause contains only interruptions.
355
+ *
356
+ * @category guards
357
+ * @since 4.0.0
358
+ */
359
+ const isInterrupted = (result) => result._tag === "Failure" && Cause.hasInterruptsOnly(result.cause);
360
+ /**
361
+ * Creates a `Failure` result from a `Cause`, carrying forward the latest success stored in a previous result.
362
+ *
363
+ * @category constructors
364
+ * @since 4.0.0
365
+ */
366
+ const failureWithPrevious = (cause, options) => failure(cause, {
367
+ previousSuccess: Option.flatMap(options.previous, (result) => isSuccess(result) ? Option.some(result) : isFailure(result) ? result.previousSuccess : Option.none()),
368
+ waiting: options.waiting
369
+ });
370
+ /**
371
+ * Creates a `Failure` result from a typed error, wrapping it in `Cause.fail`.
372
+ *
373
+ * @category constructors
374
+ * @since 4.0.0
375
+ */
376
+ const fail = (error, options) => failure(Cause.fail(error), options);
377
+ /**
378
+ * Creates a `Failure` result from a typed error while carrying forward the latest success stored in a previous result.
379
+ *
380
+ * @category constructors
381
+ * @since 4.0.0
382
+ */
383
+ const failWithPrevious = (error, options) => failureWithPrevious(Cause.fail(error), options);
384
+ /**
385
+ * Marks an `Result` as waiting, optionally touching the timestamp when the result is a `Success`.
386
+ *
387
+ * @category constructors
388
+ * @since 4.0.0
389
+ */
390
+ const waiting = (self, options) => {
391
+ if (self.waiting) return options?.touch ? touch(self) : self;
392
+ const result = Object.assign(Object.create(ResultProto), self, { waiting: true });
393
+ return options?.touch ? touch(result) : result;
394
+ };
395
+ /**
396
+ * Refreshes the timestamp of a `Success` result while preserving its value and waiting flag; non-success results are returned unchanged.
397
+ *
398
+ * @category combinators
399
+ * @since 4.0.0
400
+ */
401
+ const touch = (result) => {
402
+ if (isSuccess(result)) return success(result.value, { waiting: result.waiting });
403
+ return result;
404
+ };
405
+ /**
406
+ * Replaces a `Failure` value's stored previous success with the latest success
407
+ * found in another result.
408
+ *
409
+ * @category combinators
410
+ * @since 4.0.0
411
+ */
412
+ const replacePrevious = (self, previous) => replacePreviousImpl(self, previous);
413
+ const replacePreviousImpl = (self, previous) => self._tag === "Failure" ? failureWithPrevious(self.cause, {
414
+ previous,
415
+ waiting: self.waiting
416
+ }) : self;
417
+ /**
418
+ * Returns the current success value, or the previous success value stored in a failure, as an `Option`.
419
+ *
420
+ * @category accessors
421
+ * @since 4.0.0
422
+ */
423
+ const value = (self) => {
424
+ if (self._tag === "Success") return Option.some(self.value);
425
+ else if (self._tag === "Failure") return Option.map(self.previousSuccess, (s) => s.value);
426
+ return Option.none();
427
+ };
428
+ /**
429
+ * Returns the available value from `value`, or evaluates the fallback when no current or previous success exists.
430
+ *
431
+ * @category accessors
432
+ * @since 4.0.0
433
+ */
434
+ const getOrElse = dual(2, (self, orElse) => Option.getOrElse(value(self), orElse));
435
+ /**
436
+ * Returns the available value from `value`, or throws `NoSuchElementError` when no current or previous success exists.
437
+ *
438
+ * @category accessors
439
+ * @since 4.0.0
440
+ */
441
+ const getOrThrow = (self) => Option.getOrThrowWith(value(self), () => new Cause.NoSuchElementError("Result.getOrThrow: no value found"));
442
+ /**
443
+ * Returns the failure cause when the result is a `Failure`, otherwise `None`.
444
+ *
445
+ * @category accessors
446
+ * @since 4.0.0
447
+ */
448
+ const cause = (self) => self._tag === "Failure" ? Option.some(self.cause) : Option.none();
449
+ /**
450
+ * Returns the first typed error from a failure cause, or `None` for successes, initial results, defects, and interrupt-only causes.
451
+ *
452
+ * @category accessors
453
+ * @since 4.0.0
454
+ */
455
+ const error = (self) => self._tag === "Failure" ? Cause.findErrorOption(self.cause) : Option.none();
456
+ /**
457
+ * Converts a result to an `Exit`, succeeding with a success value, failing with a failure cause, or failing with `NoSuchElementError` for `Initial`.
458
+ *
459
+ * @category combinators
460
+ * @since 4.0.0
461
+ */
462
+ const toExit = (self) => {
463
+ switch (self._tag) {
464
+ case "Success": return Exit.succeed(self.value);
465
+ case "Failure": return Exit.failCause(self.cause);
466
+ default: return Exit.fail(new Cause.NoSuchElementError());
467
+ }
468
+ };
469
+ /**
470
+ * Maps the success value of an `Result`, also mapping any previous success stored in a failure while leaving initial results unchanged.
471
+ *
472
+ * @category combinators
473
+ * @since 4.0.0
474
+ */
475
+ const map = dual(2, (self, f) => {
476
+ switch (self._tag) {
477
+ case "Initial": return initial(self.waiting);
478
+ case "Failure": return failure(self.cause, {
479
+ previousSuccess: Option.map(self.previousSuccess, (s) => success(f(s.value), s)),
480
+ waiting: self.waiting
481
+ });
482
+ case "Success": return success(f(self.value), self);
483
+ }
484
+ });
485
+ /**
486
+ * Maps the success value of an `Result` and flattens the result.
487
+ *
488
+ * **When to use**
489
+ *
490
+ * Use to sequence computations that may return another `Result` while
491
+ * preserving initial and failure states.
492
+ *
493
+ * **Details**
494
+ *
495
+ * Initial results are left unchanged. Failures preserve their cause and remap
496
+ * the stored previous success when the mapping function returns a success.
497
+ *
498
+ * @category combinators
499
+ * @since 4.0.0
500
+ */
501
+ const flatMap = dual(2, (self, f) => {
502
+ switch (self._tag) {
503
+ case "Initial": return initial(self.waiting);
504
+ case "Failure": return failure(self.cause, {
505
+ previousSuccess: Option.flatMap(self.previousSuccess, (s) => {
506
+ const next = f(s.value, s);
507
+ return isSuccess(next) ? Option.some(next) : Option.none();
508
+ }),
509
+ waiting: self.waiting
510
+ });
511
+ case "Success": return f(self.value, self);
512
+ }
513
+ });
514
+ /**
515
+ * Pattern matches an `Result` by calling the handler for `Initial`, `Failure`, or `Success`.
516
+ *
517
+ * @category combinators
518
+ * @since 4.0.0
519
+ */
520
+ const match = dual(2, (self, options) => {
521
+ switch (self._tag) {
522
+ case "Initial": return options.onInitial(self);
523
+ case "Failure": return options.onFailure(self);
524
+ case "Success": return options.onSuccess(self);
525
+ }
526
+ });
527
+ /**
528
+ * Pattern matches a result, handling successes and initials directly while splitting failures into typed errors or squashed non-error causes passed to `onDefect`.
529
+ *
530
+ * @category combinators
531
+ * @since 4.0.0
532
+ */
533
+ const matchWithError = dual(2, (self, options) => {
534
+ switch (self._tag) {
535
+ case "Initial": return options.onInitial(self);
536
+ case "Failure": {
537
+ const result = Cause.findError(self.cause);
538
+ if (Either.isFailure(result)) return options.onDefect(Cause.squash(result.failure), self);
539
+ return options.onError(result.success, self);
540
+ }
541
+ case "Success": return options.onSuccess(self);
542
+ }
543
+ });
544
+ /**
545
+ * 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.
546
+ *
547
+ * @category combinators
548
+ * @since 4.0.0
549
+ */
550
+ const matchWithWaiting = dual(2, (self, options) => {
551
+ if (self.waiting) return options.onWaiting(self);
552
+ switch (self._tag) {
553
+ case "Initial": return options.onWaiting(self);
554
+ case "Failure": {
555
+ const e = Cause.findError(self.cause);
556
+ if (Either.isFailure(e)) return options.onDefect(Cause.squash(e.failure), self);
557
+ return options.onError(e.success, self);
558
+ }
559
+ case "Success": return options.onSuccess(self);
560
+ }
561
+ });
562
+ const all = (results) => allImpl(results);
563
+ const allImpl = (results) => {
564
+ let waiting = false;
565
+ if (isIterable(results)) {
566
+ const list = Array.from(results);
567
+ const successes = [];
568
+ for (let i = 0; i < list.length; i++) {
569
+ const result = list[i];
570
+ if (!isResult(result)) {
571
+ successes[i] = result;
572
+ continue;
573
+ }
574
+ if (!isSuccess(result)) return result;
575
+ successes[i] = result.value;
576
+ if (result.waiting) waiting = true;
577
+ }
578
+ return success(successes, { waiting });
579
+ }
580
+ const successes = {};
581
+ for (const [key, result] of Object.entries(results)) {
582
+ if (!isResult(result)) {
583
+ successes[key] = result;
584
+ continue;
585
+ }
586
+ if (!isSuccess(result)) return result;
587
+ successes[key] = result.value;
588
+ if (result.waiting) waiting = true;
589
+ }
590
+ return success(successes, { waiting });
591
+ };
592
+ const builder = (self) => {
593
+ return new BuilderImpl(self);
594
+ };
595
+ var BuilderImpl = class {
596
+ constructor(result) {
597
+ this.result = result;
598
+ }
599
+ result;
600
+ output = Option.none();
601
+ when(refinement, f) {
602
+ if (Option.isNone(this.output) && refinement(this.result)) {
603
+ const b = f(this.result);
604
+ if (Option.isSome(b)) this.output = b;
605
+ }
606
+ return this;
607
+ }
608
+ pipe() {
609
+ return pipeArguments(this, arguments);
610
+ }
611
+ onWaiting(f) {
612
+ return this.when((r) => r.waiting, (r) => Option.some(f(r)));
613
+ }
614
+ onInitialOrWaiting(f) {
615
+ return this.when((r) => isInitial(r) || r.waiting, (r) => Option.some(f(r)));
616
+ }
617
+ onInitial(f) {
618
+ return this.when(isInitial, (r) => Option.some(f(r)));
619
+ }
620
+ onSuccess(f) {
621
+ return this.when(isSuccess, (r) => Option.some(f(r.value, r)));
622
+ }
623
+ onFailure(f) {
624
+ return this.when(isFailure, (r) => Option.some(f(r.cause, r)));
625
+ }
626
+ onError(f) {
627
+ return this.onErrorIf(constTrue, f);
628
+ }
629
+ onErrorIf(refinement, f) {
630
+ return this.when(isFailure, (result) => Cause.findErrorOption(result.cause).pipe(Option.filter(refinement), Option.map((error) => f(error, result))));
631
+ }
632
+ onErrorTag(tag, f) {
633
+ return this.onErrorIf((e) => hasProperty(e, "_tag") && (Array.isArray(tag) ? tag.includes(e._tag) : e._tag === tag), f);
634
+ }
635
+ onDefect(f) {
636
+ return this.when(isFailure, (result) => {
637
+ const defect = Cause.findDefect(result.cause);
638
+ return Either.isFailure(defect) ? Option.none() : Option.some(f(defect.success, result));
639
+ });
640
+ }
641
+ onInterrupt(f) {
642
+ return this.when(isFailure, (result) => {
643
+ const interruptors = Cause.filterInterruptors(result.cause);
644
+ return Either.isFailure(interruptors) ? Option.none() : Option.some(f(interruptors.success, result));
645
+ });
646
+ }
647
+ orElse(orElse) {
648
+ return Option.getOrElse(this.output, orElse);
649
+ }
650
+ orNull() {
651
+ return Option.getOrNull(this.output);
652
+ }
653
+ render() {
654
+ if (Option.isSome(this.output)) return this.output.value;
655
+ else if (isFailure(this.result)) throw Cause.squash(this.result.cause);
656
+ return null;
657
+ }
658
+ exhaustive() {
659
+ return this.render();
660
+ }
661
+ };
662
+ //#endregion
663
+ export { Schema as A, matchWithWaiting as C, value as D, touch as E, isResult as F, success as I, TypeId as M, failure as N, waiting as O, initial as P, matchWithError as S, toExit as T, isNotInitial as _, error as a, map as b, failureWithPrevious as c, fromExitWithPrevious as d, getOrElse as f, isInterrupted as g, isInitial as h, cause as i, schemaCodec as j, waitingFrom as k, flatMap as l, isFailure as m, all as n, fail as o, getOrThrow as p, builder as r, failWithPrevious as s, Result_exports as t, fromExit as u, isSuccess as v, replacePrevious as w, match as x, isWaiting as y };