Skip to content

Commit

Permalink
move setting unhandled rejection tracking to notify, run notify task …
Browse files Browse the repository at this point in the history
…only if it is not running, #83, #85
  • Loading branch information
zloirock committed Jul 5, 2015
1 parent cbc0f6e commit 6733fd4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
45 changes: 24 additions & 21 deletions library/modules/es6.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ function isThenable(it){
if(isObject(it))then = it.then;
return isFunction(then) ? then : false;
}
function notify(record){
function notify(record, isReject){
if(record.n)return;
record.n = true;
var chain = record.c;
// strange IE + webpack dev server bug - use .call(global)
if(chain.length)asap.call(global, function(){
asap.call(global, function(){
var value = record.v
, ok = record.s == 1
, i = 0;
Expand All @@ -99,6 +101,20 @@ function notify(record){
}
while(chain.length > i)run(chain[i++]); // variable length - can't use forEach
chain.length = 0;
record.n = false;
if(isReject)setTimeout(function(){
// strange IE + webpack dev server bug - use .call(global)
asap.call(global, function(){
if(isUnhandled(record.p)){
if(isNode){
process.emit('unhandledRejection', value, record.p);
} else if(global.console && console.error){
console.error('Unhandled promise rejection', value);
}
}
record.a = undefined;
});
}, 1);
});
}
function isUnhandled(promise){
Expand All @@ -113,28 +129,14 @@ function isUnhandled(promise){
} return true;
}
function $reject(value){
var record = this
, promise;
var record = this;
if(record.d)return;
record.d = true;
record = record.r || record; // unwrap
record.v = value;
record.s = 2;
record.a = record.c.slice();
setTimeout(function(){
// strange IE + webpack dev server bug - use .call(global)
asap.call(global, function(){
if(isUnhandled(promise = record.p)){
if(isNode){
process.emit('unhandledRejection', value, promise);
} else if(global.console && console.error){
console.error('Unhandled promise rejection', value);
}
}
record.a = undefined;
});
}, 1);
notify(record);
notify(record, true);
}
function $resolve(value){
var record = this
Expand All @@ -156,7 +158,7 @@ function $resolve(value){
} else {
record.v = value;
record.s = 1;
notify(record);
notify(record, false);
}
} catch(e){
$reject.call({r: record, d: false}, e); // wrap
Expand All @@ -175,7 +177,8 @@ if(!useNative){
s: 0, // <- state
d: false, // <- done
v: undefined, // <- value
h: false // <- handled rejection
h: false, // <- handled rejection
n: false // <- notify
};
$.hide(this, RECORD, record);
try {
Expand All @@ -199,7 +202,7 @@ if(!useNative){
var record = this[RECORD];
record.c.push(react);
if(record.a)record.a.push(react);
if(record.s)notify(record);
if(record.s)notify(record, false);
return promise;
},
// 25.4.5.1 Promise.prototype.catch(onRejected)
Expand Down
45 changes: 24 additions & 21 deletions modules/es6.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ function isThenable(it){
if(isObject(it))then = it.then;
return isFunction(then) ? then : false;
}
function notify(record){
function notify(record, isReject){
if(record.n)return;
record.n = true;
var chain = record.c;
// strange IE + webpack dev server bug - use .call(global)
if(chain.length)asap.call(global, function(){
asap.call(global, function(){
var value = record.v
, ok = record.s == 1
, i = 0;
Expand All @@ -99,6 +101,20 @@ function notify(record){
}
while(chain.length > i)run(chain[i++]); // variable length - can't use forEach
chain.length = 0;
record.n = false;
if(isReject)setTimeout(function(){
// strange IE + webpack dev server bug - use .call(global)
asap.call(global, function(){
if(isUnhandled(record.p)){
if(isNode){
process.emit('unhandledRejection', value, record.p);
} else if(global.console && console.error){
console.error('Unhandled promise rejection', value);
}
}
record.a = undefined;
});
}, 1);
});
}
function isUnhandled(promise){
Expand All @@ -113,28 +129,14 @@ function isUnhandled(promise){
} return true;
}
function $reject(value){
var record = this
, promise;
var record = this;
if(record.d)return;
record.d = true;
record = record.r || record; // unwrap
record.v = value;
record.s = 2;
record.a = record.c.slice();
setTimeout(function(){
// strange IE + webpack dev server bug - use .call(global)
asap.call(global, function(){
if(isUnhandled(promise = record.p)){
if(isNode){
process.emit('unhandledRejection', value, promise);
} else if(global.console && console.error){
console.error('Unhandled promise rejection', value);
}
}
record.a = undefined;
});
}, 1);
notify(record);
notify(record, true);
}
function $resolve(value){
var record = this
Expand All @@ -156,7 +158,7 @@ function $resolve(value){
} else {
record.v = value;
record.s = 1;
notify(record);
notify(record, false);
}
} catch(e){
$reject.call({r: record, d: false}, e); // wrap
Expand All @@ -175,7 +177,8 @@ if(!useNative){
s: 0, // <- state
d: false, // <- done
v: undefined, // <- value
h: false // <- handled rejection
h: false, // <- handled rejection
n: false // <- notify
};
$.hide(this, RECORD, record);
try {
Expand All @@ -199,7 +202,7 @@ if(!useNative){
var record = this[RECORD];
record.c.push(react);
if(record.a)record.a.push(react);
if(record.s)notify(record);
if(record.s)notify(record, false);
return promise;
},
// 25.4.5.1 Promise.prototype.catch(onRejected)
Expand Down

0 comments on commit 6733fd4

Please sign in to comment.