Commit | Line | Data |
---|---|---|
718ad8e2 | 1 | // Copyright (c) 2011 Google, Inc. |
644eff8b RK |
2 | // |
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | |
4 | // of this software and associated documentation files (the "Software"), to deal | |
5 | // in the Software without restriction, including without limitation the rights | |
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
7 | // copies of the Software, and to permit persons to whom the Software is | |
8 | // furnished to do so, subject to the following conditions: | |
9 | // | |
10 | // The above copyright notice and this permission notice shall be included in | |
11 | // all copies or substantial portions of the Software. | |
12 | // | |
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
19 | // THE SOFTWARE. | |
20 | ||
21 | /** | |
22 | * @fileoverview A general purpose object proxy that logs all method calls. | |
23 | * | |
24 | * @author konigsberg@google.com (Robert Konigsberg) | |
25 | */ | |
26 | ||
27 | var Proxy = function(delegate) { | |
28 | this.delegate__ = delegate; | |
29 | this.calls__ = []; | |
30 | this.propertiesToTrack__ = []; | |
31 | ||
32 | for (var propname in delegate) { | |
33 | var type = typeof(delegate[propname]); | |
34 | ||
35 | // Functions are passed through to the delegate, and are logged | |
36 | // prior to the call. | |
37 | if (type == "function") { | |
38 | function makeFunc(name) { | |
39 | return function() { | |
40 | this.log__(name, arguments); | |
30a5cfc6 | 41 | return this.delegate__[name].apply(this.delegate__, arguments); |
644eff8b RK |
42 | } |
43 | }; | |
44 | this[propname] = makeFunc(propname); | |
45 | } else if (type == "string" || type == "number") { | |
46 | // String and number properties are just passed through to the delegate. | |
47 | this.propertiesToTrack__.push(propname); | |
48 | function makeSetter(name) { | |
49 | return function(x) { | |
50 | this.delegate__[name] = x; | |
51 | } | |
52 | }; | |
53 | this.__defineSetter__(propname, makeSetter(propname)); | |
54 | ||
55 | function makeGetter(name) { | |
56 | return function() { | |
57 | return this.delegate__[name]; | |
58 | } | |
59 | }; | |
60 | this.__defineGetter__(propname, makeGetter(propname)); | |
61 | } | |
62 | } | |
63 | }; | |
64 | ||
65 | Proxy.prototype.log__ = function(name, args) { | |
66 | var properties = {}; | |
67 | for (var propIdx in this.propertiesToTrack__) { | |
68 | var prop = this.propertiesToTrack__[propIdx]; | |
69 | properties[prop] = this.delegate__[prop]; | |
70 | } | |
71 | var call = { name : name, args : args, properties: properties }; | |
72 | this.calls__.push(call); | |
73 | }; | |
d57dd6f2 RK |
74 | |
75 | Proxy.reset = function(proxy) { | |
76 | proxy.calls__ = []; | |
77 | } | |
e8c70e4e DV |
78 | |
79 | export default Proxy; |