Skip to content

Commit

Permalink
feat(E.C): implement strawman chaining proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 11, 2019
1 parent 4f0e4bf commit f97d5a4
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions src/E.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function EProxyHandler(x, HandledPromise) {
}

export default function makeE(HandledPromise) {
return harden(function E(x) {
function E(x) {
// p = E(x).name(args)
//
// E(x) returns a proxy on which you can call arbitrary methods. Each of
Expand All @@ -53,5 +53,91 @@ export default function makeE(HandledPromise) {

const handler = EProxyHandler(x, HandledPromise);
return harden(new Proxy({}, handler));
});
}

let EChain;
const makers = {
G(x) {
// Return getter.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return EChain(HandledPromise.get(x, prop));
},
},
);
},
D(x) {
// Return deleter.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return EChain(HandledPromise.delete(x, prop));
},
},
);
},
S(x) {
// Return setter.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return harden(value => EChain(HandledPromise.set(x, prop, value)));
},
},
);
},
M(x) {
// Return method-caller.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return harden((...args) =>
EChain(HandledPromise.applyMethod(x, prop, args)),
);
},
apply(_target, _thisArg, args = []) {
return EChain(HandledPromise.applyFunction(x, args));
},
},
);
},
P(x) {
// Return as promise.
return Promise.resolve(x);
},
};

EChain = x => {
return new Proxy(
{}, // empty shadow
{
has(_target, prop) {
return Object.keys(makers).indexOf(prop) >= 0;
},
get(_target, prop) {
return harden(makers[prop](x));
},
},
);
};

E.C = EChain;
return harden(E);
}

0 comments on commit f97d5a4

Please sign in to comment.