emitter_wrapper.js 767 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict'
  2. class EmitterWrapper {
  3. constructor (emitter) {
  4. this.listeners = {}
  5. this.emitter = emitter
  6. }
  7. addListener (event, listener) {
  8. this.emitter.addListener(event, listener)
  9. if (!this.listeners.hasOwnProperty(event)) {
  10. this.listeners[event] = []
  11. }
  12. this.listeners[event].push(listener)
  13. return this
  14. }
  15. on (event, listener) {
  16. return this.addListener(event, listener)
  17. }
  18. removeAllListeners (event) {
  19. const events = event ? [event] : Object.keys(this.listeners)
  20. events.forEach((event) => {
  21. this.listeners[event].forEach((listener) => {
  22. this.emitter.removeListener(event, listener)
  23. })
  24. delete this.listeners[event]
  25. })
  26. return this
  27. }
  28. }
  29. module.exports = EmitterWrapper