Skip to content

Commit

Permalink
Added mocked promise instead of real promise to event emitter.once
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Lundfall committed Jun 6, 2017
1 parent dbaf963 commit a42d8dc
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/core/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,28 @@ define(function(require, exports, module) {
* Listens once
* @param type
* @param handler
* @returns {Promise}
* @returns {Mocked Promise}
*/
EventEmitter.prototype.once = function once(type, handler) {
return new Promise((resolve) => {
this.on(type, function onceWrapper() {
this.removeListener(type, onceWrapper);
handler && handler.apply(this._owner, arguments);
resolve.apply(null, arguments)
}, this);
});
var resolvers = [], resolveValue, isResolved = false;
var promise = {then: function(resolveFunction){
if(isResolved){
resolveFunction(resolveValue)
} else {
resolvers.push(resolveFunction);
}
}};
this.on(type, function onceWrapper() {
this.removeListener(type, onceWrapper);
handler && handler.apply(this._owner, arguments);
resolveValue = arguments[0];
isResolved = true;
for(var i=0; i<resolvers.length; i++){
resolvers[i](resolveValue);
}
}, this);

return promise;
};

/**
Expand Down

0 comments on commit a42d8dc

Please sign in to comment.