Skip to content

Commit feda6c8

Browse files
committed
fix(captp): ensure trapcap reply iteration is serial
1 parent 592f0b7 commit feda6c8

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

packages/captp/src/captp.js

+22-23
Original file line numberDiff line numberDiff line change
@@ -443,31 +443,30 @@ export const makeCapTP = (
443443

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

446-
const resultPK = makePromiseKit();
447-
trapIteratorResultP.set(questionID, resultPK.promise);
446+
const getNextResultP = async () => {
447+
const result = await resultP;
448+
if (!result || result.done) {
449+
// We're done!
450+
trapIterator.delete(questionID);
451+
trapIteratorResultP.delete(questionID);
452+
return result;
453+
}
448454

449-
const { done } = await resultP;
450-
if (done) {
451-
trapIterator.delete(questionID);
452-
return;
453-
}
454-
const ait = trapIterator.get(questionID);
455-
456-
try {
457-
switch (method) {
458-
case 'next':
459-
case 'return':
460-
case 'throw': {
461-
resultPK.resolve(ait && ait[method] && ait[method](...args));
462-
break;
463-
}
464-
default: {
465-
assert.fail(X`Unrecognized iteration method ${method}`);
466-
}
455+
const ait = trapIterator.get(questionID);
456+
if (ait && ait[method]) {
457+
// Drive the next iteration.
458+
return ait[method](...args);
467459
}
468-
} catch (e) {
469-
resultPK.reject(e);
470-
}
460+
461+
return result;
462+
};
463+
464+
// Store the next result promise.
465+
const nextResultP = getNextResultP();
466+
trapIteratorResultP.set(questionID, nextResultP);
467+
468+
// Wait for the next iteration so that we properly report errors.
469+
await nextResultP;
471470
},
472471
// Answer to one of our questions.
473472
async CTP_RETURN(obj) {

0 commit comments

Comments
 (0)