Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/debug/entrypointregistry.js
4500 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A global registry for entry points into a program,
9
* so that they can be instrumented. Each module should register their
10
* entry points with this registry. Designed to be compiled out
11
* if no instrumentation is requested.
12
*
13
* Entry points may be registered before or after a call to
14
* goog.debug.entryPointRegistry.monitorAll. If an entry point is registered
15
* later, the existing monitor will instrument the new entry point.
16
*/
17
18
goog.provide('goog.debug.EntryPointMonitor');
19
goog.provide('goog.debug.entryPointRegistry');
20
21
goog.require('goog.asserts');
22
23
24
/**
25
* @interface
26
*/
27
goog.debug.entryPointRegistry.EntryPointMonitor = function() {};
28
29
30
/**
31
* Instruments a function.
32
*
33
* @param {!Function} fn A function to instrument.
34
* @return {!Function} The instrumented function.
35
*/
36
goog.debug.entryPointRegistry.EntryPointMonitor.prototype.wrap;
37
38
39
/**
40
* Try to remove an instrumentation wrapper created by this monitor.
41
* If the function passed to unwrap is not a wrapper created by this
42
* monitor, then we will do nothing.
43
*
44
* Notice that some wrappers may not be unwrappable. For example, if other
45
* monitors have applied their own wrappers, then it will be impossible to
46
* unwrap them because their wrappers will have captured our wrapper.
47
*
48
* So it is important that entry points are unwrapped in the reverse
49
* order that they were wrapped.
50
*
51
* @param {!Function} fn A function to unwrap.
52
* @return {!Function} The unwrapped function, or `fn` if it was not
53
* a wrapped function created by this monitor.
54
*/
55
goog.debug.entryPointRegistry.EntryPointMonitor.prototype.unwrap;
56
57
/**
58
* Alias for goog.debug.entryPointRegistry.EntryPointMonitor, for compatibility
59
* purposes.
60
* @const
61
*/
62
goog.debug.EntryPointMonitor = goog.debug.entryPointRegistry.EntryPointMonitor;
63
64
65
/**
66
* An array of entry point callbacks.
67
* @type {!Array<function(!Function)>}
68
* @private
69
*/
70
goog.debug.entryPointRegistry.refList_ = [];
71
72
73
/**
74
* Monitors that should wrap all the entry points.
75
* @type {!Array<!goog.debug.EntryPointMonitor>}
76
* @private
77
*/
78
goog.debug.entryPointRegistry.monitors_ = [];
79
80
81
/**
82
* Whether goog.debug.entryPointRegistry.monitorAll has ever been called.
83
* Checking this allows the compiler to optimize out the registrations.
84
* @type {boolean}
85
* @private
86
*/
87
goog.debug.entryPointRegistry.monitorsMayExist_ = false;
88
89
90
/**
91
* Register an entry point with this module.
92
*
93
* The entry point will be instrumented when a monitor is passed to
94
* goog.debug.entryPointRegistry.monitorAll. If this has already occurred, the
95
* entry point is instrumented immediately.
96
*
97
* @param {function(!Function)} callback A callback function which is called
98
* with a transforming function to instrument the entry point. The callback
99
* is responsible for wrapping the relevant entry point with the
100
* transforming function.
101
*/
102
goog.debug.entryPointRegistry.register = function(callback) {
103
'use strict';
104
// Don't use push(), so that this can be compiled out.
105
goog.debug.entryPointRegistry
106
.refList_[goog.debug.entryPointRegistry.refList_.length] = callback;
107
// If no one calls monitorAll, this can be compiled out.
108
if (goog.debug.entryPointRegistry.monitorsMayExist_) {
109
var monitors = goog.debug.entryPointRegistry.monitors_;
110
for (var i = 0; i < monitors.length; i++) {
111
callback(goog.bind(monitors[i].wrap, monitors[i]));
112
}
113
}
114
};
115
116
117
/**
118
* Configures a monitor to wrap all entry points.
119
*
120
* Entry points that have already been registered are immediately wrapped by
121
* the monitor. When an entry point is registered in the future, it will also
122
* be wrapped by the monitor when it is registered.
123
*
124
* @param {!goog.debug.EntryPointMonitor} monitor An entry point monitor.
125
*/
126
goog.debug.entryPointRegistry.monitorAll = function(monitor) {
127
'use strict';
128
goog.debug.entryPointRegistry.monitorsMayExist_ = true;
129
var transformer = goog.bind(monitor.wrap, monitor);
130
for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {
131
goog.debug.entryPointRegistry.refList_[i](transformer);
132
}
133
goog.debug.entryPointRegistry.monitors_.push(monitor);
134
};
135
136
137
/**
138
* Try to unmonitor all the entry points that have already been registered. If
139
* an entry point is registered in the future, it will not be wrapped by the
140
* monitor when it is registered. Note that this may fail if the entry points
141
* have additional wrapping.
142
*
143
* @param {!goog.debug.EntryPointMonitor} monitor The last monitor to wrap
144
* the entry points.
145
* @throws {Error} If the monitor is not the most recently configured monitor.
146
*/
147
goog.debug.entryPointRegistry.unmonitorAllIfPossible = function(monitor) {
148
'use strict';
149
var monitors = goog.debug.entryPointRegistry.monitors_;
150
goog.asserts.assert(
151
monitor == monitors[monitors.length - 1],
152
'Only the most recent monitor can be unwrapped.');
153
var transformer = goog.bind(monitor.unwrap, monitor);
154
for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {
155
goog.debug.entryPointRegistry.refList_[i](transformer);
156
}
157
monitors.length--;
158
};
159
160