From b95cd1bc85fbd0aa0456408fc2fa348e71d6e4c3 Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Mon, 7 Mar 2016 15:27:56 +0000 Subject: [PATCH] In domain-task, ensure completion callback always fires asynchronously --- .../npm/domain-task/package.json | 2 +- .../npm/domain-task/src/main.ts | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNet.SpaServices/npm/domain-task/package.json b/src/Microsoft.AspNet.SpaServices/npm/domain-task/package.json index c44f196368..a84ba7e23c 100644 --- a/src/Microsoft.AspNet.SpaServices/npm/domain-task/package.json +++ b/src/Microsoft.AspNet.SpaServices/npm/domain-task/package.json @@ -1,6 +1,6 @@ { "name": "domain-task", - "version": "1.0.0", + "version": "1.0.1", "description": "Tracks outstanding operations for a logical thread of execution", "main": "main.js", "scripts": { diff --git a/src/Microsoft.AspNet.SpaServices/npm/domain-task/src/main.ts b/src/Microsoft.AspNet.SpaServices/npm/domain-task/src/main.ts index 7d0e3e1412..ee74d73d55 100644 --- a/src/Microsoft.AspNet.SpaServices/npm/domain-task/src/main.ts +++ b/src/Microsoft.AspNet.SpaServices/npm/domain-task/src/main.ts @@ -9,14 +9,17 @@ export function addTask(task: PromiseLike) { state.numRemainingTasks++; task.then(() => { // The application may have other listeners chained to this promise *after* - // this listener. Since we don't want the combined task to complete until - // all the handlers for child tasks have finished, delay the following by - // one tick. + // this listener, which may in turn register further tasks. Since we don't + // want the combined task to complete until all the handlers for child tasks + // have finished, delay the response to give time for more tasks to be added + // synchronously. setTimeout(() => { state.numRemainingTasks--; if (state.numRemainingTasks === 0 && !state.hasIssuedSuccessCallback) { state.hasIssuedSuccessCallback = true; - state.completionCallback(/* error */ null); + setTimeout(() => { + state.completionCallback(/* error */ null); + }, 0); } }, 0); }, (error) => { @@ -42,7 +45,9 @@ export function run(codeToRun: () => T, completionCallback: (error: any) => v // If no tasks were registered synchronously, then we're done already if (state.numRemainingTasks === 0 && !state.hasIssuedSuccessCallback) { state.hasIssuedSuccessCallback = true; - state.completionCallback(/* error */ null); + setTimeout(() => { + state.completionCallback(/* error */ null); + }, 0); } } catch(ex) { state.completionCallback(ex);