Skip to content

Commit

Permalink
fix(captp): be sure to bind the questionID, promiseID
Browse files Browse the repository at this point in the history
By using lastPromiseID directly, it could be incremented before the
promise actually resolves, which results in an incorrect message.
  • Loading branch information
michaelfig committed Oct 15, 2019
1 parent f2bd0ef commit 0fd6f30
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) {
// new export
if (Promise.resolve(val) === val) {
lastPromiseID += 1;
slot = `p${lastPromiseID}`;
const promiseID = lastPromiseID;
slot = `p${promiseID}`;
val.then(
res =>
send({
type: 'CTP_RESOLVE',
promiseID: lastPromiseID,
promiseID,
res: serialize(harden(res)),
}),
rej =>
send({
type: 'CTP_RESOLVE',
promiseID: lastPromiseID,
promiseID,
rej: serialize(harden(rej)),
}),
);
Expand Down Expand Up @@ -80,10 +81,11 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) {
pr.rej = reject;
});
lastQuestionID += 1;
questions.set(lastQuestionID, pr);
const questionID = lastQuestionID;
questions.set(questionID, pr);
send({
type: 'CTP_CALL',
questionID: lastQuestionID,
questionID,
target: slot,
method: serialize(harden([prop, args])),
});
Expand Down Expand Up @@ -184,10 +186,11 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) {
pr.rej = reject;
});
lastQuestionID += 1;
questions.set(lastQuestionID, pr);
const questionID = lastQuestionID;
questions.set(questionID, pr);
send({
type: 'CTP_BOOTSTRAP',
questionID: lastQuestionID,
questionID,
});
return harden(pr.p);
};
Expand Down

0 comments on commit 0fd6f30

Please sign in to comment.