Last active
November 12, 2019 17:14
-
-
Save darolo/75d0a872d70e8e16f0b705b865002096 to your computer and use it in GitHub Desktop.
This file contains 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 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