/
event-proxy.js
128 lines (110 loc) · 2.54 KB
/
event-proxy.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* (c) 2012 Beau Sorensen
* MIT Licensed
* For all details and documentation:
* https://github.com/sorensen/defined-args
*/
;(function() {
'use strict'
/*!
* Module dependencies.
*/
var concat = Array.prototype.concat
, slice = Array.prototype.slice
, toString = Object.prototype.toString
, listener = 'on'
/**
* Check for array
*
* @param {Any} obj
* @returns {Boolean} result
* @api private
*/
function isArray(obj) {
return toString.call(obj) === '[object Array]'
}
/**
* Call a method of the given scope, prepending any given
* args to the function call along with sent arguments
*
* @param {Object} scope of method call
* @param {String} method name
* @param {...} additional arguments to be used every method call
* @return {Object} js object
* @api private
*/
function bind(scope, method) {
var args = slice.call(arguments, 2)
if (typeof method === 'string') {
method = scope[method]
}
if (!method) {
throw new Error('Proxy: method `' + method + '` does not exist')
}
return function() {
return method.apply(scope, concat.apply(args, arguments))
}
}
/**
* Setup all proxy methods, if an array is supplied for the methods
* the event will be proxied to a method of the same name
*
* Examples:
*
* proxy()
*
* @param {Object} scope of method calls
* @param {Array|Objects} methods to bind
* @param {Object} event emitting object
* @param {...} additional arguments to be used on every method
* @api public
*/
function proxy(scope, map, emitter) {
var args = slice.call(arguments, 3)
, tmp = {}, len, name, methods, method, i
if (isArray(map)) {
len = map.length
while (len--) {
tmp[map[len]] = map[len]
}
map = tmp
}
for (name in map) {
methods = map[name]
if (!isArray(methods)) {
methods = [methods]
}
for (i = 0; i < methods.length; i++) {
method = methods[i]
if (typeof method === 'string') {
method = scope[method]
}
if (!method) {
throw new Error('Proxy: method `' + method + '` does not exist')
}
emitter[listener](name, bind.apply(scope, concat.apply([scope, method], args)))
}
}
}
/**
* Change the emitter's listener method name
*
* @param {String} method name
* @api public
*/
proxy.config = function(method) {
listener = method || listener
}
/**
* Current library version, should match `package.json`
*/
proxy.VERSION = '0.0.1'
/*!
* Module exports.
*/
if (typeof exports !== 'undefined') {
module.exports = proxy
} else {
this.proxy = proxy
}
}).call(this);