Skip to content

Instantly share code, notes, and snippets.

@zdychacek
Last active November 11, 2016 11:19
Show Gist options
  • Select an option

  • Save zdychacek/00d4853ab6856f3c6912 to your computer and use it in GitHub Desktop.

Select an option

Save zdychacek/00d4853ab6856f3c6912 to your computer and use it in GitHub Desktop.

Revisions

  1. Ondřej Ždych revised this gist May 1, 2015. 1 changed file with 7 additions and 15 deletions.
    22 changes: 7 additions & 15 deletions proxy.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@ const Reflect = require('harmony-reflect');

    function createProxy (action) {

    function _createInnerProxy (methodNames) {
    // make a copy of `methodName` array to ensure each method call will be called properly
    methodNames = methodNames.slice();
    // create the callable proxy
    function _createCallableProxy (name) {
    const methodNames = [ name ];

    return new Proxy(function () {}, {
    get (target, name, receiver) {
    @@ -16,23 +16,15 @@ function createProxy (action) {
    },
    apply (target, name, args) {
    // call the method finally
    action(methodNames.join('.'), args);
    return action(methodNames.join('.'), args);
    }
    });
    }

    // an accumulator for a name of the method to be called eventually
    const methodNames = [];

    // create a wrapping proxy to avoid an unexpected behaviour when previous `remoteProxy` method
    // was not called but only accessed
    // create the main proxy object
    return new Proxy({}, {
    get (target, name) {
    // reset the accumulator
    methodNames.length = 0;
    methodNames.push(name);

    return _createInnerProxy(methodNames);
    return _createCallableProxy(name);
    }
    });
    }
    @@ -56,4 +48,4 @@ const a = remoteProxy.method1;
    const b = remoteProxy.method2;

    a();
    // -> Calling a remote method "method1" with arguments: []
    // -> Calling a remote method "method1" with arguments: []
  2. Ondřej Ždych revised this gist May 1, 2015. 1 changed file with 28 additions and 14 deletions.
    42 changes: 28 additions & 14 deletions proxy.js
    Original file line number Diff line number Diff line change
    @@ -2,29 +2,37 @@
    const Reflect = require('harmony-reflect');

    function createProxy (action) {

    function _createInnerProxy (methodNames) {
    // make a copy of `methodName` array to ensure each method call will be called properly
    methodNames = methodNames.slice();

    return new Proxy(function () {}, {
    get (target, name, receiver) {
    // push a name of the method into the accumulator
    methodNames.push(name);

    return receiver;
    },
    apply (target, name, args) {
    // call the method finally
    action(methodNames.join('.'), args);
    }
    });
    }

    // an accumulator for a name of the method to be called eventually
    const methodNames = [];

    // create a wrapping proxy to avoid an unexpected behaviour when previous `remoteProxy` method
    // was not called but only accessed
    return Proxy({}, {
    return new Proxy({}, {
    get (target, name) {
    // reset the accumulator
    methodNames.length = 0;
    methodNames.push(name);

    return Proxy(function () {}, {
    get (target, name, receiver) {
    // push a name of the method into the accumulator
    methodNames.push(name);

    return receiver;
    },
    apply (target, name, args) {
    // call the method finally
    action(methodNames.join('.'), args);
    }
    });
    return _createInnerProxy(methodNames);
    }
    });
    }
    @@ -42,4 +50,10 @@ remoteProxy.huhu;
    remoteProxy.hehe.hihi;

    remoteProxy.next.remote.call({ name: 'Ondrej' });
    //-> Calling a remote method "next.remote.call" with arguments: [ { name: 'Ondrej' } ]
    // -> Calling a remote method "next.remote.call" with arguments: [ { name: 'Ondrej' } ]

    const a = remoteProxy.method1;
    const b = remoteProxy.method2;

    a();
    // -> Calling a remote method "method1" with arguments: []
  3. Ondřej Ždych revised this gist Apr 30, 2015. 1 changed file with 37 additions and 21 deletions.
    58 changes: 37 additions & 21 deletions proxy.js
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,45 @@
    function createProxy (action) {
    var methodNames = [];
    // require Proxy API normalization polyfill because current V8's implementation doesn't support standardized API
    const Reflect = require('harmony-reflect');

    return Proxy.createFunction({
    get: function (proxy, name) {
    methodNames.push(name);
    function createProxy (action) {
    // an accumulator for a name of the method to be called eventually
    const methodNames = [];

    return proxy;
    }
    }, function () {
    action(methodNames.join('.'), arguments);
    });
    }
    // create a wrapping proxy to avoid an unexpected behaviour when previous `remoteProxy` method
    // was not called but only accessed
    return Proxy({}, {
    get (target, name) {
    // reset the accumulator
    methodNames.length = 0;
    methodNames.push(name);

    function callRemoteMethod (name, args) {
    args = Array.prototype.slice.call(args);
    return Proxy(function () {}, {
    get (target, name, receiver) {
    // push a name of the method into the accumulator
    methodNames.push(name);

    console.log('Calling remote method "' + name + '" with arguments', args);
    return receiver;
    },
    apply (target, name, args) {
    // call the method finally
    action(methodNames.join('.'), args);
    }
    });
    }
    });
    }

    // create proxy
    var adminServerProxy = createProxy(callRemoteMethod);

    // call method on "remote" server
    adminServerProxy.long.path.to.method.to.call.on.remote.server(1, {
    actions: true
    // create a proxy
    const remoteProxy = createProxy(function (methodName, args) {
    console.log(`Calling a remote method "${methodName}" with arguments:`, args);
    });

    // -> Calling remote method "long.path.to.method.to.call.on.remote.server" with arguments [ 1, { actions: true } ]
    remoteProxy.long.path.to.method.to.call.on.remote.server(1, { actions: true });
    // -> Calling a remote method "long.path.to.method.to.call.on.remote.server" with arguments: [ 1, { actions: true } ]

    // the following won't affect the next `remoteProxy` call
    remoteProxy.huhu;
    remoteProxy.hehe.hihi;

    remoteProxy.next.remote.call({ name: 'Ondrej' });
    //-> Calling a remote method "next.remote.call" with arguments: [ { name: 'Ondrej' } ]
  4. Ondřej Ždych revised this gist Aug 8, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion proxy.js
    Original file line number Diff line number Diff line change
    @@ -24,4 +24,6 @@ var adminServerProxy = createProxy(callRemoteMethod);
    // call method on "remote" server
    adminServerProxy.long.path.to.method.to.call.on.remote.server(1, {
    actions: true
    });
    });

    // -> Calling remote method "long.path.to.method.to.call.on.remote.server" with arguments [ 1, { actions: true } ]
  5. Ondřej Ždych created this gist Aug 8, 2014.
    27 changes: 27 additions & 0 deletions proxy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    function createProxy (action) {
    var methodNames = [];

    return Proxy.createFunction({
    get: function (proxy, name) {
    methodNames.push(name);

    return proxy;
    }
    }, function () {
    action(methodNames.join('.'), arguments);
    });
    }

    function callRemoteMethod (name, args) {
    args = Array.prototype.slice.call(args);

    console.log('Calling remote method "' + name + '" with arguments', args);
    }

    // create proxy
    var adminServerProxy = createProxy(callRemoteMethod);

    // call method on "remote" server
    adminServerProxy.long.path.to.method.to.call.on.remote.server(1, {
    actions: true
    });