Path: blob/trunk/third_party/closure/goog/debug/entrypointregistry.js
2868 views
// Copyright 2010 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A global registry for entry points into a program,16* so that they can be instrumented. Each module should register their17* entry points with this registry. Designed to be compiled out18* if no instrumentation is requested.19*20* Entry points may be registered before or after a call to21* goog.debug.entryPointRegistry.monitorAll. If an entry point is registered22* later, the existing monitor will instrument the new entry point.23*24* @author [email protected] (Nick Santos)25*/2627goog.provide('goog.debug.EntryPointMonitor');28goog.provide('goog.debug.entryPointRegistry');2930goog.require('goog.asserts');31323334/**35* @interface36*/37goog.debug.EntryPointMonitor = function() {};383940/**41* Instruments a function.42*43* @param {!Function} fn A function to instrument.44* @return {!Function} The instrumented function.45*/46goog.debug.EntryPointMonitor.prototype.wrap;474849/**50* Try to remove an instrumentation wrapper created by this monitor.51* If the function passed to unwrap is not a wrapper created by this52* monitor, then we will do nothing.53*54* Notice that some wrappers may not be unwrappable. For example, if other55* monitors have applied their own wrappers, then it will be impossible to56* unwrap them because their wrappers will have captured our wrapper.57*58* So it is important that entry points are unwrapped in the reverse59* order that they were wrapped.60*61* @param {!Function} fn A function to unwrap.62* @return {!Function} The unwrapped function, or {@code fn} if it was not63* a wrapped function created by this monitor.64*/65goog.debug.EntryPointMonitor.prototype.unwrap;666768/**69* An array of entry point callbacks.70* @type {!Array<function(!Function)>}71* @private72*/73goog.debug.entryPointRegistry.refList_ = [];747576/**77* Monitors that should wrap all the entry points.78* @type {!Array<!goog.debug.EntryPointMonitor>}79* @private80*/81goog.debug.entryPointRegistry.monitors_ = [];828384/**85* Whether goog.debug.entryPointRegistry.monitorAll has ever been called.86* Checking this allows the compiler to optimize out the registrations.87* @type {boolean}88* @private89*/90goog.debug.entryPointRegistry.monitorsMayExist_ = false;919293/**94* Register an entry point with this module.95*96* The entry point will be instrumented when a monitor is passed to97* goog.debug.entryPointRegistry.monitorAll. If this has already occurred, the98* entry point is instrumented immediately.99*100* @param {function(!Function)} callback A callback function which is called101* with a transforming function to instrument the entry point. The callback102* is responsible for wrapping the relevant entry point with the103* transforming function.104*/105goog.debug.entryPointRegistry.register = function(callback) {106// Don't use push(), so that this can be compiled out.107goog.debug.entryPointRegistry108.refList_[goog.debug.entryPointRegistry.refList_.length] = callback;109// If no one calls monitorAll, this can be compiled out.110if (goog.debug.entryPointRegistry.monitorsMayExist_) {111var monitors = goog.debug.entryPointRegistry.monitors_;112for (var i = 0; i < monitors.length; i++) {113callback(goog.bind(monitors[i].wrap, monitors[i]));114}115}116};117118119/**120* Configures a monitor to wrap all entry points.121*122* Entry points that have already been registered are immediately wrapped by123* the monitor. When an entry point is registered in the future, it will also124* be wrapped by the monitor when it is registered.125*126* @param {!goog.debug.EntryPointMonitor} monitor An entry point monitor.127*/128goog.debug.entryPointRegistry.monitorAll = function(monitor) {129goog.debug.entryPointRegistry.monitorsMayExist_ = true;130var transformer = goog.bind(monitor.wrap, monitor);131for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {132goog.debug.entryPointRegistry.refList_[i](transformer);133}134goog.debug.entryPointRegistry.monitors_.push(monitor);135};136137138/**139* Try to unmonitor all the entry points that have already been registered. If140* an entry point is registered in the future, it will not be wrapped by the141* monitor when it is registered. Note that this may fail if the entry points142* have additional wrapping.143*144* @param {!goog.debug.EntryPointMonitor} monitor The last monitor to wrap145* the entry points.146* @throws {Error} If the monitor is not the most recently configured monitor.147*/148goog.debug.entryPointRegistry.unmonitorAllIfPossible = function(monitor) {149var monitors = goog.debug.entryPointRegistry.monitors_;150goog.asserts.assert(151monitor == monitors[monitors.length - 1],152'Only the most recent monitor can be unwrapped.');153var transformer = goog.bind(monitor.unwrap, monitor);154for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {155goog.debug.entryPointRegistry.refList_[i](transformer);156}157monitors.length--;158};159160161