Skip to content

Commit

Permalink
feat(disconnected): function to break all pending promises
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 19, 2019
1 parent a43338d commit 79a930a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HandledPromise, E } from '@agoric/eventual-send';
export { E, HandledPromise, Nat, harden };

export function makeCapTP(ourId, send, bootstrapObj = undefined) {
let unplug = false;
const { serialize, unserialize } = makeMarshal(
// eslint-disable-next-line no-use-before-define
serializeSlot,
Expand Down Expand Up @@ -204,6 +205,9 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {

// Return a dispatch function.
const dispatch = obj => {
if (unplug) {
return false;
}
const fn = handler[obj.type];
if (fn) {
fn(obj);
Expand All @@ -212,5 +216,16 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
return false;
};

return harden({ dispatch, getBootstrap });
const disconnected = () => {
unplug = true;
const err = Error(`${ourId} disconnected`);
for (const pr of questions.values()) {
pr.rej(err);
}
for (const pr of imports.values()) {
pr.rej(err);
}
};

return harden({ dispatch, getBootstrap, disconnected });
}
15 changes: 15 additions & 0 deletions test/disco.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from 'tape-promise/tape';
import { harden, makeCapTP } from '../lib/captp';

test('try disconnecting captp', async t => {
try {
const { dispatch, getBootstrap, disconnected } = makeCapTP('us', obj => {}, () => harden({}));
const pr = t.rejects(getBootstrap(), Error, 'rejected after disconnect');
disconnected();
await pr;
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
});

0 comments on commit 79a930a

Please sign in to comment.