Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(task): findEventTask should return Task array (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Feb 17, 2017
1 parent c07560f commit 14c7a6f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/browser/browser.ts
Expand Up @@ -146,16 +146,16 @@ if (_global['navigator'] && _global['navigator'].geolocation) {
// handle unhandled promise rejection
function findPromiseRejectionHandler(evtName: string) {
return function(e: any) {
const eventTask = findEventTask(_global, evtName);
if (eventTask) {
const eventTasks = findEventTask(_global, evtName);
eventTasks.forEach(eventTask => {
// windows has added unhandledrejection event listener
// trigger the event listener
const PromiseRejectionEvent = _global['PromiseRejectionEvent'];
if (PromiseRejectionEvent) {
const evt = new PromiseRejectionEvent(evtName, {promise: e.promise, reason: e.rejection});
eventTask.invoke(evt);
}
}
});
};
}

Expand Down
7 changes: 4 additions & 3 deletions lib/common/utils.ts
Expand Up @@ -554,17 +554,18 @@ export function patchMicroTask(
});
}

export function findEventTask(target: any, evtName: string) {
export function findEventTask(target: any, evtName: string): Task[] {
const eventTasks: Task[] = target[zoneSymbol('eventTasks')];
const result: Task[] = [];
if (eventTasks) {
for (let i = 0; i < eventTasks.length; i++) {
const eventTask = eventTasks[i];
const data = eventTask.data;
const eventName = data && (<any>data).eventName;
if (eventName === evtName) {
return eventTask;
result.push(eventTask);
}
}
}
return undefined;
return result;
}
6 changes: 3 additions & 3 deletions lib/node/node.ts
Expand Up @@ -73,16 +73,16 @@ function patchProcess() {
// handle unhandled promise rejection
function findProcessPromiseRejectionHandler(evtName: string) {
return function(e: any) {
const eventTask = findEventTask(process, evtName);
if (eventTask) {
const eventTasks = findEventTask(process, evtName);
eventTasks.forEach(eventTask => {
// process has added unhandledrejection event listener
// trigger the event listener
if (evtName === 'unhandledRejection') {
eventTask.invoke(e.rejection, e.promise);
} else if (evtName === 'rejectionHandled') {
eventTask.invoke(e.promise);
}
}
});
};
}

Expand Down
28 changes: 28 additions & 0 deletions test/browser/browser.spec.ts
Expand Up @@ -193,6 +193,34 @@ describe('Zone', function() {
});
});
});

it('should support multiple window.addEventListener(unhandledrejection)', function(done) {
if (!promiseUnhandleRejectionSupport()) {
done();
return;
}
Zone[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true;
rootZone.fork({name: 'promise'}).run(function() {
const listener1 = (evt: any) => {
expect(evt.type).toEqual('unhandledrejection');
expect(evt.promise.constructor.name).toEqual('Promise');
expect(evt.reason.message).toBe('promise error');
window.removeEventListener('unhandledrejection', listener1);
};
const listener2 = (evt: any) => {
expect(evt.type).toEqual('unhandledrejection');
expect(evt.promise.constructor.name).toEqual('Promise');
expect(evt.reason.message).toBe('promise error');
window.removeEventListener('unhandledrejection', listener2);
done();
};
window.addEventListener('unhandledrejection', listener1);
window.addEventListener('unhandledrejection', listener2);
new Promise((resolve, reject) => {
throw new Error('promise error');
});
});
});
});
});
});
45 changes: 38 additions & 7 deletions test/node/process.spec.ts
Expand Up @@ -22,7 +22,7 @@ describe('process related test', () => {
});
});
});
it('process.nextTick should be excuted before macroTask and promise', (done) => {
it('process.nextTick should be executed before macroTask and promise', (done) => {
zoneA.run(function() {
setTimeout(() => {
result.push('timeout');
Expand Down Expand Up @@ -64,13 +64,16 @@ describe('process related test', () => {
done();
});
});
it('should support window.addEventListener(unhandledrejection)', function(done) {

it('should support process.on(unhandledRejection)', function(done) {
const hookSpy = jasmine.createSpy('hook');
Zone[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true;
Zone.current.fork({name: 'promise'}).run(function() {
process.on('unhandledRejection', function(reason, promise) {
const listener = function(reason, promise) {
hookSpy(promise, reason.message);
});
process.removeListener('unhandledRejection', listener);
};
process.on('unhandledRejection', listener);
const p = new Promise((resolve, reject) => {
throw new Error('promise error');
});
Expand All @@ -82,13 +85,15 @@ describe('process related test', () => {
});
});

it('should support window.addEventListener(rejectionHandled)', function(done) {
it('should support process.on(rejectionHandled)', function(done) {
Zone[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true;
Zone.current.fork({name: 'promise'}).run(function() {
process.on('rejectionHandled', function(promise) {
const listener = function(promise) {
expect(promise).toEqual(p);
process.removeListener('rejectionHandled', listener);
done();
});
};
process.on('rejectionHandled', listener);
const p = new Promise((resolve, reject) => {
throw new Error('promise error');
});
Expand All @@ -98,4 +103,30 @@ describe('process related test', () => {
}, 10);
});
});

it('should support multiple process.on(unhandledRejection)', function(done) {
const hookSpy = jasmine.createSpy('hook');
Zone[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true;
Zone.current.fork({name: 'promise'}).run(function() {
const listener1 = function(reason, promise) {
hookSpy(promise, reason.message);
process.removeListener('unhandledRejection', listener1);
};
const listener2 = function(reason, promise) {
hookSpy(promise, reason.message);
process.removeListener('unhandledRejection', listener2);
};
process.on('unhandledRejection', listener1);
process.on('unhandledRejection', listener2);
const p = new Promise((resolve, reject) => {
throw new Error('promise error');
});

setTimeout(function() {
expect(hookSpy.calls.count()).toBe(2);
expect(hookSpy.calls.allArgs()).toEqual([[p, 'promise error'], [p, 'promise error']]);
done();
}, 10);
});
});
});

0 comments on commit 14c7a6f

Please sign in to comment.