-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate EmitterWrapped to ES2015
- Loading branch information
Showing
2 changed files
with
36 additions
and
28 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,38 @@ | ||
function EmitterWrapper (emitter) { | ||
this.listeners = {} | ||
this.emitter = emitter | ||
} | ||
|
||
EmitterWrapper.prototype.addListener = EmitterWrapper.prototype.on = function (event, listener) { | ||
this.emitter.addListener(event, listener) | ||
'use strict' | ||
|
||
if (!this.listeners.hasOwnProperty(event)) { | ||
this.listeners[event] = [] | ||
class EmitterWrapper { | ||
constructor (emitter) { | ||
this.listeners = {} | ||
this.emitter = emitter | ||
} | ||
|
||
this.listeners[event].push(listener) | ||
addListener (event, listener) { | ||
this.emitter.addListener(event, listener) | ||
|
||
return this | ||
} | ||
if (!this.listeners.hasOwnProperty(event)) { | ||
this.listeners[event] = [] | ||
} | ||
|
||
EmitterWrapper.prototype.removeAllListeners = function (event) { | ||
var events = event ? [event] : Object.keys(this.listeners) | ||
var self = this | ||
events.forEach(function (event) { | ||
self.listeners[event].forEach(function (listener) { | ||
self.emitter.removeListener(event, listener) | ||
this.listeners[event].push(listener) | ||
|
||
return this | ||
} | ||
|
||
on (event, listener) { | ||
return this.addListener(event, listener) | ||
} | ||
|
||
removeAllListeners (event) { | ||
const events = event ? [event] : Object.keys(this.listeners) | ||
events.forEach((event) => { | ||
this.listeners[event].forEach((listener) => { | ||
this.emitter.removeListener(event, listener) | ||
}) | ||
delete this.listeners[event] | ||
}) | ||
delete self.listeners[event] | ||
}) | ||
|
||
return this | ||
return this | ||
} | ||
} | ||
|
||
module.exports = EmitterWrapper |
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