@nlozgachev/pipelined 0.62.0 → 0.64.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 +14 -14
- package/dist/{Duration-B8joKzro.d.cts → Duration-DeyxG6VQ.d.cts} +20 -20
- package/dist/{Duration-B8joKzro.d.ts → Duration-DeyxG6VQ.d.ts} +20 -20
- package/dist/{InternalTypes-LdhLQx3N.d.ts → InternalTypes-CCXa8Kvr.d.ts} +11 -11
- package/dist/{InternalTypes-DuK_XpTi.d.cts → InternalTypes-GFn4RTwD.d.cts} +11 -11
- package/dist/{Validation-DZLizBZ0.d.ts → Task-9SJCMtG4.d.ts} +690 -1395
- package/dist/{Validation-DC3uUizM.d.cts → Task-C_goFYQ1.d.cts} +690 -1395
- package/dist/composition.cjs +216 -95
- package/dist/composition.d.cts +8 -76
- package/dist/composition.d.ts +8 -76
- package/dist/composition.mjs +216 -95
- package/dist/core.cjs +4905 -1028
- package/dist/core.d.cts +371 -773
- package/dist/core.d.ts +371 -773
- package/dist/core.mjs +4905 -1028
- package/dist/data.cjs +3313 -1242
- package/dist/data.d.cts +676 -1823
- package/dist/data.d.ts +676 -1823
- package/dist/data.mjs +3309 -1239
- package/dist/index.cjs +7270 -2216
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +7269 -2216
- package/dist/types.cjs +175 -31
- package/dist/types.d.cts +6 -6
- package/dist/types.d.ts +6 -6
- package/dist/types.mjs +175 -31
- package/package.json +24 -13
package/dist/core.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { M as Maybe, R as Result, T as Task } from './
|
|
2
|
-
export { E as Equality, a as Err, F as Failed, N as None, O as Ok, b as Ordering, P as Passed, S as Some, V as Validation } from './
|
|
3
|
-
import { o as WithValue, i as WithLog, D as Deferred,
|
|
4
|
-
import { D as Duration } from './Duration-
|
|
1
|
+
import { M as Maybe, R as Result, T as Task } from './Task-C_goFYQ1.cjs';
|
|
2
|
+
export { E as Equality, a as Err, F as Failed, N as None, O as Ok, b as Ordering, P as Passed, S as Some, V as Validation } from './Task-C_goFYQ1.cjs';
|
|
3
|
+
import { o as WithValue, i as WithLog, D as Deferred, R as RetryOptions, n as WithTimeout, j as WithMinInterval, c as WithCooldown, W as WithConcurrency, m as WithSize, d as WithDuration, k as WithN, h as WithKind, e as WithError, b as TimeoutOptions, g as WithFirst, l as WithSecond } from './InternalTypes-GFn4RTwD.cjs';
|
|
4
|
+
import { D as Duration } from './Duration-DeyxG6VQ.cjs';
|
|
5
5
|
import './types.cjs';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -19,7 +19,7 @@ type Combinable<A> = {
|
|
|
19
19
|
readonly empty: A;
|
|
20
20
|
readonly combine: (b: A) => (a: A) => A;
|
|
21
21
|
};
|
|
22
|
-
declare
|
|
22
|
+
declare const Combinable: {
|
|
23
23
|
/**
|
|
24
24
|
* Combines strings by concatenation. Empty string is the neutral element.
|
|
25
25
|
*
|
|
@@ -28,7 +28,7 @@ declare namespace Combinable {
|
|
|
28
28
|
* pipe(["a", "b", "c"], Combinable.fold(Combinable.string)); // "abc"
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
string: Combinable<string>;
|
|
32
32
|
/**
|
|
33
33
|
* Combines numbers by addition. `0` is the neutral element.
|
|
34
34
|
*
|
|
@@ -37,7 +37,7 @@ declare namespace Combinable {
|
|
|
37
37
|
* pipe([1, 2, 3], Combinable.fold(Combinable.sum)); // 6
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
|
-
|
|
40
|
+
sum: Combinable<number>;
|
|
41
41
|
/**
|
|
42
42
|
* Combines numbers by multiplication. `1` is the neutral element.
|
|
43
43
|
*
|
|
@@ -46,7 +46,7 @@ declare namespace Combinable {
|
|
|
46
46
|
* pipe([2, 3, 4], Combinable.fold(Combinable.product)); // 24
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
product: Combinable<number>;
|
|
50
50
|
/**
|
|
51
51
|
* Combines booleans with logical AND. `true` is the neutral element.
|
|
52
52
|
*
|
|
@@ -55,7 +55,7 @@ declare namespace Combinable {
|
|
|
55
55
|
* pipe([true, true, false], Combinable.fold(Combinable.all)); // false
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
all: Combinable<boolean>;
|
|
59
59
|
/**
|
|
60
60
|
* Combines booleans with logical OR. `false` is the neutral element.
|
|
61
61
|
*
|
|
@@ -64,7 +64,7 @@ declare namespace Combinable {
|
|
|
64
64
|
* pipe([false, false, true], Combinable.fold(Combinable.any)); // true
|
|
65
65
|
* ```
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
any: Combinable<boolean>;
|
|
68
68
|
/**
|
|
69
69
|
* Combines arrays by concatenation. Empty array is the neutral element.
|
|
70
70
|
*
|
|
@@ -73,7 +73,7 @@ declare namespace Combinable {
|
|
|
73
73
|
* pipe([[1, 2], [3], [4, 5]], Combinable.fold(Combinable.array<number>())); // [1, 2, 3, 4, 5]
|
|
74
74
|
* ```
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
array: <A>() => Combinable<readonly A[]>;
|
|
77
77
|
/**
|
|
78
78
|
* Lifts a `Combinable<A>` to `Combinable<Maybe<A>>`. `None` is the neutral element —
|
|
79
79
|
* combining with `None` on either side returns the other value unchanged.
|
|
@@ -86,7 +86,7 @@ declare namespace Combinable {
|
|
|
86
86
|
* c.combine(Maybe.make.none())(Maybe.make.some(5)); // Some(5)
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
|
-
|
|
89
|
+
maybe: <A>(inner: Combinable<A>) => Combinable<Maybe<A>>;
|
|
90
90
|
/**
|
|
91
91
|
* Folds an array into a single value using the `Combinable`'s `empty` as the starting point.
|
|
92
92
|
*
|
|
@@ -96,7 +96,7 @@ declare namespace Combinable {
|
|
|
96
96
|
* pipe([], Combinable.fold(Combinable.sum)); // 0
|
|
97
97
|
* ```
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
fold: <A>(c: Combinable<A>) => (data: readonly A[]) => A;
|
|
100
100
|
/**
|
|
101
101
|
* Derives a `Combinable` for a record of fields from field-level `Combinable` instances.
|
|
102
102
|
*
|
|
@@ -108,8 +108,8 @@ declare namespace Combinable {
|
|
|
108
108
|
* });
|
|
109
109
|
* ```
|
|
110
110
|
*/
|
|
111
|
-
|
|
112
|
-
}
|
|
111
|
+
struct: <R extends Record<string, unknown>>(fields: { [K in keyof R]: Combinable<R[K]>; }) => Combinable<R>;
|
|
112
|
+
};
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* A synchronous memoized computation. The factory function runs exactly once —
|
|
@@ -129,7 +129,7 @@ declare namespace Combinable {
|
|
|
129
129
|
type Lazy<A> = {
|
|
130
130
|
readonly get: () => A;
|
|
131
131
|
};
|
|
132
|
-
declare
|
|
132
|
+
declare const Lazy: {
|
|
133
133
|
/**
|
|
134
134
|
* Wraps a thunk in a `Lazy`. The thunk runs exactly once, on first `evaluate`.
|
|
135
135
|
*
|
|
@@ -138,7 +138,7 @@ declare namespace Lazy {
|
|
|
138
138
|
* const expensive = Lazy.from(() => computeExpensiveValue(input));
|
|
139
139
|
* ```
|
|
140
140
|
*/
|
|
141
|
-
|
|
141
|
+
from: <A>(f: () => A) => Lazy<A>;
|
|
142
142
|
/**
|
|
143
143
|
* Forces evaluation and returns the cached result. Safe to call multiple times.
|
|
144
144
|
*
|
|
@@ -147,7 +147,7 @@ declare namespace Lazy {
|
|
|
147
147
|
* const value = Lazy.evaluate(Lazy.from(() => 42)); // 42
|
|
148
148
|
* ```
|
|
149
149
|
*/
|
|
150
|
-
|
|
150
|
+
evaluate: <A>(lazy: Lazy<A>) => A;
|
|
151
151
|
/**
|
|
152
152
|
* Transforms the result of a `Lazy` without triggering evaluation.
|
|
153
153
|
*
|
|
@@ -156,7 +156,7 @@ declare namespace Lazy {
|
|
|
156
156
|
* pipe(Lazy.from(() => loadConfig()), Lazy.map(cfg => cfg.port));
|
|
157
157
|
* ```
|
|
158
158
|
*/
|
|
159
|
-
|
|
159
|
+
map: <A, B>(f: (a: A) => B) => (lazy: Lazy<A>) => Lazy<B>;
|
|
160
160
|
/**
|
|
161
161
|
* Chains a `Lazy`-returning transformation without triggering evaluation.
|
|
162
162
|
*
|
|
@@ -168,7 +168,7 @@ declare namespace Lazy {
|
|
|
168
168
|
* );
|
|
169
169
|
* ```
|
|
170
170
|
*/
|
|
171
|
-
|
|
171
|
+
chain: <A, B>(f: (a: A) => Lazy<B>) => (lazy: Lazy<A>) => Lazy<B>;
|
|
172
172
|
/**
|
|
173
173
|
* Runs a side effect on the value without changing it. Fires once, on first `evaluate`.
|
|
174
174
|
*
|
|
@@ -177,8 +177,8 @@ declare namespace Lazy {
|
|
|
177
177
|
* pipe(Lazy.from(() => compute()), Lazy.tap(v => console.log("computed:", v)));
|
|
178
178
|
* ```
|
|
179
179
|
*/
|
|
180
|
-
|
|
181
|
-
}
|
|
180
|
+
tap: <A>(f: (a: A) => void) => (lazy: Lazy<A>) => Lazy<A>;
|
|
181
|
+
};
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
184
|
* Lens<S, A> focuses on a single value A inside a structure S, providing
|
|
@@ -205,8 +205,8 @@ type Lens<S, A> = {
|
|
|
205
205
|
readonly get: (s: S) => A;
|
|
206
206
|
readonly set: (a: A) => (s: S) => S;
|
|
207
207
|
};
|
|
208
|
-
declare
|
|
209
|
-
|
|
208
|
+
declare const Lens: {
|
|
209
|
+
from: {
|
|
210
210
|
/**
|
|
211
211
|
* Constructs a Lens from a getter and a setter.
|
|
212
212
|
*
|
|
@@ -218,7 +218,7 @@ declare namespace Lens {
|
|
|
218
218
|
* );
|
|
219
219
|
* ```
|
|
220
220
|
*/
|
|
221
|
-
|
|
221
|
+
accessors: <S, A>(get: (s: S) => A, set: (a: A) => (s: S) => S) => Lens<S, A>;
|
|
222
222
|
/**
|
|
223
223
|
* Creates a Lens that focuses on a property of an object.
|
|
224
224
|
* Call with the structure type first, then the key.
|
|
@@ -228,8 +228,8 @@ declare namespace Lens {
|
|
|
228
228
|
* const nameLens = Lens.from.property<User>()("name");
|
|
229
229
|
* ```
|
|
230
230
|
*/
|
|
231
|
-
|
|
232
|
-
}
|
|
231
|
+
property: <S>() => <K extends keyof S>(key: K) => Lens<S, S[K]>;
|
|
232
|
+
};
|
|
233
233
|
/**
|
|
234
234
|
* Reads the focused value from a structure.
|
|
235
235
|
*
|
|
@@ -238,7 +238,7 @@ declare namespace Lens {
|
|
|
238
238
|
* pipe(user, Lens.get(nameLens)); // "Alice"
|
|
239
239
|
* ```
|
|
240
240
|
*/
|
|
241
|
-
|
|
241
|
+
get: <S, A>(lens: Lens<S, A>) => (s: S) => A;
|
|
242
242
|
/**
|
|
243
243
|
* Replaces the focused value within a structure, returning a new structure.
|
|
244
244
|
*
|
|
@@ -247,7 +247,7 @@ declare namespace Lens {
|
|
|
247
247
|
* pipe(user, Lens.set(nameLens)("Bob")); // new User with name "Bob"
|
|
248
248
|
* ```
|
|
249
249
|
*/
|
|
250
|
-
|
|
250
|
+
set: <S, A>(lens: Lens<S, A>) => (a: A) => (s: S) => S;
|
|
251
251
|
/**
|
|
252
252
|
* Applies a function to the focused value, returning a new structure.
|
|
253
253
|
*
|
|
@@ -256,7 +256,7 @@ declare namespace Lens {
|
|
|
256
256
|
* pipe(user, Lens.modify(nameLens)(n => n.toUpperCase())); // "ALICE"
|
|
257
257
|
* ```
|
|
258
258
|
*/
|
|
259
|
-
|
|
259
|
+
modify: <S, A>(lens: Lens<S, A>) => (f: (a: A) => A) => (s: S) => S;
|
|
260
260
|
/**
|
|
261
261
|
* Composes two Lenses: focuses through the outer, then through the inner.
|
|
262
262
|
* Use in a pipe chain to build up a deep focus step by step.
|
|
@@ -269,7 +269,7 @@ declare namespace Lens {
|
|
|
269
269
|
* );
|
|
270
270
|
* ```
|
|
271
271
|
*/
|
|
272
|
-
|
|
272
|
+
andThen: <A, B>(inner: Lens<A, B>) => <S>(outer: Lens<S, A>) => Lens<S, B>;
|
|
273
273
|
/**
|
|
274
274
|
* Composes a Lens with an Optional, producing an Optional.
|
|
275
275
|
* Use when the next step in the focus is optional (may be absent).
|
|
@@ -282,7 +282,7 @@ declare namespace Lens {
|
|
|
282
282
|
* );
|
|
283
283
|
* ```
|
|
284
284
|
*/
|
|
285
|
-
|
|
285
|
+
andThenOptional: <A, B>(inner: Optional<A, B>) => <S>(outer: Lens<S, A>) => Optional<S, B>;
|
|
286
286
|
/**
|
|
287
287
|
* Converts a Lens to an Optional. Every Lens is a valid Optional
|
|
288
288
|
* whose get always returns Some.
|
|
@@ -296,8 +296,8 @@ declare namespace Lens {
|
|
|
296
296
|
* );
|
|
297
297
|
* ```
|
|
298
298
|
*/
|
|
299
|
-
|
|
300
|
-
}
|
|
299
|
+
toOptional: <S, A>(lens: Lens<S, A>) => Optional<S, A>;
|
|
300
|
+
};
|
|
301
301
|
|
|
302
302
|
/**
|
|
303
303
|
* A value paired with an accumulated log.
|
|
@@ -325,8 +325,8 @@ declare namespace Lens {
|
|
|
325
325
|
* ```
|
|
326
326
|
*/
|
|
327
327
|
type Logged<L, A> = WithValue<A> & WithLog<L>;
|
|
328
|
-
declare
|
|
329
|
-
|
|
328
|
+
declare const Logged: {
|
|
329
|
+
from: {
|
|
330
330
|
/**
|
|
331
331
|
* Wraps a pure value into a `Logged` with an empty log.
|
|
332
332
|
*
|
|
@@ -335,7 +335,7 @@ declare namespace Logged {
|
|
|
335
335
|
* Logged.from.value<string, number>(42); // { value: 42, log: [] }
|
|
336
336
|
* ```
|
|
337
337
|
*/
|
|
338
|
-
|
|
338
|
+
value: <W, A>(val: A) => Logged<W, A>;
|
|
339
339
|
/**
|
|
340
340
|
* Creates a `Logged` that records a single log entry and produces no
|
|
341
341
|
* meaningful value. Use this to append to the log inside a `chain`.
|
|
@@ -345,8 +345,8 @@ declare namespace Logged {
|
|
|
345
345
|
* Logged.from.entry("operation completed"); // { value: undefined, log: ["operation completed"] }
|
|
346
346
|
* ```
|
|
347
347
|
*/
|
|
348
|
-
|
|
349
|
-
}
|
|
348
|
+
entry: <W>(logEntry: W) => Logged<W, undefined>;
|
|
349
|
+
};
|
|
350
350
|
/**
|
|
351
351
|
* Transforms the value inside a `Logged` without affecting the log.
|
|
352
352
|
*
|
|
@@ -358,7 +358,7 @@ declare namespace Logged {
|
|
|
358
358
|
* ); // { value: 10, log: [] }
|
|
359
359
|
* ```
|
|
360
360
|
*/
|
|
361
|
-
|
|
361
|
+
map: <W, A, B>(f: (a: A) => B) => (data: Logged<W, A>) => Logged<W, B>;
|
|
362
362
|
/**
|
|
363
363
|
* Sequences two `Logged` computations, concatenating their logs.
|
|
364
364
|
* The value from the first is passed to `f`; the resulting log entries are
|
|
@@ -377,7 +377,7 @@ declare namespace Logged {
|
|
|
377
377
|
* Logged.run(result); // [20, ["step", "done"]]
|
|
378
378
|
* ```
|
|
379
379
|
*/
|
|
380
|
-
|
|
380
|
+
chain: <W, A, B>(f: (a: A) => Logged<W, B>) => (data: Logged<W, A>) => Logged<W, B>;
|
|
381
381
|
/**
|
|
382
382
|
* Applies a function wrapped in a `Logged` to a value wrapped in a `Logged`,
|
|
383
383
|
* concatenating both logs.
|
|
@@ -394,7 +394,7 @@ declare namespace Logged {
|
|
|
394
394
|
* Logged.run(result); // [10, ["fn-loaded", "arg-loaded"]]
|
|
395
395
|
* ```
|
|
396
396
|
*/
|
|
397
|
-
|
|
397
|
+
ap: <W, A>(arg: Logged<W, A>) => <B>(data: Logged<W, (a: A) => B>) => Logged<W, B>;
|
|
398
398
|
/**
|
|
399
399
|
* Runs a side effect on the value without changing the `Logged`.
|
|
400
400
|
* Useful for debugging or inspecting intermediate values.
|
|
@@ -407,7 +407,7 @@ declare namespace Logged {
|
|
|
407
407
|
* );
|
|
408
408
|
* ```
|
|
409
409
|
*/
|
|
410
|
-
|
|
410
|
+
tap: <W, A>(f: (a: A) => void) => (data: Logged<W, A>) => Logged<W, A>;
|
|
411
411
|
/**
|
|
412
412
|
* Extracts the value and log as a `readonly [A, ReadonlyArray<W>]` tuple.
|
|
413
413
|
* Use this at the boundary where you need to consume both.
|
|
@@ -423,7 +423,7 @@ declare namespace Logged {
|
|
|
423
423
|
* // value = 2, log = ["incremented"]
|
|
424
424
|
* ```
|
|
425
425
|
*/
|
|
426
|
-
|
|
426
|
+
run: <W, A>(data: Logged<W, A>) => readonly [A, ReadonlyArray<W>];
|
|
427
427
|
/**
|
|
428
428
|
* Lifts a Logged value into an accumulator object.
|
|
429
429
|
*
|
|
@@ -432,7 +432,7 @@ declare namespace Logged {
|
|
|
432
432
|
* pipe(Logged.from.value<string, number>(42), Logged.bindTo("value")); // Logged({ value: 42 })
|
|
433
433
|
* ```
|
|
434
434
|
*/
|
|
435
|
-
|
|
435
|
+
bindTo: <K extends string>(key: K) => <W, A>(data: Logged<W, A>) => Logged<W, { [P in K]: A; }>;
|
|
436
436
|
/**
|
|
437
437
|
* Evaluates a new Logged using the current accumulator and attaches the output to a new key.
|
|
438
438
|
*
|
|
@@ -444,7 +444,7 @@ declare namespace Logged {
|
|
|
444
444
|
* ); // Logged({ value: { a: 1, b: 2 } })
|
|
445
445
|
* ```
|
|
446
446
|
*/
|
|
447
|
-
|
|
447
|
+
bind: <K extends string, W, A, B>(key: K, f: (a: A) => Logged<W, B>) => (data: Logged<W, A>) => Logged<W, A & { [P in K]: B; }>;
|
|
448
448
|
/**
|
|
449
449
|
* Focuses a Logged computation's value transformation using a Lens.
|
|
450
450
|
*
|
|
@@ -455,8 +455,8 @@ declare namespace Logged {
|
|
|
455
455
|
* pipe(logged, Logged.focus(nameLens)(s => s.toUpperCase()));
|
|
456
456
|
* ```
|
|
457
457
|
*/
|
|
458
|
-
|
|
459
|
-
}
|
|
458
|
+
focus: <S, A>(lens: Lens<S, A>) => <W>(f: (a: A) => A) => (data: Logged<W, S>) => Logged<W, S>;
|
|
459
|
+
};
|
|
460
460
|
|
|
461
461
|
type MaybeRetry<E, O> = O extends {
|
|
462
462
|
retry: RetryOptions<E>;
|
|
@@ -541,6 +541,7 @@ type InterpretResult<I, E, A, O> = [O] extends [{
|
|
|
541
541
|
}] ? Op.Manager<I, E, A, Op.QueueState<E, A> | MaybeRetry<E, O>> : [O] extends [{
|
|
542
542
|
strategy: "buffered";
|
|
543
543
|
}] ? Op.Manager<I, E, A, Op.BufferedState<E, A> | MaybeRetry<E, O>> : never;
|
|
544
|
+
declare function interpretFn<I, E, A, O extends AllInterpretOptions<I, E>>(op: Op<I, E, A>, options: O): InterpretResult<I, E, A, O>;
|
|
544
545
|
/**
|
|
545
546
|
* A reusable description of async work — decoupled from execution strategy and lifetime.
|
|
546
547
|
*
|
|
@@ -564,10 +565,10 @@ type InterpretResult<I, E, A, O> = [O] extends [{
|
|
|
564
565
|
*
|
|
565
566
|
* const manager = Op.interpret(fetchUser, { strategy: "restartable" });
|
|
566
567
|
* manager.subscribe(state => {
|
|
567
|
-
* if (Op.
|
|
568
|
-
* if (Op.
|
|
569
|
-
* if (Op.
|
|
570
|
-
* if (Op.
|
|
568
|
+
* if (Op.is.pending(state)) showSpinner();
|
|
569
|
+
* if (Op.is.ok(state)) render(state.value);
|
|
570
|
+
* if (Op.is.err(state)) showError(state.error);
|
|
571
|
+
* if (Op.is.nil(state)) resetUI();
|
|
571
572
|
* });
|
|
572
573
|
* manager.run(userId);
|
|
573
574
|
* ```
|
|
@@ -579,595 +580,219 @@ type Op<I, E, A> = {
|
|
|
579
580
|
*/
|
|
580
581
|
readonly _factory: (input: I, signal: AbortSignal) => Deferred<Result<E, A> | null>;
|
|
581
582
|
};
|
|
583
|
+
declare const Op: {
|
|
584
|
+
make: {
|
|
585
|
+
/**
|
|
586
|
+
* Creates an Ok outcome with the given value.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```ts
|
|
590
|
+
* Op.make.ok(42); // { kind: "OpOk", value: 42 }
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
ok: <A>(value: A) => Op.Ok<A>;
|
|
594
|
+
/**
|
|
595
|
+
* Creates an Err outcome with the given error.
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```ts
|
|
599
|
+
* Op.make.err("Something went wrong"); // { kind: "OpErr", error: "Something went wrong" }
|
|
600
|
+
* ```
|
|
601
|
+
*/
|
|
602
|
+
err: <E>(error: E) => Op.Err<E>;
|
|
603
|
+
/**
|
|
604
|
+
* Creates a Nil outcome with the given cancellation/drop reason.
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
* ```ts
|
|
608
|
+
* Op.make.nil("aborted"); // { kind: "OpNil", reason: "aborted" }
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
nil: (reason: Op.NilReason) => Op.Nil;
|
|
612
|
+
};
|
|
613
|
+
is: {
|
|
614
|
+
/**
|
|
615
|
+
* Type guard that checks if an Op state is Idle.
|
|
616
|
+
*
|
|
617
|
+
* @example
|
|
618
|
+
* ```ts
|
|
619
|
+
* if (Op.is.idle(manager.state)) {
|
|
620
|
+
* console.log("Ready to execute");
|
|
621
|
+
* }
|
|
622
|
+
* ```
|
|
623
|
+
*/
|
|
624
|
+
idle: <E, A>(state: Op.State<E, A>) => state is Op.Idle;
|
|
625
|
+
/**
|
|
626
|
+
* Type guard that checks if an Op state is Pending (actively executing).
|
|
627
|
+
*
|
|
628
|
+
* @example
|
|
629
|
+
* ```ts
|
|
630
|
+
* if (Op.is.pending(manager.state)) {
|
|
631
|
+
* showSpinner();
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
pending: <E, A>(state: Op.State<E, A>) => state is Op.Pending;
|
|
636
|
+
/**
|
|
637
|
+
* Type guard that checks if an Op state is Queued (waiting in a concurrency queue).
|
|
638
|
+
*
|
|
639
|
+
* @example
|
|
640
|
+
* ```ts
|
|
641
|
+
* if (Op.is.queued(manager.state)) {
|
|
642
|
+
* console.log("Position in queue:", manager.state.position);
|
|
643
|
+
* }
|
|
644
|
+
* ```
|
|
645
|
+
*/
|
|
646
|
+
queued: <E, A>(state: Op.State<E, A>) => state is Op.Queued;
|
|
647
|
+
/**
|
|
648
|
+
* Type guard that checks if an Op state is Retrying after a failure.
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```ts
|
|
652
|
+
* if (Op.is.retrying(manager.state)) {
|
|
653
|
+
* console.log("Retry attempt:", manager.state.attempt);
|
|
654
|
+
* }
|
|
655
|
+
* ```
|
|
656
|
+
*/
|
|
657
|
+
retrying: <E, A>(state: Op.State<E, A>) => state is Op.Retrying<E>;
|
|
658
|
+
/**
|
|
659
|
+
* Type guard that checks if an Op state or outcome is Ok.
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
* ```ts
|
|
663
|
+
* if (Op.is.ok(outcome)) {
|
|
664
|
+
* render(outcome.value);
|
|
665
|
+
* }
|
|
666
|
+
* ```
|
|
667
|
+
*/
|
|
668
|
+
ok: <E, A>(state: Op.State<E, A>) => state is Op.Ok<A>;
|
|
669
|
+
/**
|
|
670
|
+
* Type guard that checks if an Op state or outcome is Err.
|
|
671
|
+
*
|
|
672
|
+
* @example
|
|
673
|
+
* ```ts
|
|
674
|
+
* if (Op.is.err(outcome)) {
|
|
675
|
+
* showError(outcome.error);
|
|
676
|
+
* }
|
|
677
|
+
* ```
|
|
678
|
+
*/
|
|
679
|
+
err: <E, A>(state: Op.State<E, A>) => state is Op.Err<E>;
|
|
680
|
+
/**
|
|
681
|
+
* Type guard that checks if an Op state or outcome is Nil.
|
|
682
|
+
*
|
|
683
|
+
* @example
|
|
684
|
+
* ```ts
|
|
685
|
+
* if (Op.is.nil(outcome)) {
|
|
686
|
+
* console.log("Skipped due to:", outcome.reason);
|
|
687
|
+
* }
|
|
688
|
+
* ```
|
|
689
|
+
*/
|
|
690
|
+
nil: <E, A>(state: Op.State<E, A>) => state is Op.Nil;
|
|
691
|
+
};
|
|
692
|
+
create: <E, A, I = void>(factory: (signal: AbortSignal) => (input: I) => Promise<A>, onError: (e: unknown) => E) => Op<I, E, A>;
|
|
693
|
+
lift: <I, A>(f: (input: I, signal: AbortSignal) => Promise<A>) => Op<I, unknown, A>;
|
|
694
|
+
match: <E, A, B>(cases: {
|
|
695
|
+
ok: (a: A) => B;
|
|
696
|
+
err: (e: E) => B;
|
|
697
|
+
nil: () => B;
|
|
698
|
+
}) => (outcome: Op.Outcome<E, A>) => B;
|
|
699
|
+
fold: <E, A, B>(onErr: (e: E) => B, onNil: () => B, onOk: (a: A) => B) => (outcome: Op.Outcome<E, A>) => B;
|
|
700
|
+
getOrElse: <E, A, B>(defaultValue: () => B) => (outcome: Op.Outcome<E, A>) => A | B;
|
|
701
|
+
map: <E, A, B>(f: (a: A) => B) => (outcome: Op.Outcome<E, A>) => Op.Outcome<E, B>;
|
|
702
|
+
mapError: <E, F, A>(f: (e: E) => F) => (outcome: Op.Outcome<E, A>) => Op.Outcome<F, A>;
|
|
703
|
+
chain: <E, A, B>(f: (a: A) => Op.Outcome<E, B>) => (outcome: Op.Outcome<E, A>) => Op.Outcome<E, B>;
|
|
704
|
+
tap: <E, A>(f: (a: A) => void) => (outcome: Op.Outcome<E, A>) => Op.Outcome<E, A>;
|
|
705
|
+
recover: <E, A, B>(f: (e: E) => Op.Outcome<E, B>) => (outcome: Op.Outcome<E, A>) => Op.Outcome<E, A | B>;
|
|
706
|
+
to: {
|
|
707
|
+
Result: <E, A>(onNil: () => E) => (outcome: Op.Outcome<E, A>) => Result<E, A>;
|
|
708
|
+
Maybe: <E, A>(outcome: Op.Outcome<E, A>) => Maybe<A>;
|
|
709
|
+
};
|
|
710
|
+
all: <E, A>(invocations: ReadonlyArray<Deferred<Op.Outcome<E, A>>>) => Deferred<ReadonlyArray<Op.Outcome<E, A>>>;
|
|
711
|
+
race: <E, A>(invocations: ReadonlyArray<Deferred<Op.Outcome<E, A>>>) => Deferred<Op.Outcome<E, A>>;
|
|
712
|
+
wire: <I, E, A, S extends Op.State<E, A>>(source: Op.Manager<I, E, A, S>, f: (a: A) => void) => () => void;
|
|
713
|
+
interpret: typeof interpretFn;
|
|
714
|
+
};
|
|
582
715
|
declare namespace Op {
|
|
583
|
-
/**
|
|
584
|
-
* The three terminal states of a completed async operation.
|
|
585
|
-
*
|
|
586
|
-
* - `Ok` — produced a value.
|
|
587
|
-
* - `Err` — produced a typed error.
|
|
588
|
-
* - `Nil` — completed without a value or error. The `reason` field says why:
|
|
589
|
-
* `"aborted"` — `abort()` was called; `"dropped"` — a new `run()` was ignored
|
|
590
|
-
* because the strategy was already busy; `"replaced"` — a newer `run()` took
|
|
591
|
-
* over a call that was already running; `"evicted"` — a newer `run()` took over
|
|
592
|
-
* a call that was waiting and had not yet started.
|
|
593
|
-
*/
|
|
594
716
|
type Outcome<E, A> = Ok<A> | Err<E> | Nil;
|
|
595
|
-
/** A successful outcome with a value. */
|
|
596
717
|
type Ok<A> = WithKind<"OpOk"> & WithValue<A>;
|
|
597
|
-
/** A failed outcome with a typed error. */
|
|
598
718
|
type Err<E> = WithKind<"OpErr"> & WithError<E>;
|
|
599
|
-
/**
|
|
600
|
-
* An outcome that produced nothing. `reason` identifies why:
|
|
601
|
-
* - `"aborted"` — `abort()` was called explicitly.
|
|
602
|
-
* - `"dropped"` — the invocation was ignored because the strategy was busy.
|
|
603
|
-
* - `"replaced"` — a newer invocation took over a call that was already running.
|
|
604
|
-
* - `"evicted"` — a newer invocation took a slot from a call that was waiting and
|
|
605
|
-
* had not yet started (buffered slot, debounce timer, throttle trailing slot).
|
|
606
|
-
*/
|
|
607
719
|
type Nil = WithKind<"OpNil"> & {
|
|
608
720
|
readonly reason: NilReason;
|
|
609
721
|
};
|
|
610
|
-
/** The reason a `Nil` outcome was produced. */
|
|
611
722
|
type NilReason = "aborted" | "dropped" | "replaced" | "evicted";
|
|
612
|
-
/** A `Nil` produced by an explicit `abort()` call. */
|
|
613
723
|
type AbortedNil = Nil & {
|
|
614
724
|
readonly reason: "aborted";
|
|
615
725
|
};
|
|
616
|
-
/** A `Nil` produced when an invocation was silently ignored (strategy was busy). */
|
|
617
726
|
type DroppedNil = Nil & {
|
|
618
727
|
readonly reason: "dropped";
|
|
619
728
|
};
|
|
620
|
-
/** A `Nil` produced when a newer invocation took over a call that was already running. */
|
|
621
729
|
type ReplacedNil = Nil & {
|
|
622
730
|
readonly reason: "replaced";
|
|
623
731
|
};
|
|
624
|
-
/** A `Nil` produced when a newer invocation took a slot from a call that was waiting and had not yet started. */
|
|
625
732
|
type EvictedNil = Nil & {
|
|
626
733
|
readonly reason: "evicted";
|
|
627
734
|
};
|
|
628
|
-
/** The full set of states a manager can emit, including transient states. */
|
|
629
735
|
type State<E, A> = Idle | Pending | Queued | Retrying<E> | Outcome<E, A>;
|
|
630
|
-
/** The manager has not been run yet (initial state). */
|
|
631
736
|
type Idle = WithKind<"Idle">;
|
|
632
|
-
/** An operation is in-flight. */
|
|
633
737
|
type Pending = WithKind<"Pending">;
|
|
634
|
-
/** An operation is waiting in a queue. `position` is 0-indexed (0 = next to run). */
|
|
635
738
|
type Queued = WithKind<"Queued"> & {
|
|
636
739
|
readonly position: number;
|
|
637
740
|
};
|
|
638
|
-
/** A retry attempt is about to start. */
|
|
639
741
|
type Retrying<E> = WithKind<"Retrying"> & {
|
|
640
742
|
readonly attempt: number;
|
|
641
743
|
readonly lastError: E;
|
|
642
744
|
readonly nextRetryIn?: number;
|
|
643
745
|
};
|
|
644
|
-
/**
|
|
645
|
-
* A stateful execution manager. `run()` both emits state transitions through
|
|
646
|
-
* subscribers and returns a `Deferred` tied to that specific invocation.
|
|
647
|
-
* `S` is narrowed to only the states reachable for this manager's strategy
|
|
648
|
-
* and configuration.
|
|
649
|
-
*
|
|
650
|
-
* @example
|
|
651
|
-
* ```ts
|
|
652
|
-
* const manager = Op.interpret(saveConfig, { strategy: "exclusive" });
|
|
653
|
-
*
|
|
654
|
-
* manager.subscribe(state => {
|
|
655
|
-
* if (Op.isPending(state)) lockForm();
|
|
656
|
-
* if (Op.isOk(state)) toast("Saved");
|
|
657
|
-
* if (Op.isErr(state)) toast(`Error: ${state.error.message}`);
|
|
658
|
-
* });
|
|
659
|
-
*
|
|
660
|
-
* // Fire and subscribe (subscriber pattern)
|
|
661
|
-
* manager.run(formData);
|
|
662
|
-
*
|
|
663
|
-
* // Or await the specific invocation's outcome
|
|
664
|
-
* const result = await manager.run(formData);
|
|
665
|
-
* if (Op.isNil(result)) return; // dropped — another save was in-flight
|
|
666
|
-
* ```
|
|
667
|
-
*/
|
|
668
746
|
type Manager<I, E, A, S extends State<E, A>> = {
|
|
669
|
-
/** The current state. Useful for synchronous reads (e.g., `useSyncExternalStore`). */
|
|
670
747
|
readonly state: S;
|
|
671
|
-
/**
|
|
672
|
-
* Submits an invocation. Emits state transitions via subscribers and returns a
|
|
673
|
-
* `Deferred` that resolves to the terminal outcome for this specific invocation.
|
|
674
|
-
* `Nil` means this invocation was not executed (dropped, replaced, or aborted).
|
|
675
|
-
*/
|
|
676
748
|
run: (input: I) => Deferred<Exclude<S, Idle | Pending | Queued | Retrying<E>>>;
|
|
677
|
-
/**
|
|
678
|
-
* Cancels any in-flight operation and clears the queue.
|
|
679
|
-
* Every pending `run()` Deferred — including queued invocations — settles to `AbortedNil`.
|
|
680
|
-
* Resolution is asynchronous; no Deferred hangs indefinitely.
|
|
681
|
-
*/
|
|
682
749
|
abort: () => void;
|
|
683
|
-
/**
|
|
684
|
-
* Registers a subscriber for state transitions. Returns an unsubscribe function.
|
|
685
|
-
* The callback fires immediately with the current state if the manager is not idle.
|
|
686
|
-
*/
|
|
687
750
|
subscribe: (cb: (state: S) => void) => () => void;
|
|
688
|
-
/** Returns state to Idle. Does not cancel any in-flight operation. */
|
|
689
751
|
reset: () => void;
|
|
690
|
-
/**
|
|
691
|
-
* Runs the input immediately, then every `interval` milliseconds.
|
|
692
|
-
* Returns a stop handle — call it to cancel future runs.
|
|
693
|
-
*/
|
|
694
752
|
poll: (input: I, options: {
|
|
695
753
|
interval: Duration;
|
|
696
754
|
}) => () => void;
|
|
697
755
|
};
|
|
698
|
-
/**
|
|
699
|
-
* A stateful manager that maintains independent per-key execution slots.
|
|
700
|
-
* Different keys run in parallel; the same key follows the `perKey` sub-strategy.
|
|
701
|
-
* `abort(key)` cancels a specific key; `abort()` cancels all.
|
|
702
|
-
* `state` is a map of each key's last known state — updated on every transition.
|
|
703
|
-
*
|
|
704
|
-
* @example
|
|
705
|
-
* ```ts
|
|
706
|
-
* const getUser = Op.interpret(fetchUser, {
|
|
707
|
-
* strategy: "keyed",
|
|
708
|
-
* key: (input) => input.id,
|
|
709
|
-
* perKey: "exclusive",
|
|
710
|
-
* });
|
|
711
|
-
*
|
|
712
|
-
* getUser.subscribe((map) => {
|
|
713
|
-
* for (const [id, state] of map) {
|
|
714
|
-
* if (Op.isPending(state)) showSpinner(id);
|
|
715
|
-
* if (Op.isOk(state)) render(id, state.value);
|
|
716
|
-
* }
|
|
717
|
-
* });
|
|
718
|
-
*
|
|
719
|
-
* getUser.run({ id: "user-1" }); // starts key "user-1"
|
|
720
|
-
* getUser.run({ id: "user-2" }); // starts key "user-2" in parallel
|
|
721
|
-
* ```
|
|
722
|
-
*/
|
|
723
756
|
type KeyedManager<I, K, E, PerKeyS> = {
|
|
724
|
-
/** Current state map. Keys are present from first `run()` through their last terminal state. */
|
|
725
757
|
readonly state: ReadonlyMap<K, PerKeyS>;
|
|
726
|
-
/**
|
|
727
|
-
* Submits an invocation for the key derived from the input. Returns a `Deferred` tied
|
|
728
|
-
* to this specific invocation. Same-key behaviour is controlled by `perKey`.
|
|
729
|
-
*/
|
|
730
758
|
run: (input: I) => Deferred<Exclude<PerKeyS, Pending | Retrying<E>>>;
|
|
731
|
-
/** Cancels the in-flight operation for a specific key, or all keys if omitted. */
|
|
732
759
|
abort: (key?: K) => void;
|
|
733
|
-
/**
|
|
734
|
-
* Registers a subscriber. The callback receives a fresh snapshot of the state map
|
|
735
|
-
* on every transition. Returns an unsubscribe function.
|
|
736
|
-
* Fires immediately with the current map if any key is active.
|
|
737
|
-
*/
|
|
738
760
|
subscribe: (cb: (state: ReadonlyMap<K, PerKeyS>) => void) => () => void;
|
|
739
|
-
/** Clears all per-key state and notifies subscribers. Does not cancel in-flight operations. */
|
|
740
761
|
reset: () => void;
|
|
741
|
-
/**
|
|
742
|
-
* Runs the input immediately, then every `interval` milliseconds.
|
|
743
|
-
* Returns a stop handle — call it to cancel future runs.
|
|
744
|
-
*/
|
|
745
762
|
poll: (input: I, options: {
|
|
746
763
|
interval: Duration;
|
|
747
764
|
}) => () => void;
|
|
748
765
|
};
|
|
749
|
-
/** States reachable by a `once` manager (no retry). */
|
|
750
766
|
type OnceState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
751
|
-
/** States reachable by a `once` manager with retry configured. */
|
|
752
767
|
type RetryableOnceState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
753
|
-
/** States reachable by a `restartable` manager (no retry). */
|
|
754
768
|
type RestartableState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | ReplacedNil;
|
|
755
|
-
/** States reachable by a `restartable` manager with retry configured. */
|
|
756
769
|
type RetryableRestartableState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | ReplacedNil;
|
|
757
|
-
/** States reachable by an `exclusive` manager (no retry). */
|
|
758
770
|
type ExclusiveState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
759
|
-
/** States reachable by an `exclusive` manager with retry configured. */
|
|
760
771
|
type RetryableExclusiveState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
761
|
-
/** States reachable by a `queue` manager (no retry, no overflow, no dedupe). */
|
|
762
772
|
type QueueState<E, A> = Idle | Pending | Queued | Ok<A> | Err<E> | AbortedNil;
|
|
763
|
-
/** States reachable by a `queue` manager with retry (no overflow, no dedupe). */
|
|
764
773
|
type RetryableQueueState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Err<E> | AbortedNil;
|
|
765
|
-
/** States reachable by a `queue` manager with `overflow:"drop"` or `dedupe` (no retry). */
|
|
766
774
|
type QueueDropState<E, A> = Idle | Pending | Queued | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
767
|
-
/** States reachable by a `queue` manager with `overflow:"drop"` or `dedupe`, with retry. */
|
|
768
775
|
type RetryableQueueDropState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
769
|
-
/** States reachable by a `queue` manager with `overflow:"replace-last"` and no `dedupe` (no retry). */
|
|
770
776
|
type QueueReplaceState<E, A> = Idle | Pending | Queued | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
771
|
-
/** States reachable by a `queue` manager with `overflow:"replace-last"` and no `dedupe`, with retry. */
|
|
772
777
|
type RetryableQueueReplaceState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
773
|
-
/** States reachable by a `queue` manager with `overflow:"replace-last"` AND `dedupe` (no retry). */
|
|
774
778
|
type QueueDropAndReplaceState<E, A> = Idle | Pending | Queued | Ok<A> | Err<E> | AbortedNil | DroppedNil | EvictedNil;
|
|
775
|
-
/** States reachable by a `queue` manager with `overflow:"replace-last"` AND `dedupe`, with retry. */
|
|
776
779
|
type RetryableQueueDropAndReplaceState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Err<E> | AbortedNil | DroppedNil | EvictedNil;
|
|
777
|
-
/** States reachable by a `buffered` manager (no retry). */
|
|
778
780
|
type BufferedState<E, A> = Idle | Pending | Queued | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
779
|
-
/** States reachable by a `buffered` manager with retry configured. */
|
|
780
781
|
type RetryableBufferedState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
781
|
-
/** States reachable by a `debounced` manager (no retry). */
|
|
782
782
|
type DebouncedState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
783
|
-
/** States reachable by a `debounced` manager with retry configured. */
|
|
784
783
|
type RetryableDebouncedState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
785
|
-
/** States reachable by a `throttled` manager (leading-only, no retry). */
|
|
786
784
|
type ThrottledState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
787
|
-
/** States reachable by a `throttled` manager (leading-only, with retry). */
|
|
788
785
|
type RetryableThrottledState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
789
|
-
/** States reachable by a `throttled` manager with `trailing: true` (no retry). */
|
|
790
786
|
type ThrottledTrailingState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
791
|
-
/** States reachable by a `throttled` manager with `trailing: true` and retry. */
|
|
792
787
|
type RetryableThrottledTrailingState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | EvictedNil;
|
|
793
|
-
/** States reachable by a `concurrent` manager with `overflow: "queue"` (no retry). */
|
|
794
788
|
type ConcurrentQueueState<E, A> = Idle | Pending | Queued | Ok<A> | Err<E> | AbortedNil;
|
|
795
|
-
/** States reachable by a `concurrent` manager with `overflow: "queue"` and retry. */
|
|
796
789
|
type RetryableConcurrentQueueState<E, A> = Idle | Pending | Queued | Retrying<E> | Ok<A> | Err<E> | AbortedNil;
|
|
797
|
-
/** States reachable by a `concurrent` manager with `overflow: "drop"` (no retry). */
|
|
798
790
|
type ConcurrentDropState<E, A> = Idle | Pending | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
799
|
-
/** States reachable by a `concurrent` manager with `overflow: "drop"` and retry. */
|
|
800
791
|
type RetryableConcurrentDropState<E, A> = Idle | Pending | Retrying<E> | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
801
|
-
/** Per-key state union for a `keyed` manager with `perKey: "exclusive"`. */
|
|
802
792
|
type KeyedExclusivePerKey<E, A> = Pending | Ok<A> | Err<E> | AbortedNil | DroppedNil;
|
|
803
|
-
/** Per-key state union for a `keyed` manager with `perKey: "restartable"`. */
|
|
804
793
|
type KeyedRestartablePerKey<E, A> = Pending | Ok<A> | Err<E> | AbortedNil | ReplacedNil;
|
|
805
794
|
type RetryOptions<E> = RetryOptions<E>;
|
|
806
795
|
type TimeoutOptions<E> = TimeoutOptions<E>;
|
|
807
|
-
/**
|
|
808
|
-
* Creates a `Nil` outcome with a reason.
|
|
809
|
-
*
|
|
810
|
-
* @example
|
|
811
|
-
* ```ts
|
|
812
|
-
* Op.nil("aborted"); // { kind: "OpNil", reason: "aborted" }
|
|
813
|
-
* Op.nil("dropped"); // { kind: "OpNil", reason: "dropped" }
|
|
814
|
-
* Op.nil("replaced"); // { kind: "OpNil", reason: "replaced" }
|
|
815
|
-
* Op.nil("evicted"); // { kind: "OpNil", reason: "evicted" }
|
|
816
|
-
* ```
|
|
817
|
-
*/
|
|
818
|
-
const nil: (reason: NilReason) => Nil;
|
|
819
|
-
/**
|
|
820
|
-
* Creates an `Op` from an async factory and an error mapper.
|
|
821
|
-
*
|
|
822
|
-
* The factory receives an `AbortSignal` and returns a function that takes the input. Capture
|
|
823
|
-
* the signal in the outer closure and pass it to cancellable APIs like `fetch`. The error
|
|
824
|
-
* mapper converts any thrown value into a typed error; it is never called for aborts.
|
|
825
|
-
*
|
|
826
|
-
* **If the factory ignores the signal**, cancellation silently stops working: the operation
|
|
827
|
-
* runs to completion and emits `Ok` even after the strategy has aborted it. This is harmless
|
|
828
|
-
* for `exclusive` and `once` (which do not abort in-flight work), but causes stale `Ok`
|
|
829
|
-
* emissions on `restartable`, `debounced`, `throttled`, `buffered`, and `queue` strategies
|
|
830
|
-
* where in-flight runs are regularly replaced or dropped.
|
|
831
|
-
*
|
|
832
|
-
* @example
|
|
833
|
-
* ```ts
|
|
834
|
-
* // With cancellation — fetch is aborted when the Op is replaced or aborted
|
|
835
|
-
* const saveProfile = Op.create(
|
|
836
|
-
* (signal) => (data: ProfileData) =>
|
|
837
|
-
* fetch("/profile", { method: "POST", body: JSON.stringify(data), signal })
|
|
838
|
-
* .then(r => {
|
|
839
|
-
* if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
|
|
840
|
-
* return r.json() as Promise<ProfileData>;
|
|
841
|
-
* }),
|
|
842
|
-
* (e) => new ApiError(e),
|
|
843
|
-
* );
|
|
844
|
-
*
|
|
845
|
-
* // No input — fetches the current user; manager.run() takes no arguments
|
|
846
|
-
* const fetchCurrentUser = Op.create(
|
|
847
|
-
* (signal) => () => fetch("/me", { signal }).then(r => {
|
|
848
|
-
* if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
|
|
849
|
-
* return r.json() as Promise<User>;
|
|
850
|
-
* }),
|
|
851
|
-
* (e) => new ApiError(e),
|
|
852
|
-
* );
|
|
853
|
-
* const manager = Op.interpret(fetchCurrentUser, { strategy: "once" });
|
|
854
|
-
* manager.run(); // no argument needed
|
|
855
|
-
* ```
|
|
856
|
-
*/
|
|
857
|
-
const create: <E, A, I = void>(factory: (signal: AbortSignal) => (input: I) => Promise<A>, onError: (e: unknown) => E) => Op<I, E, A>;
|
|
858
|
-
/**
|
|
859
|
-
* Lifts a plain async function into an Op, treating all errors as `unknown`.
|
|
860
|
-
*
|
|
861
|
-
* Use this when you have a simple async function that doesn't need custom error mapping.
|
|
862
|
-
* The signal is passed so the operation can respect cancellation.
|
|
863
|
-
*
|
|
864
|
-
* @example
|
|
865
|
-
* ```ts
|
|
866
|
-
* const fetchUser = Op.lift((id: number, signal: AbortSignal) =>
|
|
867
|
-
* fetch(`/api/users/${id}`, { signal }).then(r => r.json())
|
|
868
|
-
* );
|
|
869
|
-
* const manager = Op.interpret(fetchUser, { strategy: "restartable" });
|
|
870
|
-
* const outcome = await manager.run(42);
|
|
871
|
-
* if (Op.isOk(outcome)) console.log(outcome.value);
|
|
872
|
-
* ```
|
|
873
|
-
*/
|
|
874
|
-
const lift: <I, A>(f: (input: I, signal: AbortSignal) => Promise<A>) => Op<I, unknown, A>;
|
|
875
|
-
/**
|
|
876
|
-
* Creates a successful Outcome.
|
|
877
|
-
*
|
|
878
|
-
* @example
|
|
879
|
-
* ```ts
|
|
880
|
-
* Op.ok(42); // { kind: "OpOk", value: 42 }
|
|
881
|
-
* ```
|
|
882
|
-
*/
|
|
883
|
-
const ok: <A>(value: A) => Ok<A>;
|
|
884
|
-
/**
|
|
885
|
-
* Creates a failed Outcome with a typed error.
|
|
886
|
-
*
|
|
887
|
-
* @example
|
|
888
|
-
* ```ts
|
|
889
|
-
* Op.err(new ApiError("not found")); // { kind: "OpErr", error: ApiError }
|
|
890
|
-
* ```
|
|
891
|
-
*/
|
|
892
|
-
const err: <E>(error: E) => Err<E>;
|
|
893
|
-
/**
|
|
894
|
-
* Returns `true` if the state is `Idle`.
|
|
895
|
-
*
|
|
896
|
-
* @example
|
|
897
|
-
* ```ts
|
|
898
|
-
* manager.subscribe(state => {
|
|
899
|
-
* if (Op.isIdle(state)) hideSpinner();
|
|
900
|
-
* });
|
|
901
|
-
* ```
|
|
902
|
-
*/
|
|
903
|
-
const isIdle: <E, A>(state: State<E, A>) => state is Idle;
|
|
904
|
-
/**
|
|
905
|
-
* Returns `true` if the state is `Pending` (an operation is in-flight).
|
|
906
|
-
*
|
|
907
|
-
* @example
|
|
908
|
-
* ```ts
|
|
909
|
-
* manager.subscribe(state => {
|
|
910
|
-
* if (Op.isPending(state)) showSpinner();
|
|
911
|
-
* });
|
|
912
|
-
* ```
|
|
913
|
-
*/
|
|
914
|
-
const isPending: <E, A>(state: State<E, A>) => state is Pending;
|
|
915
|
-
/**
|
|
916
|
-
* Returns `true` if the state is `Queued` (an invocation is waiting to run).
|
|
917
|
-
*
|
|
918
|
-
* @example
|
|
919
|
-
* ```ts
|
|
920
|
-
* manager.subscribe(state => {
|
|
921
|
-
* if (Op.isQueued(state)) showQueuePosition(state.position);
|
|
922
|
-
* });
|
|
923
|
-
* ```
|
|
924
|
-
*/
|
|
925
|
-
const isQueued: <E, A>(state: State<E, A>) => state is Queued;
|
|
926
|
-
/**
|
|
927
|
-
* Returns `true` if the state is `Retrying`.
|
|
928
|
-
*
|
|
929
|
-
* @example
|
|
930
|
-
* ```ts
|
|
931
|
-
* manager.subscribe(state => {
|
|
932
|
-
* if (Op.isRetrying(state)) showRetryBadge(state.attempt);
|
|
933
|
-
* });
|
|
934
|
-
* ```
|
|
935
|
-
*/
|
|
936
|
-
const isRetrying: <E, A>(state: State<E, A>) => state is Retrying<E>;
|
|
937
|
-
/**
|
|
938
|
-
* Returns `true` if the state is `Ok` (the operation produced a value).
|
|
939
|
-
*
|
|
940
|
-
* @example
|
|
941
|
-
* ```ts
|
|
942
|
-
* manager.subscribe(state => {
|
|
943
|
-
* if (Op.isOk(state)) render(state.value);
|
|
944
|
-
* });
|
|
945
|
-
* ```
|
|
946
|
-
*/
|
|
947
|
-
const isOk: <E, A>(state: State<E, A>) => state is Ok<A>;
|
|
948
|
-
/**
|
|
949
|
-
* Returns `true` if the state is `Err` (the operation failed with a typed error).
|
|
950
|
-
*
|
|
951
|
-
* @example
|
|
952
|
-
* ```ts
|
|
953
|
-
* manager.subscribe(state => {
|
|
954
|
-
* if (Op.isErr(state)) showError(state.error);
|
|
955
|
-
* });
|
|
956
|
-
* ```
|
|
957
|
-
*/
|
|
958
|
-
const isErr: <E, A>(state: State<E, A>) => state is Err<E>;
|
|
959
|
-
/**
|
|
960
|
-
* Returns `true` if the state is `Nil` (the operation completed without a value or error).
|
|
961
|
-
*
|
|
962
|
-
* @example
|
|
963
|
-
* ```ts
|
|
964
|
-
* manager.subscribe(state => {
|
|
965
|
-
* if (Op.isNil(state)) resetUI();
|
|
966
|
-
* });
|
|
967
|
-
* ```
|
|
968
|
-
*/
|
|
969
|
-
const isNil: <E, A>(state: State<E, A>) => state is Nil;
|
|
970
|
-
/**
|
|
971
|
-
* Pattern matches on an Outcome using named case handlers.
|
|
972
|
-
*
|
|
973
|
-
* @example
|
|
974
|
-
* ```ts
|
|
975
|
-
* Op.match({
|
|
976
|
-
* ok: (user) => render(user),
|
|
977
|
-
* err: (e) => showError(e.message),
|
|
978
|
-
* nil: () => resetUI(),
|
|
979
|
-
* })(outcome);
|
|
980
|
-
* ```
|
|
981
|
-
*/
|
|
982
|
-
const match: <E, A, B>(cases: {
|
|
983
|
-
ok: (a: A) => B;
|
|
984
|
-
err: (e: E) => B;
|
|
985
|
-
nil: () => B;
|
|
986
|
-
}) => (outcome: Outcome<E, A>) => B;
|
|
987
|
-
/**
|
|
988
|
-
* Eliminates an Outcome with positional handlers.
|
|
989
|
-
* Order: `onErr`, `onNil`, `onOk` — mirrors `Result.fold` for the first and last cases.
|
|
990
|
-
*
|
|
991
|
-
* @example
|
|
992
|
-
* ```ts
|
|
993
|
-
* Op.fold(
|
|
994
|
-
* (e) => `error: ${e.message}`,
|
|
995
|
-
* () => "nothing",
|
|
996
|
-
* (v) => `value: ${v}`,
|
|
997
|
-
* )(outcome);
|
|
998
|
-
* ```
|
|
999
|
-
*/
|
|
1000
|
-
const fold: <E, A, B>(onErr: (e: E) => B, onNil: () => B, onOk: (a: A) => B) => (outcome: Outcome<E, A>) => B;
|
|
1001
|
-
/**
|
|
1002
|
-
* Returns the success value, or the result of `defaultValue()` for `Err` or `Nil`.
|
|
1003
|
-
*
|
|
1004
|
-
* @example
|
|
1005
|
-
* ```ts
|
|
1006
|
-
* Op.getOrElse(() => [] as User[])(outcome);
|
|
1007
|
-
* ```
|
|
1008
|
-
*/
|
|
1009
|
-
const getOrElse: <E, A, B>(defaultValue: () => B) => (outcome: Outcome<E, A>) => A | B;
|
|
1010
|
-
/**
|
|
1011
|
-
* Transforms the success value. `Err` and `Nil` pass through unchanged.
|
|
1012
|
-
*
|
|
1013
|
-
* @example
|
|
1014
|
-
* ```ts
|
|
1015
|
-
* pipe(outcome, Op.map(user => user.name));
|
|
1016
|
-
* ```
|
|
1017
|
-
*/
|
|
1018
|
-
const map: <E, A, B>(f: (a: A) => B) => (outcome: Outcome<E, A>) => Outcome<E, B>;
|
|
1019
|
-
/**
|
|
1020
|
-
* Transforms the error value. `Ok` and `Nil` pass through unchanged.
|
|
1021
|
-
*
|
|
1022
|
-
* @example
|
|
1023
|
-
* ```ts
|
|
1024
|
-
* pipe(outcome, Op.mapError(e => e.message));
|
|
1025
|
-
* ```
|
|
1026
|
-
*/
|
|
1027
|
-
const mapError: <E, F, A>(f: (e: E) => F) => (outcome: Outcome<E, A>) => Outcome<F, A>;
|
|
1028
|
-
/**
|
|
1029
|
-
* Chains Outcome computations. Runs `f` on `Ok`; `Err` and `Nil` pass through.
|
|
1030
|
-
*
|
|
1031
|
-
* @example
|
|
1032
|
-
* ```ts
|
|
1033
|
-
* pipe(
|
|
1034
|
-
* outcome,
|
|
1035
|
-
* Op.chain(user => user.active ? Op.ok(user) : Op.err(new Error("inactive"))),
|
|
1036
|
-
* );
|
|
1037
|
-
* ```
|
|
1038
|
-
*/
|
|
1039
|
-
const chain: <E, A, B>(f: (a: A) => Outcome<E, B>) => (outcome: Outcome<E, A>) => Outcome<E, B>;
|
|
1040
|
-
/**
|
|
1041
|
-
* Runs a side effect on the success value without changing the Outcome.
|
|
1042
|
-
*
|
|
1043
|
-
* @example
|
|
1044
|
-
* ```ts
|
|
1045
|
-
* pipe(outcome, Op.tap(user => console.log("loaded", user.id)));
|
|
1046
|
-
* ```
|
|
1047
|
-
*/
|
|
1048
|
-
const tap: <E, A>(f: (a: A) => void) => (outcome: Outcome<E, A>) => Outcome<E, A>;
|
|
1049
|
-
/**
|
|
1050
|
-
* Provides a fallback Outcome when the result is `Err`. `Ok` and `Nil` pass through.
|
|
1051
|
-
*
|
|
1052
|
-
* @example
|
|
1053
|
-
* ```ts
|
|
1054
|
-
* pipe(
|
|
1055
|
-
* outcome,
|
|
1056
|
-
* Op.recover(e => e.isRetryable ? Op.ok(cachedValue) : Op.err(e)),
|
|
1057
|
-
* );
|
|
1058
|
-
* ```
|
|
1059
|
-
*/
|
|
1060
|
-
const recover: <E, A, B>(f: (e: E) => Outcome<E, B>) => (outcome: Outcome<E, A>) => Outcome<E, A | B>;
|
|
1061
|
-
/**
|
|
1062
|
-
* Converts an Outcome to a `Result`. `Nil` becomes `Err(onNil())`.
|
|
1063
|
-
*
|
|
1064
|
-
* @example
|
|
1065
|
-
* ```ts
|
|
1066
|
-
* Op.to.Result(() => new ApiError("no result"))(outcome);
|
|
1067
|
-
* ```
|
|
1068
|
-
*/
|
|
1069
|
-
namespace to {
|
|
1070
|
-
/**
|
|
1071
|
-
* Converts an Outcome to a `Result`. `Nil` becomes `Err(onNil())`.
|
|
1072
|
-
*
|
|
1073
|
-
* @example
|
|
1074
|
-
* ```ts
|
|
1075
|
-
* Op.to.Result(() => new ApiError("no result"))(outcome);
|
|
1076
|
-
* ```
|
|
1077
|
-
*/
|
|
1078
|
-
const Result: <E, A>(onNil: () => E) => (outcome: Outcome<E, A>) => Result<E, A>;
|
|
1079
|
-
/**
|
|
1080
|
-
* Converts an Outcome to a `Maybe`. `Ok` becomes `Some`; `Err` and `Nil` become `None`.
|
|
1081
|
-
*
|
|
1082
|
-
* @example
|
|
1083
|
-
* ```ts
|
|
1084
|
-
* Op.to.Maybe(outcome); // Maybe<User>
|
|
1085
|
-
* ```
|
|
1086
|
-
*/
|
|
1087
|
-
const Maybe: <E, A>(outcome: Outcome<E, A>) => Maybe<A>;
|
|
1088
|
-
}
|
|
1089
|
-
/**
|
|
1090
|
-
* Resolves when all invocations settle, returning their outcomes in order.
|
|
1091
|
-
* An alternative to `Promise.all` that stays within the `Op` type system.
|
|
1092
|
-
*
|
|
1093
|
-
* @example
|
|
1094
|
-
* ```ts
|
|
1095
|
-
* const [a, b] = await Op.all([manager.run(inputA), manager.run(inputB)]);
|
|
1096
|
-
* ```
|
|
1097
|
-
*/
|
|
1098
|
-
const all: <E, A>(invocations: ReadonlyArray<Deferred<Outcome<E, A>>>) => Deferred<ReadonlyArray<Outcome<E, A>>>;
|
|
1099
|
-
/**
|
|
1100
|
-
* Resolves to the outcome of whichever invocation settles first.
|
|
1101
|
-
* An alternative to `Promise.race` that stays within the `Op` type system.
|
|
1102
|
-
*
|
|
1103
|
-
* @example
|
|
1104
|
-
* ```ts
|
|
1105
|
-
* const winner = await Op.race([manager.run(inputA), manager.run(inputB)]);
|
|
1106
|
-
* ```
|
|
1107
|
-
*/
|
|
1108
|
-
const race: <E, A>(invocations: ReadonlyArray<Deferred<Outcome<E, A>>>) => Deferred<Outcome<E, A>>;
|
|
1109
|
-
/**
|
|
1110
|
-
* Subscribes to a manager and calls a handler when the state reaches `OpOk`.
|
|
1111
|
-
* Returns an unsubscribe function.
|
|
1112
|
-
*
|
|
1113
|
-
* @example
|
|
1114
|
-
* ```ts
|
|
1115
|
-
* const manager = Op.interpret(fetchUser, { strategy: "restartable" });
|
|
1116
|
-
* const stop = Op.wire(manager, (user) => {
|
|
1117
|
-
* console.log("User loaded:", user.name);
|
|
1118
|
-
* });
|
|
1119
|
-
* manager.run(userId);
|
|
1120
|
-
* // ... later
|
|
1121
|
-
* stop(); // removes the subscription
|
|
1122
|
-
* ```
|
|
1123
|
-
*/
|
|
1124
|
-
const wire: <I, E, A, S extends State<E, A>>(source: Manager<I, E, A, S>, f: (a: A) => void) => () => void;
|
|
1125
|
-
/**
|
|
1126
|
-
* Attaches a concurrency strategy to an `Op`, returning a `Manager`.
|
|
1127
|
-
*
|
|
1128
|
-
* Strategy is data, not a method name. The `S` type parameter is narrowed to only
|
|
1129
|
-
* the states reachable for the chosen strategy and options — subscribers cannot
|
|
1130
|
-
* reference states that cannot occur.
|
|
1131
|
-
*
|
|
1132
|
-
* **Strategies:**
|
|
1133
|
-
* - `once` — fires once. Only the first `run()` executes; subsequent calls
|
|
1134
|
-
* return `DroppedNil` immediately. State is permanent after completion.
|
|
1135
|
-
* - `restartable` — new call cancels the previous (`ReplacedNil`). Only the latest result matters.
|
|
1136
|
-
* - `exclusive` — new calls while in-flight return `DroppedNil` immediately.
|
|
1137
|
-
* - `queue` — calls run in submission order. `Queued` state shows position.
|
|
1138
|
-
* - `buffered` — 1 in-flight + 1 waiting slot. Newer calls evict the slot (`EvictedNil`).
|
|
1139
|
-
* - `debounced` — waits `ms` ms of quiet before starting. Earlier calls get `EvictedNil`.
|
|
1140
|
-
*
|
|
1141
|
-
* **`retry` and `timeout`** can be combined with any strategy. Both are applied
|
|
1142
|
-
* internally per `run()` call — set the policy once, not at every call site.
|
|
1143
|
-
* The timeout wraps the entire retry sequence (one deadline for all attempts).
|
|
1144
|
-
* When `retry` is present, `Retrying` is added to the subscriber type.
|
|
1145
|
-
*
|
|
1146
|
-
* @example
|
|
1147
|
-
* ```ts
|
|
1148
|
-
* // Load once on mount — further calls are no-ops
|
|
1149
|
-
* const getUser = Op.interpret(fetchUser, { strategy: "once" });
|
|
1150
|
-
* getUser.subscribe(state => {
|
|
1151
|
-
* if (state.kind === "Pending") showSpinner();
|
|
1152
|
-
* if (state.kind === "OpOk") render(state.value);
|
|
1153
|
-
* });
|
|
1154
|
-
* getUser.run(userId);
|
|
1155
|
-
*
|
|
1156
|
-
* // Search: cancel the previous query when a new one starts
|
|
1157
|
-
* const search = Op.interpret(searchOp, { strategy: "restartable" });
|
|
1158
|
-
*
|
|
1159
|
-
* // Form submit: ignore double-clicks while in-flight
|
|
1160
|
-
* const submit = Op.interpret(submitOp, {
|
|
1161
|
-
* strategy: "exclusive",
|
|
1162
|
-
* retry: { attempts: 3, backoff: n => n * 500 },
|
|
1163
|
-
* timeout: { ms: 10_000, onTimeout: () => new ApiError("timed out") },
|
|
1164
|
-
* });
|
|
1165
|
-
*
|
|
1166
|
-
* // Auto-save: current save commits fully; latest pending edit saves next
|
|
1167
|
-
* const save = Op.interpret(saveOp, { strategy: "buffered" });
|
|
1168
|
-
* ```
|
|
1169
|
-
*/
|
|
1170
|
-
function interpret<I, E, A, O extends AllInterpretOptions<I, E>>(op: Op<I, E, A>, options: O): InterpretResult<I, E, A, O>;
|
|
1171
796
|
}
|
|
1172
797
|
|
|
1173
798
|
/** Keys of T for which undefined is assignable (i.e. optional fields). */
|
|
@@ -1196,8 +821,8 @@ type Optional<S, A> = {
|
|
|
1196
821
|
readonly get: (s: S) => Maybe<A>;
|
|
1197
822
|
readonly set: (a: A) => (s: S) => S;
|
|
1198
823
|
};
|
|
1199
|
-
declare
|
|
1200
|
-
|
|
824
|
+
declare const Optional: {
|
|
825
|
+
from: {
|
|
1201
826
|
/**
|
|
1202
827
|
* Constructs an Optional from a getter (returning Maybe<A>) and a setter.
|
|
1203
828
|
*
|
|
@@ -1209,7 +834,7 @@ declare namespace Optional {
|
|
|
1209
834
|
* );
|
|
1210
835
|
* ```
|
|
1211
836
|
*/
|
|
1212
|
-
|
|
837
|
+
accessors: <S, A>(get: (s: S) => Maybe<A>, set: (a: A) => (s: S) => S) => Optional<S, A>;
|
|
1213
838
|
/**
|
|
1214
839
|
* Creates an Optional that focuses on an optional property of an object.
|
|
1215
840
|
* Only keys whose type includes undefined (i.e. `field?: T`) are accepted.
|
|
@@ -1221,8 +846,8 @@ declare namespace Optional {
|
|
|
1221
846
|
* const bioOpt = Optional.from.property<Profile>()("bio");
|
|
1222
847
|
* ```
|
|
1223
848
|
*/
|
|
1224
|
-
|
|
1225
|
-
}
|
|
849
|
+
property: <S>() => <K extends OptionalKeys<S>>(key: K) => Optional<S, NonNullable<S[K]>>;
|
|
850
|
+
};
|
|
1226
851
|
/**
|
|
1227
852
|
* Creates an Optional that focuses on an element at a given index in an array.
|
|
1228
853
|
* Returns None when the index is out of bounds; set is a no-op when out of bounds.
|
|
@@ -1235,7 +860,7 @@ declare namespace Optional {
|
|
|
1235
860
|
* pipe([], Optional.get(firstItem)); // None
|
|
1236
861
|
* ```
|
|
1237
862
|
*/
|
|
1238
|
-
|
|
863
|
+
index: <A>(i: number) => Optional<A[], A>;
|
|
1239
864
|
/**
|
|
1240
865
|
* Reads the focused value from a structure, returning Maybe<A>.
|
|
1241
866
|
*
|
|
@@ -1244,7 +869,7 @@ declare namespace Optional {
|
|
|
1244
869
|
* pipe(profile, Optional.get(bioOpt)); // Some("...") or None
|
|
1245
870
|
* ```
|
|
1246
871
|
*/
|
|
1247
|
-
|
|
872
|
+
get: <S, A>(opt: Optional<S, A>) => (s: S) => Maybe<A>;
|
|
1248
873
|
/**
|
|
1249
874
|
* Replaces the focused value within a structure.
|
|
1250
875
|
* For indexed focuses, this is a no-op when the index is out of bounds.
|
|
@@ -1254,7 +879,7 @@ declare namespace Optional {
|
|
|
1254
879
|
* pipe(profile, Optional.set(bioOpt)("hello"));
|
|
1255
880
|
* ```
|
|
1256
881
|
*/
|
|
1257
|
-
|
|
882
|
+
set: <S, A>(opt: Optional<S, A>) => (a: A) => (s: S) => S;
|
|
1258
883
|
/**
|
|
1259
884
|
* Applies a function to the focused value if it is present; returns the
|
|
1260
885
|
* structure unchanged if the focus is absent.
|
|
@@ -1264,7 +889,7 @@ declare namespace Optional {
|
|
|
1264
889
|
* pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
|
|
1265
890
|
* ```
|
|
1266
891
|
*/
|
|
1267
|
-
|
|
892
|
+
modify: <S, A>(opt: Optional<S, A>) => (f: (a: A) => A) => (s: S) => S;
|
|
1268
893
|
/**
|
|
1269
894
|
* Returns the focused value or a default when the focus is absent.
|
|
1270
895
|
*
|
|
@@ -1273,7 +898,7 @@ declare namespace Optional {
|
|
|
1273
898
|
* pipe(profile, Optional.getOrElse(bioOpt)(() => "no bio"));
|
|
1274
899
|
* ```
|
|
1275
900
|
*/
|
|
1276
|
-
|
|
901
|
+
getOrElse: <S, A>(opt: Optional<S, A>) => (defaultValue: () => A) => (s: S) => A;
|
|
1277
902
|
/**
|
|
1278
903
|
* Extracts a value from an Optional focus using handlers for the present
|
|
1279
904
|
* and absent cases.
|
|
@@ -1283,7 +908,7 @@ declare namespace Optional {
|
|
|
1283
908
|
* pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
|
|
1284
909
|
* ```
|
|
1285
910
|
*/
|
|
1286
|
-
|
|
911
|
+
fold: <S, A>(opt: Optional<S, A>) => <B>(onNone: () => B, onSome: (a: A) => B) => (s: S) => B;
|
|
1287
912
|
/**
|
|
1288
913
|
* Pattern matches on an Optional focus using a named-case object.
|
|
1289
914
|
*
|
|
@@ -1295,7 +920,7 @@ declare namespace Optional {
|
|
|
1295
920
|
* );
|
|
1296
921
|
* ```
|
|
1297
922
|
*/
|
|
1298
|
-
|
|
923
|
+
match: <S, A>(opt: Optional<S, A>) => <B>(cases: {
|
|
1299
924
|
none: () => B;
|
|
1300
925
|
some: (a: A) => B;
|
|
1301
926
|
}) => (s: S) => B;
|
|
@@ -1311,7 +936,7 @@ declare namespace Optional {
|
|
|
1311
936
|
* );
|
|
1312
937
|
* ```
|
|
1313
938
|
*/
|
|
1314
|
-
|
|
939
|
+
andThen: <A, B>(inner: Optional<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
|
|
1315
940
|
/**
|
|
1316
941
|
* Composes an Optional with a Lens, producing an Optional.
|
|
1317
942
|
* The Lens focuses within the value found by the Optional.
|
|
@@ -1324,8 +949,8 @@ declare namespace Optional {
|
|
|
1324
949
|
* );
|
|
1325
950
|
* ```
|
|
1326
951
|
*/
|
|
1327
|
-
|
|
1328
|
-
}
|
|
952
|
+
andThenLens: <A, B>(inner: Lens<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
|
|
953
|
+
};
|
|
1329
954
|
|
|
1330
955
|
/**
|
|
1331
956
|
* Pair<A, B> represents a pair of two values that are always both present.
|
|
@@ -1350,8 +975,8 @@ declare namespace Optional {
|
|
|
1350
975
|
* ```
|
|
1351
976
|
*/
|
|
1352
977
|
type Pair<A, B> = readonly [A, B];
|
|
1353
|
-
declare
|
|
1354
|
-
|
|
978
|
+
declare const Pair: {
|
|
979
|
+
from: {
|
|
1355
980
|
/**
|
|
1356
981
|
* Creates a Pair from two values.
|
|
1357
982
|
*
|
|
@@ -1360,7 +985,7 @@ declare namespace Pair {
|
|
|
1360
985
|
* Pair.from.pair("Paris", 2_161_000); // ["Paris", 2161000]
|
|
1361
986
|
* ```
|
|
1362
987
|
*/
|
|
1363
|
-
|
|
988
|
+
pair: <A, B>(first: A, second: B) => Pair<A, B>;
|
|
1364
989
|
/**
|
|
1365
990
|
* Creates a Pair from a two-element array.
|
|
1366
991
|
*
|
|
@@ -1369,8 +994,8 @@ declare namespace Pair {
|
|
|
1369
994
|
* Pair.from.array(["Paris", 2_161_000] as const); // ["Paris", 2161000]
|
|
1370
995
|
* ```
|
|
1371
996
|
*/
|
|
1372
|
-
|
|
1373
|
-
}
|
|
997
|
+
array: <A, B>(arr: readonly [A, B]) => Pair<A, B>;
|
|
998
|
+
};
|
|
1374
999
|
/**
|
|
1375
1000
|
* Returns the first value from the pair.
|
|
1376
1001
|
*
|
|
@@ -1379,7 +1004,7 @@ declare namespace Pair {
|
|
|
1379
1004
|
* Pair.first(Pair.from.pair("Paris", 2_161_000)); // "Paris"
|
|
1380
1005
|
* ```
|
|
1381
1006
|
*/
|
|
1382
|
-
|
|
1007
|
+
first: <A, B>(p: Pair<A, B>) => A;
|
|
1383
1008
|
/**
|
|
1384
1009
|
* Returns the second value from the pair.
|
|
1385
1010
|
*
|
|
@@ -1388,7 +1013,7 @@ declare namespace Pair {
|
|
|
1388
1013
|
* Pair.second(Pair.from.pair("Paris", 2_161_000)); // 2161000
|
|
1389
1014
|
* ```
|
|
1390
1015
|
*/
|
|
1391
|
-
|
|
1016
|
+
second: <A, B>(p: Pair<A, B>) => B;
|
|
1392
1017
|
/**
|
|
1393
1018
|
* Transforms the first value, leaving the second unchanged.
|
|
1394
1019
|
*
|
|
@@ -1397,7 +1022,7 @@ declare namespace Pair {
|
|
|
1397
1022
|
* pipe(Pair.from.pair("alice", 42), Pair.mapFirst((s) => s.toUpperCase())); // ["ALICE", 42]
|
|
1398
1023
|
* ```
|
|
1399
1024
|
*/
|
|
1400
|
-
|
|
1025
|
+
mapFirst: <A, C>(f: (a: A) => C) => <B>(p: Pair<A, B>) => Pair<C, B>;
|
|
1401
1026
|
/**
|
|
1402
1027
|
* Transforms the second value, leaving the first unchanged.
|
|
1403
1028
|
*
|
|
@@ -1406,7 +1031,7 @@ declare namespace Pair {
|
|
|
1406
1031
|
* pipe(Pair.from.pair("alice", 42), Pair.mapSecond((n) => n * 2)); // ["alice", 84]
|
|
1407
1032
|
* ```
|
|
1408
1033
|
*/
|
|
1409
|
-
|
|
1034
|
+
mapSecond: <B, D>(f: (b: B) => D) => <A>(p: Pair<A, B>) => Pair<A, D>;
|
|
1410
1035
|
/**
|
|
1411
1036
|
* Transforms both values independently in a single step.
|
|
1412
1037
|
*
|
|
@@ -1421,7 +1046,7 @@ declare namespace Pair {
|
|
|
1421
1046
|
* ); // ["ALICE", 84]
|
|
1422
1047
|
* ```
|
|
1423
1048
|
*/
|
|
1424
|
-
|
|
1049
|
+
mapBoth: <A, C, B, D>(onFirst: (a: A) => C, onSecond: (b: B) => D) => (p: Pair<A, B>) => Pair<C, D>;
|
|
1425
1050
|
/**
|
|
1426
1051
|
* Applies a binary function to both values, collapsing the pair into a single value.
|
|
1427
1052
|
* Useful as the final step when consuming a pair in a pipeline.
|
|
@@ -1432,7 +1057,7 @@ declare namespace Pair {
|
|
|
1432
1057
|
* // "Alice: 100"
|
|
1433
1058
|
* ```
|
|
1434
1059
|
*/
|
|
1435
|
-
|
|
1060
|
+
fold: <A, B, C>(f: (a: A, b: B) => C) => (p: Pair<A, B>) => C;
|
|
1436
1061
|
/**
|
|
1437
1062
|
* Swaps the two values: `[A, B]` becomes `[B, A]`.
|
|
1438
1063
|
*
|
|
@@ -1441,8 +1066,8 @@ declare namespace Pair {
|
|
|
1441
1066
|
* Pair.swap(Pair.from.pair("key", 1)); // [1, "key"]
|
|
1442
1067
|
* ```
|
|
1443
1068
|
*/
|
|
1444
|
-
|
|
1445
|
-
|
|
1069
|
+
swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
|
|
1070
|
+
to: {
|
|
1446
1071
|
/**
|
|
1447
1072
|
* Converts the pair to a heterogeneous readonly array `readonly (A | B)[]`.
|
|
1448
1073
|
*
|
|
@@ -1451,8 +1076,8 @@ declare namespace Pair {
|
|
|
1451
1076
|
* Pair.to.Array(Pair.from.pair("hello", 42)); // ["hello", 42]
|
|
1452
1077
|
* ```
|
|
1453
1078
|
*/
|
|
1454
|
-
|
|
1455
|
-
}
|
|
1079
|
+
Array: <A, B>(p: Pair<A, B>) => readonly (A | B)[];
|
|
1080
|
+
};
|
|
1456
1081
|
/**
|
|
1457
1082
|
* Runs a side effect with both values without changing the pair.
|
|
1458
1083
|
* Useful for logging or debugging in the middle of a pipeline.
|
|
@@ -1466,8 +1091,8 @@ declare namespace Pair {
|
|
|
1466
1091
|
* ); // logs "Paris: 2161000", returns ["Paris", 2.161]
|
|
1467
1092
|
* ```
|
|
1468
1093
|
*/
|
|
1469
|
-
|
|
1470
|
-
}
|
|
1094
|
+
tap: <A, B>(f: (a: A, b: B) => void) => (p: Pair<A, B>) => Pair<A, B>;
|
|
1095
|
+
};
|
|
1471
1096
|
|
|
1472
1097
|
/**
|
|
1473
1098
|
* A boolean-valued function over a type `A`.
|
|
@@ -1496,7 +1121,7 @@ declare namespace Pair {
|
|
|
1496
1121
|
* ```
|
|
1497
1122
|
*/
|
|
1498
1123
|
type Predicate<A> = (a: A) => boolean;
|
|
1499
|
-
declare
|
|
1124
|
+
declare const Predicate: {
|
|
1500
1125
|
/**
|
|
1501
1126
|
* Negates a predicate: the result passes exactly when the original fails.
|
|
1502
1127
|
*
|
|
@@ -1509,7 +1134,7 @@ declare namespace Predicate {
|
|
|
1509
1134
|
* isNotBlank(" "); // false
|
|
1510
1135
|
* ```
|
|
1511
1136
|
*/
|
|
1512
|
-
|
|
1137
|
+
not: <A>(p: Predicate<A>) => Predicate<A>;
|
|
1513
1138
|
/**
|
|
1514
1139
|
* Combines two predicates with logical AND: passes only when both hold.
|
|
1515
1140
|
*
|
|
@@ -1527,7 +1152,7 @@ declare namespace Predicate {
|
|
|
1527
1152
|
* isPositiveEven(-2); // false — even but not positive
|
|
1528
1153
|
* ```
|
|
1529
1154
|
*/
|
|
1530
|
-
|
|
1155
|
+
and: <A>(second: Predicate<A>) => (first: Predicate<A>) => Predicate<A>;
|
|
1531
1156
|
/**
|
|
1532
1157
|
* Combines two predicates with logical OR: passes when either holds.
|
|
1533
1158
|
*
|
|
@@ -1545,7 +1170,7 @@ declare namespace Predicate {
|
|
|
1545
1170
|
* getsDiscount(30); // false
|
|
1546
1171
|
* ```
|
|
1547
1172
|
*/
|
|
1548
|
-
|
|
1173
|
+
or: <A>(second: Predicate<A>) => (first: Predicate<A>) => Predicate<A>;
|
|
1549
1174
|
/**
|
|
1550
1175
|
* Adapts a `Predicate<A>` to work on a different input type `B` by applying `f`
|
|
1551
1176
|
* to extract the relevant `A` from a `B` before running the check.
|
|
@@ -1568,7 +1193,7 @@ declare namespace Predicate {
|
|
|
1568
1193
|
* isAdultUser({ name: "Bob", age: 15 }); // false
|
|
1569
1194
|
* ```
|
|
1570
1195
|
*/
|
|
1571
|
-
|
|
1196
|
+
using: <A, B>(f: (b: B) => A) => (p: Predicate<A>) => Predicate<B>;
|
|
1572
1197
|
/**
|
|
1573
1198
|
* Combines an array of predicates with AND: passes only when every predicate holds.
|
|
1574
1199
|
* Returns `true` for an empty array (vacuous truth).
|
|
@@ -1587,7 +1212,7 @@ declare namespace Predicate {
|
|
|
1587
1212
|
* Predicate.all([])("anything"); // true
|
|
1588
1213
|
* ```
|
|
1589
1214
|
*/
|
|
1590
|
-
|
|
1215
|
+
all: <A>(predicates: ReadonlyArray<Predicate<A>>) => Predicate<A>;
|
|
1591
1216
|
/**
|
|
1592
1217
|
* Combines an array of predicates with OR: passes when at least one holds.
|
|
1593
1218
|
* Returns `false` for an empty array.
|
|
@@ -1605,8 +1230,8 @@ declare namespace Predicate {
|
|
|
1605
1230
|
* Predicate.any([])("anything"); // false
|
|
1606
1231
|
* ```
|
|
1607
1232
|
*/
|
|
1608
|
-
|
|
1609
|
-
|
|
1233
|
+
any: <A>(predicates: ReadonlyArray<Predicate<A>>) => Predicate<A>;
|
|
1234
|
+
from: {
|
|
1610
1235
|
/**
|
|
1611
1236
|
* Converts a `Refinement<A, B>` into a `Predicate<A>`, discarding the compile-time
|
|
1612
1237
|
* narrowing. Use this when you want to combine a type guard with plain predicates
|
|
@@ -1629,8 +1254,8 @@ declare namespace Predicate {
|
|
|
1629
1254
|
* isShortString(42); // false
|
|
1630
1255
|
* ```
|
|
1631
1256
|
*/
|
|
1632
|
-
|
|
1633
|
-
}
|
|
1257
|
+
Refinement: <A, B extends A>(r: Refinement<A, B>) => Predicate<A>;
|
|
1258
|
+
};
|
|
1634
1259
|
/**
|
|
1635
1260
|
* Performs declarative conditional branching over `[predicate, handler]` pairs,
|
|
1636
1261
|
* returning the handler result of the first matching predicate or evaluating the fallback.
|
|
@@ -1647,8 +1272,8 @@ declare namespace Predicate {
|
|
|
1647
1272
|
* classifyNumber(-5); // "negative"
|
|
1648
1273
|
* ```
|
|
1649
1274
|
*/
|
|
1650
|
-
|
|
1651
|
-
}
|
|
1275
|
+
match: <A, B>(branches: ReadonlyArray<readonly [Predicate<A>, (a: A) => B]>, fallback: (a: A) => B) => (a: A) => B;
|
|
1276
|
+
};
|
|
1652
1277
|
|
|
1653
1278
|
/**
|
|
1654
1279
|
* A computation that reads from a shared environment `R` and produces a value `A`.
|
|
@@ -1676,7 +1301,7 @@ declare namespace Predicate {
|
|
|
1676
1301
|
* ```
|
|
1677
1302
|
*/
|
|
1678
1303
|
type Reader<R, A> = (env: R) => A;
|
|
1679
|
-
declare
|
|
1304
|
+
declare const Reader: {
|
|
1680
1305
|
/**
|
|
1681
1306
|
* Lifts a pure value into a Reader. The environment is ignored.
|
|
1682
1307
|
*
|
|
@@ -1686,7 +1311,7 @@ declare namespace Reader {
|
|
|
1686
1311
|
* always42(anyConfig); // 42
|
|
1687
1312
|
* ```
|
|
1688
1313
|
*/
|
|
1689
|
-
|
|
1314
|
+
resolve: <R, A>(value: A) => Reader<R, A>;
|
|
1690
1315
|
/**
|
|
1691
1316
|
* Returns the full environment as the result.
|
|
1692
1317
|
* The fundamental way to access the environment in a pipeline.
|
|
@@ -1699,7 +1324,7 @@ declare namespace Reader {
|
|
|
1699
1324
|
* )(appConfig); // "https://api.example.com"
|
|
1700
1325
|
* ```
|
|
1701
1326
|
*/
|
|
1702
|
-
|
|
1327
|
+
ask: <R>() => Reader<R, R>;
|
|
1703
1328
|
/**
|
|
1704
1329
|
* Projects a value from the environment using a selector function.
|
|
1705
1330
|
* Equivalent to `pipe(Reader.ask(), Reader.map(f))` but more direct.
|
|
@@ -1710,7 +1335,7 @@ declare namespace Reader {
|
|
|
1710
1335
|
* getBaseUrl(appConfig); // "https://api.example.com"
|
|
1711
1336
|
* ```
|
|
1712
1337
|
*/
|
|
1713
|
-
|
|
1338
|
+
asks: <R, A>(f: (env: R) => A) => Reader<R, A>;
|
|
1714
1339
|
/**
|
|
1715
1340
|
* Transforms the value produced by a Reader.
|
|
1716
1341
|
*
|
|
@@ -1722,7 +1347,7 @@ declare namespace Reader {
|
|
|
1722
1347
|
* )(appConfig); // "HTTPS://API.EXAMPLE.COM"
|
|
1723
1348
|
* ```
|
|
1724
1349
|
*/
|
|
1725
|
-
|
|
1350
|
+
map: <R, A, B>(f: (a: A) => B) => (data: Reader<R, A>) => Reader<R, B>;
|
|
1726
1351
|
/**
|
|
1727
1352
|
* Sequences two Readers. Both see the same environment.
|
|
1728
1353
|
* The output of the first is passed to `f`, which returns the next Reader.
|
|
@@ -1741,7 +1366,7 @@ declare namespace Reader {
|
|
|
1741
1366
|
* )(appConfig); // "https://api.example.com/items?key=secret"
|
|
1742
1367
|
* ```
|
|
1743
1368
|
*/
|
|
1744
|
-
|
|
1369
|
+
chain: <R, A, B>(f: (a: A) => Reader<R, B>) => (data: Reader<R, A>) => Reader<R, B>;
|
|
1745
1370
|
/**
|
|
1746
1371
|
* Applies a function wrapped in a Reader to a value wrapped in a Reader.
|
|
1747
1372
|
* Both Readers see the same environment.
|
|
@@ -1756,7 +1381,7 @@ declare namespace Reader {
|
|
|
1756
1381
|
* )(appConfig);
|
|
1757
1382
|
* ```
|
|
1758
1383
|
*/
|
|
1759
|
-
|
|
1384
|
+
ap: <R, A>(arg: Reader<R, A>) => <B>(data: Reader<R, (a: A) => B>) => Reader<R, B>;
|
|
1760
1385
|
/**
|
|
1761
1386
|
* Executes a side effect on the produced value without changing the Reader.
|
|
1762
1387
|
* Useful for logging or debugging inside a pipeline.
|
|
@@ -1770,7 +1395,7 @@ declare namespace Reader {
|
|
|
1770
1395
|
* )(appConfig);
|
|
1771
1396
|
* ```
|
|
1772
1397
|
*/
|
|
1773
|
-
|
|
1398
|
+
tap: <R, A>(f: (a: A) => void) => (data: Reader<R, A>) => Reader<R, A>;
|
|
1774
1399
|
/**
|
|
1775
1400
|
* Adapts a Reader to work with a different (typically wider) environment
|
|
1776
1401
|
* by transforming the environment before passing it to the Reader.
|
|
@@ -1790,7 +1415,7 @@ declare namespace Reader {
|
|
|
1790
1415
|
* buildUrlFromApp(appEnv); // works with the full AppEnv
|
|
1791
1416
|
* ```
|
|
1792
1417
|
*/
|
|
1793
|
-
|
|
1418
|
+
local: <R2, R>(f: (env: R2) => R) => <A>(data: Reader<R, A>) => Reader<R2, A>;
|
|
1794
1419
|
/**
|
|
1795
1420
|
* Runs a Reader by supplying the environment. Use this at the edge of your
|
|
1796
1421
|
* program where the environment is available.
|
|
@@ -1803,7 +1428,7 @@ declare namespace Reader {
|
|
|
1803
1428
|
* ); // "https://api.example.com/users?key=secret"
|
|
1804
1429
|
* ```
|
|
1805
1430
|
*/
|
|
1806
|
-
|
|
1431
|
+
run: <R>(env: R) => <A>(data: Reader<R, A>) => A;
|
|
1807
1432
|
/**
|
|
1808
1433
|
* Lifts a Reader value into an accumulator object.
|
|
1809
1434
|
*
|
|
@@ -1812,7 +1437,7 @@ declare namespace Reader {
|
|
|
1812
1437
|
* pipe(Reader.resolve(42), Reader.bindTo("value")); // Reader({ value: 42 })
|
|
1813
1438
|
* ```
|
|
1814
1439
|
*/
|
|
1815
|
-
|
|
1440
|
+
bindTo: <K extends string>(key: K) => <R, A>(data: Reader<R, A>) => Reader<R, { [P in K]: A; }>;
|
|
1816
1441
|
/**
|
|
1817
1442
|
* Evaluates a new Reader using the current accumulator and attaches the output to a new key.
|
|
1818
1443
|
*
|
|
@@ -1824,8 +1449,8 @@ declare namespace Reader {
|
|
|
1824
1449
|
* ); // Reader({ a: 1, b: 2 })
|
|
1825
1450
|
* ```
|
|
1826
1451
|
*/
|
|
1827
|
-
|
|
1828
|
-
}
|
|
1452
|
+
bind: <K extends string, R, A, B>(key: K, f: (a: A) => Reader<R, B>) => (data: Reader<R, A>) => Reader<R, A & { [P in K]: B; }>;
|
|
1453
|
+
};
|
|
1829
1454
|
|
|
1830
1455
|
/**
|
|
1831
1456
|
* A function from `A` to `A is B` — a type predicate paired with a runtime check.
|
|
@@ -1850,8 +1475,8 @@ declare namespace Reader {
|
|
|
1850
1475
|
* ```
|
|
1851
1476
|
*/
|
|
1852
1477
|
type Refinement<A, B extends A> = (a: A) => a is B;
|
|
1853
|
-
declare
|
|
1854
|
-
|
|
1478
|
+
declare const Refinement: {
|
|
1479
|
+
from: {
|
|
1855
1480
|
/**
|
|
1856
1481
|
* Creates a `Refinement<A, B>` from a plain boolean predicate.
|
|
1857
1482
|
*
|
|
@@ -1868,8 +1493,8 @@ declare namespace Refinement {
|
|
|
1868
1493
|
* Refinement.from.predicate(n => n > 0);
|
|
1869
1494
|
* ```
|
|
1870
1495
|
*/
|
|
1871
|
-
|
|
1872
|
-
}
|
|
1496
|
+
predicate: <A, B extends A>(f: (a: A) => boolean) => Refinement<A, B>;
|
|
1497
|
+
};
|
|
1873
1498
|
/**
|
|
1874
1499
|
* Chains two refinements: if `ab` narrows `A` to `B` and `bc` narrows `B` to `C`,
|
|
1875
1500
|
* the result narrows `A` directly to `C`.
|
|
@@ -1892,7 +1517,7 @@ declare namespace Refinement {
|
|
|
1892
1517
|
* );
|
|
1893
1518
|
* ```
|
|
1894
1519
|
*/
|
|
1895
|
-
|
|
1520
|
+
compose: <A, B extends A, C extends B>(bc: Refinement<B, C>) => (ab: Refinement<A, B>) => Refinement<A, C>;
|
|
1896
1521
|
/**
|
|
1897
1522
|
* Intersects two refinements: the result narrows `A` to `B & C`, passing only
|
|
1898
1523
|
* when both refinements hold simultaneously.
|
|
@@ -1910,7 +1535,7 @@ declare namespace Refinement {
|
|
|
1910
1535
|
* isNonEmptyString(""); // false
|
|
1911
1536
|
* ```
|
|
1912
1537
|
*/
|
|
1913
|
-
|
|
1538
|
+
and: <A, C extends A>(second: Refinement<A, C>) => <B extends A>(first: Refinement<A, B>) => Refinement<A, B & C>;
|
|
1914
1539
|
/**
|
|
1915
1540
|
* Unions two refinements: the result narrows `A` to `B | C`, passing when either
|
|
1916
1541
|
* refinement holds.
|
|
@@ -1928,8 +1553,8 @@ declare namespace Refinement {
|
|
|
1928
1553
|
* isStringOrNumber(true); // false
|
|
1929
1554
|
* ```
|
|
1930
1555
|
*/
|
|
1931
|
-
|
|
1932
|
-
|
|
1556
|
+
or: <A, C extends A>(second: Refinement<A, C>) => <B extends A>(first: Refinement<A, B>) => Refinement<A, B | C>;
|
|
1557
|
+
to: {
|
|
1933
1558
|
/**
|
|
1934
1559
|
* Converts a `Refinement<A, B>` into a function `(a: A) => Maybe<B>`.
|
|
1935
1560
|
*
|
|
@@ -1946,7 +1571,7 @@ declare namespace Refinement {
|
|
|
1946
1571
|
* pipe(42, Refinement.to.Maybe(isPositive)); // Some(42)
|
|
1947
1572
|
* ```
|
|
1948
1573
|
*/
|
|
1949
|
-
|
|
1574
|
+
Maybe: <A, B extends A>(r: Refinement<A, B>) => (a: A) => Maybe<B>;
|
|
1950
1575
|
/**
|
|
1951
1576
|
* Converts a `Refinement<A, B>` into a function `(a: A) => Result<E, B>`.
|
|
1952
1577
|
*
|
|
@@ -1963,9 +1588,9 @@ declare namespace Refinement {
|
|
|
1963
1588
|
* pipe("hi", Refinement.to.Result(isNonEmpty, () => "must not be empty")); // Ok("hi")
|
|
1964
1589
|
* ```
|
|
1965
1590
|
*/
|
|
1966
|
-
|
|
1967
|
-
}
|
|
1968
|
-
}
|
|
1591
|
+
Result: <A, B extends A, E>(r: Refinement<A, B>, onFail: (a: A) => E) => (a: A) => Result<E, B>;
|
|
1592
|
+
};
|
|
1593
|
+
};
|
|
1969
1594
|
|
|
1970
1595
|
type NotAsked = WithKind<"NotAsked">;
|
|
1971
1596
|
type Loading = WithKind<"Loading">;
|
|
@@ -1992,8 +1617,8 @@ type Success<A> = WithKind<"Success"> & WithValue<A>;
|
|
|
1992
1617
|
* ```
|
|
1993
1618
|
*/
|
|
1994
1619
|
type RemoteData<E, A> = NotAsked | Loading | Failure<E> | Success<A>;
|
|
1995
|
-
declare
|
|
1996
|
-
|
|
1620
|
+
declare const RemoteData: {
|
|
1621
|
+
make: {
|
|
1997
1622
|
/**
|
|
1998
1623
|
* Creates a NotAsked RemoteData.
|
|
1999
1624
|
*
|
|
@@ -2002,7 +1627,7 @@ declare namespace RemoteData {
|
|
|
2002
1627
|
* RemoteData.make.notAsked(); // NotAsked
|
|
2003
1628
|
* ```
|
|
2004
1629
|
*/
|
|
2005
|
-
|
|
1630
|
+
notAsked: () => NotAsked;
|
|
2006
1631
|
/**
|
|
2007
1632
|
* Creates a Loading RemoteData.
|
|
2008
1633
|
*
|
|
@@ -2011,7 +1636,7 @@ declare namespace RemoteData {
|
|
|
2011
1636
|
* RemoteData.make.loading(); // Loading
|
|
2012
1637
|
* ```
|
|
2013
1638
|
*/
|
|
2014
|
-
|
|
1639
|
+
loading: () => Loading;
|
|
2015
1640
|
/**
|
|
2016
1641
|
* Creates a Failure RemoteData with the given error.
|
|
2017
1642
|
*
|
|
@@ -2020,7 +1645,7 @@ declare namespace RemoteData {
|
|
|
2020
1645
|
* RemoteData.make.failure("Network error"); // Failure("Network error")
|
|
2021
1646
|
* ```
|
|
2022
1647
|
*/
|
|
2023
|
-
|
|
1648
|
+
failure: <E>(error: E) => Failure<E>;
|
|
2024
1649
|
/**
|
|
2025
1650
|
* Creates a Success RemoteData with the given value.
|
|
2026
1651
|
*
|
|
@@ -2029,9 +1654,9 @@ declare namespace RemoteData {
|
|
|
2029
1654
|
* RemoteData.make.success(42); // Success(42)
|
|
2030
1655
|
* ```
|
|
2031
1656
|
*/
|
|
2032
|
-
|
|
2033
|
-
}
|
|
2034
|
-
|
|
1657
|
+
success: <A>(value: A) => Success<A>;
|
|
1658
|
+
};
|
|
1659
|
+
is: {
|
|
2035
1660
|
/**
|
|
2036
1661
|
* Type guard that checks if a RemoteData is NotAsked.
|
|
2037
1662
|
*
|
|
@@ -2043,7 +1668,7 @@ declare namespace RemoteData {
|
|
|
2043
1668
|
* }
|
|
2044
1669
|
* ```
|
|
2045
1670
|
*/
|
|
2046
|
-
|
|
1671
|
+
notAsked: <E, A>(data: RemoteData<E, A>) => data is NotAsked;
|
|
2047
1672
|
/**
|
|
2048
1673
|
* Type guard that checks if a RemoteData is Loading.
|
|
2049
1674
|
*
|
|
@@ -2055,7 +1680,7 @@ declare namespace RemoteData {
|
|
|
2055
1680
|
* }
|
|
2056
1681
|
* ```
|
|
2057
1682
|
*/
|
|
2058
|
-
|
|
1683
|
+
loading: <E, A>(data: RemoteData<E, A>) => data is Loading;
|
|
2059
1684
|
/**
|
|
2060
1685
|
* Type guard that checks if a RemoteData is Failure.
|
|
2061
1686
|
*
|
|
@@ -2067,7 +1692,7 @@ declare namespace RemoteData {
|
|
|
2067
1692
|
* }
|
|
2068
1693
|
* ```
|
|
2069
1694
|
*/
|
|
2070
|
-
|
|
1695
|
+
failure: <E, A>(data: RemoteData<E, A>) => data is Failure<E>;
|
|
2071
1696
|
/**
|
|
2072
1697
|
* Type guard that checks if a RemoteData is Success.
|
|
2073
1698
|
*
|
|
@@ -2079,8 +1704,8 @@ declare namespace RemoteData {
|
|
|
2079
1704
|
* }
|
|
2080
1705
|
* ```
|
|
2081
1706
|
*/
|
|
2082
|
-
|
|
2083
|
-
}
|
|
1707
|
+
success: <E, A>(data: RemoteData<E, A>) => data is Success<A>;
|
|
1708
|
+
};
|
|
2084
1709
|
/**
|
|
2085
1710
|
* Transforms the success value inside a RemoteData.
|
|
2086
1711
|
*
|
|
@@ -2090,7 +1715,7 @@ declare namespace RemoteData {
|
|
|
2090
1715
|
* pipe(RemoteData.make.loading(), RemoteData.map(n => n * 2)); // Loading
|
|
2091
1716
|
* ```
|
|
2092
1717
|
*/
|
|
2093
|
-
|
|
1718
|
+
map: <A, B>(f: (a: A) => B) => <E>(data: RemoteData<E, A>) => RemoteData<E, B>;
|
|
2094
1719
|
/**
|
|
2095
1720
|
* Transforms the error value inside a RemoteData.
|
|
2096
1721
|
*
|
|
@@ -2099,7 +1724,7 @@ declare namespace RemoteData {
|
|
|
2099
1724
|
* pipe(RemoteData.make.failure("oops"), RemoteData.mapError(e => e.toUpperCase())); // Failure("OOPS")
|
|
2100
1725
|
* ```
|
|
2101
1726
|
*/
|
|
2102
|
-
|
|
1727
|
+
mapError: <E, F>(f: (e: E) => F) => <A>(data: RemoteData<E, A>) => RemoteData<F, A>;
|
|
2103
1728
|
/**
|
|
2104
1729
|
* Chains RemoteData computations. If the input is Success, passes the value to f.
|
|
2105
1730
|
* Otherwise, propagates the current state.
|
|
@@ -2112,7 +1737,7 @@ declare namespace RemoteData {
|
|
|
2112
1737
|
* );
|
|
2113
1738
|
* ```
|
|
2114
1739
|
*/
|
|
2115
|
-
|
|
1740
|
+
chain: <E2, A, B>(f: (a: A) => RemoteData<E2, B>) => <E1 = never>(data: RemoteData<E1, A>) => RemoteData<E1 | E2, B>;
|
|
2116
1741
|
/**
|
|
2117
1742
|
* Applies a function wrapped in a RemoteData to a value wrapped in a RemoteData.
|
|
2118
1743
|
*
|
|
@@ -2126,7 +1751,7 @@ declare namespace RemoteData {
|
|
|
2126
1751
|
* ); // Success(8)
|
|
2127
1752
|
* ```
|
|
2128
1753
|
*/
|
|
2129
|
-
|
|
1754
|
+
ap: <E, A>(arg: RemoteData<E, A>) => <B>(data: RemoteData<E, (a: A) => B>) => RemoteData<E, B>;
|
|
2130
1755
|
/**
|
|
2131
1756
|
* Extracts the value from a RemoteData by providing handlers for all four cases.
|
|
2132
1757
|
*
|
|
@@ -2143,7 +1768,7 @@ declare namespace RemoteData {
|
|
|
2143
1768
|
* );
|
|
2144
1769
|
* ```
|
|
2145
1770
|
*/
|
|
2146
|
-
|
|
1771
|
+
fold: <E, A, B>(onFailure: (e: E) => B, onNotAsked: () => B, onLoading: () => B, onSuccess: (a: A) => B) => (data: RemoteData<E, A>) => B;
|
|
2147
1772
|
/**
|
|
2148
1773
|
* Pattern matches on a RemoteData, returning the result of the matching case.
|
|
2149
1774
|
*
|
|
@@ -2160,7 +1785,7 @@ declare namespace RemoteData {
|
|
|
2160
1785
|
* );
|
|
2161
1786
|
* ```
|
|
2162
1787
|
*/
|
|
2163
|
-
|
|
1788
|
+
match: <E, A, B>(cases: {
|
|
2164
1789
|
notAsked: () => B;
|
|
2165
1790
|
loading: () => B;
|
|
2166
1791
|
failure: (e: E) => B;
|
|
@@ -2177,7 +1802,7 @@ declare namespace RemoteData {
|
|
|
2177
1802
|
* pipe(RemoteData.make.loading<string, number>(), RemoteData.getOrElse(() => null)); // null — typed as number | null
|
|
2178
1803
|
* ```
|
|
2179
1804
|
*/
|
|
2180
|
-
|
|
1805
|
+
getOrElse: <B>(defaultValue: () => B) => <E, A>(data: RemoteData<E, A>) => A | B;
|
|
2181
1806
|
/**
|
|
2182
1807
|
* Executes a side effect on the success value without changing the RemoteData.
|
|
2183
1808
|
*
|
|
@@ -2190,7 +1815,7 @@ declare namespace RemoteData {
|
|
|
2190
1815
|
* );
|
|
2191
1816
|
* ```
|
|
2192
1817
|
*/
|
|
2193
|
-
|
|
1818
|
+
tap: <E, A>(f: (a: A) => void) => (data: RemoteData<E, A>) => RemoteData<E, A>;
|
|
2194
1819
|
/**
|
|
2195
1820
|
* Executes a side effect on the failure error without changing the RemoteData.
|
|
2196
1821
|
* Useful for logging errors.
|
|
@@ -2204,18 +1829,18 @@ declare namespace RemoteData {
|
|
|
2204
1829
|
* );
|
|
2205
1830
|
* ```
|
|
2206
1831
|
*/
|
|
2207
|
-
|
|
1832
|
+
tapError: <E, A>(f: (e: E) => void) => (data: RemoteData<E, A>) => RemoteData<E, A>;
|
|
2208
1833
|
/**
|
|
2209
1834
|
* Recovers from a Failure state by providing a fallback RemoteData.
|
|
2210
1835
|
* The fallback can produce a different success type, widening the result to `RemoteData<E, A | B>`.
|
|
2211
1836
|
*/
|
|
2212
|
-
|
|
2213
|
-
|
|
1837
|
+
recover: <E, B>(fallback: (e: E) => RemoteData<E, B>) => <A>(data: RemoteData<E, A>) => RemoteData<E, A | B>;
|
|
1838
|
+
to: {
|
|
2214
1839
|
/**
|
|
2215
1840
|
* Converts a RemoteData to a Maybe.
|
|
2216
1841
|
* Success becomes Some, all other states become None.
|
|
2217
1842
|
*/
|
|
2218
|
-
|
|
1843
|
+
Maybe: <E, A>(data: RemoteData<E, A>) => Maybe<A>;
|
|
2219
1844
|
/**
|
|
2220
1845
|
* Converts a RemoteData to a Result.
|
|
2221
1846
|
* Success becomes Ok, Failure becomes Err.
|
|
@@ -2229,9 +1854,9 @@ declare namespace RemoteData {
|
|
|
2229
1854
|
* ); // Ok(42)
|
|
2230
1855
|
* ```
|
|
2231
1856
|
*/
|
|
2232
|
-
|
|
2233
|
-
}
|
|
2234
|
-
|
|
1857
|
+
Result: <E>(onNotReady: () => E) => <A>(data: RemoteData<E, A>) => Result<E, A>;
|
|
1858
|
+
};
|
|
1859
|
+
from: {
|
|
2235
1860
|
/**
|
|
2236
1861
|
* Converts a Result to a RemoteData.
|
|
2237
1862
|
* Ok becomes Success, Err becomes Failure.
|
|
@@ -2242,7 +1867,7 @@ declare namespace RemoteData {
|
|
|
2242
1867
|
* setState(RemoteData.from.Result(result)); // Success(user) or Failure(msg)
|
|
2243
1868
|
* ```
|
|
2244
1869
|
*/
|
|
2245
|
-
|
|
1870
|
+
Result: <E, A>(data: Result<E, A>) => RemoteData<E, A>;
|
|
2246
1871
|
/**
|
|
2247
1872
|
* Converts a Maybe to a RemoteData.
|
|
2248
1873
|
* Some becomes Success, None becomes Failure using the onNone error producer.
|
|
@@ -2253,8 +1878,8 @@ declare namespace RemoteData {
|
|
|
2253
1878
|
* pipe(Maybe.make.none(), RemoteData.from.Maybe(() => "not found")); // Failure("not found")
|
|
2254
1879
|
* ```
|
|
2255
1880
|
*/
|
|
2256
|
-
|
|
2257
|
-
}
|
|
1881
|
+
Maybe: <E>(onNone: () => E) => <A>(data: Maybe<A>) => RemoteData<E, A>;
|
|
1882
|
+
};
|
|
2258
1883
|
/**
|
|
2259
1884
|
* Filters a `Success` value. When the predicate passes, the value is kept. When it fails,
|
|
2260
1885
|
* `Success` becomes `Failure` using the error produced by `onFalse`. All other states pass through unchanged.
|
|
@@ -2268,8 +1893,8 @@ declare namespace RemoteData {
|
|
|
2268
1893
|
* RemoteData.filter(n => n > 0, () => "error")(RemoteData.make.loading()); // Loading
|
|
2269
1894
|
* ```
|
|
2270
1895
|
*/
|
|
2271
|
-
|
|
2272
|
-
}
|
|
1896
|
+
filter: <E, A>(pred: (a: A) => boolean, onFalse: (a: A) => E) => (data: RemoteData<E, A>) => RemoteData<E, A>;
|
|
1897
|
+
};
|
|
2273
1898
|
|
|
2274
1899
|
/**
|
|
2275
1900
|
* A Resource pairs an async acquisition step with a guaranteed cleanup step.
|
|
@@ -2303,8 +1928,8 @@ type Resource<E, A> = {
|
|
|
2303
1928
|
readonly acquire: Task.Result<E, A>;
|
|
2304
1929
|
readonly release: (a: A) => Task<void>;
|
|
2305
1930
|
};
|
|
2306
|
-
declare
|
|
2307
|
-
|
|
1931
|
+
declare const Resource: {
|
|
1932
|
+
from: {
|
|
2308
1933
|
/**
|
|
2309
1934
|
* Creates a Resource from an acquire operation that may fail and a release function.
|
|
2310
1935
|
*
|
|
@@ -2316,7 +1941,7 @@ declare namespace Resource {
|
|
|
2316
1941
|
* );
|
|
2317
1942
|
* ```
|
|
2318
1943
|
*/
|
|
2319
|
-
|
|
1944
|
+
handlers: <E, A>(acquire: Task.Result<E, A>, release: (a: A) => Task<void>) => Resource<E, A>;
|
|
2320
1945
|
/**
|
|
2321
1946
|
* Creates a Resource from an acquire operation that cannot fail.
|
|
2322
1947
|
* Use this when opening the resource is guaranteed to succeed, such as
|
|
@@ -2330,8 +1955,8 @@ declare namespace Resource {
|
|
|
2330
1955
|
* );
|
|
2331
1956
|
* ```
|
|
2332
1957
|
*/
|
|
2333
|
-
|
|
2334
|
-
}
|
|
1958
|
+
Task: <E, A>(acquire: Task<A>, release: (a: A) => Task<void>) => Resource<E, A>;
|
|
1959
|
+
};
|
|
2335
1960
|
/**
|
|
2336
1961
|
* Acquires the resource, runs `f` with it, then releases it.
|
|
2337
1962
|
*
|
|
@@ -2347,7 +1972,7 @@ declare namespace Resource {
|
|
|
2347
1972
|
* // conn is closed whether the query succeeds or fails
|
|
2348
1973
|
* ```
|
|
2349
1974
|
*/
|
|
2350
|
-
|
|
1975
|
+
use: <E, A, B>(f: (a: A) => Task.Result<E, B>) => (resource: Resource<E, A>) => Task.Result<E, B>;
|
|
2351
1976
|
/**
|
|
2352
1977
|
* Acquires two resources in sequence and presents them as a tuple.
|
|
2353
1978
|
* Resources are released in reverse order: the second is released before the first.
|
|
@@ -2365,8 +1990,8 @@ declare namespace Resource {
|
|
|
2365
1990
|
* )();
|
|
2366
1991
|
* ```
|
|
2367
1992
|
*/
|
|
2368
|
-
|
|
2369
|
-
}
|
|
1993
|
+
combine: <E, A, B>(resourceA: Resource<E, A>, resourceB: Resource<E, B>) => Resource<E, readonly [A, B]>;
|
|
1994
|
+
};
|
|
2370
1995
|
|
|
2371
1996
|
/**
|
|
2372
1997
|
* A synchronous computation that threads a piece of mutable state `S` through
|
|
@@ -2393,7 +2018,7 @@ declare namespace Resource {
|
|
|
2393
2018
|
* ```
|
|
2394
2019
|
*/
|
|
2395
2020
|
type State<S, A> = (s: S) => readonly [A, S];
|
|
2396
|
-
declare
|
|
2021
|
+
declare const State: {
|
|
2397
2022
|
/**
|
|
2398
2023
|
* Lifts a pure value into a State computation. The state passes through unchanged.
|
|
2399
2024
|
*
|
|
@@ -2402,7 +2027,7 @@ declare namespace State {
|
|
|
2402
2027
|
* State.run(10)(State.resolve(42)); // [42, 10] — value 42, state unchanged
|
|
2403
2028
|
* ```
|
|
2404
2029
|
*/
|
|
2405
|
-
|
|
2030
|
+
resolve: <S, A>(value: A) => State<S, A>;
|
|
2406
2031
|
/**
|
|
2407
2032
|
* Produces the current state as the value, without modifying it.
|
|
2408
2033
|
*
|
|
@@ -2412,7 +2037,7 @@ declare namespace State {
|
|
|
2412
2037
|
* State.run(["a", "b"])(readStack); // [["a", "b"], ["a", "b"]]
|
|
2413
2038
|
* ```
|
|
2414
2039
|
*/
|
|
2415
|
-
|
|
2040
|
+
get: <S>() => State<S, S>;
|
|
2416
2041
|
/**
|
|
2417
2042
|
* Reads a projection of the state without modifying it.
|
|
2418
2043
|
* Equivalent to `pipe(State.get(), State.map(f))` but more direct.
|
|
@@ -2424,7 +2049,7 @@ declare namespace State {
|
|
|
2424
2049
|
* State.run({ count: 5, label: "x" })(readCount); // [5, { count: 5, label: "x" }]
|
|
2425
2050
|
* ```
|
|
2426
2051
|
*/
|
|
2427
|
-
|
|
2052
|
+
gets: <S, A>(f: (s: S) => A) => State<S, A>;
|
|
2428
2053
|
/**
|
|
2429
2054
|
* Replaces the current state with a new value. Produces no meaningful value.
|
|
2430
2055
|
*
|
|
@@ -2434,7 +2059,7 @@ declare namespace State {
|
|
|
2434
2059
|
* State.run(99)(reset); // [undefined, 0]
|
|
2435
2060
|
* ```
|
|
2436
2061
|
*/
|
|
2437
|
-
|
|
2062
|
+
put: <S>(newState: S) => State<S, undefined>;
|
|
2438
2063
|
/**
|
|
2439
2064
|
* Applies a function to the current state to produce the next state.
|
|
2440
2065
|
* Produces no meaningful value.
|
|
@@ -2447,7 +2072,7 @@ declare namespace State {
|
|
|
2447
2072
|
* State.run(["a"])(push("b")); // [undefined, ["a", "b"]]
|
|
2448
2073
|
* ```
|
|
2449
2074
|
*/
|
|
2450
|
-
|
|
2075
|
+
modify: <S>(f: (s: S) => S) => State<S, undefined>;
|
|
2451
2076
|
/**
|
|
2452
2077
|
* Transforms the value produced by a State computation.
|
|
2453
2078
|
* The state transformation is unchanged.
|
|
@@ -2462,7 +2087,7 @@ declare namespace State {
|
|
|
2462
2087
|
* State.run(["a", "b", "c"])(readLength); // [3, ["a", "b", "c"]]
|
|
2463
2088
|
* ```
|
|
2464
2089
|
*/
|
|
2465
|
-
|
|
2090
|
+
map: <S, A, B>(f: (a: A) => B) => (st: State<S, A>) => State<S, B>;
|
|
2466
2091
|
/**
|
|
2467
2092
|
* Sequences two State computations. The state output of the first is passed
|
|
2468
2093
|
* as the state input to the second.
|
|
@@ -2483,7 +2108,7 @@ declare namespace State {
|
|
|
2483
2108
|
* State.evaluate([])(program); // ["a", "b"]
|
|
2484
2109
|
* ```
|
|
2485
2110
|
*/
|
|
2486
|
-
|
|
2111
|
+
chain: <S, A, B>(f: (a: A) => State<S, B>) => (st: State<S, A>) => State<S, B>;
|
|
2487
2112
|
/**
|
|
2488
2113
|
* Applies a function wrapped in a State to a value wrapped in a State.
|
|
2489
2114
|
* The function computation runs first; its output state is the input to the
|
|
@@ -2501,7 +2126,7 @@ declare namespace State {
|
|
|
2501
2126
|
* State.evaluate(3)(program); // 6 + 3 = 9
|
|
2502
2127
|
* ```
|
|
2503
2128
|
*/
|
|
2504
|
-
|
|
2129
|
+
ap: <S, A>(arg: State<S, A>) => <B>(fn: State<S, (a: A) => B>) => State<S, B>;
|
|
2505
2130
|
/**
|
|
2506
2131
|
* Runs a side effect on the produced value without changing the State computation.
|
|
2507
2132
|
*
|
|
@@ -2514,7 +2139,7 @@ declare namespace State {
|
|
|
2514
2139
|
* );
|
|
2515
2140
|
* ```
|
|
2516
2141
|
*/
|
|
2517
|
-
|
|
2142
|
+
tap: <S, A>(f: (a: A) => void) => (st: State<S, A>) => State<S, A>;
|
|
2518
2143
|
/**
|
|
2519
2144
|
* Runs a State computation with an initial state, returning both the
|
|
2520
2145
|
* produced value and the final state as a pair.
|
|
@@ -2531,7 +2156,7 @@ declare namespace State {
|
|
|
2531
2156
|
* State.run(0)(program); // [1, 1]
|
|
2532
2157
|
* ```
|
|
2533
2158
|
*/
|
|
2534
|
-
|
|
2159
|
+
run: <S>(initialState: S) => <A>(st: State<S, A>) => readonly [A, S];
|
|
2535
2160
|
/**
|
|
2536
2161
|
* Runs a State computation with an initial state, returning only the
|
|
2537
2162
|
* produced value (discarding the final state).
|
|
@@ -2544,7 +2169,7 @@ declare namespace State {
|
|
|
2544
2169
|
* )); // ["x"]
|
|
2545
2170
|
* ```
|
|
2546
2171
|
*/
|
|
2547
|
-
|
|
2172
|
+
evaluate: <S>(initialState: S) => <A>(st: State<S, A>) => A;
|
|
2548
2173
|
/**
|
|
2549
2174
|
* Runs a State computation with an initial state, returning only the
|
|
2550
2175
|
* final state (discarding the produced value).
|
|
@@ -2557,7 +2182,7 @@ declare namespace State {
|
|
|
2557
2182
|
* )); // 20
|
|
2558
2183
|
* ```
|
|
2559
2184
|
*/
|
|
2560
|
-
|
|
2185
|
+
execute: <S>(initialState: S) => <A>(st: State<S, A>) => S;
|
|
2561
2186
|
/**
|
|
2562
2187
|
* Lifts a State value into an accumulator object.
|
|
2563
2188
|
*
|
|
@@ -2566,7 +2191,7 @@ declare namespace State {
|
|
|
2566
2191
|
* pipe(State.resolve(42), State.bindTo("value")); // State({ value: 42 })
|
|
2567
2192
|
* ```
|
|
2568
2193
|
*/
|
|
2569
|
-
|
|
2194
|
+
bindTo: <K extends string>(key: K) => <S, A>(data: State<S, A>) => State<S, { [P in K]: A; }>;
|
|
2570
2195
|
/**
|
|
2571
2196
|
* Evaluates a new State using the current accumulator and attaches the output to a new key.
|
|
2572
2197
|
*
|
|
@@ -2578,7 +2203,7 @@ declare namespace State {
|
|
|
2578
2203
|
* ); // State({ a: 1, b: 2 })
|
|
2579
2204
|
* ```
|
|
2580
2205
|
*/
|
|
2581
|
-
|
|
2206
|
+
bind: <K extends string, S, A, B>(key: K, f: (a: A) => State<S, B>) => (data: State<S, A>) => State<S, A & { [P in K]: B; }>;
|
|
2582
2207
|
/**
|
|
2583
2208
|
* Focuses a State computation on a sub-state using a Lens.
|
|
2584
2209
|
*
|
|
@@ -2590,8 +2215,8 @@ declare namespace State {
|
|
|
2590
2215
|
* const focusedProgram = pipe(increment, State.focus(countLens));
|
|
2591
2216
|
* ```
|
|
2592
2217
|
*/
|
|
2593
|
-
|
|
2594
|
-
}
|
|
2218
|
+
focus: <S, A>(lens: Lens<S, A>) => <B>(stateOp: State<A, B>) => State<S, B>;
|
|
2219
|
+
};
|
|
2595
2220
|
|
|
2596
2221
|
/**
|
|
2597
2222
|
* An event stream pipeline for a typed message schema `S`.
|
|
@@ -2644,63 +2269,7 @@ type Stream<S extends Record<string, unknown>> = {
|
|
|
2644
2269
|
/** @internal */
|
|
2645
2270
|
_isEmitting: boolean;
|
|
2646
2271
|
};
|
|
2647
|
-
declare
|
|
2648
|
-
/**
|
|
2649
|
-
* Message payload emitted across a Stream.
|
|
2650
|
-
*/
|
|
2651
|
-
type Message<S extends Record<string, unknown>> = {
|
|
2652
|
-
[K in keyof S & string]: WithKind<K> & WithValue<S[K]>;
|
|
2653
|
-
}[keyof S & string];
|
|
2654
|
-
/**
|
|
2655
|
-
* Options for constructing a `Stream` instance.
|
|
2656
|
-
*/
|
|
2657
|
-
type Options = {
|
|
2658
|
-
readonly name?: string;
|
|
2659
|
-
readonly onError?: (error: unknown) => void;
|
|
2660
|
-
};
|
|
2661
|
-
/**
|
|
2662
|
-
* Options for sequence matching and listener execution.
|
|
2663
|
-
*/
|
|
2664
|
-
type SequenceOptions<S extends Record<string, unknown>> = {
|
|
2665
|
-
/** Match events in exact array sequence order (default: false). */
|
|
2666
|
-
readonly ordered?: boolean;
|
|
2667
|
-
/** Match sequence strictly consecutively without intermediary events (default: false). */
|
|
2668
|
-
readonly strict?: boolean;
|
|
2669
|
-
/** Automatically unsubscribe after the first match/cycle completes (default: false). */
|
|
2670
|
-
readonly once?: boolean;
|
|
2671
|
-
/** Event kind(s) that reset sequence tracking to index 0. */
|
|
2672
|
-
readonly reset?: (keyof S & string) | ReadonlyArray<keyof S & string>;
|
|
2673
|
-
/** Event kind(s) in the sequence that may be present or skipped. */
|
|
2674
|
-
readonly optional?: (keyof S & string) | ReadonlyArray<keyof S & string>;
|
|
2675
|
-
};
|
|
2676
|
-
/**
|
|
2677
|
-
* Handle for an active stateful subscription.
|
|
2678
|
-
*/
|
|
2679
|
-
type Subscription<State> = {
|
|
2680
|
-
readonly unsubscribe: () => void;
|
|
2681
|
-
readonly getState: () => State;
|
|
2682
|
-
};
|
|
2683
|
-
/**
|
|
2684
|
-
* Options for structural stream forwarding.
|
|
2685
|
-
*/
|
|
2686
|
-
type ForwardOptions<S extends Record<string, unknown>> = {
|
|
2687
|
-
readonly from: Stream<S>;
|
|
2688
|
-
readonly to: Stream<S> | ReadonlyArray<Stream<S>>;
|
|
2689
|
-
readonly only?: ReadonlyArray<keyof S & string>;
|
|
2690
|
-
};
|
|
2691
|
-
/**
|
|
2692
|
-
* Builder handle returned by `Stream.listen`.
|
|
2693
|
-
*/
|
|
2694
|
-
type ListenerBuilder<S extends Record<string, unknown>> = {
|
|
2695
|
-
/**
|
|
2696
|
-
* Stateful reduction over events/sequences.
|
|
2697
|
-
*/
|
|
2698
|
-
readonly reduce: <State>(reducer: (msg: Message<S>, state: State) => State, initialState: State) => Subscription<State>;
|
|
2699
|
-
/**
|
|
2700
|
-
* Stateless side-effect execution.
|
|
2701
|
-
*/
|
|
2702
|
-
readonly tap: (effect: (msg: Message<S>) => void) => () => void;
|
|
2703
|
-
};
|
|
2272
|
+
declare const Stream: {
|
|
2704
2273
|
/**
|
|
2705
2274
|
* Constructs a new `Stream` instance.
|
|
2706
2275
|
*
|
|
@@ -2709,7 +2278,7 @@ declare namespace Stream {
|
|
|
2709
2278
|
* const stream = Stream.make<AppMessages>({ name: "app" });
|
|
2710
2279
|
* ```
|
|
2711
2280
|
*/
|
|
2712
|
-
|
|
2281
|
+
make: <S extends Record<string, unknown>>(options?: Stream.Options) => Stream<S>;
|
|
2713
2282
|
/**
|
|
2714
2283
|
* Emits a message payload to one or more target streams.
|
|
2715
2284
|
*
|
|
@@ -2728,7 +2297,7 @@ declare namespace Stream {
|
|
|
2728
2297
|
* });
|
|
2729
2298
|
* ```
|
|
2730
2299
|
*/
|
|
2731
|
-
|
|
2300
|
+
emit: <S extends Record<string, unknown>, K extends keyof S & string>(target: Stream<S> | ReadonlyArray<Stream<S>>, message: WithKind<K> & WithValue<S[K]>) => void;
|
|
2732
2301
|
/**
|
|
2733
2302
|
* Forwards messages from one stream to another (or multiple).
|
|
2734
2303
|
*
|
|
@@ -2741,7 +2310,7 @@ declare namespace Stream {
|
|
|
2741
2310
|
* });
|
|
2742
2311
|
* ```
|
|
2743
2312
|
*/
|
|
2744
|
-
|
|
2313
|
+
forward: <S extends Record<string, unknown>>(options: Stream.ForwardOptions<S>) => () => void;
|
|
2745
2314
|
/**
|
|
2746
2315
|
* Initiates listener registration on a stream for specific event kind(s) or sequence.
|
|
2747
2316
|
*
|
|
@@ -2757,7 +2326,36 @@ declare namespace Stream {
|
|
|
2757
2326
|
* );
|
|
2758
2327
|
* ```
|
|
2759
2328
|
*/
|
|
2760
|
-
|
|
2329
|
+
listen: <S extends Record<string, unknown>, K extends keyof S & string>(stream: Stream<S>, events: K | ReadonlyArray<K>, options?: Stream.SequenceOptions<S>) => Stream.ListenerBuilder<S>;
|
|
2330
|
+
};
|
|
2331
|
+
declare namespace Stream {
|
|
2332
|
+
type Message<S extends Record<string, unknown>> = {
|
|
2333
|
+
[K in keyof S & string]: WithKind<K> & WithValue<S[K]>;
|
|
2334
|
+
}[keyof S & string];
|
|
2335
|
+
type Options = {
|
|
2336
|
+
readonly name?: string;
|
|
2337
|
+
readonly onError?: (error: unknown) => void;
|
|
2338
|
+
};
|
|
2339
|
+
type SequenceOptions<S extends Record<string, unknown>> = {
|
|
2340
|
+
readonly ordered?: boolean;
|
|
2341
|
+
readonly strict?: boolean;
|
|
2342
|
+
readonly once?: boolean;
|
|
2343
|
+
readonly reset?: (keyof S & string) | ReadonlyArray<keyof S & string>;
|
|
2344
|
+
readonly optional?: (keyof S & string) | ReadonlyArray<keyof S & string>;
|
|
2345
|
+
};
|
|
2346
|
+
type Subscription<State> = {
|
|
2347
|
+
readonly unsubscribe: () => void;
|
|
2348
|
+
readonly getState: () => State;
|
|
2349
|
+
};
|
|
2350
|
+
type ForwardOptions<S extends Record<string, unknown>> = {
|
|
2351
|
+
readonly from: Stream<S>;
|
|
2352
|
+
readonly to: Stream<S> | ReadonlyArray<Stream<S>>;
|
|
2353
|
+
readonly only?: ReadonlyArray<keyof S & string>;
|
|
2354
|
+
};
|
|
2355
|
+
type ListenerBuilder<S extends Record<string, unknown>> = {
|
|
2356
|
+
readonly reduce: <State>(reducer: (msg: Message<S>, state: State) => State, initialState: State) => Subscription<State>;
|
|
2357
|
+
readonly tap: (effect: (msg: Message<S>) => void) => () => void;
|
|
2358
|
+
};
|
|
2761
2359
|
}
|
|
2762
2360
|
|
|
2763
2361
|
type TheseFirst<T> = WithKind<"First"> & WithFirst<T>;
|
|
@@ -2787,8 +2385,8 @@ type TheseBoth<First, Second> = WithKind<"Both"> & WithFirst<First> & WithSecond
|
|
|
2787
2385
|
* ```
|
|
2788
2386
|
*/
|
|
2789
2387
|
type These<A, B> = TheseFirst<A> | TheseSecond<B> | TheseBoth<A, B>;
|
|
2790
|
-
declare
|
|
2791
|
-
|
|
2388
|
+
declare const These: {
|
|
2389
|
+
make: {
|
|
2792
2390
|
/**
|
|
2793
2391
|
* Creates a These holding only a first value.
|
|
2794
2392
|
*
|
|
@@ -2797,7 +2395,7 @@ declare namespace These {
|
|
|
2797
2395
|
* These.make.first(42); // { kind: "First", first: 42 }
|
|
2798
2396
|
* ```
|
|
2799
2397
|
*/
|
|
2800
|
-
|
|
2398
|
+
first: <A>(value: A) => TheseFirst<A>;
|
|
2801
2399
|
/**
|
|
2802
2400
|
* Creates a These holding only a second value.
|
|
2803
2401
|
*
|
|
@@ -2806,7 +2404,7 @@ declare namespace These {
|
|
|
2806
2404
|
* These.make.second("warning"); // { kind: "Second", second: "warning" }
|
|
2807
2405
|
* ```
|
|
2808
2406
|
*/
|
|
2809
|
-
|
|
2407
|
+
second: <B>(value: B) => TheseSecond<B>;
|
|
2810
2408
|
/**
|
|
2811
2409
|
* Creates a These holding both a first and a second value simultaneously.
|
|
2812
2410
|
*
|
|
@@ -2815,9 +2413,9 @@ declare namespace These {
|
|
|
2815
2413
|
* These.make.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
|
|
2816
2414
|
* ```
|
|
2817
2415
|
*/
|
|
2818
|
-
|
|
2819
|
-
}
|
|
2820
|
-
|
|
2416
|
+
both: <A, B>(f: A, s: B) => TheseBoth<A, B>;
|
|
2417
|
+
};
|
|
2418
|
+
is: {
|
|
2821
2419
|
/**
|
|
2822
2420
|
* Type guard — checks if a These holds only a first value.
|
|
2823
2421
|
*
|
|
@@ -2829,7 +2427,7 @@ declare namespace These {
|
|
|
2829
2427
|
* }
|
|
2830
2428
|
* ```
|
|
2831
2429
|
*/
|
|
2832
|
-
|
|
2430
|
+
first: <A, B>(data: These<A, B>) => data is TheseFirst<A>;
|
|
2833
2431
|
/**
|
|
2834
2432
|
* Type guard — checks if a These holds only a second value.
|
|
2835
2433
|
*
|
|
@@ -2841,7 +2439,7 @@ declare namespace These {
|
|
|
2841
2439
|
* }
|
|
2842
2440
|
* ```
|
|
2843
2441
|
*/
|
|
2844
|
-
|
|
2442
|
+
second: <A, B>(data: These<A, B>) => data is TheseSecond<B>;
|
|
2845
2443
|
/**
|
|
2846
2444
|
* Type guard — checks if a These holds both values simultaneously.
|
|
2847
2445
|
*
|
|
@@ -2853,8 +2451,8 @@ declare namespace These {
|
|
|
2853
2451
|
* }
|
|
2854
2452
|
* ```
|
|
2855
2453
|
*/
|
|
2856
|
-
|
|
2857
|
-
}
|
|
2454
|
+
both: <A, B>(data: These<A, B>) => data is TheseBoth<A, B>;
|
|
2455
|
+
};
|
|
2858
2456
|
/**
|
|
2859
2457
|
* Returns true if the These contains a first value (First or Both).
|
|
2860
2458
|
*
|
|
@@ -2865,7 +2463,7 @@ declare namespace These {
|
|
|
2865
2463
|
* These.hasFirst(These.make.second("warn")); // false
|
|
2866
2464
|
* ```
|
|
2867
2465
|
*/
|
|
2868
|
-
|
|
2466
|
+
hasFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A> | TheseBoth<A, B>;
|
|
2869
2467
|
/**
|
|
2870
2468
|
* Returns true if the These contains a second value (Second or Both).
|
|
2871
2469
|
*
|
|
@@ -2876,7 +2474,7 @@ declare namespace These {
|
|
|
2876
2474
|
* These.hasSecond(These.make.first(42)); // false
|
|
2877
2475
|
* ```
|
|
2878
2476
|
*/
|
|
2879
|
-
|
|
2477
|
+
hasSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B> | TheseBoth<A, B>;
|
|
2880
2478
|
/**
|
|
2881
2479
|
* Transforms the first value, leaving the second unchanged.
|
|
2882
2480
|
*
|
|
@@ -2887,7 +2485,7 @@ declare namespace These {
|
|
|
2887
2485
|
* pipe(These.make.second("warn"), These.mapFirst(n => n * 2)); // Second("warn")
|
|
2888
2486
|
* ```
|
|
2889
2487
|
*/
|
|
2890
|
-
|
|
2488
|
+
mapFirst: <A, C>(f: (a: A) => C) => <B>(data: These<A, B>) => These<C, B>;
|
|
2891
2489
|
/**
|
|
2892
2490
|
* Transforms the second value, leaving the first unchanged.
|
|
2893
2491
|
*
|
|
@@ -2897,7 +2495,7 @@ declare namespace These {
|
|
|
2897
2495
|
* pipe(These.make.both(5, "warn"), These.mapSecond(e => e.toUpperCase())); // Both(5, "WARN")
|
|
2898
2496
|
* ```
|
|
2899
2497
|
*/
|
|
2900
|
-
|
|
2498
|
+
mapSecond: <B, D>(f: (b: B) => D) => <A>(data: These<A, B>) => These<A, D>;
|
|
2901
2499
|
/**
|
|
2902
2500
|
* Transforms both the first and second values independently.
|
|
2903
2501
|
*
|
|
@@ -2909,7 +2507,7 @@ declare namespace These {
|
|
|
2909
2507
|
* ); // Both(10, "WARN")
|
|
2910
2508
|
* ```
|
|
2911
2509
|
*/
|
|
2912
|
-
|
|
2510
|
+
mapBoth: <A, C, B, D>(onFirst: (a: A) => C, onSecond: (b: B) => D) => (data: These<A, B>) => These<C, D>;
|
|
2913
2511
|
/**
|
|
2914
2512
|
* Chains These computations by passing the first value to f.
|
|
2915
2513
|
* Second propagates unchanged; First and Both apply f to the first value.
|
|
@@ -2923,7 +2521,7 @@ declare namespace These {
|
|
|
2923
2521
|
* pipe(These.make.second("warn"), These.chainFirst(double)); // Second("warn")
|
|
2924
2522
|
* ```
|
|
2925
2523
|
*/
|
|
2926
|
-
|
|
2524
|
+
chainFirst: <A, B, C>(f: (a: A) => These<C, B>) => (data: These<A, B>) => These<C, B>;
|
|
2927
2525
|
/**
|
|
2928
2526
|
* Chains These computations by passing the second value to f.
|
|
2929
2527
|
* First propagates unchanged; Second and Both apply f to the second value.
|
|
@@ -2937,7 +2535,7 @@ declare namespace These {
|
|
|
2937
2535
|
* pipe(These.make.first(5), These.chainSecond(shout)); // First(5)
|
|
2938
2536
|
* ```
|
|
2939
2537
|
*/
|
|
2940
|
-
|
|
2538
|
+
chainSecond: <A, B, D>(f: (b: B) => These<A, D>) => (data: These<A, B>) => These<A, D>;
|
|
2941
2539
|
/**
|
|
2942
2540
|
* Extracts a value from a These by providing handlers for all three cases.
|
|
2943
2541
|
*
|
|
@@ -2953,7 +2551,7 @@ declare namespace These {
|
|
|
2953
2551
|
* );
|
|
2954
2552
|
* ```
|
|
2955
2553
|
*/
|
|
2956
|
-
|
|
2554
|
+
fold: <A, B, C>(onFirst: (a: A) => C, onSecond: (b: B) => C, onBoth: (a: A, b: B) => C) => (data: These<A, B>) => C;
|
|
2957
2555
|
/**
|
|
2958
2556
|
* Pattern matches on a These, returning the result of the matching case.
|
|
2959
2557
|
*
|
|
@@ -2969,7 +2567,7 @@ declare namespace These {
|
|
|
2969
2567
|
* );
|
|
2970
2568
|
* ```
|
|
2971
2569
|
*/
|
|
2972
|
-
|
|
2570
|
+
match: <A, B, C>(cases: {
|
|
2973
2571
|
first: (a: A) => C;
|
|
2974
2572
|
second: (b: B) => C;
|
|
2975
2573
|
both: (a: A, b: B) => C;
|
|
@@ -2986,7 +2584,7 @@ declare namespace These {
|
|
|
2986
2584
|
* pipe(These.make.second("warn"), These.getFirstOrElse(() => null)); // null — typed as number | null
|
|
2987
2585
|
* ```
|
|
2988
2586
|
*/
|
|
2989
|
-
|
|
2587
|
+
getFirstOrElse: <A, C>(defaultValue: () => C) => <B>(data: These<A, B>) => A | C;
|
|
2990
2588
|
/**
|
|
2991
2589
|
* Returns the second value, or a default if the These has no second value.
|
|
2992
2590
|
* The default can be a different type, widening the result to `B | D`.
|
|
@@ -2999,7 +2597,7 @@ declare namespace These {
|
|
|
2999
2597
|
* pipe(These.make.first(5), These.getSecondOrElse(() => null)); // null — typed as string | null
|
|
3000
2598
|
* ```
|
|
3001
2599
|
*/
|
|
3002
|
-
|
|
2600
|
+
getSecondOrElse: <B, D>(defaultValue: () => D) => <A>(data: These<A, B>) => B | D;
|
|
3003
2601
|
/**
|
|
3004
2602
|
* Runs a side effect on the first value without changing the These.
|
|
3005
2603
|
* Useful for logging or debugging.
|
|
@@ -3009,7 +2607,7 @@ declare namespace These {
|
|
|
3009
2607
|
* pipe(These.make.first(5), These.tap(console.log)); // logs 5, returns First(5)
|
|
3010
2608
|
* ```
|
|
3011
2609
|
*/
|
|
3012
|
-
|
|
2610
|
+
tap: <A>(f: (a: A) => void) => <B>(data: These<A, B>) => These<A, B>;
|
|
3013
2611
|
/**
|
|
3014
2612
|
* Swaps the roles of first and second values.
|
|
3015
2613
|
* - First(a) → Second(a)
|
|
@@ -3023,7 +2621,7 @@ declare namespace These {
|
|
|
3023
2621
|
* These.swap(These.make.both(5, "warn")); // Both("warn", 5)
|
|
3024
2622
|
* ```
|
|
3025
2623
|
*/
|
|
3026
|
-
|
|
3027
|
-
}
|
|
2624
|
+
swap: <A, B>(data: These<A, B>) => These<B, A>;
|
|
2625
|
+
};
|
|
3028
2626
|
|
|
3029
2627
|
export { Combinable, Deferred, type Failure, Lazy, Lens, type Loading, Logged, Maybe, type NotAsked, Op, Optional, Pair, Predicate, Reader, Refinement, RemoteData, Resource, Result, State, Stream, type Success, Task, These, type TheseBoth, type TheseFirst, type TheseSecond };
|