Skip to content
This repository was archived by the owner on Jul 29, 2021. It is now read-only.

Commit f44c037

Browse files
authored
Merge pull request #3 from jakobwesthoff/taskqueue-race
Wait for TaskQueue to be ready before calling wrapper chain
2 parents 45fdc75 + 0cdbb42 commit f44c037

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

index.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,28 @@ function callHandlers(handlers, args, method) {
443443
for (var i = handlers.length - 1; i >= 0; i -= 1) {
444444
method = handlers[i].bind(null, method, args);
445445
}
446+
447+
//make sure the handler chain is not started, before the corresponding
448+
//pouchdb instance is fully initialized. Otherwise it might result
449+
//in a double calling the augmented methods
450+
var promise;
451+
if (!args.base.taskqueue.isReady) {
452+
promise = new Promise(function (resolve, reject) {
453+
args.base.taskqueue.addTask(function (failed) {
454+
if (failed) {
455+
reject(failed);
456+
} else {
457+
resolve();
458+
}
459+
});
460+
}).then(function () {
461+
return method();
462+
});
463+
} else {
464+
promise = method();
465+
}
466+
446467
//start running the chain.
447-
var promise = method();
448468
nodify(promise, callback);
449469
return promise;
450470
}

0 commit comments

Comments
 (0)