Last active
April 27, 2018 14:27
-
-
Save DanielZhangQingLong/42beafb20bc8618f6f9451f4f04eff48 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
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