-
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(events): refactor karma EventEmitter (#3012)
- Loading branch information
1 parent
667b47e
commit 8197408
Showing
4 changed files
with
48 additions
and
61 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,75 +1,58 @@ | ||
var events = require('events') | ||
var util = require('util') | ||
'use strict' | ||
|
||
var helper = require('./helper') | ||
const EventEmitter = require('events').EventEmitter | ||
const helper = require('./helper') | ||
|
||
var bindAllEvents = function (object, context) { | ||
context = context || this | ||
function bufferEvents (emitter, eventsToBuffer) { | ||
const listeners = [] | ||
const eventsToReply = [] | ||
|
||
var bindMethod = function (method) { | ||
context.on(helper.camelToSnake(method.substr(2)), function () { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
args.push(context) | ||
object[method].apply(object, args) | ||
}) | ||
function genericListener () { | ||
eventsToReply.push(Array.from(arguments)) | ||
} | ||
|
||
for (var method in object) { | ||
if (helper.isFunction(object[method]) && method.substr(0, 2) === 'on') { | ||
bindMethod(method) | ||
} | ||
} | ||
} | ||
|
||
var bufferEvents = function (emitter, eventsToBuffer) { | ||
var listeners = [] | ||
var eventsToReply = [] | ||
var genericListener = function () { | ||
eventsToReply.push(Array.prototype.slice.call(arguments)) | ||
} | ||
|
||
eventsToBuffer.forEach(function (eventName) { | ||
var listener = genericListener.bind(null, eventName) | ||
eventsToBuffer.forEach((eventName) => { | ||
const listener = genericListener.bind(null, eventName) | ||
listeners.push(listener) | ||
emitter.on(eventName, listener) | ||
}) | ||
|
||
return function () { | ||
if (!eventsToReply) { | ||
return | ||
} | ||
|
||
// remove all buffering listeners | ||
listeners.forEach(function (listener, i) { | ||
listeners.forEach((listener, i) => { | ||
emitter.removeListener(eventsToBuffer[i], listener) | ||
}) | ||
|
||
// reply | ||
eventsToReply.forEach(function (args) { | ||
events.EventEmitter.prototype.emit.apply(emitter, args) | ||
eventsToReply.forEach((args) => { | ||
EventEmitter.prototype.emit.apply(emitter, args) | ||
}) | ||
|
||
// free-up | ||
listeners = eventsToReply = null | ||
listeners.length = 0 | ||
eventsToReply.length = 0 | ||
} | ||
} | ||
|
||
// TODO(vojta): log.debug all events | ||
var EventEmitter = function () { | ||
this.bind = bindAllEvents | ||
class KarmaEventEmitter extends EventEmitter { | ||
bind (object) { | ||
Object.keys(object).forEach((method) => { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
devoto13
Collaborator
|
||
if (method.startsWith('on') && helper.isFunction(object[method])) { | ||
this.on(helper.camelToSnake(method.substr(2)), function () { | ||
object[method].apply(object, Array.from(arguments).concat(this)) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
this.emitAsync = function (name) { | ||
emitAsync (name) { | ||
// TODO(vojta): allow passing args | ||
// TODO(vojta): ignore/throw if listener call done() multiple times | ||
var pending = this.listeners(name).length | ||
var deferred = helper.defer() | ||
var done = function () { | ||
let pending = this.listeners(name).length | ||
const deferred = helper.defer() | ||
|
||
this.emit(name, () => { | ||
if (!--pending) { | ||
deferred.resolve() | ||
} | ||
} | ||
|
||
this.emit(name, done) | ||
}) | ||
|
||
if (!pending) { | ||
deferred.resolve() | ||
|
@@ -79,9 +62,5 @@ var EventEmitter = function () { | |
} | ||
} | ||
|
||
util.inherits(EventEmitter, events.EventEmitter) | ||
|
||
// PUBLISH | ||
exports.EventEmitter = EventEmitter | ||
exports.bindAll = bindAllEvents | ||
exports.EventEmitter = KarmaEventEmitter | ||
exports.bufferEvents = bufferEvents |
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
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
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
Unfortunately this refactoring was not correct.
Object.keys() provides the enumerable own properties of an object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
for...in
iterates the enumerable properties including theprototype
propertieshttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
This difference shows up when an event emitter (eg a reporter) is written using es6
class
or older equivalent approaches. The super old karma code tends to write objects with methods as own properties so the two solutions do not differ.For the purpose of the
EventEmitter
we wantfor..in
so we bind the class methods.(To be sure I really dislike this way-too-tricky bind-with-name-hacking code).