-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (43 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Generated by CoffeeScript 2.3.2
// ![PlayFrame](https://avatars3.githubusercontent.com/u/47147479)
// # Proxy
// ###### Proxy traps for given methods
// ## Installation
// ```sh
// npm install --save @playframe/proxy
// ```
// ## Usage
// ```js
// 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
var create;
({create} = Object);
// Let's define a higher order function takes a list of methods,
module.exports = (methods) => {
var proto;
// 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) => {
return proto[method] = function(x, y) {
return this._h(method, x, y);
};
});
// and returns a factory function,
// that creates a proxy for given handle
return (handle) => {
return create(proto, {
_h: {
value: handle
}
});
};
};