X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;ds=sidebyside;f=auto_tests%2Ftests%2FProxy.js;fp=auto_tests%2Ftests%2FProxy.js;h=51a6e949fc8190c69154708a62ecfa696cf512f5;hb=644eff8b76ca1a39ac2841d4f76a46a32042f071;hp=0000000000000000000000000000000000000000;hpb=2cf95fff7f77658eccb3d00e7d52d081863c61ff;p=dygraphs.git diff --git a/auto_tests/tests/Proxy.js b/auto_tests/tests/Proxy.js new file mode 100644 index 0000000..51a6e94 --- /dev/null +++ b/auto_tests/tests/Proxy.js @@ -0,0 +1,74 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +/** + * @fileoverview A general purpose object proxy that logs all method calls. + * + * @author konigsberg@google.com (Robert Konigsberg) + */ + +var Proxy = function(delegate) { + this.delegate__ = delegate; + this.calls__ = []; + this.propertiesToTrack__ = []; + + for (var propname in delegate) { + var type = typeof(delegate[propname]); + + // Functions are passed through to the delegate, and are logged + // prior to the call. + if (type == "function") { + function makeFunc(name) { + return function() { + this.log__(name, arguments); + this.delegate__[name].apply(this.delegate__, arguments); + } + }; + this[propname] = makeFunc(propname); + } else if (type == "string" || type == "number") { + // String and number properties are just passed through to the delegate. + this.propertiesToTrack__.push(propname); + function makeSetter(name) { + return function(x) { + this.delegate__[name] = x; + } + }; + this.__defineSetter__(propname, makeSetter(propname)); + + function makeGetter(name) { + return function() { + return this.delegate__[name]; + } + }; + this.__defineGetter__(propname, makeGetter(propname)); + } + } +}; + +Proxy.prototype.log__ = function(name, args) { + var properties = {}; + for (var propIdx in this.propertiesToTrack__) { + var prop = this.propertiesToTrack__[propIdx]; + properties[prop] = this.delegate__[prop]; + } + var call = { name : name, args : args, properties: properties }; + this.calls__.push(call); +}; +