0.0.8 • Published 12 years ago

easy-proxy v0.0.8

Weekly downloads
22
License
-
Repository
github
Last release
12 years ago

Easy Proxy

Shims in Direct-Proxies and then builds a simplified handler on top of that. The traps are abstracted down into a smaller set of traps with normalized parameters and simple to use forwarding.

npm install easy-proxy

Basics

var EasyProxy = require('easy-proxy');

var proxied = EasyProxy(target, {
  list:   function(reflect, target, type, own, hidden)              { return reflect() },
  has:    function(reflect, target, name, own)                      { return reflect() },
  get:    function(reflect, target, name, own, receiver, normalize) { return reflect() },
  set:    function(reflect, target, name, val, receiver, normalize) { return reflect() },
  unset:  function(reflect, target, name)                           { return reflect() },
  fix:    function(reflect, target, type)                           { return reflect() },
  invoke: function(reflect, target, type, args, receiver)           { return reflect() },
});

Usage

Any traps not provided in the handler will default to reflecting normally onto the target.

reflect

The first parameter is always a function that when called with no arguments will execute the default forwarding action. This allows for easy interception and modification. It also accepts a single object passed that specified overrides on the default action.

  • trap: Direct Proxies traps name name to execute instead of the default
  • target: Redirect the action to another target
  • name: Change the name that actual is executed again for traps that are for specific properties
  • value: Change the value for set
  • receiver: Change the receiver for get, set, and invoke
// redirect all set operations to global instead;
var redirected = EasyProxy({}, {
  set: function(reflect){
    return reflect({ target: global });
  }
});

normalize

For get and set, normalize is a function provided as the last parameter. Its pupose is normalize the difference between a descriptor and normal value since these traps are set+defineProperty and get+getOwnPropertyDescriptor.

var typeofWrap = EasyProxy(global, {
  get: function(reflect, target, name, own, receiver, normalize){
    return normalize(typeof target[name]);
  }
});
0.0.8

12 years ago

0.0.7

12 years ago

0.0.5

12 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago