Skip to content

Instantly share code, notes, and snippets.

@miketromba
Created January 26, 2021 21:27
Show Gist options
  • Save miketromba/611721078a6756b8f478802c3788232e to your computer and use it in GitHub Desktop.
Save miketromba/611721078a6756b8f478802c3788232e to your computer and use it in GitHub Desktop.
Pipe data from one fn call to another with delivery guarentee
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