t-tasks 1.2.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/t-tasks.d.ts +1790 -0
- package/dist/t-tasks.esm.js +752 -2
- package/dist/t-tasks.esm.js.map +1 -1
- package/dist/t-tasks.js +788 -2
- package/dist/t-tasks.js.map +1 -1
- package/package.json +12 -7
- package/src/either.ts +89 -56
- package/src/maybe.ts +83 -50
- package/src/task.ts +372 -213
- package/dist/either.d.ts +0 -626
- package/dist/index.d.ts +0 -3
- package/dist/maybe.d.ts +0 -595
- package/dist/t-tasks.modern.mjs +0 -2
- package/dist/t-tasks.modern.mjs.map +0 -1
- package/dist/t-tasks.umd.js +0 -2
- package/dist/t-tasks.umd.js.map +0 -1
- package/dist/task.d.ts +0 -489
|
@@ -0,0 +1,1790 @@
|
|
|
1
|
+
//#region src/maybe.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Just a value
|
|
4
|
+
*
|
|
5
|
+
* Maybe data type specialization representing an existing value
|
|
6
|
+
*
|
|
7
|
+
* @template R underlying value
|
|
8
|
+
*/
|
|
9
|
+
export interface Just<R> {
|
|
10
|
+
readonly just: R;
|
|
11
|
+
/**
|
|
12
|
+
* Maybe peeker function
|
|
13
|
+
*
|
|
14
|
+
* Applied to 'just value' returns self invoking op(value) in process
|
|
15
|
+
* Applied to 'nothing' returns self without invoking callback
|
|
16
|
+
*
|
|
17
|
+
* @param op function to be invoked with underlying value
|
|
18
|
+
* @returns self
|
|
19
|
+
*/
|
|
20
|
+
tap(op: (value: R) => void): this;
|
|
21
|
+
/**
|
|
22
|
+
* Maybe fallback peeker function
|
|
23
|
+
*
|
|
24
|
+
* Applied to 'just value' returns self without invoking callback
|
|
25
|
+
* Applied to 'nothing' returns self invoking op() in process
|
|
26
|
+
*
|
|
27
|
+
* @param op function to be invoked
|
|
28
|
+
* @returns self
|
|
29
|
+
*/
|
|
30
|
+
orTap(op: () => void): this;
|
|
31
|
+
/**
|
|
32
|
+
* Maybe pattern matching peeker function
|
|
33
|
+
*
|
|
34
|
+
* Applied to 'just value' returns self invoking op.just(value) in process
|
|
35
|
+
* Applied to 'nothing' returns self invoking op.nothing() in process
|
|
36
|
+
*
|
|
37
|
+
* @param op.just function to be invoked with underlying value in case of just
|
|
38
|
+
* @param op.nothing function to be invoked in case of nothing
|
|
39
|
+
* @returns self
|
|
40
|
+
*/
|
|
41
|
+
matchTap(op: {
|
|
42
|
+
just: (value: R) => void;
|
|
43
|
+
nothing: () => void;
|
|
44
|
+
}): this;
|
|
45
|
+
/**
|
|
46
|
+
* Maybe transformer function
|
|
47
|
+
*
|
|
48
|
+
* Applied to 'just value' returns 'just op(value)'
|
|
49
|
+
* Applied to 'nothing' returns self without invoking transformer
|
|
50
|
+
*
|
|
51
|
+
* @template R2 transformer function's return type
|
|
52
|
+
* @param op transformer to be invoked with underlying value
|
|
53
|
+
* @returns 'just op(value)' or 'nothing'
|
|
54
|
+
*/
|
|
55
|
+
map<R2>(this: Just<R>, op: (value: R) => R2): Just<R2>;
|
|
56
|
+
map<R2>(this: Maybe<R>, op: (value: R) => R2): Maybe<R2>;
|
|
57
|
+
/**
|
|
58
|
+
* Maybe fallback transformer function
|
|
59
|
+
*
|
|
60
|
+
* Applied to 'just value' returns self without invoking transformer
|
|
61
|
+
* Applied to 'nothing' returns 'just op()'
|
|
62
|
+
*
|
|
63
|
+
* @param op function to be invoked
|
|
64
|
+
* @returns 'just value' or 'just op()'
|
|
65
|
+
*/
|
|
66
|
+
orMap<R2>(this: Just<R>, op: () => R2): Just<R>;
|
|
67
|
+
orMap<R2>(this: Maybe<R>, op: () => R2): Just<R | R2>;
|
|
68
|
+
/**
|
|
69
|
+
* Maybe pattern matching extractor function
|
|
70
|
+
*
|
|
71
|
+
* Applied to 'just value' returns op.just(value)
|
|
72
|
+
* Applied to 'nothing' returns op.nothing()
|
|
73
|
+
*
|
|
74
|
+
* @template R2 just extractor function's return type
|
|
75
|
+
* @template R3 nothing extractor function's return type
|
|
76
|
+
* @param op.just extractor to be invoked with underlying value in case of just
|
|
77
|
+
* @param op.nothing extractor to be invoked in case of nothing
|
|
78
|
+
* @returns op.just(value) or op.nothing()
|
|
79
|
+
*/
|
|
80
|
+
match<R2, R3 = R2>(this: Just<R>, op: {
|
|
81
|
+
just: (value: R) => R2;
|
|
82
|
+
nothing: () => R3;
|
|
83
|
+
}): R2;
|
|
84
|
+
match<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
85
|
+
just: (value: R) => R2;
|
|
86
|
+
nothing: () => R3;
|
|
87
|
+
}): R2 | R3;
|
|
88
|
+
matchMap<R2, R3 = R2>(this: Just<R>, op: {
|
|
89
|
+
just: (value: R) => R2;
|
|
90
|
+
nothing: () => R3;
|
|
91
|
+
}): Just<R2>;
|
|
92
|
+
matchMap<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
93
|
+
just: (value: R) => R2;
|
|
94
|
+
nothing: () => R3;
|
|
95
|
+
}): Just<R2 | R3>;
|
|
96
|
+
/**
|
|
97
|
+
* Maybe composition function
|
|
98
|
+
*
|
|
99
|
+
* Applied to 'just value' returns 'op(value)'
|
|
100
|
+
* Applied to 'nothing' returns self without invoking composition function
|
|
101
|
+
*
|
|
102
|
+
* @template R2 transformer function result's underlying type
|
|
103
|
+
* @param op transformer to be invoked with underlying value
|
|
104
|
+
* @returns 'op(value)' or 'nothing'
|
|
105
|
+
*/
|
|
106
|
+
chain<R2>(this: Just<R>, op: (value: R) => Just<R2>): Just<R2>;
|
|
107
|
+
chain<R2>(this: Just<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
|
|
108
|
+
chain<R2>(this: Just<R>, op: (value: R) => Maybe<R2>): Maybe<R2>;
|
|
109
|
+
chain<R2>(this: Maybe<R>, op: (value: R) => Just<R2>): Maybe<R2>;
|
|
110
|
+
chain<R2>(this: Maybe<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
|
|
111
|
+
chain<R2>(this: Maybe<R>, op: (value: R) => Maybe<R2>): Maybe<R2>;
|
|
112
|
+
/**
|
|
113
|
+
* Maybe fallback composition function
|
|
114
|
+
*
|
|
115
|
+
* Applied to 'just value' returns self without invoking composition function
|
|
116
|
+
* Applied to 'nothing' returns op()
|
|
117
|
+
*
|
|
118
|
+
* @template R2 transformer function result's underlying type
|
|
119
|
+
* @param op transformer to be invoked
|
|
120
|
+
* @returns 'just value' or 'op()'
|
|
121
|
+
*/
|
|
122
|
+
orChain<R2>(this: Just<R>, op: () => Just<R2>): Just<R>;
|
|
123
|
+
orChain<R2>(this: Just<R>, op: () => Nothing<R2>): Just<R>;
|
|
124
|
+
orChain<R2>(this: Just<R>, op: () => Maybe<R2>): Just<R>;
|
|
125
|
+
orChain<R2>(this: Maybe<R>, op: () => Just<R2>): Just<R | R2>;
|
|
126
|
+
orChain<R2>(this: Maybe<R>, op: () => Nothing<R2>): Maybe<R>;
|
|
127
|
+
orChain<R2>(this: Maybe<R>, op: () => Maybe<R2>): Maybe<R | R2>;
|
|
128
|
+
/**
|
|
129
|
+
* Maybe pattern matching composition function
|
|
130
|
+
*
|
|
131
|
+
* Applied to 'just value' returns 'op.just(value)'
|
|
132
|
+
* Applied to 'nothing' returns op.nothing()
|
|
133
|
+
*
|
|
134
|
+
* @template R2 just transformer function result's underlying type
|
|
135
|
+
* @template R3 nothing transformer function result's underlying type
|
|
136
|
+
* @param op.just transformer to be invoked with underlying value in case of just
|
|
137
|
+
* @param op.nothing transformer to be invoked in case of nothing
|
|
138
|
+
* @returns 'op.just(value)' or 'op.nothing()'
|
|
139
|
+
*/
|
|
140
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
141
|
+
just: (value: R) => Just<R2>;
|
|
142
|
+
nothing: () => Just<R3>;
|
|
143
|
+
}): Just<R2>;
|
|
144
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
145
|
+
just: (value: R) => Just<R2>;
|
|
146
|
+
nothing: () => Nothing<R3>;
|
|
147
|
+
}): Just<R2>;
|
|
148
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
149
|
+
just: (value: R) => Nothing<R2>;
|
|
150
|
+
nothing: () => Just<R3>;
|
|
151
|
+
}): Nothing<never>;
|
|
152
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
153
|
+
just: (value: R) => Nothing<R2>;
|
|
154
|
+
nothing: () => Nothing<R3>;
|
|
155
|
+
}): Nothing<never>;
|
|
156
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
157
|
+
just: (value: R) => Just<R2>;
|
|
158
|
+
nothing: () => Maybe<R3>;
|
|
159
|
+
}): Just<R2>;
|
|
160
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
161
|
+
just: (value: R) => Nothing<R2>;
|
|
162
|
+
nothing: () => Maybe<R3>;
|
|
163
|
+
}): Nothing<never>;
|
|
164
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
165
|
+
just: (value: R) => Maybe<R2>;
|
|
166
|
+
nothing: () => Just<R3>;
|
|
167
|
+
}): Maybe<R2>;
|
|
168
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
169
|
+
just: (value: R) => Maybe<R2>;
|
|
170
|
+
nothing: () => Nothing<R3>;
|
|
171
|
+
}): Maybe<R2>;
|
|
172
|
+
matchChain<R2, R3 = R2>(this: Just<R>, op: {
|
|
173
|
+
just: (value: R) => Maybe<R2>;
|
|
174
|
+
nothing: () => Maybe<R3>;
|
|
175
|
+
}): Maybe<R2>;
|
|
176
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
177
|
+
just: (value: R) => Just<R2>;
|
|
178
|
+
nothing: () => Just<R3>;
|
|
179
|
+
}): Just<R2 | R3>;
|
|
180
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
181
|
+
just: (value: R) => Just<R2>;
|
|
182
|
+
nothing: () => Nothing<R3>;
|
|
183
|
+
}): Maybe<R2>;
|
|
184
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
185
|
+
just: (value: R) => Nothing<R2>;
|
|
186
|
+
nothing: () => Just<R3>;
|
|
187
|
+
}): Maybe<R3>;
|
|
188
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
189
|
+
just: (value: R) => Nothing<R2>;
|
|
190
|
+
nothing: () => Nothing<R3>;
|
|
191
|
+
}): Nothing<never>;
|
|
192
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
193
|
+
just: (value: R) => Just<R2>;
|
|
194
|
+
nothing: () => Maybe<R3>;
|
|
195
|
+
}): Maybe<R2 | R3>;
|
|
196
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
197
|
+
just: (value: R) => Nothing<R2>;
|
|
198
|
+
nothing: () => Maybe<R3>;
|
|
199
|
+
}): Maybe<R3>;
|
|
200
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
201
|
+
just: (value: R) => Maybe<R2>;
|
|
202
|
+
nothing: () => Just<R3>;
|
|
203
|
+
}): Maybe<R2 | R3>;
|
|
204
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
205
|
+
just: (value: R) => Maybe<R2>;
|
|
206
|
+
nothing: () => Nothing<R3>;
|
|
207
|
+
}): Maybe<R2>;
|
|
208
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
209
|
+
just: (value: R) => Maybe<R2>;
|
|
210
|
+
nothing: () => Maybe<R3>;
|
|
211
|
+
}): Maybe<R2 | R3>;
|
|
212
|
+
/**
|
|
213
|
+
* Maybe type guard for 'just'
|
|
214
|
+
*
|
|
215
|
+
* @returns 'true' in case wrapped value exists (and resolves this type to be 'just')
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* if (maybe.isJust()) {
|
|
220
|
+
* console.log(maybe.just);
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
isJust(): this is Just<R>;
|
|
225
|
+
/**
|
|
226
|
+
* Maybe type guard for 'nothing'
|
|
227
|
+
*
|
|
228
|
+
* @returns 'true' in case wrapped value is 'nothing' (and resolves this type to be 'nothing')
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* if (maybe.isNothing()) {
|
|
233
|
+
* console.log('nothing');
|
|
234
|
+
* }
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
isNothing(): this is Nothing<R>;
|
|
238
|
+
/**
|
|
239
|
+
* Wrap Maybe to singleton generator
|
|
240
|
+
*
|
|
241
|
+
* Useful in order to avoid ambiguous yield types
|
|
242
|
+
*
|
|
243
|
+
* @returns generator of Maybe wrapping this
|
|
244
|
+
*/
|
|
245
|
+
generator: MaybeGeneratorFunction<[], R, Just<R>, R>;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Absolutely Nothing
|
|
249
|
+
*
|
|
250
|
+
* Maybe data type specialization representing an absence of any value
|
|
251
|
+
*/
|
|
252
|
+
export interface Nothing<R> {
|
|
253
|
+
/**
|
|
254
|
+
* Maybe peeker function
|
|
255
|
+
*
|
|
256
|
+
* Applied to 'just value' returns self invoking op(value) in process
|
|
257
|
+
* Applied to 'nothing' returns self without invoking callback
|
|
258
|
+
*
|
|
259
|
+
* @param op function to be invoked with underlying value
|
|
260
|
+
* @returns self
|
|
261
|
+
*/
|
|
262
|
+
tap(op: (value: R) => void): this;
|
|
263
|
+
/**
|
|
264
|
+
* Maybe fallback peeker function
|
|
265
|
+
*
|
|
266
|
+
* Applied to 'just value' returns self without invoking callback
|
|
267
|
+
* Applied to 'nothing' returns self invoking op() in process
|
|
268
|
+
*
|
|
269
|
+
* @param op function to be invoked
|
|
270
|
+
* @returns self
|
|
271
|
+
*/
|
|
272
|
+
orTap(op: () => void): this;
|
|
273
|
+
/**
|
|
274
|
+
* Maybe pattern matching peeker function
|
|
275
|
+
*
|
|
276
|
+
* Applied to 'just value' returns self invoking op.just(value) in process
|
|
277
|
+
* Applied to 'nothing' returns self invoking op.nothing() in process
|
|
278
|
+
*
|
|
279
|
+
* @param op.just function to be invoked with underlying value in case of just
|
|
280
|
+
* @param op.nothing function to be invoked in case of nothing
|
|
281
|
+
* @returns self
|
|
282
|
+
*/
|
|
283
|
+
matchTap(op: {
|
|
284
|
+
just: (value: R) => void;
|
|
285
|
+
nothing: () => void;
|
|
286
|
+
}): this;
|
|
287
|
+
/**
|
|
288
|
+
* Maybe transformer function
|
|
289
|
+
*
|
|
290
|
+
* Applied to 'just value' returns 'just op(value)'
|
|
291
|
+
* Applied to 'nothing' returns self without invoking transformer
|
|
292
|
+
*
|
|
293
|
+
* @template R2 transformer function's return type
|
|
294
|
+
* @param op transformer to be invoked with underlying value
|
|
295
|
+
* @returns 'just op(value)' or 'nothing'
|
|
296
|
+
*/
|
|
297
|
+
map<R2>(this: Nothing<R>, op: (value: R) => R2): Nothing<never>;
|
|
298
|
+
map<R2>(this: Maybe<R>, op: (value: R) => R2): Maybe<R2>;
|
|
299
|
+
/**
|
|
300
|
+
* Maybe fallback transformer function
|
|
301
|
+
*
|
|
302
|
+
* Applied to 'just value' returns self without invoking transformer
|
|
303
|
+
* Applied to 'nothing' returns 'just op()'
|
|
304
|
+
*
|
|
305
|
+
* @param op function to be invoked
|
|
306
|
+
* @returns 'just value' or 'just op()'
|
|
307
|
+
*/
|
|
308
|
+
orMap<R2>(this: Nothing<R>, op: () => R2): Just<R2>;
|
|
309
|
+
orMap<R2>(this: Maybe<R>, op: () => R2): Just<R | R2>;
|
|
310
|
+
/**
|
|
311
|
+
* Maybe pattern matching extractor function
|
|
312
|
+
*
|
|
313
|
+
* Applied to 'just value' returns op.just(value)
|
|
314
|
+
* Applied to 'nothing' returns op.nothing()
|
|
315
|
+
*
|
|
316
|
+
* @template R2 just extractor function's return type
|
|
317
|
+
* @template R3 nothing extractor function's return type
|
|
318
|
+
* @param op.just extractor to be invoked with underlying value in case of just
|
|
319
|
+
* @param op.nothing extractor to be invoked in case of nothing
|
|
320
|
+
* @returns op.just(value) or op.nothing()
|
|
321
|
+
*/
|
|
322
|
+
match<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
323
|
+
just: (value: R) => R2;
|
|
324
|
+
nothing: () => R3;
|
|
325
|
+
}): R3;
|
|
326
|
+
match<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
327
|
+
just: (value: R) => R2;
|
|
328
|
+
nothing: () => R3;
|
|
329
|
+
}): R2 | R3;
|
|
330
|
+
matchMap<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
331
|
+
just: (value: R) => R2;
|
|
332
|
+
nothing: () => R3;
|
|
333
|
+
}): Just<R3>;
|
|
334
|
+
matchMap<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
335
|
+
just: (value: R) => R2;
|
|
336
|
+
nothing: () => R3;
|
|
337
|
+
}): Just<R2 | R3>;
|
|
338
|
+
/**
|
|
339
|
+
* Maybe composition function
|
|
340
|
+
*
|
|
341
|
+
* Applied to 'just value' returns 'op(value)'
|
|
342
|
+
* Applied to 'nothing' returns self without invoking composition function
|
|
343
|
+
*
|
|
344
|
+
* @template R2 transformer function result's underlying type
|
|
345
|
+
* @param op transformer to be invoked with underlying value
|
|
346
|
+
* @returns 'op(value)' or 'nothing'
|
|
347
|
+
*/
|
|
348
|
+
chain<R2>(this: Nothing<R>, op: (value: R) => Just<R2>): Nothing<never>;
|
|
349
|
+
chain<R2>(this: Nothing<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
|
|
350
|
+
chain<R2>(this: Nothing<R>, op: (value: R) => Maybe<R2>): Nothing<never>;
|
|
351
|
+
chain<R2>(this: Maybe<R>, op: (value: R) => Just<R2>): Maybe<R2>;
|
|
352
|
+
chain<R2>(this: Maybe<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
|
|
353
|
+
chain<R2>(this: Maybe<R>, op: (value: R) => Maybe<R2>): Maybe<R2>;
|
|
354
|
+
/**
|
|
355
|
+
* Maybe fallback composition function
|
|
356
|
+
*
|
|
357
|
+
* Applied to 'just value' returns self without invoking composition function
|
|
358
|
+
* Applied to 'nothing' returns op()
|
|
359
|
+
*
|
|
360
|
+
* @template R2 transformer function result's underlying type
|
|
361
|
+
* @param op transformer to be invoked
|
|
362
|
+
* @returns 'just value' or 'op()'
|
|
363
|
+
*/
|
|
364
|
+
orChain<R2>(this: Nothing<R>, op: () => Just<R2>): Just<R2>;
|
|
365
|
+
orChain<R2>(this: Nothing<R>, op: () => Nothing<R2>): Nothing<never>;
|
|
366
|
+
orChain<R2>(this: Nothing<R>, op: () => Maybe<R2>): Maybe<R2>;
|
|
367
|
+
orChain<R2>(this: Maybe<R>, op: () => Just<R2>): Just<R | R2>;
|
|
368
|
+
orChain<R2>(this: Maybe<R>, op: () => Nothing<R2>): Maybe<R>;
|
|
369
|
+
orChain<R2>(this: Maybe<R>, op: () => Maybe<R2>): Maybe<R | R2>;
|
|
370
|
+
/**
|
|
371
|
+
* Maybe pattern matching composition function
|
|
372
|
+
*
|
|
373
|
+
* Applied to 'just value' returns 'op.just(value)'
|
|
374
|
+
* Applied to 'nothing' returns op.nothing()
|
|
375
|
+
*
|
|
376
|
+
* @template R2 just transformer function result's underlying type
|
|
377
|
+
* @template R3 nothing transformer function result's underlying type
|
|
378
|
+
* @param op.just transformer to be invoked with underlying value in case of just
|
|
379
|
+
* @param op.nothing transformer to be invoked in case of nothing
|
|
380
|
+
* @returns 'op.just(value)' or 'op.nothing()'
|
|
381
|
+
*/
|
|
382
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
383
|
+
just: (value: R) => Just<R2>;
|
|
384
|
+
nothing: () => Just<R3>;
|
|
385
|
+
}): Just<R3>;
|
|
386
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
387
|
+
just: (value: R) => Just<R2>;
|
|
388
|
+
nothing: () => Nothing<R3>;
|
|
389
|
+
}): Nothing<never>;
|
|
390
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
391
|
+
just: (value: R) => Nothing<R2>;
|
|
392
|
+
nothing: () => Just<R3>;
|
|
393
|
+
}): Just<R3>;
|
|
394
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
395
|
+
just: (value: R) => Nothing<R2>;
|
|
396
|
+
nothing: () => Nothing<R3>;
|
|
397
|
+
}): Nothing<never>;
|
|
398
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
399
|
+
just: (value: R) => Just<R2>;
|
|
400
|
+
nothing: () => Maybe<R3>;
|
|
401
|
+
}): Maybe<R3>;
|
|
402
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
403
|
+
just: (value: R) => Nothing<R2>;
|
|
404
|
+
nothing: () => Maybe<R3>;
|
|
405
|
+
}): Maybe<R3>;
|
|
406
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
407
|
+
just: (value: R) => Maybe<R2>;
|
|
408
|
+
nothing: () => Just<R3>;
|
|
409
|
+
}): Just<R3>;
|
|
410
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
411
|
+
just: (value: R) => Maybe<R2>;
|
|
412
|
+
nothing: () => Nothing<R3>;
|
|
413
|
+
}): Nothing<never>;
|
|
414
|
+
matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
|
|
415
|
+
just: (value: R) => Maybe<R2>;
|
|
416
|
+
nothing: () => Maybe<R3>;
|
|
417
|
+
}): Maybe<R3>;
|
|
418
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
419
|
+
just: (value: R) => Just<R2>;
|
|
420
|
+
nothing: () => Just<R3>;
|
|
421
|
+
}): Just<R2 | R3>;
|
|
422
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
423
|
+
just: (value: R) => Just<R2>;
|
|
424
|
+
nothing: () => Nothing<R3>;
|
|
425
|
+
}): Maybe<R2>;
|
|
426
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
427
|
+
just: (value: R) => Nothing<R2>;
|
|
428
|
+
nothing: () => Just<R3>;
|
|
429
|
+
}): Maybe<R3>;
|
|
430
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
431
|
+
just: (value: R) => Nothing<R2>;
|
|
432
|
+
nothing: () => Nothing<R3>;
|
|
433
|
+
}): Nothing<never>;
|
|
434
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
435
|
+
just: (value: R) => Just<R2>;
|
|
436
|
+
nothing: () => Maybe<R3>;
|
|
437
|
+
}): Maybe<R2 | R3>;
|
|
438
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
439
|
+
just: (value: R) => Nothing<R2>;
|
|
440
|
+
nothing: () => Maybe<R3>;
|
|
441
|
+
}): Maybe<R3>;
|
|
442
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
443
|
+
just: (value: R) => Maybe<R2>;
|
|
444
|
+
nothing: () => Just<R3>;
|
|
445
|
+
}): Maybe<R2 | R3>;
|
|
446
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
447
|
+
just: (value: R) => Maybe<R2>;
|
|
448
|
+
nothing: () => Nothing<R3>;
|
|
449
|
+
}): Maybe<R2>;
|
|
450
|
+
matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
|
|
451
|
+
just: (value: R) => Maybe<R2>;
|
|
452
|
+
nothing: () => Maybe<R3>;
|
|
453
|
+
}): Maybe<R2 | R3>;
|
|
454
|
+
/**
|
|
455
|
+
* Maybe type guard for 'just'
|
|
456
|
+
*
|
|
457
|
+
* @returns 'true' in case wrapped value exists (and resolves this type to be 'just')
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* ```typescript
|
|
461
|
+
* if (maybe.isJust()) {
|
|
462
|
+
* console.log(maybe.just);
|
|
463
|
+
* }
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
isJust(): this is Just<R>;
|
|
467
|
+
/**
|
|
468
|
+
* Maybe type guard for 'nothing'
|
|
469
|
+
*
|
|
470
|
+
* @returns 'true' in case wrapped value is 'nothing' (and resolves this type to be 'nothing')
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```typescript
|
|
474
|
+
* if (maybe.isNothing()) {
|
|
475
|
+
* console.log('nothing');
|
|
476
|
+
* }
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
isNothing(): this is Nothing<R>;
|
|
480
|
+
/**
|
|
481
|
+
* Wrap Maybe to singleton generator
|
|
482
|
+
*
|
|
483
|
+
* Useful in order to avoid ambiguous yield types
|
|
484
|
+
*
|
|
485
|
+
* @returns generator of Maybe wrapping this
|
|
486
|
+
*/
|
|
487
|
+
generator: MaybeGeneratorFunction<[], R, Nothing<R>, R>;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Generic Maybe monad interface
|
|
491
|
+
*
|
|
492
|
+
* As per classic Maybe monad implementation can either contain just a value or contain nothing
|
|
493
|
+
* Used throughout the library to represent optional return type, specifically return type of cancelled tasks
|
|
494
|
+
*
|
|
495
|
+
* @template R underlying value
|
|
496
|
+
*/
|
|
497
|
+
export type Maybe<R> = Just<R> | Nothing<R>;
|
|
498
|
+
export declare namespace Just {
|
|
499
|
+
/**
|
|
500
|
+
* Standalone Just value extractor
|
|
501
|
+
*
|
|
502
|
+
* Useful for passing as a function to collection transformers
|
|
503
|
+
*
|
|
504
|
+
* @template R underlying value type
|
|
505
|
+
* @param just Just instance
|
|
506
|
+
* @returns underlying value
|
|
507
|
+
*/
|
|
508
|
+
function just<R>(just: Just<R>): R;
|
|
509
|
+
}
|
|
510
|
+
export declare namespace Nothing {}
|
|
511
|
+
export type MaybeGenerator<T, TT extends Maybe<T>, R> = Generator<TT, R, T>;
|
|
512
|
+
export type MaybeGeneratorFunction<A extends unknown[], T, TT extends Maybe<T>, R> = (...args: A) => MaybeGenerator<T, TT, R>;
|
|
513
|
+
export declare namespace Maybe {
|
|
514
|
+
/**
|
|
515
|
+
* Non-empty monad constructor
|
|
516
|
+
*
|
|
517
|
+
* @template R underlying type
|
|
518
|
+
* @param value underlying value
|
|
519
|
+
* @returns 'just value'
|
|
520
|
+
*/
|
|
521
|
+
function just<R>(value: R): Just<R>;
|
|
522
|
+
/**
|
|
523
|
+
* Empty monad constructor
|
|
524
|
+
*
|
|
525
|
+
* @returns 'nothing'
|
|
526
|
+
*/
|
|
527
|
+
function nothing<R = never>(): Nothing<R>;
|
|
528
|
+
/**
|
|
529
|
+
* Maybe constructor from optional value
|
|
530
|
+
*
|
|
531
|
+
* Resolves to 'nothing' in case of undefined value and 'just value' otherwise
|
|
532
|
+
*
|
|
533
|
+
* @param value possibly undefined value
|
|
534
|
+
* @returns either 'just value' or 'nothing'
|
|
535
|
+
*/
|
|
536
|
+
function fromOptional(value: undefined): Nothing<never>;
|
|
537
|
+
function fromOptional<R>(value: Exclude<R, undefined>): Just<R>;
|
|
538
|
+
function fromOptional<R>(value: R | undefined): Maybe<R>;
|
|
539
|
+
/**
|
|
540
|
+
* Maybe constructor from optional or nullable value
|
|
541
|
+
*
|
|
542
|
+
* Resolves to 'nothing' in case of undefined or null value and 'just value' otherwise
|
|
543
|
+
*
|
|
544
|
+
* @param value possibly undefined value
|
|
545
|
+
* @returns either 'just value' or 'nothing'
|
|
546
|
+
*/
|
|
547
|
+
function fromNullable(value: undefined): Nothing<never>;
|
|
548
|
+
function fromNullable(value: null): Nothing<never>;
|
|
549
|
+
function fromNullable<R>(value: Exclude<R, null | undefined>): Just<R>;
|
|
550
|
+
function fromNullable<R>(value: R | null | undefined): Maybe<R>;
|
|
551
|
+
/**
|
|
552
|
+
* Standalone Maybe value extractor
|
|
553
|
+
*
|
|
554
|
+
* Applied to 'just value' returns underlying value without using fallback
|
|
555
|
+
* Applied to 'nothing' returns fallback
|
|
556
|
+
*
|
|
557
|
+
* @template R underlying value type
|
|
558
|
+
* @template R2 fallback value type
|
|
559
|
+
* @param maybe Maybe instance
|
|
560
|
+
* @param fallback value to be returned in case of 'nothing'
|
|
561
|
+
* @returns underlying value or fallback
|
|
562
|
+
*/
|
|
563
|
+
function fromMaybe<R>(maybe: Just<R>, fallback: unknown): R;
|
|
564
|
+
function fromMaybe<R, R2>(maybe: Nothing<R>, fallback: R2): R2;
|
|
565
|
+
function fromMaybe<R, R2>(maybe: Maybe<R>, fallback: R2): R | R2;
|
|
566
|
+
/**
|
|
567
|
+
* Standalone type guard for 'just'
|
|
568
|
+
*
|
|
569
|
+
* Useful for passing as a predicate to collection transformers
|
|
570
|
+
*
|
|
571
|
+
* @template R underlying value type
|
|
572
|
+
* @param maybe Maybe instance
|
|
573
|
+
* @returns 'true' in case wrapped value exists (and resolves argument type to be 'just')
|
|
574
|
+
*/
|
|
575
|
+
function isJust<R>(maybe: Maybe<R>): maybe is Just<R>;
|
|
576
|
+
/**
|
|
577
|
+
* Standalone type guard for 'nothing'
|
|
578
|
+
*
|
|
579
|
+
* Useful for passing as a predicate to collection transformers
|
|
580
|
+
*
|
|
581
|
+
* @template R underlying value type
|
|
582
|
+
* @param maybe Maybe instance
|
|
583
|
+
* @returns 'true' in case wrapped value is 'nothing' (and resolves argument type to be 'nothing')
|
|
584
|
+
*/
|
|
585
|
+
function isNothing<R>(maybe: Maybe<R>): maybe is Nothing<R>;
|
|
586
|
+
/**
|
|
587
|
+
* Standalone list predicate
|
|
588
|
+
*
|
|
589
|
+
* @param maybes a list of Maybe
|
|
590
|
+
* @returns true in case every list element is Just
|
|
591
|
+
*/
|
|
592
|
+
function everyJust<R>(maybes: Maybe<R>[]): maybes is Just<R>[];
|
|
593
|
+
/**
|
|
594
|
+
* Standalone list predicate
|
|
595
|
+
*
|
|
596
|
+
* @param maybes a list of Maybe
|
|
597
|
+
* @returns true in case at least one list element is Just
|
|
598
|
+
*/
|
|
599
|
+
function someJust<R>(maybes: Maybe<R>[]): boolean;
|
|
600
|
+
/**
|
|
601
|
+
* Standalone list predicate
|
|
602
|
+
*
|
|
603
|
+
* @param maybes a list of Maybe
|
|
604
|
+
* @returns true in case every list element is Nothing
|
|
605
|
+
*/
|
|
606
|
+
function everyNothing<R>(maybes: Maybe<R>[]): maybes is Nothing<never>[];
|
|
607
|
+
/**
|
|
608
|
+
* Standalone list predicate
|
|
609
|
+
*
|
|
610
|
+
* @param maybes a list of Maybe
|
|
611
|
+
* @returns true in case at least one list element is Nothing
|
|
612
|
+
*/
|
|
613
|
+
function someNothing<R>(maybes: Maybe<R>[]): boolean;
|
|
614
|
+
/**
|
|
615
|
+
* Create compound Maybe from generator function
|
|
616
|
+
*
|
|
617
|
+
* Applying yield to a Maybe within the generator function unwraps the Maybe and returns underlying value in case of success
|
|
618
|
+
* However the convenient option for typescript is to use ```yield* maybe.generator()``` as otherwise one may have to deal with union types
|
|
619
|
+
*
|
|
620
|
+
* @template TT yielded Maybe type
|
|
621
|
+
* @template R returned underlying type
|
|
622
|
+
* @param maybeGeneratorFunction Maybe generator function
|
|
623
|
+
* @returns Just wrapping the result of generator function or Nothing
|
|
624
|
+
*/
|
|
625
|
+
function generate<T, TT extends Maybe<T>, R>(maybeGeneratorFunction: MaybeGeneratorFunction<[], T, TT, R>): Maybe<R>;
|
|
626
|
+
}
|
|
627
|
+
//#endregion
|
|
628
|
+
//#region src/either.d.ts
|
|
629
|
+
/**
|
|
630
|
+
* Right (correct) value of type R
|
|
631
|
+
*
|
|
632
|
+
* Either data type specialization representing a correct value
|
|
633
|
+
*
|
|
634
|
+
* @template R underlying value type
|
|
635
|
+
* @template L underlying error type (needed for type merging, in fact always considered to be never)
|
|
636
|
+
*/
|
|
637
|
+
export interface Right<R, L> {
|
|
638
|
+
readonly right: R;
|
|
639
|
+
/**
|
|
640
|
+
* Either peeker function
|
|
641
|
+
*
|
|
642
|
+
* Applied to 'right value' returns self invoking op(value) in process
|
|
643
|
+
* Applied to 'left error' returns self without invoking callback
|
|
644
|
+
*
|
|
645
|
+
* @param op function to be invoked with underlying value
|
|
646
|
+
* @returns self
|
|
647
|
+
*/
|
|
648
|
+
tap(op: (value: R) => void): this;
|
|
649
|
+
/**
|
|
650
|
+
* Either fallback peeker function
|
|
651
|
+
*
|
|
652
|
+
* Applied to 'right value' returns self without invoking callback
|
|
653
|
+
* Applied to 'left error' returns self invoking op(error) in process
|
|
654
|
+
*
|
|
655
|
+
* @param op function to be invoked with underlying error
|
|
656
|
+
* @returns self
|
|
657
|
+
*/
|
|
658
|
+
orTap(op: (error: L) => void): this;
|
|
659
|
+
/**
|
|
660
|
+
* Either pattern matching peeker function
|
|
661
|
+
*
|
|
662
|
+
* Applied to 'right value' returns self invoking op.right(value) in process
|
|
663
|
+
* Applied to 'left error' returns self invoking op.left(error) in process
|
|
664
|
+
*
|
|
665
|
+
* @param op.right function to be invoked with underlying value in case of 'right'
|
|
666
|
+
* @param op.left function to be invoked with underlying error in case of 'left'
|
|
667
|
+
* @returns self
|
|
668
|
+
*/
|
|
669
|
+
matchTap(op: {
|
|
670
|
+
right: (value: R) => void;
|
|
671
|
+
left: (error: L) => void;
|
|
672
|
+
}): this;
|
|
673
|
+
/**
|
|
674
|
+
* Either transformer function
|
|
675
|
+
*
|
|
676
|
+
* Applied to 'right value' returns 'right op(value)'
|
|
677
|
+
* Applied to 'left error' returns self without invoking transformer
|
|
678
|
+
*
|
|
679
|
+
* @template R2 transformer function's return type
|
|
680
|
+
* @param op transformer to be invoked with underlying value
|
|
681
|
+
* @returns 'right op(value)' or 'left error'
|
|
682
|
+
*/
|
|
683
|
+
map<R2>(this: Right<R, L>, op: (value: R) => R2): Right<R2, never>;
|
|
684
|
+
map<R2>(this: Either<R, L>, op: (value: R) => R2): Either<R2, L>;
|
|
685
|
+
/**
|
|
686
|
+
* Either fallback transformer function
|
|
687
|
+
*
|
|
688
|
+
* Applied to 'right value' returns self without invoking transformer
|
|
689
|
+
* Applied to 'left error' returns 'right op(error)'
|
|
690
|
+
*
|
|
691
|
+
* @param op transformer to be invoked with underlying error
|
|
692
|
+
* @returns 'right value' or 'right op(error)'
|
|
693
|
+
*/
|
|
694
|
+
orMap<R2>(this: Right<R, L>, op: (error: L) => R2): Right<R, never>;
|
|
695
|
+
orMap<R2>(this: Either<R, L>, op: (error: L) => R2): Right<R | R2, never>;
|
|
696
|
+
/**
|
|
697
|
+
* Either pattern matching extractor function
|
|
698
|
+
*
|
|
699
|
+
* Applied to 'right value' returns op.right(value)
|
|
700
|
+
* Applied to 'left error' returns op.left(error)
|
|
701
|
+
*
|
|
702
|
+
* @template R2 right extractor function's return type
|
|
703
|
+
* @template R3 left extractor function's return type
|
|
704
|
+
* @param op.right extractor to be invoked with underlying value in case of 'right'
|
|
705
|
+
* @param op.left extractor to be invoked with underlying error in case of 'left'
|
|
706
|
+
* @returns op.right(value) or op.left(error)
|
|
707
|
+
*/
|
|
708
|
+
match<R2, R3 = R2>(this: Right<R, L>, op: {
|
|
709
|
+
right: (value: R) => R2;
|
|
710
|
+
left: (error: L) => R3;
|
|
711
|
+
}): R2;
|
|
712
|
+
match<R2, R3 = R2>(this: Either<R, L>, op: {
|
|
713
|
+
right: (value: R) => R2;
|
|
714
|
+
left: (error: L) => R3;
|
|
715
|
+
}): R2 | R3;
|
|
716
|
+
matchMap<R2, R3 = R2>(this: Right<R, L>, op: {
|
|
717
|
+
right: (value: R) => R2;
|
|
718
|
+
left: (error: L) => R3;
|
|
719
|
+
}): Right<R2, never>;
|
|
720
|
+
matchMap<R2, R3 = R2>(this: Either<R, L>, op: {
|
|
721
|
+
right: (value: R) => R2;
|
|
722
|
+
left: (error: L) => R3;
|
|
723
|
+
}): Right<R2 | R3, never>;
|
|
724
|
+
/**
|
|
725
|
+
* Either composition function
|
|
726
|
+
*
|
|
727
|
+
* Applied to 'right value' returns 'op(value)'
|
|
728
|
+
* Applied to 'left error' returns self without invoking composition function
|
|
729
|
+
*
|
|
730
|
+
* @template R2 transformer function result's underlying value type
|
|
731
|
+
* @template L2 transformer function result's underlying error type
|
|
732
|
+
* @param op transformer to be invoked with underlying value
|
|
733
|
+
* @returns 'op(value)' or 'left error'
|
|
734
|
+
*/
|
|
735
|
+
chain<R2, L2>(this: Right<R, L>, op: (value: R) => Right<R2, L2>): Right<R2, never>;
|
|
736
|
+
chain<R2, L2>(this: Right<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L2>;
|
|
737
|
+
chain<R2, L2>(this: Right<R, L>, op: (value: R) => Either<R2, L2>): Either<R2, L2>;
|
|
738
|
+
chain<R2, L2>(this: Either<R, L>, op: (value: R) => Right<R2, L2>): Either<R2, L>;
|
|
739
|
+
chain<R2, L2>(this: Either<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L | L2>;
|
|
740
|
+
chain<R2, L2>(this: Either<R, L>, op: (value: R) => Either<R2, L2>): Either<R2, L | L2>;
|
|
741
|
+
/**
|
|
742
|
+
* Either composition function
|
|
743
|
+
*
|
|
744
|
+
* Applied to 'right value' returns self without invoking composition function
|
|
745
|
+
* Applied to 'left error' returns 'op(error)'
|
|
746
|
+
*
|
|
747
|
+
* @template R2 transformer function result's underlying value type
|
|
748
|
+
* @template L2 transformer function result's underlying error type
|
|
749
|
+
* @param op transformer to be invoked with underlying error
|
|
750
|
+
* @returns 'right value' or 'op(error)'
|
|
751
|
+
*/
|
|
752
|
+
orChain<R2, L2>(this: Right<R, L>, op: (error: L) => Right<R2, L2>): Right<R, never>;
|
|
753
|
+
orChain<R2, L2>(this: Right<R, L>, op: (error: L) => Left<R2, L2>): Right<R, never>;
|
|
754
|
+
orChain<R2, L2>(this: Right<R, L>, op: (error: L) => Either<R2, L2>): Right<R, never>;
|
|
755
|
+
orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Right<R2, L2>): Right<R | R2, never>;
|
|
756
|
+
orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Left<R2, L2>): Either<R, L2>;
|
|
757
|
+
orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Either<R2, L2>): Either<R | R2, L2>;
|
|
758
|
+
/**
|
|
759
|
+
* Either pattern matching composition function
|
|
760
|
+
*
|
|
761
|
+
* Applied to 'right value' returns 'op.right(value)'
|
|
762
|
+
* Applied to 'left error' returns 'op.left(error)'
|
|
763
|
+
*
|
|
764
|
+
* @template R2 right transformer function result's underlying value type
|
|
765
|
+
* @template L2 right transformer function result's underlying error type
|
|
766
|
+
* @template R3 left transformer function result's underlying value type
|
|
767
|
+
* @template L3 left transformer function result's underlying error type
|
|
768
|
+
* @param op.right transformer to be invoked with underlying value in case of 'right'
|
|
769
|
+
* @param op.left transformer to be invoked with underlying error in case of 'left'
|
|
770
|
+
* @returns 'op(value)' or 'op(error)'
|
|
771
|
+
*/
|
|
772
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
773
|
+
right: (value: R) => Right<R2, L2>;
|
|
774
|
+
left: (error: L) => Right<R3, L3>;
|
|
775
|
+
}): Right<R2, never>;
|
|
776
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
777
|
+
right: (value: R) => Left<R2, L2>;
|
|
778
|
+
left: (error: L) => Right<R3, L3>;
|
|
779
|
+
}): Left<never, L2>;
|
|
780
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
781
|
+
right: (value: R) => Right<R2, L2>;
|
|
782
|
+
left: (error: L) => Left<R3, L3>;
|
|
783
|
+
}): Right<R2, never>;
|
|
784
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
785
|
+
right: (value: R) => Left<R2, L2>;
|
|
786
|
+
left: (error: L) => Left<R3, L3>;
|
|
787
|
+
}): Left<never, L2>;
|
|
788
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
789
|
+
right: (value: R) => Either<R2, L2>;
|
|
790
|
+
left: (error: L) => Right<R3, L3>;
|
|
791
|
+
}): Either<R2, L2>;
|
|
792
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
793
|
+
right: (value: R) => Either<R2, L2>;
|
|
794
|
+
left: (error: L) => Left<R3, L3>;
|
|
795
|
+
}): Either<R2, L2>;
|
|
796
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
797
|
+
right: (value: R) => Right<R2, L2>;
|
|
798
|
+
left: (error: L) => Either<R3, L3>;
|
|
799
|
+
}): Right<R2, never>;
|
|
800
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
801
|
+
right: (value: R) => Left<R2, L2>;
|
|
802
|
+
left: (error: L) => Either<R3, L3>;
|
|
803
|
+
}): Left<never, L2>;
|
|
804
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
|
|
805
|
+
right: (value: R) => Either<R2, L2>;
|
|
806
|
+
left: (error: L) => Either<R3, L3>;
|
|
807
|
+
}): Either<R2, L2>;
|
|
808
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
809
|
+
right: (value: R) => Right<R2, L2>;
|
|
810
|
+
left: (error: L) => Right<R3, L3>;
|
|
811
|
+
}): Right<R2 | R3, never>;
|
|
812
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
813
|
+
right: (value: R) => Left<R2, L2>;
|
|
814
|
+
left: (error: L) => Right<R3, L3>;
|
|
815
|
+
}): Either<R3, L2>;
|
|
816
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
817
|
+
right: (value: R) => Right<R2, L2>;
|
|
818
|
+
left: (error: L) => Left<R3, L3>;
|
|
819
|
+
}): Either<R2, L3>;
|
|
820
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
821
|
+
right: (value: R) => Left<R2, L2>;
|
|
822
|
+
left: (error: L) => Left<R3, L3>;
|
|
823
|
+
}): Left<never, L2 | L3>;
|
|
824
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
825
|
+
right: (value: R) => Either<R2, L2>;
|
|
826
|
+
left: (error: L) => Right<R3, L3>;
|
|
827
|
+
}): Either<R2 | R3, L2>;
|
|
828
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
829
|
+
right: (value: R) => Either<R2, L2>;
|
|
830
|
+
left: (error: L) => Left<R3, L3>;
|
|
831
|
+
}): Either<R2, L2 | L3>;
|
|
832
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
833
|
+
right: (value: R) => Right<R2, L2>;
|
|
834
|
+
left: (error: L) => Either<R3, L3>;
|
|
835
|
+
}): Either<R2 | R3, L3>;
|
|
836
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
837
|
+
right: (value: R) => Left<R2, L2>;
|
|
838
|
+
left: (error: L) => Either<R3, L3>;
|
|
839
|
+
}): Either<R3, L2 | L3>;
|
|
840
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
841
|
+
right: (value: R) => Either<R2, L2>;
|
|
842
|
+
left: (error: L) => Either<R3, L3>;
|
|
843
|
+
}): Either<R2 | R3, L2 | L3>;
|
|
844
|
+
/**
|
|
845
|
+
* Either type guard for 'right'
|
|
846
|
+
*
|
|
847
|
+
* @returns 'true' in case this is 'right value' (and resolves type to be 'right')
|
|
848
|
+
*
|
|
849
|
+
* @example
|
|
850
|
+
* ```typescript
|
|
851
|
+
* if (either.isRight()) {
|
|
852
|
+
* console.log(either.right)
|
|
853
|
+
* }
|
|
854
|
+
* ```
|
|
855
|
+
*/
|
|
856
|
+
isRight(): this is Right<R, L>;
|
|
857
|
+
/**
|
|
858
|
+
* Either type guard for 'left'
|
|
859
|
+
*
|
|
860
|
+
* @returns 'true' in case this is 'left error' (and resolves type to be 'left')
|
|
861
|
+
*
|
|
862
|
+
* @example
|
|
863
|
+
* ```typescript
|
|
864
|
+
* if (either.isLeft()) {
|
|
865
|
+
* console.error(either.left)
|
|
866
|
+
* }
|
|
867
|
+
* ```
|
|
868
|
+
*/
|
|
869
|
+
isLeft(): this is Left<R, L>;
|
|
870
|
+
/**
|
|
871
|
+
* Wrap Either to singleton generator
|
|
872
|
+
*
|
|
873
|
+
* Useful in order to avoid ambiguous yield types
|
|
874
|
+
*
|
|
875
|
+
* @returns generator of Either wrapping this
|
|
876
|
+
*/
|
|
877
|
+
generator: EitherGeneratorFunction<[], R, Right<R, L>, R>;
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* Left (erroneous) value of type L
|
|
881
|
+
*
|
|
882
|
+
* Either data type specialization representing an erroneous value
|
|
883
|
+
*
|
|
884
|
+
* @template R underlying value type (needed for type merging, in fact always considered to be never)
|
|
885
|
+
* @template L underlying error type
|
|
886
|
+
*/
|
|
887
|
+
export interface Left<R, L> {
|
|
888
|
+
readonly left: L;
|
|
889
|
+
/**
|
|
890
|
+
* Either peeker function
|
|
891
|
+
*
|
|
892
|
+
* Applied to 'right value' returns self invoking op(value) in process
|
|
893
|
+
* Applied to 'left error' returns self without invoking callback
|
|
894
|
+
*
|
|
895
|
+
* @param op function to be invoked with underlying value
|
|
896
|
+
* @returns self
|
|
897
|
+
*/
|
|
898
|
+
tap(op: (value: R) => void): this;
|
|
899
|
+
/**
|
|
900
|
+
* Either fallback peeker function
|
|
901
|
+
*
|
|
902
|
+
* Applied to 'right value' returns self without invoking callback
|
|
903
|
+
* Applied to 'left error' returns self invoking op(error) in process
|
|
904
|
+
*
|
|
905
|
+
* @param op function to be invoked with underlying error
|
|
906
|
+
* @returns self
|
|
907
|
+
*/
|
|
908
|
+
orTap(op: (error: L) => void): this;
|
|
909
|
+
/**
|
|
910
|
+
* Either pattern matching peeker function
|
|
911
|
+
*
|
|
912
|
+
* Applied to 'right value' returns self invoking op.right(value) in process
|
|
913
|
+
* Applied to 'left error' returns self invoking op.left(error) in process
|
|
914
|
+
*
|
|
915
|
+
* @param op.right function to be invoked with underlying value in case of 'right'
|
|
916
|
+
* @param op.left function to be invoked with underlying error in case of 'left'
|
|
917
|
+
* @returns self
|
|
918
|
+
*/
|
|
919
|
+
matchTap(op: {
|
|
920
|
+
right: (value: R) => void;
|
|
921
|
+
left: (error: L) => void;
|
|
922
|
+
}): this;
|
|
923
|
+
/**
|
|
924
|
+
* Either transformer function
|
|
925
|
+
*
|
|
926
|
+
* Applied to 'right value' returns 'right op(value)'
|
|
927
|
+
* Applied to 'left error' returns self without invoking transformer
|
|
928
|
+
*
|
|
929
|
+
* @template R2 transformer function's return type
|
|
930
|
+
* @param op transformer to be invoked with underlying value
|
|
931
|
+
* @returns 'right op(value)' or 'left error'
|
|
932
|
+
*/
|
|
933
|
+
map<R2>(this: Left<R, L>, op: (value: R) => R2): Left<never, L>;
|
|
934
|
+
map<R2>(this: Either<R, L>, op: (value: R) => R2): Either<R2, L>;
|
|
935
|
+
/**
|
|
936
|
+
* Either fallback transformer function
|
|
937
|
+
*
|
|
938
|
+
* Applied to 'right value' returns self without invoking transformer
|
|
939
|
+
* Applied to 'left error' returns 'right op(error)'
|
|
940
|
+
*
|
|
941
|
+
* @param op transformer to be invoked with underlying error
|
|
942
|
+
* @returns 'right value' or 'right op(error)'
|
|
943
|
+
*/
|
|
944
|
+
orMap<R2>(this: Left<R, L>, op: (error: L) => R2): Right<R2, never>;
|
|
945
|
+
orMap<R2>(this: Either<R, L>, op: (error: L) => R2): Right<R | R2, never>;
|
|
946
|
+
/**
|
|
947
|
+
* Either pattern matching extractor function
|
|
948
|
+
*
|
|
949
|
+
* Applied to 'right value' returns op.right(value)
|
|
950
|
+
* Applied to 'left error' returns op.left(error)
|
|
951
|
+
*
|
|
952
|
+
* @template R2 right extractor function's return type
|
|
953
|
+
* @template R3 left extractor function's return type
|
|
954
|
+
* @param op.right extractor to be invoked with underlying value in case of 'right'
|
|
955
|
+
* @param op.left extractor to be invoked with underlying error in case of 'left'
|
|
956
|
+
* @returns op.right(value) or op.left(error)
|
|
957
|
+
*/
|
|
958
|
+
match<R2, R3 = R2>(this: Left<R, L>, op: {
|
|
959
|
+
right: (value: R) => R2;
|
|
960
|
+
left: (error: L) => R3;
|
|
961
|
+
}): R3;
|
|
962
|
+
match<R2, R3 = R2>(this: Either<R, L>, op: {
|
|
963
|
+
right: (value: R) => R2;
|
|
964
|
+
left: (error: L) => R3;
|
|
965
|
+
}): R2 | R3;
|
|
966
|
+
matchMap<R2, R3 = R2>(this: Left<R, L>, op: {
|
|
967
|
+
right: (value: R) => R2;
|
|
968
|
+
left: (error: L) => R3;
|
|
969
|
+
}): Right<R3, never>;
|
|
970
|
+
matchMap<R2, R3 = R2>(this: Either<R, L>, op: {
|
|
971
|
+
right: (value: R) => R2;
|
|
972
|
+
left: (error: L) => R3;
|
|
973
|
+
}): Right<R2 | R3, never>;
|
|
974
|
+
/**
|
|
975
|
+
* Either composition function
|
|
976
|
+
*
|
|
977
|
+
* Applied to 'right value' returns 'op(value)'
|
|
978
|
+
* Applied to 'left error' returns self without invoking composition function
|
|
979
|
+
*
|
|
980
|
+
* @template R2 transformer function result's underlying value type
|
|
981
|
+
* @template L2 transformer function result's underlying error type
|
|
982
|
+
* @param op transformer to be invoked with underlying value
|
|
983
|
+
* @returns 'op(value)' or 'left error'
|
|
984
|
+
*/
|
|
985
|
+
chain<R2, L2>(this: Left<R, L>, op: (value: R) => Right<R2, L2>): Left<never, L>;
|
|
986
|
+
chain<R2, L2>(this: Left<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L>;
|
|
987
|
+
chain<R2, L2>(this: Left<R, L>, op: (value: R) => Either<R2, L2>): Left<never, L>;
|
|
988
|
+
chain<R2, L2>(this: Either<R, L>, op: (value: R) => Right<R2, L2>): Either<R2, L>;
|
|
989
|
+
chain<R2, L2>(this: Either<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L | L2>;
|
|
990
|
+
chain<R2, L2>(this: Either<R, L>, op: (value: R) => Either<R2, L2>): Either<R2, L | L2>;
|
|
991
|
+
/**
|
|
992
|
+
* Either fallback composition function
|
|
993
|
+
*
|
|
994
|
+
* Applied to 'right value' returns self without invoking composition function
|
|
995
|
+
* Applied to 'left error' returns 'op(error)'
|
|
996
|
+
*
|
|
997
|
+
* @template R2 transformer function result's underlying value type
|
|
998
|
+
* @template L2 transformer function result's underlying error type
|
|
999
|
+
* @param op transformer to be invoked with underlying error
|
|
1000
|
+
* @returns 'right value' or 'op(error)'
|
|
1001
|
+
*/
|
|
1002
|
+
orChain<R2, L2>(this: Left<R, L>, op: (error: L) => Right<R2, L2>): Right<R2, never>;
|
|
1003
|
+
orChain<R2, L2>(this: Left<R, L>, op: (error: L) => Left<R2, L2>): Left<never, L2>;
|
|
1004
|
+
orChain<R2, L2>(this: Left<R, L>, op: (error: L) => Either<R2, L2>): Either<R2, L2>;
|
|
1005
|
+
orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Right<R2, L2>): Right<R | R2, never>;
|
|
1006
|
+
orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Left<R2, L2>): Either<R, L2>;
|
|
1007
|
+
orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Either<R2, L2>): Either<R | R2, L2>;
|
|
1008
|
+
/**
|
|
1009
|
+
* Either pattern matching composition function
|
|
1010
|
+
*
|
|
1011
|
+
* Applied to 'right value' returns 'op.right(value)'
|
|
1012
|
+
* Applied to 'left error' returns 'op.left(error)'
|
|
1013
|
+
*
|
|
1014
|
+
* @template R2 right transformer function result's underlying value type
|
|
1015
|
+
* @template L2 right transformer function result's underlying error type
|
|
1016
|
+
* @template R3 left transformer function result's underlying value type
|
|
1017
|
+
* @template L3 left transformer function result's underlying error type
|
|
1018
|
+
* @param op.right transformer to be invoked with underlying value in case of 'right'
|
|
1019
|
+
* @param op.left transformer to be invoked with underlying error in case of 'left'
|
|
1020
|
+
* @returns 'op(value)' or 'op(error)'
|
|
1021
|
+
*/
|
|
1022
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1023
|
+
right: (value: R) => Right<R2, L2>;
|
|
1024
|
+
left: (error: L) => Right<R3, L3>;
|
|
1025
|
+
}): Right<R3, never>;
|
|
1026
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1027
|
+
right: (value: R) => Left<R2, L2>;
|
|
1028
|
+
left: (error: L) => Right<R3, L3>;
|
|
1029
|
+
}): Right<R3, never>;
|
|
1030
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1031
|
+
right: (value: R) => Right<R2, L2>;
|
|
1032
|
+
left: (error: L) => Left<R3, L3>;
|
|
1033
|
+
}): Left<never, L3>;
|
|
1034
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1035
|
+
right: (value: R) => Left<R2, L2>;
|
|
1036
|
+
left: (error: L) => Left<R3, L3>;
|
|
1037
|
+
}): Left<never, L3>;
|
|
1038
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1039
|
+
right: (value: R) => Either<R2, L2>;
|
|
1040
|
+
left: (error: L) => Right<R3, L3>;
|
|
1041
|
+
}): Right<R3, never>;
|
|
1042
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1043
|
+
right: (value: R) => Either<R2, L2>;
|
|
1044
|
+
left: (error: L) => Left<R3, L3>;
|
|
1045
|
+
}): Left<never, L3>;
|
|
1046
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1047
|
+
right: (value: R) => Right<R2, L2>;
|
|
1048
|
+
left: (error: L) => Either<R3, L3>;
|
|
1049
|
+
}): Either<R3, L3>;
|
|
1050
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1051
|
+
right: (value: R) => Left<R2, L2>;
|
|
1052
|
+
left: (error: L) => Either<R3, L3>;
|
|
1053
|
+
}): Either<R3, L3>;
|
|
1054
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
|
|
1055
|
+
right: (value: R) => Either<R2, L2>;
|
|
1056
|
+
left: (error: L) => Either<R3, L3>;
|
|
1057
|
+
}): Either<R3, L3>;
|
|
1058
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1059
|
+
right: (value: R) => Right<R2, L2>;
|
|
1060
|
+
left: (error: L) => Right<R3, L3>;
|
|
1061
|
+
}): Right<R2 | R3, never>;
|
|
1062
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1063
|
+
right: (value: R) => Left<R2, L2>;
|
|
1064
|
+
left: (error: L) => Right<R3, L3>;
|
|
1065
|
+
}): Either<R3, L2>;
|
|
1066
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1067
|
+
right: (value: R) => Right<R2, L2>;
|
|
1068
|
+
left: (error: L) => Left<R3, L3>;
|
|
1069
|
+
}): Either<R2, L3>;
|
|
1070
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1071
|
+
right: (value: R) => Left<R2, L2>;
|
|
1072
|
+
left: (error: L) => Left<R3, L3>;
|
|
1073
|
+
}): Left<never, L2 | L3>;
|
|
1074
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1075
|
+
right: (value: R) => Either<R2, L2>;
|
|
1076
|
+
left: (error: L) => Right<R3, L3>;
|
|
1077
|
+
}): Either<R2 | R3, L2>;
|
|
1078
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1079
|
+
right: (value: R) => Either<R2, L2>;
|
|
1080
|
+
left: (error: L) => Left<R3, L3>;
|
|
1081
|
+
}): Either<R2, L2 | L3>;
|
|
1082
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1083
|
+
right: (value: R) => Right<R2, L2>;
|
|
1084
|
+
left: (error: L) => Either<R3, L3>;
|
|
1085
|
+
}): Either<R2 | R3, L3>;
|
|
1086
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1087
|
+
right: (value: R) => Left<R2, L2>;
|
|
1088
|
+
left: (error: L) => Either<R3, L3>;
|
|
1089
|
+
}): Either<R3, L2 | L3>;
|
|
1090
|
+
matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
|
|
1091
|
+
right: (value: R) => Either<R2, L2>;
|
|
1092
|
+
left: (error: L) => Either<R3, L3>;
|
|
1093
|
+
}): Either<R2 | R3, L2 | L3>;
|
|
1094
|
+
/**
|
|
1095
|
+
* Either type guard for 'right'
|
|
1096
|
+
*
|
|
1097
|
+
* @returns 'true' in case this is 'right value' (and resolves type to be 'right')
|
|
1098
|
+
*
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```typescript
|
|
1101
|
+
* if (either.isRight()) {
|
|
1102
|
+
* console.log(either.right)
|
|
1103
|
+
* }
|
|
1104
|
+
* ```
|
|
1105
|
+
*/
|
|
1106
|
+
isRight(): this is Right<R, L>;
|
|
1107
|
+
/**
|
|
1108
|
+
* Either type guard for 'left'
|
|
1109
|
+
*
|
|
1110
|
+
* @returns 'true' in case this is 'left error' (and resolves type to be 'left')
|
|
1111
|
+
*
|
|
1112
|
+
* @example
|
|
1113
|
+
* ```typescript
|
|
1114
|
+
* if (either.isLeft()) {
|
|
1115
|
+
* console.error(either.left)
|
|
1116
|
+
* }
|
|
1117
|
+
* ```
|
|
1118
|
+
*/
|
|
1119
|
+
isLeft(): this is Left<R, L>;
|
|
1120
|
+
/**
|
|
1121
|
+
* Wrap Either to singleton generator
|
|
1122
|
+
*
|
|
1123
|
+
* Useful in order to avoid ambiguous yield types
|
|
1124
|
+
*
|
|
1125
|
+
* @returns generator of Either wrapping this
|
|
1126
|
+
*/
|
|
1127
|
+
generator: EitherGeneratorFunction<[], R, Left<R, L>, R>;
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Either data type: either Right value of type R or Left value of type L
|
|
1131
|
+
*
|
|
1132
|
+
* As per classic Either monad implementation can either contain a right (correct) value or a left (erroneous) value
|
|
1133
|
+
* Used throughout the library to represent the result of failable operations, namely failed tasks
|
|
1134
|
+
*
|
|
1135
|
+
* @template R underlying value type
|
|
1136
|
+
* @template L underlying error type
|
|
1137
|
+
*/
|
|
1138
|
+
export type Either<R, L> = Right<R, L> | Left<R, L>;
|
|
1139
|
+
export declare namespace Right {
|
|
1140
|
+
/**
|
|
1141
|
+
* Standalone Right value extractor
|
|
1142
|
+
*
|
|
1143
|
+
* Useful for passing as a function to collection transformers
|
|
1144
|
+
*
|
|
1145
|
+
* @template R underlying value type
|
|
1146
|
+
* @template L underlying error type
|
|
1147
|
+
* @param right Right instance
|
|
1148
|
+
* @returns underlying value
|
|
1149
|
+
*/
|
|
1150
|
+
function right<R, L>(right: Right<R, L>): R;
|
|
1151
|
+
}
|
|
1152
|
+
export declare namespace Left {
|
|
1153
|
+
/**
|
|
1154
|
+
* Standalone Left error extractor
|
|
1155
|
+
*
|
|
1156
|
+
* Useful for passing as a function to collection transformers
|
|
1157
|
+
*
|
|
1158
|
+
* @template R underlying value type
|
|
1159
|
+
* @template L underlying error type
|
|
1160
|
+
* @param left Left instance
|
|
1161
|
+
* @returns underlying error
|
|
1162
|
+
*/
|
|
1163
|
+
function left<R, L>(left: Left<R, L>): L;
|
|
1164
|
+
}
|
|
1165
|
+
export type EitherGenerator<T, TT extends Either<T, any>, R> = Generator<TT, R, T>;
|
|
1166
|
+
export type EitherGeneratorFunction<A extends unknown[], T, TT extends Either<T, any>, R> = (...args: A) => EitherGenerator<T, TT, R>;
|
|
1167
|
+
export declare namespace Either {
|
|
1168
|
+
/**
|
|
1169
|
+
* Right monad constructor
|
|
1170
|
+
*
|
|
1171
|
+
* @template R underlying value type
|
|
1172
|
+
* @param value underlying value
|
|
1173
|
+
* @returns 'right value'
|
|
1174
|
+
*/
|
|
1175
|
+
function right<R>(value: R): Right<R, never>;
|
|
1176
|
+
/**
|
|
1177
|
+
* Left monad constructor
|
|
1178
|
+
*
|
|
1179
|
+
* @template L underlying error type
|
|
1180
|
+
* @param error underlying error
|
|
1181
|
+
* @returns 'left error'
|
|
1182
|
+
*/
|
|
1183
|
+
function left<L>(error: L): Left<never, L>;
|
|
1184
|
+
/**
|
|
1185
|
+
* Either constructor from optional value
|
|
1186
|
+
*
|
|
1187
|
+
* Resolves to 'left error' in case of undefined value and 'right value' otherwise
|
|
1188
|
+
*
|
|
1189
|
+
* @param value possibly undefined value
|
|
1190
|
+
* @param error error to use in case of undefined value
|
|
1191
|
+
* @returns either 'right value' or 'left error'
|
|
1192
|
+
*/
|
|
1193
|
+
function fromOptional<L>(value: undefined, error: L): Left<never, L>;
|
|
1194
|
+
function fromOptional<R>(value: Exclude<R, undefined>, error: unknown): Right<R, never>;
|
|
1195
|
+
function fromOptional<R, L>(value: R | undefined, error: L): Either<R, L>;
|
|
1196
|
+
/**
|
|
1197
|
+
* Either constructor from optional or nullable value
|
|
1198
|
+
*
|
|
1199
|
+
* Resolves to 'left error' in case of undefined or null value and 'right value' otherwise
|
|
1200
|
+
*
|
|
1201
|
+
* @param value possibly undefined or nullable value
|
|
1202
|
+
* @param error error to use in case of undefined or nullable value
|
|
1203
|
+
* @returns either 'right value' or 'left error'
|
|
1204
|
+
*/
|
|
1205
|
+
function fromNullable<L>(value: undefined, error: L): Left<never, L>;
|
|
1206
|
+
function fromNullable<L>(value: null, error: L): Left<never, L>;
|
|
1207
|
+
function fromNullable<R>(value: Exclude<R, null | undefined>, error: unknown): Right<R, never>;
|
|
1208
|
+
function fromNullable<R, L>(value: R | null | undefined, error: L): Either<R, L>;
|
|
1209
|
+
/**
|
|
1210
|
+
* Standalone Either value extractor
|
|
1211
|
+
*
|
|
1212
|
+
* Applied to 'right value' returns underlying value without using fallback
|
|
1213
|
+
* Applied to 'left error' returns fallback
|
|
1214
|
+
*
|
|
1215
|
+
* @template R underlying value type
|
|
1216
|
+
* @template L underlying error type
|
|
1217
|
+
* @template R2 fallback value type
|
|
1218
|
+
* @param either Either instance
|
|
1219
|
+
* @param fallback value to be returned in case of 'left error'
|
|
1220
|
+
* @returns underlying value or fallback
|
|
1221
|
+
*/
|
|
1222
|
+
function fromEither<R, L>(either: Right<R, L>, fallback: unknown): R;
|
|
1223
|
+
function fromEither<R, L, R2>(either: Left<R, L>, fallback: R2): R2;
|
|
1224
|
+
function fromEither<R, L, R2>(either: Either<R, L>, fallback: R2): R | R2;
|
|
1225
|
+
/**
|
|
1226
|
+
* Standalone type guard for 'right'
|
|
1227
|
+
*
|
|
1228
|
+
* Useful for passing as a predicate to collection transformers
|
|
1229
|
+
*
|
|
1230
|
+
* @template R underlying value type
|
|
1231
|
+
* @param either Either instance
|
|
1232
|
+
* @returns 'true' in case argument is 'right value' (and resolves type to be 'right')
|
|
1233
|
+
*/
|
|
1234
|
+
function isRight<R, L>(either: Either<R, L>): either is Right<R, never>;
|
|
1235
|
+
/**
|
|
1236
|
+
* Standalone type guard for 'left'
|
|
1237
|
+
*
|
|
1238
|
+
* Useful for passing as a predicate to collection transformers
|
|
1239
|
+
*
|
|
1240
|
+
* @template R underlying value type
|
|
1241
|
+
* @param either Either instance
|
|
1242
|
+
* @returns 'true' in case argument is 'left error' (and resolves type to be 'left')
|
|
1243
|
+
*/
|
|
1244
|
+
function isLeft<R, L>(either: Either<R, L>): either is Left<never, L>;
|
|
1245
|
+
/**
|
|
1246
|
+
* Standalone list predicate
|
|
1247
|
+
*
|
|
1248
|
+
* @param eithers a list of Either
|
|
1249
|
+
* @returns true in case every list element is Right
|
|
1250
|
+
*/
|
|
1251
|
+
function everyRight<R, L>(eithers: Either<R, L>[]): eithers is Right<R, never>[];
|
|
1252
|
+
/**
|
|
1253
|
+
* Standalone list predicate
|
|
1254
|
+
*
|
|
1255
|
+
* @param eithers a list of Either
|
|
1256
|
+
* @returns true in case at least one list element is Right
|
|
1257
|
+
*/
|
|
1258
|
+
function someRight<R, L>(eithers: Either<R, L>[]): boolean;
|
|
1259
|
+
/**
|
|
1260
|
+
* Standalone list predicate
|
|
1261
|
+
*
|
|
1262
|
+
* @param eithers a list of Either
|
|
1263
|
+
* @returns true in case every list element is Left
|
|
1264
|
+
*/
|
|
1265
|
+
function everyLeft<R, L>(eithers: Either<R, L>[]): eithers is Left<never, L>[];
|
|
1266
|
+
/**
|
|
1267
|
+
* Standalone list predicate
|
|
1268
|
+
*
|
|
1269
|
+
* @param eithers a list of Either
|
|
1270
|
+
* @returns true in case at least one list element is Left
|
|
1271
|
+
*/
|
|
1272
|
+
function someLeft<R, L>(eithers: Either<R, L>[]): boolean;
|
|
1273
|
+
/**
|
|
1274
|
+
* Create compound Either from generator function
|
|
1275
|
+
*
|
|
1276
|
+
* Applying yield to an Either within the generator function unwraps the Either and returns underlying value in case of success
|
|
1277
|
+
* However the convenient option for typescript is to use ```yield* either.generator()``` as otherwise one may have to deal with union types
|
|
1278
|
+
*
|
|
1279
|
+
* @template TT yielded Either type
|
|
1280
|
+
* @template R returned underlying type
|
|
1281
|
+
* @param eitherGeneratorFunction Either generator function
|
|
1282
|
+
* @returns Right wrapping the result of generator function or Left
|
|
1283
|
+
*/
|
|
1284
|
+
function generate<T, TT extends Either<T, any>, R>(eitherGeneratorFunction: EitherGeneratorFunction<[], T, TT, R>): Either<R, any>;
|
|
1285
|
+
}
|
|
1286
|
+
//#endregion
|
|
1287
|
+
//#region src/task.d.ts
|
|
1288
|
+
/**
|
|
1289
|
+
* Shortcut for monadic Either type, where erroneous value is of type any
|
|
1290
|
+
*
|
|
1291
|
+
* @template R underlying type
|
|
1292
|
+
*/
|
|
1293
|
+
export type Rejectable<R> = Either<R, any>;
|
|
1294
|
+
/**
|
|
1295
|
+
* Shortcut for underlying task result type
|
|
1296
|
+
*
|
|
1297
|
+
* @template R underlying type
|
|
1298
|
+
*/
|
|
1299
|
+
export type Cancelable<R> = Maybe<Rejectable<R>>;
|
|
1300
|
+
/**
|
|
1301
|
+
* Function returning Task
|
|
1302
|
+
*
|
|
1303
|
+
* @template A argument types
|
|
1304
|
+
* @template R returned task resolve type
|
|
1305
|
+
*/
|
|
1306
|
+
export type TaskFunction<A extends unknown[], R> = (...args: A) => Task<R>;
|
|
1307
|
+
/**
|
|
1308
|
+
* Task generator
|
|
1309
|
+
*
|
|
1310
|
+
* @template TT yielded task type
|
|
1311
|
+
* @template R returned task resolve type
|
|
1312
|
+
*
|
|
1313
|
+
* @example
|
|
1314
|
+
* ```typescript
|
|
1315
|
+
* const generatorFunction = function*(): TaskGenerator<unknown, Task<string>, number> {
|
|
1316
|
+
* const v = yield* someTaskFunction().generator();
|
|
1317
|
+
*
|
|
1318
|
+
* return v.length;
|
|
1319
|
+
* };
|
|
1320
|
+
* ```
|
|
1321
|
+
*/
|
|
1322
|
+
export type TaskGenerator<T, TT extends Task<T>, R> = Generator<TT, R, T>;
|
|
1323
|
+
/**
|
|
1324
|
+
* Function returning task generator (generator function)
|
|
1325
|
+
*
|
|
1326
|
+
* @template A argument types
|
|
1327
|
+
* @template TT yielded task type
|
|
1328
|
+
* @template R returned task resolve type
|
|
1329
|
+
*
|
|
1330
|
+
* @example
|
|
1331
|
+
* ```typescript
|
|
1332
|
+
* const generatorFunction: TaskGeneratorFunction<[], unknown, Task<string>, number> = function*() {
|
|
1333
|
+
* const v = yield* someTaskFunction().generator();
|
|
1334
|
+
*
|
|
1335
|
+
* return v.length;
|
|
1336
|
+
* };
|
|
1337
|
+
* ```
|
|
1338
|
+
*/
|
|
1339
|
+
export type TaskGeneratorFunction<A extends unknown[], T, TT extends Task<T>, R> = (...args: A) => TaskGenerator<T, TT, R>;
|
|
1340
|
+
/**
|
|
1341
|
+
* Task monad interface
|
|
1342
|
+
*
|
|
1343
|
+
* @template R task resolve type
|
|
1344
|
+
*/
|
|
1345
|
+
export interface Task<R> extends TaskBase<R> {
|
|
1346
|
+
/**
|
|
1347
|
+
* Invoke callback when task is resolved (and only then)
|
|
1348
|
+
*
|
|
1349
|
+
* @param op callback to invoke
|
|
1350
|
+
* @returns self
|
|
1351
|
+
*/
|
|
1352
|
+
tap(op: (value: R) => void): Task<R>;
|
|
1353
|
+
/**
|
|
1354
|
+
* Invoke callback when task is rejected (and only then)
|
|
1355
|
+
*
|
|
1356
|
+
* @param op callback to invoke
|
|
1357
|
+
* @returns self
|
|
1358
|
+
*/
|
|
1359
|
+
tapRejected(op: (error: any) => void): Task<R>;
|
|
1360
|
+
/**
|
|
1361
|
+
* Invoke callback when task is canceled (and only then)
|
|
1362
|
+
*
|
|
1363
|
+
* @param op callback to invoke
|
|
1364
|
+
* @returns self
|
|
1365
|
+
*/
|
|
1366
|
+
tapCanceled(op: () => void): Task<R>;
|
|
1367
|
+
/**
|
|
1368
|
+
* Invoke a dedicated callback according to task resolution
|
|
1369
|
+
*
|
|
1370
|
+
* @param op.resolved callback to invoke on success
|
|
1371
|
+
* @param op.rejected callback to invoke on failure
|
|
1372
|
+
* @param op.canceled callback to invoke on cancelation
|
|
1373
|
+
* @returns self
|
|
1374
|
+
*/
|
|
1375
|
+
matchTap(op: {
|
|
1376
|
+
resolved: (value: R) => void;
|
|
1377
|
+
rejected: (error: any) => void;
|
|
1378
|
+
canceled: () => void;
|
|
1379
|
+
}): Task<R>;
|
|
1380
|
+
/**
|
|
1381
|
+
* Invoke transformer when task is resolved (and only then) and return it's result instead
|
|
1382
|
+
*
|
|
1383
|
+
* @template R2 transformer return type
|
|
1384
|
+
* @param op transformer to invoke
|
|
1385
|
+
* @returns task returning transformer result
|
|
1386
|
+
*/
|
|
1387
|
+
map<R2>(op: (value: R) => R2): Task<R2>;
|
|
1388
|
+
/**
|
|
1389
|
+
* Invoke transformer when task is rejected (and only then) and return it's result instead
|
|
1390
|
+
*
|
|
1391
|
+
* @template R2 fallback return type
|
|
1392
|
+
* @param op transformer to invoke
|
|
1393
|
+
* @returns task returning fallback result in case of failure
|
|
1394
|
+
*/
|
|
1395
|
+
mapRejected<R2>(op: (error: any) => R2): Task<R | R2>;
|
|
1396
|
+
/**
|
|
1397
|
+
* Invoke transformer when task is canceled (and only then) and return it's result instead
|
|
1398
|
+
*
|
|
1399
|
+
* @template R2 fallback return type
|
|
1400
|
+
* @param op transformer to invoke
|
|
1401
|
+
* @returns task returning fallback result in case of cancelation
|
|
1402
|
+
*/
|
|
1403
|
+
mapCanceled<R2>(op: () => R2): Task<R | R2>;
|
|
1404
|
+
/**
|
|
1405
|
+
* Invoke a dedicated transformer according to task resolution
|
|
1406
|
+
*
|
|
1407
|
+
* @template R2 success transformer return type
|
|
1408
|
+
* @template R3 failure transformer return type
|
|
1409
|
+
* @template R4 cancelation transformer return type
|
|
1410
|
+
* @param op.resolved transformer to invoke on success
|
|
1411
|
+
* @param op.rejected transformer to invoke on failure
|
|
1412
|
+
* @param op.canceled transformer to invoke on cancelation
|
|
1413
|
+
* @returns task resolving to a corresponding transformer result
|
|
1414
|
+
*/
|
|
1415
|
+
matchMap<R2, R3 = R2, R4 = R3>(op: {
|
|
1416
|
+
resolved: (value: R) => R2;
|
|
1417
|
+
rejected: (error: any) => R3;
|
|
1418
|
+
canceled: () => R4;
|
|
1419
|
+
}): Task<R2 | R3 | R4>;
|
|
1420
|
+
/**
|
|
1421
|
+
* Invoke transformer when task is resolved (and only then) and continue execution with it's result
|
|
1422
|
+
*
|
|
1423
|
+
* @template R2 transformer task resolve type
|
|
1424
|
+
* @param op transformer to invoke
|
|
1425
|
+
* @returns task chaining to transformer result task
|
|
1426
|
+
*/
|
|
1427
|
+
chain<R2>(op: (value: R) => Task<R2>): Task<R2>;
|
|
1428
|
+
/**
|
|
1429
|
+
* Invoke transformer when task is rejected (and only then) and continue execution with it's result
|
|
1430
|
+
*
|
|
1431
|
+
* @template R2 fallback task resolve type
|
|
1432
|
+
* @param op transformer to invoke
|
|
1433
|
+
* @returns task chaining to fallback task in case of failure
|
|
1434
|
+
*/
|
|
1435
|
+
chainRejected<R2>(op: (error: any) => Task<R2>): Task<R | R2>;
|
|
1436
|
+
/**
|
|
1437
|
+
* Invoke transformer when task is canceled (and only then) and continue execution with it's result
|
|
1438
|
+
*
|
|
1439
|
+
* @template R2 fallback task resolve type
|
|
1440
|
+
* @param op transformer to invoke
|
|
1441
|
+
* @returns task chaining to fallback task in case of cancelation
|
|
1442
|
+
*/
|
|
1443
|
+
chainCanceled<R2>(op: () => Task<R2>): Task<R | R2>;
|
|
1444
|
+
/**
|
|
1445
|
+
* Invoke a dedicated transformer according to task resolution and continue execution with it's result
|
|
1446
|
+
*
|
|
1447
|
+
* @template R2 success transformer resolve type
|
|
1448
|
+
* @template R3 failure transformer resolve type
|
|
1449
|
+
* @template R4 cancelation transformer resolve type
|
|
1450
|
+
* @param op.resolved transformer to invoke on success
|
|
1451
|
+
* @param op.rejected transformer to invoke on failure
|
|
1452
|
+
* @param op.canceled transformer to invoke on cancelation
|
|
1453
|
+
* @returns task chaining to a corresponding transformer result
|
|
1454
|
+
*/
|
|
1455
|
+
matchChain<R2, R3 = R2, R4 = R3>(op: {
|
|
1456
|
+
resolved: (value: R) => Task<R2>;
|
|
1457
|
+
rejected: (error: any) => Task<R3>;
|
|
1458
|
+
canceled: () => Task<R4>;
|
|
1459
|
+
}): Task<R2 | R3 | R4>;
|
|
1460
|
+
resolved: () => Cancelable<R> | null;
|
|
1461
|
+
/**
|
|
1462
|
+
* Return underlying promise in order to await result
|
|
1463
|
+
*
|
|
1464
|
+
* @returns underlying promise
|
|
1465
|
+
*/
|
|
1466
|
+
resolve: () => TaskInvoke<R>;
|
|
1467
|
+
/**
|
|
1468
|
+
* Invoke underlying canel method without error
|
|
1469
|
+
*/
|
|
1470
|
+
cancel: () => boolean;
|
|
1471
|
+
/**
|
|
1472
|
+
* Invoke underlying canel method with error
|
|
1473
|
+
*
|
|
1474
|
+
* @param error error value to be injected from outside
|
|
1475
|
+
*/
|
|
1476
|
+
reject: (error: any) => boolean;
|
|
1477
|
+
/**
|
|
1478
|
+
* Wrap task to singleton generator
|
|
1479
|
+
*
|
|
1480
|
+
* Userful in order to avoid ambiguous yied types
|
|
1481
|
+
*
|
|
1482
|
+
* @returns generator of task wrapping the task
|
|
1483
|
+
* @example
|
|
1484
|
+
* ```typescript
|
|
1485
|
+
* const getString = () => Task.resolved('hello');
|
|
1486
|
+
* const getLength = (data: string) => Task.resolved(data.length);
|
|
1487
|
+
*
|
|
1488
|
+
* // ..... //
|
|
1489
|
+
*
|
|
1490
|
+
* Task.generate(function* () {
|
|
1491
|
+
* const data = yield getString(); // data: string | number
|
|
1492
|
+
* const length = yield getLength(data); // length: string | number
|
|
1493
|
+
*
|
|
1494
|
+
* return length;
|
|
1495
|
+
* });
|
|
1496
|
+
*
|
|
1497
|
+
* // ..... //
|
|
1498
|
+
*
|
|
1499
|
+
* Task.generate(function* () {
|
|
1500
|
+
* const data = yield* getString().generator(); // data: string
|
|
1501
|
+
* const length = yield* getLength(data).generator(); // length: number
|
|
1502
|
+
*
|
|
1503
|
+
* return length;
|
|
1504
|
+
* });
|
|
1505
|
+
* ```
|
|
1506
|
+
*/
|
|
1507
|
+
generator: TaskGeneratorFunction<[], unknown, Task<R>, R>;
|
|
1508
|
+
}
|
|
1509
|
+
export declare namespace Task {
|
|
1510
|
+
/**
|
|
1511
|
+
* Custom task monad constructor
|
|
1512
|
+
*
|
|
1513
|
+
* @template R returned task's resolve type
|
|
1514
|
+
* @param invoke promise defining task execution
|
|
1515
|
+
* @param cancel cancelation function
|
|
1516
|
+
* @returns task resolving to resolve value of invoke
|
|
1517
|
+
*
|
|
1518
|
+
* @note low-level primitive for creating custom tasks, not intended for general use
|
|
1519
|
+
*/
|
|
1520
|
+
function create<R>(invoke: TaskInvoke<R>, cancel: TaskCancel): Task<R>;
|
|
1521
|
+
/**
|
|
1522
|
+
* Invariant task constructor creating resolved task from plain value
|
|
1523
|
+
*
|
|
1524
|
+
* @template R returned task's resolve type
|
|
1525
|
+
* @param value value to be returned upon awaiting
|
|
1526
|
+
* @returns task resolving to specified value
|
|
1527
|
+
*/
|
|
1528
|
+
function resolved<R>(value: R): Task<R>;
|
|
1529
|
+
/**
|
|
1530
|
+
* Invariant task constructor creating rejected task from error value
|
|
1531
|
+
*
|
|
1532
|
+
* @template R returned task's resolve type
|
|
1533
|
+
* @param error error to be returned upon awaiting
|
|
1534
|
+
* @returns task resolving to specified value
|
|
1535
|
+
*/
|
|
1536
|
+
function rejected<R>(error: any): Task<R>;
|
|
1537
|
+
/**
|
|
1538
|
+
* Invariant task constructor creating canceled task
|
|
1539
|
+
*
|
|
1540
|
+
* @template R returned task's resolve type
|
|
1541
|
+
* @returns task resolving to specified value
|
|
1542
|
+
*/
|
|
1543
|
+
function canceled<R>(): Task<R>;
|
|
1544
|
+
/**
|
|
1545
|
+
* Lift from promise to task resolving to that promise result
|
|
1546
|
+
*
|
|
1547
|
+
* Userfull for converting promises to tasks
|
|
1548
|
+
* If converted task is canceled or failed externally return value will be ignored without side-effects
|
|
1549
|
+
* All tasks are no-trowing by default, any occured errors are returned as Left<any>
|
|
1550
|
+
*
|
|
1551
|
+
* @template R returned task resolve type
|
|
1552
|
+
* @param promise promise to be resolved
|
|
1553
|
+
* @returns task resolving to specified promise value
|
|
1554
|
+
*
|
|
1555
|
+
* @example
|
|
1556
|
+
* ```typescript
|
|
1557
|
+
* const task = Task.fromPromise(someAsyncOperation('someData')).map((result) => result.length);
|
|
1558
|
+
* ```
|
|
1559
|
+
*/
|
|
1560
|
+
function fromPromise<R>(promise: PromiseLike<R>): Task<R>;
|
|
1561
|
+
/**
|
|
1562
|
+
* Lift from sync function to task resolving to said function result
|
|
1563
|
+
*
|
|
1564
|
+
* Generally, functions are not considered tasks as they cannot be interrupted in any way
|
|
1565
|
+
* However there may be cases when impure function performs actions leading to cancelling or failing the task they are called from
|
|
1566
|
+
* Without such wrapper such cases would lead to ignoring said cancelation and continuilg task chain
|
|
1567
|
+
* This function is mostly used internally and is not expected to be widely used except of some edge scenarios
|
|
1568
|
+
*
|
|
1569
|
+
* If converted task is canceled or failed externally return value will be ignored without side-effects
|
|
1570
|
+
* All tasks are no-trowing by default, any occured errors are returned as Left<any>
|
|
1571
|
+
*
|
|
1572
|
+
* @template R returned task resolve type
|
|
1573
|
+
* @param producer function to be wrapped to a task
|
|
1574
|
+
* @param cancelRef reference object allowing to set cancelation function prior to invoking function
|
|
1575
|
+
* @returns task resolving to the specified function's result
|
|
1576
|
+
*
|
|
1577
|
+
* @example
|
|
1578
|
+
* ```typescript
|
|
1579
|
+
* const cancelRef = { cancel: () => {} };
|
|
1580
|
+
*
|
|
1581
|
+
* const task = Task.fromFunction(() => someFunction('someData'), cancelRef).map((result) => result.length);
|
|
1582
|
+
* ```
|
|
1583
|
+
*/
|
|
1584
|
+
function fromFunction<R>(producer: () => R, cancelRef: {
|
|
1585
|
+
cancel: TaskCancel;
|
|
1586
|
+
}): Task<R>;
|
|
1587
|
+
/**
|
|
1588
|
+
* Generic callback task
|
|
1589
|
+
*
|
|
1590
|
+
* Usefull for creating custom tasks from success and error callbacks and cancelaton function. Synchronous callbacks are not supported, @see fromFuction
|
|
1591
|
+
*
|
|
1592
|
+
* @param create task body with success and error callbacks
|
|
1593
|
+
* @param cancel cancelation function intended for stopping task execution
|
|
1594
|
+
* @returns task resolving to success value
|
|
1595
|
+
*
|
|
1596
|
+
* @example
|
|
1597
|
+
* ```typescript
|
|
1598
|
+
* export function timeout(delay: number) {
|
|
1599
|
+
* return fromCallback<NodeJS.Timeout, void>((resolve) => setTimeout(() => resolve(), delay), clearTimeout);
|
|
1600
|
+
* }
|
|
1601
|
+
* ```
|
|
1602
|
+
*/
|
|
1603
|
+
function fromCallback<H, R>(create: (resolve: (result: R) => void, reject: (error: any) => void, cancel: () => void) => H, onCancel: (handler: H) => void): Task<R>;
|
|
1604
|
+
/**
|
|
1605
|
+
* Lift an async operation that accepts an AbortSignal into a Task.
|
|
1606
|
+
*
|
|
1607
|
+
* Creates an internal AbortController and passes its signal to `producer`.
|
|
1608
|
+
* If the resulting task is canceled or rejected externally, `controller.abort()`
|
|
1609
|
+
* is called automatically.
|
|
1610
|
+
*
|
|
1611
|
+
* @template R returned task resolve type
|
|
1612
|
+
* @param producer factory function receiving an AbortSignal
|
|
1613
|
+
* @returns task wrapping the operation
|
|
1614
|
+
*
|
|
1615
|
+
* @example
|
|
1616
|
+
* ```typescript
|
|
1617
|
+
* const fetchUser = (id: string) =>
|
|
1618
|
+
* Task.fromAbortSignal((signal) =>
|
|
1619
|
+
* fetch(`/api/users/${id}`, { signal }).then((res) => res.json())
|
|
1620
|
+
* );
|
|
1621
|
+
* ```
|
|
1622
|
+
*/
|
|
1623
|
+
function fromAbortSignal<R>(producer: (signal: AbortSignal) => PromiseLike<R>): Task<R>;
|
|
1624
|
+
/**
|
|
1625
|
+
* Convinience shortcut for yielding async functions as tasks
|
|
1626
|
+
*
|
|
1627
|
+
* @template R returned generator resolve type
|
|
1628
|
+
* @param promise promise to be resolved
|
|
1629
|
+
* @returns generator to be be used with yield*
|
|
1630
|
+
*/
|
|
1631
|
+
function promiseGenerator<R>(promise: PromiseLike<R>): TaskGenerator<unknown, Task<R>, R>;
|
|
1632
|
+
/**
|
|
1633
|
+
* Convinience shortcut for yielding timers
|
|
1634
|
+
*
|
|
1635
|
+
* @template R returned generator resolve type
|
|
1636
|
+
* @param promise promise to be resolved
|
|
1637
|
+
* @returns generator to be be used with yield*
|
|
1638
|
+
*/
|
|
1639
|
+
function timeoutGenerator(delay: number): TaskGenerator<unknown, Task<void>, void>;
|
|
1640
|
+
/**
|
|
1641
|
+
* Lift from function returning value/promise to function returning task resolving to that value
|
|
1642
|
+
*
|
|
1643
|
+
* Userfull for converting exising async functions to task functions for further use
|
|
1644
|
+
*
|
|
1645
|
+
* @template A returned function's argument types
|
|
1646
|
+
* @template R returned function's result task's resolve type
|
|
1647
|
+
* @param promiseFunction function returning promise or plain value to be resolved
|
|
1648
|
+
* @returns task function wrapping specified promise function
|
|
1649
|
+
* @see Task.fromPromise
|
|
1650
|
+
*
|
|
1651
|
+
* @example
|
|
1652
|
+
* ```typescript
|
|
1653
|
+
* const taskFunction = liftPromiseFunction(someAsyncOperation);
|
|
1654
|
+
*
|
|
1655
|
+
* const task = taskFunction('someData').map((result) => result.length);
|
|
1656
|
+
* ```
|
|
1657
|
+
*/
|
|
1658
|
+
function lift<A extends unknown[], R>(promiseFunction: (...args: A) => PromiseLike<R>): TaskFunction<A, R>;
|
|
1659
|
+
/**
|
|
1660
|
+
* Create compound task from generator function
|
|
1661
|
+
*
|
|
1662
|
+
* Applying yield to task within the generator function awaits the task and returns underlying value in case of success
|
|
1663
|
+
* However the convinient option for typescript is to use ```yield* task.generator()``` as othervise one may have to deal with union types
|
|
1664
|
+
*
|
|
1665
|
+
* In case of failure an error is thrown and may be caught by try-catch
|
|
1666
|
+
*
|
|
1667
|
+
* If error is not caught the task is interrupted and returns 'just left error' immediately. In case of cancelation the task is interrupted and returns 'nothing' immediately
|
|
1668
|
+
*
|
|
1669
|
+
* @template TT yielded task type
|
|
1670
|
+
* @template R returned task resolve type
|
|
1671
|
+
* @param taskGeneratorFunction task generator function
|
|
1672
|
+
* @returns task resolving to generator's return type
|
|
1673
|
+
*
|
|
1674
|
+
* @note compound task execution is interrupted only at yield statements, so despite returning immediately any promise-based chains would continue running until the first yield
|
|
1675
|
+
* @note due to type unpredictability you HAVE to use ```.generator()``` together with yield* to avoid type issues, despite the fact that task may be yielded directly
|
|
1676
|
+
*
|
|
1677
|
+
* @example
|
|
1678
|
+
* ```typescript
|
|
1679
|
+
* const task = Task.generate(function*() {
|
|
1680
|
+
* try {
|
|
1681
|
+
* const value1 = yield* delayedValueTask('data', 400).generator();
|
|
1682
|
+
*
|
|
1683
|
+
* const value2 = yield* delayedValueTask(value1.length, 300).generator();
|
|
1684
|
+
*
|
|
1685
|
+
* return value2;
|
|
1686
|
+
* } catch (e) {
|
|
1687
|
+
* return -1;
|
|
1688
|
+
* }
|
|
1689
|
+
* });
|
|
1690
|
+
* ```
|
|
1691
|
+
*/
|
|
1692
|
+
function generate<T, TT extends Task<T>, R>(taskGeneratorFunction: TaskGeneratorFunction<[], T, TT, R>): Task<R>;
|
|
1693
|
+
/**
|
|
1694
|
+
* Task function generator
|
|
1695
|
+
*
|
|
1696
|
+
* Simplifies wrighting functions returning generated tasks
|
|
1697
|
+
*
|
|
1698
|
+
* @template Args generator arguments
|
|
1699
|
+
* @template TT yielded task type
|
|
1700
|
+
* @template R returned task resolve type
|
|
1701
|
+
* @param taskGeneratorFunction function* (...args) task generator function
|
|
1702
|
+
* @returns task resolving to generator's return type
|
|
1703
|
+
*/
|
|
1704
|
+
function generateFunction<A extends any[], T, TT extends Task<T>, R>(taskGeneratorFunction: TaskGeneratorFunction<A, T, TT, R>): TaskFunction<A, R>;
|
|
1705
|
+
/**
|
|
1706
|
+
* Generic timeout task
|
|
1707
|
+
*
|
|
1708
|
+
* Usefull for creating delays in task chains or implementing limiting tasks
|
|
1709
|
+
*
|
|
1710
|
+
* @param delay duration in ms after that the task resolves to void
|
|
1711
|
+
* @returns task resolving to void (undefined) after specified delay
|
|
1712
|
+
*
|
|
1713
|
+
* @example
|
|
1714
|
+
* ```typescript
|
|
1715
|
+
* const delayedValueTask = <T>(value: T, delay: number) => Task.timeout(delay).map(() => value);
|
|
1716
|
+
* // ... //
|
|
1717
|
+
* const value = yield* delayedValueTask(42, 1000).generator();
|
|
1718
|
+
*
|
|
1719
|
+
* console.log("It's past 1 second and here's a value:", value)
|
|
1720
|
+
* ```
|
|
1721
|
+
*/
|
|
1722
|
+
function timeout(delay: number): Task<void>;
|
|
1723
|
+
/**
|
|
1724
|
+
* Start multiple tasks one after another
|
|
1725
|
+
*
|
|
1726
|
+
* @param taskFunctions list of task functions (without arguments)
|
|
1727
|
+
* @returns composite task invoring every task in order and resolving to the list of results
|
|
1728
|
+
*/
|
|
1729
|
+
function sequence<T>(taskFunctions: Iterable<TaskFunction<[], T>>): Task<T[]>;
|
|
1730
|
+
/**
|
|
1731
|
+
* Execute all tasks in parallel and return list of results
|
|
1732
|
+
*
|
|
1733
|
+
* Resulting task resolves when all tasks are resolved, returning list of tasks results maintaining result order
|
|
1734
|
+
* In case of failure (external or internal) resulting task is immediately immediately rejected with that error and all pending tasks are canceled
|
|
1735
|
+
* In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
|
|
1736
|
+
*
|
|
1737
|
+
* @note direct equivalent of Promise.all
|
|
1738
|
+
*
|
|
1739
|
+
* @template T underlying task type
|
|
1740
|
+
* @param tasks iterable of tasks to execute
|
|
1741
|
+
* @retuirns task resolving to the list of results
|
|
1742
|
+
*/
|
|
1743
|
+
function all<T>(tasks: Iterable<Task<T>>): Task<T[]>;
|
|
1744
|
+
/**
|
|
1745
|
+
* Execute all tasks in parallel and return the result of the first successful one
|
|
1746
|
+
*
|
|
1747
|
+
* Resulting task resolves when the first tasks is successfuly resolved, returning it's result
|
|
1748
|
+
* In case of external task rejecttion all tasks are rejected with that error and resulting task is rejected with an array of errors
|
|
1749
|
+
* In case of internal task rejection the execution is continued untill fthe first task resolves succesfully or all the tasks are rejected this way resulting task is rejected with an array of errors
|
|
1750
|
+
* In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
|
|
1751
|
+
*
|
|
1752
|
+
* @note direct equivalent of Promise.any
|
|
1753
|
+
*
|
|
1754
|
+
* @template T underlying task type
|
|
1755
|
+
* @param tasks iterable of tasks to execute
|
|
1756
|
+
* @retuirns task resolving to the result of first successfull task
|
|
1757
|
+
*/
|
|
1758
|
+
function any<T>(tasks: Iterable<Task<T>>): Task<unknown>;
|
|
1759
|
+
/**
|
|
1760
|
+
* Limit task execution based on another task
|
|
1761
|
+
*
|
|
1762
|
+
* If the second task resolves sooner than the first, resulting task is canceled
|
|
1763
|
+
* Otherwise the resulting task resolves together with the first one
|
|
1764
|
+
*
|
|
1765
|
+
* @param task task to limit
|
|
1766
|
+
* @param limitTask limiting task
|
|
1767
|
+
* @returns task limites according to limitTask
|
|
1768
|
+
*/
|
|
1769
|
+
function limit<T>(task: Task<T>, limitTask: Task<void>): Task<unknown>;
|
|
1770
|
+
/**
|
|
1771
|
+
* Repeat task untill successful
|
|
1772
|
+
*
|
|
1773
|
+
* Given task is re-generated in case of failure (both external and internal)
|
|
1774
|
+
* Resulting task is still cancelable by standard means
|
|
1775
|
+
*
|
|
1776
|
+
* @param taskFunction task preoducing function
|
|
1777
|
+
*/
|
|
1778
|
+
function repeat<T>(taskFunction: TaskFunction<[], T>): Task<T>;
|
|
1779
|
+
}
|
|
1780
|
+
type TaskInvoke<R> = Promise<Cancelable<R>>;
|
|
1781
|
+
type TaskCancel = (error: Maybe<any>) => boolean;
|
|
1782
|
+
/**
|
|
1783
|
+
* @hidden
|
|
1784
|
+
*/
|
|
1785
|
+
interface TaskBase<R> {
|
|
1786
|
+
_invoke(op: (result: Cancelable<R>) => void): void;
|
|
1787
|
+
_cancel(error: Maybe<any>): boolean;
|
|
1788
|
+
}
|
|
1789
|
+
//#endregion
|
|
1790
|
+
//# sourceMappingURL=t-tasks.d.ts.map
|