Skip to content

Commit 691024b

Browse files
committed
Fix recursion issue when using es6-promise
1 parent fb75382 commit 691024b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/worker.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,13 @@ Worker.prototype.save = function save(filename) {
296296
/* ----- SET / GET ----- */
297297

298298
Worker.prototype.set = function set(opt) {
299-
// TODO: Test null/undefined input to this function.
300299
// TODO: Implement ordered pairs?
301300

301+
// Silently ignore invalid or empty input.
302+
if (objType(opt) !== 'object') {
303+
return this;
304+
}
305+
302306
// Build an array of setter functions to queue.
303307
var fns = Object.keys(opt || {}).map(function (key) {
304308
if (key in Worker.template.prop) {
@@ -412,9 +416,13 @@ Worker.prototype.then = function then(onFulfilled, onRejected) {
412416
if (onFulfilled) { onFulfilled = onFulfilled.bind(self); }
413417
if (onRejected) { onRejected = onRejected.bind(self); }
414418

419+
// Cast self into a Promise to avoid es6-promise recursively defining `then`.
420+
var selfPromise = ('_state' in self) ?
421+
Worker.convert(Object.assign({}, self), Promise.prototype) : self;
422+
415423
// Update progress while queuing, calling, and resolving `then`.
416424
self.updateProgress(null, null, 1, [onFulfilled]);
417-
var returnVal = Promise.prototype.then.call(self, function then_pre(val) {
425+
var returnVal = Promise.prototype.then.call(selfPromise, function then_pre(val) {
418426
self.updateProgress(null, onFulfilled);
419427
return val;
420428
}).then(onFulfilled, onRejected).then(function then_post(val) {
@@ -434,8 +442,12 @@ Worker.prototype.thenCore = function thenCore(onFulfilled, onRejected) {
434442
if (onFulfilled) { onFulfilled = onFulfilled.bind(self); }
435443
if (onRejected) { onRejected = onRejected.bind(self); }
436444

445+
// Cast self into a Promise to avoid es6-promise recursively defining `then`.
446+
var selfPromise = ('_state' in self) ?
447+
Worker.convert(Object.assign({}, self), Promise.prototype) : self;
448+
437449
// Return the promise, after casting it into a Worker and preserving props.
438-
var returnVal = Promise.prototype.then.call(self, onFulfilled, onRejected);
450+
var returnVal = Promise.prototype.then.call(selfPromise, onFulfilled, onRejected);
439451
return Worker.convert(returnVal, self.__proto__);
440452
};
441453

0 commit comments

Comments
 (0)