-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfuncMap.js
executable file
·93 lines (87 loc) · 2.44 KB
/
funcMap.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
/**
* A class that manages event listeners for the override applied to
* EventTarget.[addEventListener, removeEventListener]
*/
export default function FuncMap() {
/**
* @type {Array<Function[]>}
* @private
*/
this._map = [];
}
/**
* Adds a mapping of original listener -> wrapped original listener
* @param {Function} fnKey - The original listener function
* @param {Function} fnValue - The wrapped original listener function
*/
FuncMap.prototype.set = function(fnKey, fnValue) {
this._map.push([fnKey, fnValue]);
};
/**
* Returns the wrapped original listener that is mapped to the supplied function
* if it exists in the FuncMap's mapping
* @param {Function} fnKey - The original listener function
* @return {?Function}
*/
FuncMap.prototype.get = function(fnKey) {
for (var i = 0; i < this._map.length; i++) {
if (this._map[i][0] === fnKey) {
return this._map[i][1];
}
}
return null;
};
/**
* Returns the index of the wrapper for the supplied original function
* if it exists in the FuncMap's mapping
* @param {Function} fnKey - The original listener function
* @return {number}
*/
FuncMap.prototype.find = function(fnKey) {
for (var i = 0; i < this._map.length; i++) {
if (this._map[i][0] === fnKey) {
return i;
}
}
return -1;
};
/**
* Returns the wrapped original listener function for the supplied original
* listener function. If the wrapped original listener does not exist in
* FuncMap's mapping it is added.
* @param {Function} func - The original listener function
* @param {Function} initter - The a function that returns a wrapped version
* of the original listener function
* @return {?Function}
*/
FuncMap.prototype.add_or_get = function(func, initter) {
var fnValue = this.get(func);
if (!fnValue) {
fnValue = initter();
this.set(func, fnValue);
}
return fnValue;
};
/**
* Removes the mapping of the original listener function to its wrapped counter part
* @param {Function} func - The original listener function
* @return {?Function}
*/
FuncMap.prototype.remove = function(func) {
var idx = this.find(func);
if (idx >= 0) {
var fnMapping = this._map.splice(idx, 1);
return fnMapping[0][1];
}
return null;
};
/**
* Calls all wrapped listener functions contained in the FuncMap's mapping
* with the supplied param
* @param {*} param
*/
FuncMap.prototype.map = function(param) {
for (var i = 0; i < this._map.length; i++) {
this._map[i][1](param);
}
};