Last active
November 11, 2016 11:19
-
-
Save zdychacek/00d4853ab6856f3c6912 to your computer and use it in GitHub Desktop.
Revisions
-
Ondřej Ždych revised this gist
May 1, 2015 . 1 changed file with 7 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,9 +3,9 @@ const Reflect = require('harmony-reflect'); function createProxy (action) { // 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 return action(methodNames.join('.'), args); } }); } // create the main proxy object return new Proxy({}, { get (target, name) { return _createCallableProxy(name); } }); } @@ -56,4 +48,4 @@ const a = remoteProxy.method1; const b = remoteProxy.method2; a(); // -> Calling a remote method "method1" with arguments: [] -
Ondřej Ždych revised this gist
May 1, 2015 . 1 changed file with 28 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 new Proxy({}, { get (target, name) { // reset the accumulator methodNames.length = 0; methodNames.push(name); 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' } ] const a = remoteProxy.method1; const b = remoteProxy.method2; a(); // -> Calling a remote method "method1" with arguments: [] -
Ondřej Ždych revised this gist
Apr 30, 2015 . 1 changed file with 37 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,45 @@ // require Proxy API normalization polyfill because current V8's implementation doesn't support standardized API const Reflect = require('harmony-reflect'); function createProxy (action) { // 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({}, { 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); } }); } }); } // create a proxy const remoteProxy = createProxy(function (methodName, args) { console.log(`Calling a remote method "${methodName}" with arguments:`, args); }); 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' } ] -
Ondřej Ždych revised this gist
Aug 8, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } ] -
Ondřej Ždych created this gist
Aug 8, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 });