Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding in E.sendOnly() #1865

Merged
merged 21 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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