jeudi 3 août 2017

ES6 Proxy wrapper

Modern ECMAScript-262 from 6 version supports Proxy object, which allows to intercept custom object invocations (http://ift.tt/1GHAAv1 )

By using Proxy-object, user can write something like obj.func1(123).func2('ABC').something, there nothing right to obj is not existing actually. Of course, by proper Proxy declaration, this chain can exist. The simplest way is return new Proxy on every property access and function invocation.

But is there way to automate this process, maybe some NPM library? The goal is transform custom free-form expression with Proxy to declarative string. Upper example will transform to something like:

const result = obj.func1(123).func2('ABC').something;
result.__INVOCATION_VIEW__ === [
  { type: 'function', name: 'func1', args: [123] },
  { type: 'function', name: 'func2', args: ['ABC'] },
  { type: 'get', name: 'something' }
]

Pure theoretically, solution is possible anyway, because any entity in invocation chain is available as string in subsequent Proxy. But implementation is much complex. Maybe there is existing library for it?





Aucun commentaire:

Enregistrer un commentaire