@nlozgachev/pipelined 0.28.0 → 0.30.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 +105 -102
- package/dist/{Task-BZT0wedE.d.mts → Task-BDcKwFAj.d.mts} +33 -20
- package/dist/{Task-BW66NsGR.d.ts → Task-CnF22Q2o.d.ts} +33 -20
- package/dist/{chunk-CA3VE4YD.mjs → chunk-FWYOEWJ2.mjs} +10 -2
- package/dist/{chunk-QJS6D6MW.mjs → chunk-PV7JOUKL.mjs} +117 -39
- package/dist/{chunk-7JF44HJH.mjs → chunk-SDGDJ7CU.mjs} +17 -16
- package/dist/core.d.mts +236 -66
- package/dist/core.d.ts +236 -66
- package/dist/core.js +133 -54
- package/dist/core.mjs +2 -2
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +142 -55
- package/dist/index.mjs +3 -3
- package/dist/utils.d.mts +17 -2
- package/dist/utils.d.ts +17 -2
- package/dist/utils.js +26 -17
- package/dist/utils.mjs +2 -2
- package/package.json +3 -3
package/dist/core.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { M as Maybe, W as WithValue, a as WithLog, D as Deferred, R as Result, b as WithKind, c as WithError, d as RetryOptions, e as TimeoutOptions, f as WithTimeout, g as WithMinInterval, h as WithCooldown, i as WithConcurrency, j as WithSize, k as WithMs, l as WithN, T as Task, m as WithErrors, n as WithFirst, o as WithSecond } from './Task-
|
|
2
|
-
export { E as
|
|
1
|
+
import { M as Maybe, W as WithValue, a as WithLog, D as Deferred, R as Result, b as WithKind, c as WithError, d as RetryOptions, e as TimeoutOptions, f as WithTimeout, g as WithMinInterval, h as WithCooldown, i as WithConcurrency, j as WithSize, k as WithMs, l as WithN, T as Task, m as WithErrors, n as WithFirst, o as WithSecond } from './Task-BDcKwFAj.mjs';
|
|
2
|
+
export { E as Error, N as None, O as Ok, S as Some } from './Task-BDcKwFAj.mjs';
|
|
3
3
|
import { N as NonEmptyList } from './NonEmptyList-BlGFjor5.mjs';
|
|
4
4
|
|
|
5
5
|
/** Keys of T for which undefined is assignable (i.e. optional fields). */
|
|
@@ -505,10 +505,10 @@ type InterpretResult<I, E, A, O> = [O] extends [{
|
|
|
505
505
|
*
|
|
506
506
|
* const manager = Op.interpret(fetchUser, { strategy: "restartable" });
|
|
507
507
|
* manager.subscribe(state => {
|
|
508
|
-
* if (state
|
|
509
|
-
* if (state
|
|
510
|
-
* if (state
|
|
511
|
-
* if (state
|
|
508
|
+
* if (Op.isPending(state)) showSpinner();
|
|
509
|
+
* if (Op.isOk(state)) render(state.value);
|
|
510
|
+
* if (Op.isError(state)) showError(state.error);
|
|
511
|
+
* if (Op.isNil(state)) resetUI();
|
|
512
512
|
* });
|
|
513
513
|
* manager.run(userId);
|
|
514
514
|
* ```
|
|
@@ -532,11 +532,11 @@ declare namespace Op {
|
|
|
532
532
|
* over a call that was already running; `"evicted"` — a newer `run()` took over
|
|
533
533
|
* a call that was waiting and had not yet started.
|
|
534
534
|
*/
|
|
535
|
-
type Outcome<E, A> = Ok<A> |
|
|
535
|
+
type Outcome<E, A> = Ok<A> | Error<E> | Nil;
|
|
536
536
|
/** A successful outcome with a value. */
|
|
537
|
-
type Ok<A> = WithKind<"
|
|
537
|
+
type Ok<A> = WithKind<"OpOk"> & WithValue<A>;
|
|
538
538
|
/** A failed outcome with a typed error. */
|
|
539
|
-
type
|
|
539
|
+
type Error<E> = WithKind<"OpError"> & WithError<E>;
|
|
540
540
|
/**
|
|
541
541
|
* An outcome that produced nothing. `reason` identifies why:
|
|
542
542
|
* - `"aborted"` — `abort()` was called explicitly.
|
|
@@ -545,7 +545,7 @@ declare namespace Op {
|
|
|
545
545
|
* - `"evicted"` — a newer invocation took a slot from a call that was waiting and
|
|
546
546
|
* had not yet started (buffered slot, debounce timer, throttle trailing slot).
|
|
547
547
|
*/
|
|
548
|
-
type Nil = WithKind<"
|
|
548
|
+
type Nil = WithKind<"OpNil"> & {
|
|
549
549
|
readonly reason: NilReason;
|
|
550
550
|
};
|
|
551
551
|
/** The reason a `Nil` outcome was produced. */
|
|
@@ -593,9 +593,9 @@ declare namespace Op {
|
|
|
593
593
|
* const manager = Op.interpret(saveConfig, { strategy: "exclusive" });
|
|
594
594
|
*
|
|
595
595
|
* manager.subscribe(state => {
|
|
596
|
-
* if (state
|
|
597
|
-
* if (state
|
|
598
|
-
* if (state
|
|
596
|
+
* if (Op.isPending(state)) lockForm();
|
|
597
|
+
* if (Op.isOk(state)) toast("Saved");
|
|
598
|
+
* if (Op.isError(state)) toast(`Error: ${state.error.message}`);
|
|
599
599
|
* });
|
|
600
600
|
*
|
|
601
601
|
* // Fire and subscribe (subscriber pattern)
|
|
@@ -626,6 +626,15 @@ declare namespace Op {
|
|
|
626
626
|
* The callback fires immediately with the current state if the manager is not idle.
|
|
627
627
|
*/
|
|
628
628
|
subscribe: (cb: (state: S) => void) => () => void;
|
|
629
|
+
/** Returns state to Idle. Does not cancel any in-flight operation. */
|
|
630
|
+
reset: () => void;
|
|
631
|
+
/**
|
|
632
|
+
* Runs the input immediately, then every `interval` milliseconds.
|
|
633
|
+
* Returns a stop handle — call it to cancel future runs.
|
|
634
|
+
*/
|
|
635
|
+
poll: (input: I, options: {
|
|
636
|
+
interval: number;
|
|
637
|
+
}) => () => void;
|
|
629
638
|
};
|
|
630
639
|
/**
|
|
631
640
|
* A stateful manager that maintains independent per-key execution slots.
|
|
@@ -643,8 +652,8 @@ declare namespace Op {
|
|
|
643
652
|
*
|
|
644
653
|
* getUser.subscribe((map) => {
|
|
645
654
|
* for (const [id, state] of map) {
|
|
646
|
-
* if (state
|
|
647
|
-
* if (state
|
|
655
|
+
* if (Op.isPending(state)) showSpinner(id);
|
|
656
|
+
* if (Op.isOk(state)) render(id, state.value);
|
|
648
657
|
* }
|
|
649
658
|
* });
|
|
650
659
|
*
|
|
@@ -668,63 +677,72 @@ declare namespace Op {
|
|
|
668
677
|
* Fires immediately with the current map if any key is active.
|
|
669
678
|
*/
|
|
670
679
|
subscribe: (cb: (state: ReadonlyMap<K, PerKeyS>) => void) => () => void;
|
|
680
|
+
/** Clears all per-key state and notifies subscribers. Does not cancel in-flight operations. */
|
|
681
|
+
reset: () => void;
|
|
682
|
+
/**
|
|
683
|
+
* Runs the input immediately, then every `interval` milliseconds.
|
|
684
|
+
* Returns a stop handle — call it to cancel future runs.
|
|
685
|
+
*/
|
|
686
|
+
poll: (input: I, options: {
|
|
687
|
+
interval: number;
|
|
688
|
+
}) => () => void;
|
|
671
689
|
};
|
|
672
690
|
/** States reachable by a `once` manager (no retry). */
|
|
673
|
-
type OnceState<E, A> = Idle | Pending | Ok<A> |
|
|
691
|
+
type OnceState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
674
692
|
/** States reachable by a `once` manager with retry configured. */
|
|
675
|
-
type RetryableOnceState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
693
|
+
type RetryableOnceState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
676
694
|
/** States reachable by a `restartable` manager (no retry). */
|
|
677
|
-
type RestartableState<E, A> = Idle | Pending | Ok<A> |
|
|
695
|
+
type RestartableState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | ReplacedNil;
|
|
678
696
|
/** States reachable by a `restartable` manager with retry configured. */
|
|
679
|
-
type RetryableRestartableState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
697
|
+
type RetryableRestartableState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | ReplacedNil;
|
|
680
698
|
/** States reachable by an `exclusive` manager (no retry). */
|
|
681
|
-
type ExclusiveState<E, A> = Idle | Pending | Ok<A> |
|
|
699
|
+
type ExclusiveState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
682
700
|
/** States reachable by an `exclusive` manager with retry configured. */
|
|
683
|
-
type RetryableExclusiveState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
701
|
+
type RetryableExclusiveState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
684
702
|
/** States reachable by a `queue` manager (no retry, no overflow, no dedupe). */
|
|
685
|
-
type QueueState<E, A> = Idle | Pending | Queued | Ok<A> |
|
|
703
|
+
type QueueState<E, A> = Idle | Pending | Queued | Ok<A> | Error<E> | AbortedNil;
|
|
686
704
|
/** States reachable by a `queue` manager with retry (no overflow, no dedupe). */
|
|
687
|
-
type RetryableQueueState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> |
|
|
705
|
+
type RetryableQueueState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Error<E> | AbortedNil;
|
|
688
706
|
/** States reachable by a `queue` manager with `overflow:"drop"` or `dedupe` (no retry). */
|
|
689
|
-
type QueueDropState<E, A> = Idle | Pending | Queued | Ok<A> |
|
|
707
|
+
type QueueDropState<E, A> = Idle | Pending | Queued | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
690
708
|
/** States reachable by a `queue` manager with `overflow:"drop"` or `dedupe`, with retry. */
|
|
691
|
-
type RetryableQueueDropState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> |
|
|
709
|
+
type RetryableQueueDropState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
692
710
|
/** States reachable by a `queue` manager with `overflow:"replace-last"` and no `dedupe` (no retry). */
|
|
693
|
-
type QueueReplaceState<E, A> = Idle | Pending | Queued | Ok<A> |
|
|
711
|
+
type QueueReplaceState<E, A> = Idle | Pending | Queued | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
694
712
|
/** States reachable by a `queue` manager with `overflow:"replace-last"` and no `dedupe`, with retry. */
|
|
695
|
-
type RetryableQueueReplaceState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> |
|
|
713
|
+
type RetryableQueueReplaceState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
696
714
|
/** States reachable by a `queue` manager with `overflow:"replace-last"` AND `dedupe` (no retry). */
|
|
697
|
-
type QueueDropAndReplaceState<E, A> = Idle | Pending | Queued | Ok<A> |
|
|
715
|
+
type QueueDropAndReplaceState<E, A> = Idle | Pending | Queued | Ok<A> | Error<E> | AbortedNil | DroppedNil | EvictedNil;
|
|
698
716
|
/** States reachable by a `queue` manager with `overflow:"replace-last"` AND `dedupe`, with retry. */
|
|
699
|
-
type RetryableQueueDropAndReplaceState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> |
|
|
717
|
+
type RetryableQueueDropAndReplaceState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Error<E> | AbortedNil | DroppedNil | EvictedNil;
|
|
700
718
|
/** States reachable by a `buffered` manager (no retry). */
|
|
701
|
-
type BufferedState<E, A> = Idle | Pending | Queued | Ok<A> |
|
|
719
|
+
type BufferedState<E, A> = Idle | Pending | Queued | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
702
720
|
/** States reachable by a `buffered` manager with retry configured. */
|
|
703
|
-
type RetryableBufferedState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> |
|
|
721
|
+
type RetryableBufferedState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
704
722
|
/** States reachable by a `debounced` manager (no retry). */
|
|
705
|
-
type DebouncedState<E, A> = Idle | Pending | Ok<A> |
|
|
723
|
+
type DebouncedState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
706
724
|
/** States reachable by a `debounced` manager with retry configured. */
|
|
707
|
-
type RetryableDebouncedState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
725
|
+
type RetryableDebouncedState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
708
726
|
/** States reachable by a `throttled` manager (leading-only, no retry). */
|
|
709
|
-
type ThrottledState<E, A> = Idle | Pending | Ok<A> |
|
|
727
|
+
type ThrottledState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
710
728
|
/** States reachable by a `throttled` manager (leading-only, with retry). */
|
|
711
|
-
type RetryableThrottledState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
729
|
+
type RetryableThrottledState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
712
730
|
/** States reachable by a `throttled` manager with `trailing: true` (no retry). */
|
|
713
|
-
type ThrottledTrailingState<E, A> = Idle | Pending | Ok<A> |
|
|
731
|
+
type ThrottledTrailingState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
714
732
|
/** States reachable by a `throttled` manager with `trailing: true` and retry. */
|
|
715
|
-
type RetryableThrottledTrailingState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
733
|
+
type RetryableThrottledTrailingState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | EvictedNil;
|
|
716
734
|
/** States reachable by a `concurrent` manager with `overflow: "queue"` (no retry). */
|
|
717
|
-
type ConcurrentQueueState<E, A> = Idle | Pending | Queued | Ok<A> |
|
|
735
|
+
type ConcurrentQueueState<E, A> = Idle | Pending | Queued | Ok<A> | Error<E> | AbortedNil;
|
|
718
736
|
/** States reachable by a `concurrent` manager with `overflow: "queue"` and retry. */
|
|
719
|
-
type RetryableConcurrentQueueState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> |
|
|
737
|
+
type RetryableConcurrentQueueState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Error<E> | AbortedNil;
|
|
720
738
|
/** States reachable by a `concurrent` manager with `overflow: "drop"` (no retry). */
|
|
721
|
-
type ConcurrentDropState<E, A> = Idle | Pending | Ok<A> |
|
|
739
|
+
type ConcurrentDropState<E, A> = Idle | Pending | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
722
740
|
/** States reachable by a `concurrent` manager with `overflow: "drop"` and retry. */
|
|
723
|
-
type RetryableConcurrentDropState<E, A> = Idle | Pending | Retrying<E> | Ok<A> |
|
|
741
|
+
type RetryableConcurrentDropState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
724
742
|
/** Per-key state union for a `keyed` manager with `perKey: "exclusive"`. */
|
|
725
|
-
type KeyedExclusivePerKey<E, A> = Pending | Ok<A> |
|
|
743
|
+
type KeyedExclusivePerKey<E, A> = Pending | Ok<A> | Error<E> | AbortedNil | DroppedNil;
|
|
726
744
|
/** Per-key state union for a `keyed` manager with `perKey: "restartable"`. */
|
|
727
|
-
type KeyedRestartablePerKey<E, A> = Pending | Ok<A> |
|
|
745
|
+
type KeyedRestartablePerKey<E, A> = Pending | Ok<A> | Error<E> | AbortedNil | ReplacedNil;
|
|
728
746
|
type RetryOptions<E> = RetryOptions<E>;
|
|
729
747
|
type TimeoutOptions<E> = TimeoutOptions<E>;
|
|
730
748
|
/**
|
|
@@ -732,10 +750,10 @@ declare namespace Op {
|
|
|
732
750
|
*
|
|
733
751
|
* @example
|
|
734
752
|
* ```ts
|
|
735
|
-
* Op.nil("aborted"); // { kind: "
|
|
736
|
-
* Op.nil("dropped"); // { kind: "
|
|
737
|
-
* Op.nil("replaced"); // { kind: "
|
|
738
|
-
* Op.nil("evicted"); // { kind: "
|
|
753
|
+
* Op.nil("aborted"); // { kind: "OpNil", reason: "aborted" }
|
|
754
|
+
* Op.nil("dropped"); // { kind: "OpNil", reason: "dropped" }
|
|
755
|
+
* Op.nil("replaced"); // { kind: "OpNil", reason: "replaced" }
|
|
756
|
+
* Op.nil("evicted"); // { kind: "OpNil", reason: "evicted" }
|
|
739
757
|
* ```
|
|
740
758
|
*/
|
|
741
759
|
const nil: (reason: NilReason) => Nil;
|
|
@@ -778,12 +796,29 @@ declare namespace Op {
|
|
|
778
796
|
* ```
|
|
779
797
|
*/
|
|
780
798
|
const create: <E, A, I = void>(factory: (signal: AbortSignal) => (input: I) => Promise<A>, onError: (e: unknown) => E) => Op<I, E, A>;
|
|
799
|
+
/**
|
|
800
|
+
* Lifts a plain async function into an Op, treating all errors as `unknown`.
|
|
801
|
+
*
|
|
802
|
+
* Use this when you have a simple async function that doesn't need custom error mapping.
|
|
803
|
+
* The signal is passed so the operation can respect cancellation.
|
|
804
|
+
*
|
|
805
|
+
* @example
|
|
806
|
+
* ```ts
|
|
807
|
+
* const fetchUser = Op.lift((id: number, signal: AbortSignal) =>
|
|
808
|
+
* fetch(`/api/users/${id}`, { signal }).then(r => r.json())
|
|
809
|
+
* );
|
|
810
|
+
* const manager = Op.interpret(fetchUser, { strategy: "restartable" });
|
|
811
|
+
* const outcome = await manager.run(42);
|
|
812
|
+
* if (Op.isOk(outcome)) console.log(outcome.value);
|
|
813
|
+
* ```
|
|
814
|
+
*/
|
|
815
|
+
const lift: <I, A>(f: (input: I, signal: AbortSignal) => Promise<A>) => Op<I, unknown, A>;
|
|
781
816
|
/**
|
|
782
817
|
* Creates a successful Outcome.
|
|
783
818
|
*
|
|
784
819
|
* @example
|
|
785
820
|
* ```ts
|
|
786
|
-
* Op.ok(42); // { kind: "
|
|
821
|
+
* Op.ok(42); // { kind: "OpOk", value: 42 }
|
|
787
822
|
* ```
|
|
788
823
|
*/
|
|
789
824
|
const ok: <A>(value: A) => Ok<A>;
|
|
@@ -792,37 +827,87 @@ declare namespace Op {
|
|
|
792
827
|
*
|
|
793
828
|
* @example
|
|
794
829
|
* ```ts
|
|
795
|
-
* Op.
|
|
830
|
+
* Op.error(new ApiError("not found")); // { kind: "OpError", error: ApiError }
|
|
831
|
+
* ```
|
|
832
|
+
*/
|
|
833
|
+
const error: <E>(error: E) => Error<E>;
|
|
834
|
+
/**
|
|
835
|
+
* Returns `true` if the state is `Idle`.
|
|
836
|
+
*
|
|
837
|
+
* @example
|
|
838
|
+
* ```ts
|
|
839
|
+
* manager.subscribe(state => {
|
|
840
|
+
* if (Op.isIdle(state)) hideSpinner();
|
|
841
|
+
* });
|
|
842
|
+
* ```
|
|
843
|
+
*/
|
|
844
|
+
const isIdle: <E, A>(state: State<E, A>) => state is Idle;
|
|
845
|
+
/**
|
|
846
|
+
* Returns `true` if the state is `Pending` (an operation is in-flight).
|
|
847
|
+
*
|
|
848
|
+
* @example
|
|
849
|
+
* ```ts
|
|
850
|
+
* manager.subscribe(state => {
|
|
851
|
+
* if (Op.isPending(state)) showSpinner();
|
|
852
|
+
* });
|
|
796
853
|
* ```
|
|
797
854
|
*/
|
|
798
|
-
const
|
|
855
|
+
const isPending: <E, A>(state: State<E, A>) => state is Pending;
|
|
799
856
|
/**
|
|
800
|
-
* Returns `true` if the
|
|
857
|
+
* Returns `true` if the state is `Queued` (an invocation is waiting to run).
|
|
801
858
|
*
|
|
802
859
|
* @example
|
|
803
860
|
* ```ts
|
|
804
|
-
*
|
|
861
|
+
* manager.subscribe(state => {
|
|
862
|
+
* if (Op.isQueued(state)) showQueuePosition(state.position);
|
|
863
|
+
* });
|
|
864
|
+
* ```
|
|
865
|
+
*/
|
|
866
|
+
const isQueued: <E, A>(state: State<E, A>) => state is Queued;
|
|
867
|
+
/**
|
|
868
|
+
* Returns `true` if the state is `Retrying`.
|
|
869
|
+
*
|
|
870
|
+
* @example
|
|
871
|
+
* ```ts
|
|
872
|
+
* manager.subscribe(state => {
|
|
873
|
+
* if (Op.isRetrying(state)) showRetryBadge(state.attempt);
|
|
874
|
+
* });
|
|
875
|
+
* ```
|
|
876
|
+
*/
|
|
877
|
+
const isRetrying: <E, A>(state: State<E, A>) => state is Retrying<E>;
|
|
878
|
+
/**
|
|
879
|
+
* Returns `true` if the state is `Ok` (the operation produced a value).
|
|
880
|
+
*
|
|
881
|
+
* @example
|
|
882
|
+
* ```ts
|
|
883
|
+
* manager.subscribe(state => {
|
|
884
|
+
* if (Op.isOk(state)) render(state.value);
|
|
885
|
+
* });
|
|
805
886
|
* ```
|
|
806
887
|
*/
|
|
807
|
-
const isOk: <E, A>(
|
|
888
|
+
const isOk: <E, A>(state: State<E, A>) => state is Ok<A>;
|
|
808
889
|
/**
|
|
809
|
-
* Returns `true` if the
|
|
890
|
+
* Returns `true` if the state is `Error` (the operation failed with a typed error).
|
|
810
891
|
*
|
|
811
892
|
* @example
|
|
812
893
|
* ```ts
|
|
813
|
-
*
|
|
894
|
+
* manager.subscribe(state => {
|
|
895
|
+
* if (Op.isError(state)) showError(state.error);
|
|
896
|
+
* });
|
|
814
897
|
* ```
|
|
815
898
|
*/
|
|
816
|
-
const
|
|
899
|
+
const isError: <E, A>(state: State<E, A>) => state is Error<E>;
|
|
817
900
|
/**
|
|
818
|
-
* Returns `true` if the
|
|
901
|
+
* Returns `true` if the state is `Nil` (the operation completed without a value or error).
|
|
819
902
|
*
|
|
820
903
|
* @example
|
|
821
904
|
* ```ts
|
|
822
|
-
*
|
|
905
|
+
* manager.subscribe(state => {
|
|
906
|
+
* if (Op.isNil(state)) resetUI();
|
|
907
|
+
* });
|
|
823
908
|
* ```
|
|
824
909
|
*/
|
|
825
|
-
const isNil: <E, A>(
|
|
910
|
+
const isNil: <E, A>(state: State<E, A>) => state is Nil;
|
|
826
911
|
/**
|
|
827
912
|
* Pattern matches on an Outcome using named case handlers.
|
|
828
913
|
*
|
|
@@ -830,19 +915,19 @@ declare namespace Op {
|
|
|
830
915
|
* ```ts
|
|
831
916
|
* Op.match({
|
|
832
917
|
* ok: (user) => render(user),
|
|
833
|
-
*
|
|
918
|
+
* error: (e) => showError(e.message),
|
|
834
919
|
* nil: () => resetUI(),
|
|
835
920
|
* })(outcome);
|
|
836
921
|
* ```
|
|
837
922
|
*/
|
|
838
923
|
const match: <E, A, B>(cases: {
|
|
839
924
|
ok: (a: A) => B;
|
|
840
|
-
|
|
925
|
+
error: (e: E) => B;
|
|
841
926
|
nil: () => B;
|
|
842
927
|
}) => (outcome: Outcome<E, A>) => B;
|
|
843
928
|
/**
|
|
844
929
|
* Eliminates an Outcome with positional handlers.
|
|
845
|
-
* Order: `
|
|
930
|
+
* Order: `onError`, `onOk`, `onNil` — mirrors `Result.fold` for the first two.
|
|
846
931
|
*
|
|
847
932
|
* @example
|
|
848
933
|
* ```ts
|
|
@@ -853,7 +938,7 @@ declare namespace Op {
|
|
|
853
938
|
* )(outcome);
|
|
854
939
|
* ```
|
|
855
940
|
*/
|
|
856
|
-
const fold: <E, A, B>(
|
|
941
|
+
const fold: <E, A, B>(onError: (e: E) => B, onOk: (a: A) => B, onNil: () => B) => (outcome: Outcome<E, A>) => B;
|
|
857
942
|
/**
|
|
858
943
|
* Returns the success value, or the result of `defaultValue()` for `Err` or `Nil`.
|
|
859
944
|
*
|
|
@@ -888,7 +973,7 @@ declare namespace Op {
|
|
|
888
973
|
* ```ts
|
|
889
974
|
* pipe(
|
|
890
975
|
* outcome,
|
|
891
|
-
* Op.chain(user => user.active ? Op.ok(user) : Op.
|
|
976
|
+
* Op.chain(user => user.active ? Op.ok(user) : Op.error(new Error("inactive"))),
|
|
892
977
|
* );
|
|
893
978
|
* ```
|
|
894
979
|
*/
|
|
@@ -909,7 +994,7 @@ declare namespace Op {
|
|
|
909
994
|
* ```ts
|
|
910
995
|
* pipe(
|
|
911
996
|
* outcome,
|
|
912
|
-
* Op.recover(e => e.isRetryable ? Op.ok(cachedValue) : Op.
|
|
997
|
+
* Op.recover(e => e.isRetryable ? Op.ok(cachedValue) : Op.error(e)),
|
|
913
998
|
* );
|
|
914
999
|
* ```
|
|
915
1000
|
*/
|
|
@@ -952,6 +1037,37 @@ declare namespace Op {
|
|
|
952
1037
|
* ```
|
|
953
1038
|
*/
|
|
954
1039
|
const race: <E, A>(invocations: ReadonlyArray<Deferred<Outcome<E, A>>>) => Deferred<Outcome<E, A>>;
|
|
1040
|
+
/**
|
|
1041
|
+
* Subscribes to a manager and calls a handler when the state reaches `OpOk`.
|
|
1042
|
+
* Returns an unsubscribe function.
|
|
1043
|
+
*
|
|
1044
|
+
* @example
|
|
1045
|
+
* ```ts
|
|
1046
|
+
* const manager = Op.interpret(fetchUser, { strategy: "restartable" });
|
|
1047
|
+
* const stop = Op.wire(manager, (user) => {
|
|
1048
|
+
* console.log("User loaded:", user.name);
|
|
1049
|
+
* });
|
|
1050
|
+
* manager.run(userId);
|
|
1051
|
+
* // ... later
|
|
1052
|
+
* stop(); // removes the subscription
|
|
1053
|
+
* ```
|
|
1054
|
+
*/
|
|
1055
|
+
const wire: <I, E, A, S extends State<E, A>>(source: Manager<I, E, A, S>, f: (a: A) => void) => () => void;
|
|
1056
|
+
/**
|
|
1057
|
+
* Wires multiple source-handler pairs, returning a combined cleanup function.
|
|
1058
|
+
* When any source reaches `OpOk`, its corresponding handler is called.
|
|
1059
|
+
*
|
|
1060
|
+
* @example
|
|
1061
|
+
* ```ts
|
|
1062
|
+
* const stop = Op.wireAll(
|
|
1063
|
+
* [userManager, (u) => render(u)],
|
|
1064
|
+
* [settingsManager, (s) => applySettings(s)],
|
|
1065
|
+
* );
|
|
1066
|
+
* // ... later
|
|
1067
|
+
* stop(); // removes all subscriptions
|
|
1068
|
+
* ```
|
|
1069
|
+
*/
|
|
1070
|
+
const wireAll: <I, E, A, S extends State<E, A>>(...pairs: Array<[Manager<I, E, A, S>, (a: A) => void]>) => () => void;
|
|
955
1071
|
/**
|
|
956
1072
|
* Attaches a concurrency strategy to an `Op`, returning a `Manager`.
|
|
957
1073
|
*
|
|
@@ -979,7 +1095,7 @@ declare namespace Op {
|
|
|
979
1095
|
* const getUser = Op.interpret(fetchUser, { strategy: "once" });
|
|
980
1096
|
* getUser.subscribe(state => {
|
|
981
1097
|
* if (state.kind === "Pending") showSpinner();
|
|
982
|
-
* if (state.kind === "
|
|
1098
|
+
* if (state.kind === "OpOk") render(state.value);
|
|
983
1099
|
* });
|
|
984
1100
|
* getUser.run(userId);
|
|
985
1101
|
*
|
|
@@ -1680,6 +1796,20 @@ declare namespace RemoteData {
|
|
|
1680
1796
|
* ```
|
|
1681
1797
|
*/
|
|
1682
1798
|
const fromMaybe: <E>(onNone: () => E) => <A>(data: Maybe<A>) => RemoteData<E, A>;
|
|
1799
|
+
/**
|
|
1800
|
+
* Filters a `Success` value. When the predicate passes, the value is kept. When it fails,
|
|
1801
|
+
* `Success` becomes `Failure` using the error produced by `onFalse`. All other states pass through unchanged.
|
|
1802
|
+
*
|
|
1803
|
+
* @example
|
|
1804
|
+
* ```ts
|
|
1805
|
+
* RemoteData.filter(n => n > 0, n => `${n} is not a valid price`)(RemoteData.success(9.99));
|
|
1806
|
+
* // Success(9.99)
|
|
1807
|
+
* RemoteData.filter(n => n > 0, n => `${n} is not a valid price`)(RemoteData.success(-1));
|
|
1808
|
+
* // Failure("-1 is not a valid price")
|
|
1809
|
+
* RemoteData.filter(n => n > 0, () => "error")(RemoteData.loading()); // Loading
|
|
1810
|
+
* ```
|
|
1811
|
+
*/
|
|
1812
|
+
const filter: <E, A>(pred: (a: A) => boolean, onFalse: (a: A) => E) => (data: RemoteData<E, A>) => RemoteData<E, A>;
|
|
1683
1813
|
}
|
|
1684
1814
|
|
|
1685
1815
|
/**
|
|
@@ -1773,6 +1903,22 @@ declare namespace TaskResult {
|
|
|
1773
1903
|
* ```
|
|
1774
1904
|
*/
|
|
1775
1905
|
const tapError: <E, A>(f: (e: E) => void) => (data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1906
|
+
/**
|
|
1907
|
+
* Executes a `TaskResult` with an optional signal, returning `Promise<Result<E, A>>`.
|
|
1908
|
+
* Use as a terminal step in a `pipe` chain.
|
|
1909
|
+
*
|
|
1910
|
+
* @example
|
|
1911
|
+
* ```ts
|
|
1912
|
+
* const controller = new AbortController();
|
|
1913
|
+
* const result = await pipe(
|
|
1914
|
+
* fetchUser("42"),
|
|
1915
|
+
* TaskResult.chain(user => fetchPosts(user.id)),
|
|
1916
|
+
* TaskResult.run(controller.signal),
|
|
1917
|
+
* );
|
|
1918
|
+
* if (Result.isOk(result)) render(result.value);
|
|
1919
|
+
* ```
|
|
1920
|
+
*/
|
|
1921
|
+
const run: (signal?: AbortSignal) => <E, A>(task: TaskResult<E, A>) => Promise<Result<E, A>>;
|
|
1776
1922
|
}
|
|
1777
1923
|
|
|
1778
1924
|
/**
|
|
@@ -2393,6 +2539,30 @@ declare namespace Validation {
|
|
|
2393
2539
|
* ```
|
|
2394
2540
|
*/
|
|
2395
2541
|
const toResult: <E, A>(data: Validation<E, A>) => Result<NonEmptyList<E>, A>;
|
|
2542
|
+
/**
|
|
2543
|
+
* Converts a Validation to a Maybe. `Valid` becomes `Some`; `Invalid` becomes `None`
|
|
2544
|
+
* (errors are discarded).
|
|
2545
|
+
*
|
|
2546
|
+
* @example
|
|
2547
|
+
* ```ts
|
|
2548
|
+
* Validation.toMaybe(Validation.valid(42)); // Some(42)
|
|
2549
|
+
* Validation.toMaybe(Validation.invalid("bad")); // None
|
|
2550
|
+
* ```
|
|
2551
|
+
*/
|
|
2552
|
+
const toMaybe: <E, A>(data: Validation<E, A>) => Maybe<A>;
|
|
2553
|
+
/**
|
|
2554
|
+
* Converts a `Result` to a `Validation`. `Ok` becomes `Valid`; `Err(e)` becomes `Invalid([e])`.
|
|
2555
|
+
*
|
|
2556
|
+
* Useful when bridging from error-short-circuiting `Result` pipelines into
|
|
2557
|
+
* error-accumulating `Validation` pipelines.
|
|
2558
|
+
*
|
|
2559
|
+
* @example
|
|
2560
|
+
* ```ts
|
|
2561
|
+
* Validation.fromResult(Result.ok(42)); // Valid(42)
|
|
2562
|
+
* Validation.fromResult(Result.error("bad")); // Invalid(["bad"])
|
|
2563
|
+
* ```
|
|
2564
|
+
*/
|
|
2565
|
+
const fromResult: <E, A>(data: Result<E, A>) => Validation<E, A>;
|
|
2396
2566
|
/**
|
|
2397
2567
|
* Combines two independent Validation instances into a tuple.
|
|
2398
2568
|
* If both are Valid, returns Valid with both values as a tuple.
|