Skip to content

Commit

Permalink
fix(captp): ensure trapcap reply iteration is serial
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jul 17, 2021
1 parent 592f0b7 commit feda6c8
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,31 +443,30 @@ export const makeCapTP = (

const [method, args] = unserialize(serialized);

const resultPK = makePromiseKit();
trapIteratorResultP.set(questionID, resultPK.promise);
const getNextResultP = async () => {
const result = await resultP;
if (!result || result.done) {
// We're done!
trapIterator.delete(questionID);
trapIteratorResultP.delete(questionID);
return result;
}

const { done } = await resultP;
if (done) {
trapIterator.delete(questionID);
return;
}
const ait = trapIterator.get(questionID);

try {
switch (method) {
case 'next':
case 'return':
case 'throw': {
resultPK.resolve(ait && ait[method] && ait[method](...args));
break;
}
default: {
assert.fail(X`Unrecognized iteration method ${method}`);
}
const ait = trapIterator.get(questionID);
if (ait && ait[method]) {
// Drive the next iteration.
return ait[method](...args);
}
} catch (e) {
resultPK.reject(e);
}

return result;
};

// Store the next result promise.
const nextResultP = getNextResultP();
trapIteratorResultP.set(questionID, nextResultP);

// Wait for the next iteration so that we properly report errors.
await nextResultP;
},
// Answer to one of our questions.
async CTP_RETURN(obj) {
Expand Down

0 comments on commit feda6c8

Please sign in to comment.