Created
January 26, 2021 21:27
-
-
Save miketromba/611721078a6756b8f478802c3788232e to your computer and use it in GitHub Desktop.
Pipe data from one fn call to another with delivery guarentee
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
class Pipe { | |
constructor(){ | |
this.onChangeHandler = null | |
this.cache = [] | |
} | |
onChange(fn){ | |
this.onChangeHandler = fn | |
this.flush() | |
} | |
flush(){ | |
if(this.cache.length){ | |
for(const item of this.cache){ | |
this.onChangeHandler(item) | |
} | |
this.cache.length = 0 | |
} | |
} | |
push(data){ | |
if(!this.onChangeHandler){ | |
this.cache.push(data) | |
} else { | |
this.onChangeHandler(data) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment