mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
In domain-task, ensure completion callback always fires asynchronously
This commit is contained in:
@@ -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": {
|
||||
|
||||
@@ -9,14 +9,17 @@ export function addTask(task: PromiseLike<any>) {
|
||||
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<T>(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);
|
||||
|
||||
Reference in New Issue
Block a user