From ab7b6490f3a8e2bd469db0d0194c74bd407b7b63 Mon Sep 17 00:00:00 2001 From: Diana Thayer Date: Fri, 14 May 2021 09:47:05 -0700 Subject: [PATCH] fix: await taskqueue in pouchdb-wrappers --- .../pouchdb-wrappers/lib/index.js | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/pouchdb-wrappers/lib/index.js b/packages/node_modules/pouchdb-wrappers/lib/index.js index dd52871d..89bd107b 100644 --- a/packages/node_modules/pouchdb-wrappers/lib/index.js +++ b/packages/node_modules/pouchdb-wrappers/lib/index.js @@ -463,8 +463,26 @@ function callHandlers(handlers, args, method) { for (var i = handlers.length - 1; i >= 0; i -= 1) { method = handlers[i].bind(null, method, args); } - //start running the chain. - var promise = method(); + // start running the chain. + // make sure the handler chain is not started, before the corresponding + // pouchdb instance is fully initialized. Otherwise it might result + // in a double calling the augmented methods + var promise; + if (!args.base.taskqueue.isReady) { + promise = new Promise(function (resolve, reject) { + args.base.taskqueue.addTask(function (failed) { + if (failed) { + reject(failed); + } else { + resolve(); + } + }); + }).then(function () { + return method(); + }); + } else { + promise = method(); + } nodify(promise, callback); return promise; }