From a42d8dc2b0d43ad933c555ff4e562e40dee8b346 Mon Sep 17 00:00:00 2001 From: Karl Lundfall Date: Tue, 6 Jun 2017 17:00:34 +0200 Subject: [PATCH] Added mocked promise instead of real promise to event emitter.once --- src/core/EventEmitter.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/core/EventEmitter.js b/src/core/EventEmitter.js index 300823d9..828fdd43 100644 --- a/src/core/EventEmitter.js +++ b/src/core/EventEmitter.js @@ -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