Skip to content

Instantly share code, notes, and snippets.

@darolo
Last active November 12, 2019 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darolo/75d0a872d70e8e16f0b705b865002096 to your computer and use it in GitHub Desktop.
Save darolo/75d0a872d70e8e16f0b705b865002096 to your computer and use it in GitHub Desktop.
class Observable {
constructor(){
this.observers = []
}
add(callback){
if( typeof callback === 'function' )
this.observers.push({
callback: callback
})
return this
}
remove(callback){
let index = this.observers.findIndex( observer => observer.callback == callback )
if(~index) return this.observers.splice(index, 1)
return []
}
notify(data){
this.observers.forEach( observer => observer.callback(data) )
return this
}
clear(){
this.observers = [];
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment