t-tasks 1.2.0 → 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/task.d.ts DELETED
@@ -1,489 +0,0 @@
1
- import { Maybe } from './maybe';
2
- import { Either } from './either';
3
- /**
4
- * Shortcut for monadic Either type, where erroneous value is of type any
5
- *
6
- * @template R underlying type
7
- */
8
- export type Rejectable<R> = Either<R, any>;
9
- /**
10
- * Shortcut for underlying task result type
11
- *
12
- * @template R underlying type
13
- */
14
- export type Cancelable<R> = Maybe<Rejectable<R>>;
15
- /**
16
- * Function returning Task
17
- *
18
- * @template A argument types
19
- * @template R returned task resolve type
20
- */
21
- export type TaskFunction<A extends unknown[], R> = (...args: A) => Task<R>;
22
- /**
23
- * Task generator
24
- *
25
- * @template TT yielded task type
26
- * @template R returned task resolve type
27
- *
28
- * @example
29
- * ```typescript
30
- * const generatorFunction = function*(): TaskGenerator<unknown, Task<string>, number> {
31
- * const v = yield* someTaskFunction().generator();
32
- *
33
- * return v.length;
34
- * };
35
- * ```
36
- */
37
- export type TaskGenerator<T, TT extends Task<T>, R> = Generator<TT, R, T>;
38
- /**
39
- * Function returning task generator (generator function)
40
- *
41
- * @template A argument types
42
- * @template TT yielded task type
43
- * @template R returned task resolve type
44
- *
45
- * @example
46
- * ```typescript
47
- * const generatorFunction: TaskGeneratorFunction<[], unknown, Task<string>, number> = function*() {
48
- * const v = yield* someTaskFunction().generator();
49
- *
50
- * return v.length;
51
- * };
52
- * ```
53
- */
54
- export type TaskGeneratorFunction<A extends unknown[], T, TT extends Task<T>, R> = (...args: A) => TaskGenerator<T, TT, R>;
55
- /**
56
- * Task monad interface
57
- *
58
- * @template R task resolve type
59
- */
60
- export interface Task<R> extends TaskBase<R> {
61
- /**
62
- * Invoke callback when task is resolved (and only then)
63
- *
64
- * @param op callback to invoke
65
- * @returns self
66
- */
67
- tap(op: (value: R) => void): Task<R>;
68
- /**
69
- * Invoke callback when task is rejected (and only then)
70
- *
71
- * @param op callback to invoke
72
- * @returns self
73
- */
74
- tapRejected(op: (error: any) => void): Task<R>;
75
- /**
76
- * Invoke callback when task is canceled (and only then)
77
- *
78
- * @param op callback to invoke
79
- * @returns self
80
- */
81
- tapCanceled(op: () => void): Task<R>;
82
- /**
83
- * Invoke a dedicated callback according to task resolution
84
- *
85
- * @param op.resolved callback to invoke on success
86
- * @param op.rejected callback to invoke on failure
87
- * @param op.canceled callback to invoke on cancelation
88
- * @returns self
89
- */
90
- matchTap(op: {
91
- resolved: (value: R) => void;
92
- rejected: (error: any) => void;
93
- canceled: () => void;
94
- }): Task<R>;
95
- /**
96
- * Invoke transformer when task is resolved (and only then) and return it's result instead
97
- *
98
- * @template R2 transformer return type
99
- * @param op transformer to invoke
100
- * @returns task returning transformer result
101
- */
102
- map<R2>(op: (value: R) => R2): Task<R2>;
103
- /**
104
- * Invoke transformer when task is rejected (and only then) and return it's result instead
105
- *
106
- * @template R2 fallback return type
107
- * @param op transformer to invoke
108
- * @returns task returning fallback result in case of failure
109
- */
110
- mapRejected<R2>(op: (error: any) => R2): Task<R | R2>;
111
- /**
112
- * Invoke transformer when task is canceled (and only then) and return it's result instead
113
- *
114
- * @template R2 fallback return type
115
- * @param op transformer to invoke
116
- * @returns task returning fallback result in case of cancelation
117
- */
118
- mapCanceled<R2>(op: () => R2): Task<R | R2>;
119
- /**
120
- * Invoke a dedicated transformer according to task resolution
121
- *
122
- * @template R2 success transformer return type
123
- * @template R3 failure transformer return type
124
- * @template R4 cancelation transformer return type
125
- * @param op.resolved transformer to invoke on success
126
- * @param op.rejected transformer to invoke on failure
127
- * @param op.canceled transformer to invoke on cancelation
128
- * @returns task resolving to a corresponding transformer result
129
- */
130
- matchMap<R2, R3 = R2, R4 = R3>(op: {
131
- resolved: (value: R) => R2;
132
- rejected: (error: any) => R3;
133
- canceled: () => R4;
134
- }): Task<R2 | R3 | R4>;
135
- /**
136
- * Invoke transformer when task is resolved (and only then) and continue execution with it's result
137
- *
138
- * @template R2 transformer task resolve type
139
- * @param op transformer to invoke
140
- * @returns task chaining to transformer result task
141
- */
142
- chain<R2>(op: (value: R) => Task<R2>): Task<R2>;
143
- /**
144
- * Invoke transformer when task is rejected (and only then) and continue execution with it's result
145
- *
146
- * @template R2 fallback task resolve type
147
- * @param op transformer to invoke
148
- * @returns task chaining to fallback task in case of failure
149
- */
150
- chainRejected<R2>(op: (error: any) => Task<R2>): Task<R | R2>;
151
- /**
152
- * Invoke transformer when task is canceled (and only then) and continue execution with it's result
153
- *
154
- * @template R2 fallback task resolve type
155
- * @param op transformer to invoke
156
- * @returns task chaining to fallback task in case of cancelation
157
- */
158
- chainCanceled<R2>(op: () => Task<R2>): Task<R | R2>;
159
- /**
160
- * Invoke a dedicated transformer according to task resolution and continue execution with it's result
161
- *
162
- * @template R2 success transformer resolve type
163
- * @template R3 failure transformer resolve type
164
- * @template R4 cancelation transformer resolve type
165
- * @param op.resolved transformer to invoke on success
166
- * @param op.rejected transformer to invoke on failure
167
- * @param op.canceled transformer to invoke on cancelation
168
- * @returns task chaining to a corresponding transformer result
169
- */
170
- matchChain<R2, R3 = R2, R4 = R3>(op: {
171
- resolved: (value: R) => Task<R2>;
172
- rejected: (error: any) => Task<R3>;
173
- canceled: () => Task<R4>;
174
- }): Task<R2 | R3 | R4>;
175
- /**
176
- * Return underlying promise in order to await result
177
- *
178
- * @returns underlying promise
179
- */
180
- resolve: () => TaskInvoke<R>;
181
- /**
182
- * Invoke underlying canel method without error
183
- */
184
- cancel: () => void;
185
- /**
186
- * Invoke underlying canel method with error
187
- *
188
- * @param error error value to be injected from outside
189
- */
190
- reject: (error: any) => void;
191
- /**
192
- * Wrap task to singleton generator
193
- *
194
- * Userful in order to avoid ambiguous yied types
195
- *
196
- * @returns generator of task wrapping the task
197
- * @example
198
- * ```typescript
199
- * const getString = () => Task.resolved('hello');
200
- * const getLength = (data: string) => Task.resolved(data.length);
201
- *
202
- * // ..... //
203
- *
204
- * Task.generate(function* () {
205
- * const data = yield getString(); // data: string | number
206
- * const length = yield getLength(data); // length: string | number
207
- *
208
- * return length;
209
- * });
210
- *
211
- * // ..... //
212
- *
213
- * Task.generate(function* () {
214
- * const data = yield* getString().generator(); // data: string
215
- * const length = yield* getLength(data).generator(); // length: number
216
- *
217
- * return length;
218
- * });
219
- * ```
220
- */
221
- generator: TaskGeneratorFunction<[], unknown, Task<R>, R>;
222
- }
223
- export declare namespace Task {
224
- /**
225
- * Custom task monad constructor
226
- *
227
- * @template R returned task's resolve type
228
- * @param invoke promise defining task execution
229
- * @param cancel cancelation function
230
- * @returns task resolving to resolve value of invoke
231
- *
232
- * @note low-level primitive for creating custom tasks, not intended for general use
233
- */
234
- function create<R>(invoke: TaskInvoke<R>, cancel: TaskCancel): Task<R>;
235
- /**
236
- * Invariant task constructor creating resolved task from plain value
237
- *
238
- * @template R returned task's resolve type
239
- * @param value value to be returned upon awaiting
240
- * @returns task resolving to specified value
241
- */
242
- function resolved<R>(value: R): Task<R>;
243
- /**
244
- * Invariant task constructor creating rejected task from error value
245
- *
246
- * @template R returned task's resolve type
247
- * @param error error to be returned upon awaiting
248
- * @returns task resolving to specified value
249
- */
250
- function rejected<R>(error: any): Task<R>;
251
- /**
252
- * Invariant task constructor creating canceled task
253
- *
254
- * @template R returned task's resolve type
255
- * @returns task resolving to specified value
256
- */
257
- function canceled<R>(): Task<R>;
258
- /**
259
- * Lift from promise to task resolving to that promise result
260
- *
261
- * Userfull for converting promises to tasks
262
- * If converted task is canceled or failed externally return value will be ignored without side-effects
263
- * All tasks are no-trowing by default, any occured errors are returned as Left<any>
264
- *
265
- * @template R returned task resolve type
266
- * @param promise promise to be resolved
267
- * @returns task resolving to specified promise value
268
- *
269
- * @example
270
- * ```typescript
271
- * const task = Task.fromPromise(someAsyncOperation('someData')).map((result) => result.length);
272
- * ```
273
- */
274
- function fromPromise<R>(promise: PromiseLike<R>): Task<R>;
275
- /**
276
- * Lift from sync function to task resolving to said function result
277
- *
278
- * Generally, functions are not considered tasks as they cannot be interrupted in any way
279
- * However there may be cases when impure function performs actions leading to cancelling or failing the task they are called from
280
- * Without such wrapper such cases would lead to ignoring said cancelation and continuilg task chain
281
- * This function is mostly used internally and is not expected to be widely used except of some edge scenarios
282
- *
283
- * If converted task is canceled or failed externally return value will be ignored without side-effects
284
- * All tasks are no-trowing by default, any occured errors are returned as Left<any>
285
- *
286
- * @template R returned task resolve type
287
- * @param producer function to be wrapped to a task
288
- * @param cancelRef reference object allowing to set cancelation function prior to invoking function
289
- * @returns task resolving to the specified function's result
290
- *
291
- * @example
292
- * ```typescript
293
- * const cancelRef = { cancel: () => {} };
294
- *
295
- * const task = Task.fromFunction(() => someFunction('someData'), cancelRef).map((result) => result.length);
296
- * ```
297
- */
298
- function fromFunction<R>(producer: () => R, cancelRef: {
299
- cancel: TaskCancel;
300
- }): Task<R>;
301
- /**
302
- * Convinience shortcut for yielding async functions as tasks
303
- *
304
- * @template R returned generator resolve type
305
- * @param promise promise to be resolved
306
- * @returns generator to be be used with yield*
307
- */
308
- function promiseGenerator<R>(promise: PromiseLike<R>): TaskGenerator<unknown, Task<R>, R>;
309
- /**
310
- * Convinience shortcut for yielding timers
311
- *
312
- * @template R returned generator resolve type
313
- * @param promise promise to be resolved
314
- * @returns generator to be be used with yield*
315
- */
316
- function timeoutGenerator(delay: number): TaskGenerator<unknown, Task<void>, void>;
317
- /**
318
- * Lift from function returning value/promise to function returning task resolving to that value
319
- *
320
- * Userfull for converting exising async functions to task functions for further use
321
- *
322
- * @template A returned function's argument types
323
- * @template R returned function's result task's resolve type
324
- * @param promiseFunction function returning promise or plain value to be resolved
325
- * @returns task function wrapping specified promise function
326
- * @see Task.fromPromise
327
- *
328
- * @example
329
- * ```typescript
330
- * const taskFunction = liftPromiseFunction(someAsyncOperation);
331
- *
332
- * const task = taskFunction('someData').map((result) => result.length);
333
- * ```
334
- */
335
- function lift<A extends unknown[], R>(promiseFunction: (...args: A) => PromiseLike<R>): TaskFunction<A, R>;
336
- /**
337
- * Create compound task from generator function
338
- *
339
- * Applying yield to task within the generator function awaits the task and returns underlying value in case of success
340
- * However the convinient option for typescript is to use ```yield* task.generator()``` as othervise one may have to deal with union types
341
- *
342
- * In case of failure an error is thrown and may be caught by try-catch
343
- *
344
- * 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
345
- *
346
- * @template TT yielded task type
347
- * @template R returned task resolve type
348
- * @param taskGeneratorFunction task generator function
349
- * @returns task resolving to generator's return type
350
- *
351
- * @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
352
- * @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
353
- *
354
- * @example
355
- * ```typescript
356
- * const task = Task.generate(function*() {
357
- * try {
358
- * const value1 = yield* delayedValueTask('data', 400).generator();
359
- *
360
- * const value2 = yield* delayedValueTask(value1.length, 300).generator();
361
- *
362
- * return value2;
363
- * } catch (e) {
364
- * return -1;
365
- * }
366
- * });
367
- * ```
368
- */
369
- function generate<T, TT extends Task<T>, R>(taskGeneratorFunction: TaskGeneratorFunction<[], T, TT, R>): Task<R>;
370
- /**
371
- * Task function generator
372
- *
373
- * Simplifies wrighting functions returning generated tasks
374
- *
375
- * @template Args generator arguments
376
- * @template TT yielded task type
377
- * @template R returned task resolve type
378
- * @param taskGeneratorFunction function* (...args) task generator function
379
- * @returns task resolving to generator's return type
380
- */
381
- function generateFunction<A extends any[], T, TT extends Task<T>, R>(taskGeneratorFunction: TaskGeneratorFunction<A, T, TT, R>): TaskFunction<A, R>;
382
- /**
383
- * Generic timeout task
384
- *
385
- * Usefull for creating delays in task chains or implementing limiting tasks
386
- *
387
- * @param delay duration in ms after that the task resolves to void
388
- * @returns task resolving to void (undefined) after specified delay
389
- *
390
- * @example
391
- * ```typescript
392
- * const delayedValueTask = <T>(value: T, delay: number) => Task.timeout(delay).map(() => value);
393
- * // ... //
394
- * const value = yield* delayedValueTask(42, 1000).generator();
395
- *
396
- * console.log("It's past 1 second and here's a value:", value)
397
- * ```
398
- */
399
- function timeout(delay: number): Task<void>;
400
- /**
401
- * Generic callback task
402
- *
403
- * Usefull for creating custom tasks from success and error callbacks and cancelaton function. Synchronous callbacks are not supported, @see fromFuction
404
- *
405
- * @param create task body with success and error callbacks
406
- * @param cancel cancelation function intended for stopping task execution
407
- * @returns task resolving to success value
408
- *
409
- * @example
410
- * ```typescript
411
- * export function timeout(delay: number) {
412
- * return fromCallback<NodeJS.Timeout, void>((resolve) => setTimeout(() => resolve(), delay), clearTimeout);
413
- * }
414
- * ```
415
- */
416
- function fromCallback<H, R>(create: (resolve: (result: R) => void, reject: (error: any) => void, cancel: () => void) => H, onCancel: (handler: H) => void): Task<R>;
417
- /**
418
- * Start multiple tasks one after another
419
- *
420
- * @param taskFunctions list of task functions (without arguments)
421
- * @returns composite task invoring every task in order and resolving to the list of results
422
- */
423
- function sequence<T>(taskFunctions: Iterable<TaskFunction<[], T>>): Task<T[]>;
424
- /**
425
- * Execute all tasks in parallel and return list of results
426
- *
427
- * Resulting task resolves when all tasks are resolved, returning list of tasks results maintaining result order
428
- * In case of failure (external or internal) resulting task is immediately immediately rejected with that error and all pending tasks are canceled
429
- * In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
430
- *
431
- * @note direct equivalent of Promise.all
432
- *
433
- * @template T underlying task type
434
- * @param tasks iterable of tasks to execute
435
- * @retuirns task resolving to the list of results
436
- */
437
- function all<T>(tasks: Iterable<Task<T>>): Task<T[]>;
438
- /**
439
- * Execute all tasks in parallel and return the result of the first successful one
440
- *
441
- * Resulting task resolves when the first tasks is successfuly resolved, returning it's result
442
- * In case of external task rejecttion all tasks are rejected with that error and resulting task is rejected with an array of errors
443
- * 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
444
- * In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
445
- *
446
- * @note direct equivalent of Promise.any
447
- *
448
- * @template T underlying task type
449
- * @param tasks iterable of tasks to execute
450
- * @retuirns task resolving to the result of first successfull task
451
- */
452
- function any<T>(tasks: Iterable<Task<T>>): Task<T>;
453
- /**
454
- * Limit task execution based on another task
455
- *
456
- * If the second task resolves sooner than the first, resulting task is canceled
457
- * Otherwise the resulting task resolves together with the first one
458
- *
459
- * @param task task to limit
460
- * @param limitTask limiting task
461
- * @returns task limites according to limitTask
462
- */
463
- function limit<T>(task: Task<T>, limitTask: Task<void>): Task<T>;
464
- /**
465
- * Repeat task untill successful
466
- *
467
- * Given task is re-generated in case of failure (both external and internal)
468
- * Resulting task is still cancelable by standard means
469
- *
470
- * @param taskFunction task preoducing function
471
- */
472
- function repeat<T>(taskFunction: TaskFunction<[], T>): Task<T>;
473
- }
474
- type TaskInvoke<R> = Promise<Cancelable<R>>;
475
- type TaskCancel = (error: Maybe<any>) => void;
476
- /**
477
- * @hidden
478
- */
479
- interface TaskBase<R> {
480
- /**
481
- * @hidden
482
- */
483
- readonly _invoke: TaskInvoke<R>;
484
- /**
485
- * @hidden
486
- */
487
- readonly _cancel: TaskCancel;
488
- }
489
- export {};