Merge pull request #300 from jjangga0214/refactor/src/Utils/Emitter.js
refactor src/Utils/Emitter.js
This commit is contained in:
@@ -10,7 +10,6 @@ export default class Emitter {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
emit(event, ...args) {
|
emit(event, ...args) {
|
||||||
this._callbacks = this._callbacks || {}
|
this._callbacks = this._callbacks || {}
|
||||||
const callbacks = this._callbacks[event]
|
const callbacks = this._callbacks[event]
|
||||||
@@ -22,33 +21,22 @@ export default class Emitter {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove event listener for given event. If fn is not provided, all event
|
// Remove event listener for given event.
|
||||||
// listeners for that event will be removed. If neither is provided, all
|
// If fn is not provided, all event listeners for that event will be removed.
|
||||||
// event listeners will be removed.
|
// If neither is provided, all event listeners will be removed.
|
||||||
off(event, fn) {
|
off(event, fn) {
|
||||||
if (!this._callbacks || (arguments.length === 0)) {
|
|
||||||
|
if (!arguments.length) {
|
||||||
this._callbacks = {}
|
this._callbacks = {}
|
||||||
return this
|
} else {
|
||||||
}
|
// event listeners for the given event
|
||||||
|
const callbacks = this._callbacks ? this._callbacks[event] : null
|
||||||
// specific event
|
if (callbacks) {
|
||||||
const callbacks = this._callbacks[event]
|
if (fn) {
|
||||||
if (!callbacks) {
|
this._callbacks[event] = callbacks.filter(cb => cb !== fn) // remove specific handler
|
||||||
return this
|
} else {
|
||||||
}
|
delete this._callbacks[event] // remove all handlers
|
||||||
|
}
|
||||||
// remove all handlers
|
|
||||||
if (arguments.length === 1) {
|
|
||||||
delete this._callbacks[event]
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove specific handler
|
|
||||||
for (let i = 0; i < callbacks.length; i += 1) {
|
|
||||||
const callback = callbacks[i]
|
|
||||||
if (callback === fn) {
|
|
||||||
callbacks.splice(i, 1)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user