Created
February 15, 2021 18:03
-
-
Save safareli/8f30a837c40f137434fff33c173e70aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Canceler { | |
(): void; | |
} | |
/** No operation canceler which "does nothing". | |
* | |
* ```js | |
* concatCancelers(a, emptyCanceler) ==== concatCancelers(emptyCanceler, a) ==== a | |
* ``` | |
*/ | |
export const emptyCanceler = () => {}; | |
/** | |
* Concat two cancelers into one which will executed original cancelers from | |
* left to right one by one sequentially. | |
* | |
* `concatCancelers` is associative for any input `a,b,c: Canceler`: | |
* ```js | |
* concatCancelers(a, concatCancelers(b, c)) ==== concatCancelers(concatCancelers(a, b), c) | |
*/ | |
export const concatCancelers = (a: Canceler, b: Canceler): Canceler => () => { | |
a(); | |
b(); | |
}; | |
/** | |
* Combines arbitrary number of cancelers into one which will executing them | |
* left to right one by one sequentially. | |
*/ | |
export const foldCancelers = (cancelers: Canceler[]): Canceler => | |
cancelers.reduce(concatCancelers, emptyCanceler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment