44 */
55
66import { dispatchGlobalEvent } from '../../common/dispatch/global-event'
7- import { ee } from '../event-emitter/contextual-ee'
87import { registerHandler as defaultRegister } from '../event-emitter/register-handler'
98import { featurePriority } from '../../loaders/features/features'
109import { EventContext } from '../event-emitter/event-context'
1110
12- const registry = { }
13-
1411/**
1512 * Adds an entry to the centralized drain registry specifying that a particular agent has events of a particular named
1613 * event-group "bucket" that should be drained at the time the agent drains all its buffered events. Buffered events
1714 * accumulate because instrumentation begins as soon as possible, before the agent has finished lazy-loading the code
1815 * responsible for aggregating and reporting captured data.
19- * @param {string } agentIdentifier - A 16 character string uniquely identifying the agent .
16+ * @param {Object } agentRef - The agent reference object .
2017 * @param {string } group - The named "bucket" for the events this feature will be bucketing for later collection.
2118 */
22- export function registerDrain ( agentIdentifier , group ) {
19+ export function registerDrain ( agentRef , group ) {
20+ if ( ! agentRef ) return
2321 // Here `item` captures the registered properties of a feature-group: whether it is ready for its buffered events
2422 // to be drained (`staged`) and the `priority` order in which it should be drained relative to other feature groups.
2523 const item = { staged : false , priority : featurePriority [ group ] || 0 }
26- curateRegistry ( agentIdentifier )
27- if ( ! registry [ agentIdentifier ] . get ( group ) ) registry [ agentIdentifier ] . set ( group , item )
24+ if ( ! agentRef . runtime . drainRegistry . get ( group ) ) agentRef . runtime . drainRegistry . set ( group , item )
2825}
2926
3027/**
3128 * Removes an item from the registry and immediately re-checks if the registry is ready to "drain all"
32- * @param {* } agentIdentifier - A 16 character string uniquely identifying the agent .
29+ * @param {Object } agentRef - The agent reference object .
3330 * @param {* } group - The named "bucket" to be removed from the registry
3431 */
35- export function deregisterDrain ( agentIdentifier , group ) {
36- if ( ! agentIdentifier || ! registry [ agentIdentifier ] ) return
37- if ( registry [ agentIdentifier ] . get ( group ) ) registry [ agentIdentifier ] . delete ( group )
38- drainGroup ( agentIdentifier , group , false )
39- if ( registry [ agentIdentifier ] . size ) checkCanDrainAll ( agentIdentifier )
40- }
41-
42- /**
43- * Registers the specified agent with the centralized event buffer registry if it is not already registered.
44- * Agents without an identifier (as in the case of some tests) will be excluded from the registry.
45- * @param {string } agentIdentifier - A 16 character string uniquely identifying an agent.
46- */
47- function curateRegistry ( agentIdentifier ) {
48- if ( ! agentIdentifier ) throw new Error ( 'agentIdentifier required' )
49- if ( ! registry [ agentIdentifier ] ) registry [ agentIdentifier ] = new Map ( )
32+ export function deregisterDrain ( agentRef , group ) {
33+ if ( ! agentRef ) return
34+ const drainRegistry = agentRef . runtime . drainRegistry
35+ if ( ! drainRegistry ) return
36+ if ( drainRegistry . get ( group ) ) drainRegistry . delete ( group )
37+ drainGroup ( agentRef , group , false )
38+ if ( drainRegistry . size ) checkCanDrainAll ( agentRef )
5039}
5140
5241/**
5342 * Drain buffered events out of the event emitter. Each feature should have its events bucketed by "group" and drain
5443 * its own named group explicitly, when ready.
55- * @param {string } agentIdentifier - A unique 16 character ID corresponding to an instantiated agent .
44+ * @param {Object } agentRef - The agent reference object .
5645 * @param {string } featureName - A named group into which the feature's buffered events are bucketed.
5746 * @param {boolean } force - Whether to force the drain to occur immediately, bypassing the registry and staging logic.
5847 */
59- export function drain ( agentIdentifier = '' , featureName = 'feature' , force = false ) {
60- curateRegistry ( agentIdentifier )
48+ export function drain ( agentRef , featureName = 'feature' , force = false ) {
49+ if ( ! agentRef ) return
6150 // If the feature for the specified agent is not in the registry, that means the instrument file was bypassed.
6251 // This could happen in tests, or loaders that directly import the aggregator. In these cases it is safe to
6352 // drain the feature group immediately rather than waiting to drain all at once.
64- if ( ! agentIdentifier || ! registry [ agentIdentifier ] . get ( featureName ) || force ) return drainGroup ( agentIdentifier , featureName )
53+ if ( ! agentRef . runtime . drainRegistry . get ( featureName ) || force ) return drainGroup ( agentRef , featureName )
6554
6655 // When `drain` is called, this feature is ready to drain (staged).
67- registry [ agentIdentifier ] . get ( featureName ) . staged = true
56+ agentRef . runtime . drainRegistry . get ( featureName ) . staged = true
6857
69- checkCanDrainAll ( agentIdentifier )
58+ checkCanDrainAll ( agentRef )
7059}
7160
7261/** Checks all items in the registry to see if they have been "staged". If ALL items are staged, it will drain all registry items (drainGroup). It not, nothing will happen */
73- function checkCanDrainAll ( agentIdentifier ) {
62+ function checkCanDrainAll ( agentRef ) {
63+ if ( ! agentRef ) return
7464 // Only when the event-groups for all features are ready to drain (staged) do we execute the drain. This has the effect
7565 // that the last feature to call drain triggers drain for all features.
76- const items = Array . from ( registry [ agentIdentifier ] )
66+ const items = Array . from ( agentRef . runtime . drainRegistry )
7767 if ( items . every ( ( [ key , values ] ) => values . staged ) ) {
7868 items . sort ( ( a , b ) => a [ 1 ] . priority - b [ 1 ] . priority )
7969 items . forEach ( ( [ group ] ) => {
80- registry [ agentIdentifier ] . delete ( group )
81- drainGroup ( agentIdentifier , group )
70+ agentRef . runtime . drainRegistry . delete ( group )
71+ drainGroup ( agentRef , group )
8272 } )
8373 }
8474}
@@ -88,13 +78,13 @@ function checkCanDrainAll (agentIdentifier) {
8878 * the subscribed handlers for the group.
8979 * @param {* } group - The name of a particular feature's event "bucket".
9080 */
91- function drainGroup ( agentIdentifier , group , activateGroup = true ) {
92- const baseEE = agentIdentifier ? ee . get ( agentIdentifier ) : ee
81+ function drainGroup ( agentRef , group , activateGroup = true ) {
82+ if ( ! agentRef ) return
83+ const baseEE = agentRef . ee
9384 const handlers = defaultRegister . handlers // other storage in registerHandler
94- if ( baseEE . aborted || ! baseEE . backlog || ! handlers ) return
85+ if ( ! baseEE || baseEE . aborted || ! baseEE . backlog || ! handlers ) return
9586
9687 dispatchGlobalEvent ( {
97- agentIdentifier,
9888 type : 'lifecycle' ,
9989 name : 'drain' ,
10090 feature : group
0 commit comments