event.js 406 B

12345678910111213141516171819202122232425
  1. class Event {
  2. constructor () {
  3. this.listeners = []
  4. }
  5. addListener (callback) {
  6. this.listeners.push(callback)
  7. }
  8. removeListener (callback) {
  9. const index = this.listeners.indexOf(callback)
  10. if (index !== -1) {
  11. this.listeners.splice(index, 1)
  12. }
  13. }
  14. emit (...args) {
  15. for (const listener of this.listeners) {
  16. listener(...args)
  17. }
  18. }
  19. }
  20. module.exports = Event