Skip to content

Commit

Permalink
feat(api): implement dispatch function, more exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the default export is now named makeCapTP

Other named exports are E, harden, HandledPromise, Nat.
  • Loading branch information
michaelfig committed Oct 15, 2019
1 parent 0ce49a9 commit cad73a2
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 54 deletions.
82 changes: 50 additions & 32 deletions lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import { makeMarshal, QCLASS } from '@agoric/marshal';
import harden from '@agoric/harden';
import Nat from '@agoric/nat';
import { HandledPromise } from '@agoric/eventual-send';
import { HandledPromise, E } from '@agoric/eventual-send';

export default function makeCapTP(ourId, send, bootstrapObj = undefined) {
export { E, HandledPromise, Nat, harden };

export function makeCapTP(ourId, send, bootstrapObj = undefined) {
const { serialize, unserialize } = makeMarshal(
// eslint-disable-next-line no-use-before-define
serializeSlot,
Expand Down Expand Up @@ -67,40 +69,44 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) {
});
}

function makeRemote(slot) {
const handler = {
POST(_o, prop, args) {
// Support: o~.[prop](...args) remote method invocation
// FIXME: Implement a HandledPromise here to support pipelining.
const pr = {};
pr.p = new Promise((resolve, reject) => {
pr.res = resolve;
pr.rej = reject;
});
lastQuestionID += 1;
const questionID = lastQuestionID;
questions.set(questionID, pr);
send({
type: 'CTP_CALL',
questionID,
target: slot,
method: serialize(harden([prop, args])),
});
return harden(pr.p);
},
};

const pr = {};
pr.p = Promise.makeHandled((res, rej, resolveWithPresence) => {
pr.rej = rej;
pr.resPres = () => resolveWithPresence(handler);
pr.res = res;
}, handler);
return harden(pr);
}

function unserializeSlot(data, slots) {
const slot = slots[Nat(data.index)];
let val;
if (!slotToVal.has(slot)) {
// Make a new handled promise for the slot.
const handler = {
POST(_o, prop, args) {
// Support: o~.[prop](...args) remote method invocation
const pr = {};
pr.p = new Promise((resolve, reject) => {
pr.res = resolve;
pr.rej = reject;
});
lastQuestionID += 1;
const questionID = lastQuestionID;
questions.set(questionID, pr);
send({
type: 'CTP_CALL',
questionID,
target: slot,
method: serialize(harden([prop, args])),
});
return harden(pr.p);
},
};

const pr = {};
pr.p = Promise.makeHandled((res, rej, resolveWithPresence) => {
pr.rej = rej;
pr.resPres = () => resolveWithPresence(handler);
pr.res = res;
}, handler);
harden(pr);

const pr = makeRemote(slot);
if (slot[0] === 'o') {
// A new presence
const presence = pr.resPres();
Expand Down Expand Up @@ -194,5 +200,17 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) {
});
return harden(pr.p);
};
return [handler, getBootstrap];
harden(handler);

// Return a dispatch function.
const dispatch = obj => {
const fn = handler[obj.type];
if (fn) {
fn(obj);
return true;
}
return false;
};

return harden({ dispatch, getBootstrap });
}
103 changes: 98 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
"author": "Michael FIG <michael@fig.org>",
"homepage": "https://github.com/Agoric/CapTP#readme",
"license": "Apache-2.0",
"main": "lib/captp.js",
"main": "dist/captp.cjs.js",
"module": "dist/captp.esm.js",
"browser": "dist/captp.umd.js",
"directories": {
"lib": "lib",
"test": "test"
},
"files": [
"lib"
"lib",
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/Agoric/CapTP.git"
},
"scripts": {
"build": "rollup -c",
"test": "tape -r esm 'test/**/*.js'",
"lint-fix": "eslint --fix '**/*.{js,jsx}'",
"lint-check": "eslint '**/*.{js,jsx}'",
Expand All @@ -46,7 +50,10 @@
"@agoric/harden": "^0.0.4",
"@agoric/marshal": "^0.1.1",
"@agoric/nat": "^2.0.1",
"esm": "^3.2.5"
"esm": "^3.2.5",
"rollup": "^1.24.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0"
},
"bugs": {
"url": "https://github.com/Agoric/CapTP/issues"
Expand Down
27 changes: 27 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default [
{
input: 'lib/captp.js',
output: [
{
file: 'dist/captp.umd.js',
format: 'umd',
name: 'CapTP',
},
{
file: 'dist/captp.esm.js',
format: 'esm',
},
{
file: 'dist/captp.cjs.js',
format: 'cjs',
},
],
plugins: [
resolve(),
commonjs({include: 'node_modules/**'}),
],
},
];
29 changes: 15 additions & 14 deletions test/loopback.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { test } from 'tape-promise/tape';
import { E } from '@agoric/eventual-send';
import harden from '@agoric/harden';
import makeCapTP from '../lib/captp';
import { E, harden, makeCapTP } from '../lib/captp';

// TODO: remove .only when you have this test working
test('try loopback captp', async t => {
try {
const debug = false;
let right;
const left = makeCapTP('left', obj => {
if (debug) {
console.log('toRight', obj);
}
right[0][obj.type](obj);
});
right = makeCapTP(
let rightDispatch;
const { dispatch: leftDispatch, getBootstrap: leftBootstrap } = makeCapTP(
'left',
obj => {
if (debug) {
console.log('toRight', obj);
}
rightDispatch(obj);
},
);
({ dispatch: rightDispatch } = makeCapTP(
'right',
obj => {
if (debug) {
console.log('toLeft', obj);
}
left[0][obj.type](obj);
leftDispatch(obj);
},
harden({
encourager: {
Expand All @@ -40,8 +41,8 @@ test('try loopback captp', async t => {
},
},
}),
);
const rightRef = left[1]();
));
const rightRef = leftBootstrap();
const { comment, bang } = await E.C(rightRef).G.encourager.M.encourage(
'buddy',
).P;
Expand Down

0 comments on commit cad73a2

Please sign in to comment.