Path: blob/trunk/third_party/closure/goog/debug/entrypointregistry.js
4500 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview A global registry for entry points into a program,8* so that they can be instrumented. Each module should register their9* entry points with this registry. Designed to be compiled out10* if no instrumentation is requested.11*12* Entry points may be registered before or after a call to13* goog.debug.entryPointRegistry.monitorAll. If an entry point is registered14* later, the existing monitor will instrument the new entry point.15*/1617goog.provide('goog.debug.EntryPointMonitor');18goog.provide('goog.debug.entryPointRegistry');1920goog.require('goog.asserts');212223/**24* @interface25*/26goog.debug.entryPointRegistry.EntryPointMonitor = function() {};272829/**30* Instruments a function.31*32* @param {!Function} fn A function to instrument.33* @return {!Function} The instrumented function.34*/35goog.debug.entryPointRegistry.EntryPointMonitor.prototype.wrap;363738/**39* Try to remove an instrumentation wrapper created by this monitor.40* If the function passed to unwrap is not a wrapper created by this41* monitor, then we will do nothing.42*43* Notice that some wrappers may not be unwrappable. For example, if other44* monitors have applied their own wrappers, then it will be impossible to45* unwrap them because their wrappers will have captured our wrapper.46*47* So it is important that entry points are unwrapped in the reverse48* order that they were wrapped.49*50* @param {!Function} fn A function to unwrap.51* @return {!Function} The unwrapped function, or `fn` if it was not52* a wrapped function created by this monitor.53*/54goog.debug.entryPointRegistry.EntryPointMonitor.prototype.unwrap;5556/**57* Alias for goog.debug.entryPointRegistry.EntryPointMonitor, for compatibility58* purposes.59* @const60*/61goog.debug.EntryPointMonitor = goog.debug.entryPointRegistry.EntryPointMonitor;626364/**65* An array of entry point callbacks.66* @type {!Array<function(!Function)>}67* @private68*/69goog.debug.entryPointRegistry.refList_ = [];707172/**73* Monitors that should wrap all the entry points.74* @type {!Array<!goog.debug.EntryPointMonitor>}75* @private76*/77goog.debug.entryPointRegistry.monitors_ = [];787980/**81* Whether goog.debug.entryPointRegistry.monitorAll has ever been called.82* Checking this allows the compiler to optimize out the registrations.83* @type {boolean}84* @private85*/86goog.debug.entryPointRegistry.monitorsMayExist_ = false;878889/**90* Register an entry point with this module.91*92* The entry point will be instrumented when a monitor is passed to93* goog.debug.entryPointRegistry.monitorAll. If this has already occurred, the94* entry point is instrumented immediately.95*96* @param {function(!Function)} callback A callback function which is called97* with a transforming function to instrument the entry point. The callback98* is responsible for wrapping the relevant entry point with the99* transforming function.100*/101goog.debug.entryPointRegistry.register = function(callback) {102'use strict';103// Don't use push(), so that this can be compiled out.104goog.debug.entryPointRegistry105.refList_[goog.debug.entryPointRegistry.refList_.length] = callback;106// If no one calls monitorAll, this can be compiled out.107if (goog.debug.entryPointRegistry.monitorsMayExist_) {108var monitors = goog.debug.entryPointRegistry.monitors_;109for (var i = 0; i < monitors.length; i++) {110callback(goog.bind(monitors[i].wrap, monitors[i]));111}112}113};114115116/**117* Configures a monitor to wrap all entry points.118*119* Entry points that have already been registered are immediately wrapped by120* the monitor. When an entry point is registered in the future, it will also121* be wrapped by the monitor when it is registered.122*123* @param {!goog.debug.EntryPointMonitor} monitor An entry point monitor.124*/125goog.debug.entryPointRegistry.monitorAll = function(monitor) {126'use strict';127goog.debug.entryPointRegistry.monitorsMayExist_ = true;128var transformer = goog.bind(monitor.wrap, monitor);129for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {130goog.debug.entryPointRegistry.refList_[i](transformer);131}132goog.debug.entryPointRegistry.monitors_.push(monitor);133};134135136/**137* Try to unmonitor all the entry points that have already been registered. If138* an entry point is registered in the future, it will not be wrapped by the139* monitor when it is registered. Note that this may fail if the entry points140* have additional wrapping.141*142* @param {!goog.debug.EntryPointMonitor} monitor The last monitor to wrap143* the entry points.144* @throws {Error} If the monitor is not the most recently configured monitor.145*/146goog.debug.entryPointRegistry.unmonitorAllIfPossible = function(monitor) {147'use strict';148var monitors = goog.debug.entryPointRegistry.monitors_;149goog.asserts.assert(150monitor == monitors[monitors.length - 1],151'Only the most recent monitor can be unwrapped.');152var transformer = goog.bind(monitor.unwrap, monitor);153for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {154goog.debug.entryPointRegistry.refList_[i](transformer);155}156monitors.length--;157};158159160