Skip to content

Commit

Permalink
initial attempt to add+export E() wrapper
Browse files Browse the repository at this point in the history
No tests yet. Requires the addition of `@agoric/harden` as a dependency.

refs Agoric#30
  • Loading branch information
warner committed Sep 30, 2019
1 parent 1fa651a commit 2a3e5ef
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
13 changes: 13 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"url": "https://github.com/Agoric/eventual-send/issues"
},
"homepage": "https://github.com/Agoric/eventual-send#readme",
"dependencies": {},
"dependencies": {
"@agoric/harden": "^0.0.4"
},
"devDependencies": {
"eslint": "^5.3.0",
"eslint-config-airbnb": "^17.1.0",
Expand Down
57 changes: 57 additions & 0 deletions src/E.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import harden from '@agoric/harden';

/**
* A Proxy handler for E(x).
*
* @param {Promise} ep Promise with eventual send API
* @returns {ProxyHandler} the Proxy handler
*/
function EPromiseHandler(ep) {
return harden({
get(_target, p, _receiver) {
if (`${p}` !== p) {
return undefined;
}
// Harden this Promise because it's our only opportunity to ensure
// p1=E(x).foo() is hardened. The Handled Promise API does not (yet)
// allow the handler to synchronously influence the promise returned
// by the handled methods, so we must freeze it from the outside. See
// #95 for details.
return (...args) => harden(ep.post(p, args));
},
deleteProperty(_target, p) {
return harden(ep.delete(p));
},
set(_target, p, value, _receiver) {
return harden(ep.put(p, value));
},
apply(_target, _thisArg, argArray = []) {
return harden(ep.post(undefined, argArray));
},
has(_target, _p) {
// We just pretend everything exists.
return true;
},
});
}

export default function E(x) {
// p = E(x).name(args)
//
// E(x) returns a proxy on which you can call arbitrary methods. Each of
// these method calls returns a promise. The method will be invoked on
// whatever 'x' designates (or resolves to) in a future turn, not this
// one.

const targetP = Promise.resolve(x);
// targetP might resolve to a Presence
const handler = EPromiseHandler(targetP);
return harden(new Proxy({}, handler));
}

// Like Promise.resolve, except that if applied to a presence, it
// would be better for it to return the remote promise for this
// specimen, rather than a fresh local promise fulfilled by this
// specimen.
// TODO: for now, just alias Promise.resolve.
E.resolve = specimen => Promise.resolve(specimen);
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import E from './E';

export { E };

// Create HandledPromise static methods as a bridge from v0.2.4 to
// new proposal support (wavy dot's infrastructure).
export function makeHandledPromise(EPromise) {
Expand Down

0 comments on commit 2a3e5ef

Please sign in to comment.