Skip to content

playframe/proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PlayFrame

Proxy

Proxy traps for given methods

Installation

npm install --save @playframe/proxy

Usage

import proxy from '@playframe/proxy'

const methods = ['walk', 'run', 'fly']

const handler = (method, arg1, arg2)=>
  console.log(`${method}(${arg1}, ${arg2})`)


const Proxy = proxy(methods)
const trap = Proxy(handler)
trap.run('very', 'fast') // log> run(very, fast)

Annotated Source

Caching Object.create for perf and minification

{create} = Object

Let's define a higher order function takes a list of methods,

module.exports = (methods)=>

creates a prototype with traps calling a handler function passing method name and 2 arguments

  proto = create null # not extending Object.prototype
  methods.forEach (method)=>
    proto[method] = (x, y)->
      @_h method, x, y

and returns a factory function, that creates a proxy for given handle

  (handle)=> create proto, _h: value: handle

About

0.15 kB Proxy traps for given methods

Resources

License

Stars

Watchers

Forks

Packages

No packages published