Skip to content

Commit

Permalink
Merge pull request #1865 from zarutian/patch-3
Browse files Browse the repository at this point in the history
adding in E.sendOnly()
  • Loading branch information
michaelfig authored Oct 17, 2020
2 parents f1958ba + deb46fe commit 8dae74a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/eventual-send/src/E.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ function EProxyHandler(x, HandledPromise) {
});
}

/**
* A Proxy handler for E.sendOnly(x)
* For now it is just a variant on the E(x) Proxy handler.
*
* @param {*} x Any value passed to E.sendOnly(x)
* @returns {ProxyHandler} the Proxy handler
*/
function EsendOnlyProxyHandler(x, HandledPromise) {
return harden({
...readOnlyProxyHandler,
get(_target, p, _receiver) {
if (`${p}` !== p) {
return undefined;
}
return (...args) => {
HandledPromise.applyMethod(x, p, args);
return undefined;
};
},
apply(_target, _thisArg, argsArray = []) {
HandledPromise.applyFunction(x, argsArray);
return undefined;
},
has(_target, _p) {
// We just pretend that every thing exists.
return true;
},
});
}

export default function makeE(HandledPromise) {
function E(x) {
const handler = EProxyHandler(x, HandledPromise);
Expand All @@ -67,6 +97,10 @@ export default function makeE(HandledPromise) {

E.G = makeEGetterProxy;
E.resolve = HandledPromise.resolve;
E.sendOnly = x => {
const handler = EsendOnlyProxyHandler(x, HandledPromise);
return harden(new Proxy(() => {}, handler));
};

E.when = (x, onfulfilled = undefined, onrejected = undefined) =>
HandledPromise.resolve(x).then(onfulfilled, onrejected);
Expand Down
46 changes: 46 additions & 0 deletions packages/eventual-send/test/test-e.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ test('E method calls', async t => {
t.is(await d, 12, 'method call works');
});

test('E sendOnly method calls', async t => {
let testIncrDoneResolve;
const testIncrDone = new Promise(resolve => {
testIncrDoneResolve = resolve;
});

let count = 0;
const counter = {
incr(n) {
count += n;
testIncrDoneResolve(); // only here for the test.
return count;
},
};
const result = E.sendOnly(counter).incr(42);
t.is(typeof result, 'undefined', 'return is undefined as expected');
await testIncrDone;
t.is(count, 42, 'sendOnly method call variant works');
});

test('E call missing method', async t => {
const x = {
double(n) {
Expand All @@ -59,6 +79,32 @@ test('E call missing method', async t => {
});
});

test.skip('E sendOnly call missing method', async t => {
let testDecrDoneResolve;
const testDecrDone = new Promise(resolve => {
testDecrDoneResolve = resolve;
});

let count = 279;
const counter = {
incr(n) {
count += n;
testDecrDoneResolve(); // only here for the test
return count;
},
};

t.throwsAsync(
async () => {
E.sendOnly(counter).decr(210);
await testDecrDone;
},
{
message: 'target has no method "decr", has [incr]',
},
);
});

test('E call undefined method', async t => {
const x = {
double(n) {
Expand Down

0 comments on commit 8dae74a

Please sign in to comment.