event.js 420 B

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