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

Wait for TaskQueue to be ready before calling wrapper chain #3

Merged
merged 3 commits into from
May 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,28 @@ function callHandlers(handlers, args, method) {
for (var i = handlers.length - 1; i >= 0; i -= 1) {
method = handlers[i].bind(null, method, args);
}

//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();
}
Copy link

@gr2m gr2m Jul 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another pattern and maybe more readable would be

var promise = Promise.resolve()
if (!args.base.taskqueue.isReady) {
  promise = promise.then(function () {
    new Promise(function (resolve, reject) {
      args.base.taskqueue.addTask(function (failed) {
        if (failed) {
          return reject(failed);
        }

        resolve();
      });
    });
  })
}

promise = promise.then(function () {
  return method()
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I usually use this pattern as well. I have got no idea, why I didn't do it here. Should I change it and push a new commit, or do you want to stay with the original implementation?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you agree that it’s more readable, send another commit, yeah. We can squash & merge the commits once we merge. Thanks


//start running the chain.
var promise = method();
nodify(promise, callback);
return promise;
}
Expand Down