Skip to content

Instantly share code, notes, and snippets.

@DanielZhangQingLong
Last active April 27, 2018 14:27
Show Gist options
  • Save DanielZhangQingLong/42beafb20bc8618f6f9451f4f04eff48 to your computer and use it in GitHub Desktop.
Save DanielZhangQingLong/42beafb20bc8618f6f9451f4f04eff48 to your computer and use it in GitHub Desktop.
export default class EventEmitter {
constructor() {
this._events = {};
}
// Add typed listeners e.g. change: [listeners...]
on(type, listener) {
this._events[type] = this._events[type] || [];
// The structure _events is {type: [], type2: []}
// Only push it into the array not invoke.
this._events[type].push(listener);
console.log('_events for change', this._events['change']);
}
// Invoke all the listener of some-type
emit(type) {
if(this._events[type]){
this._events[type].map((listener) => {
listener();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment