@@ -32,8 +32,9 @@ export function registerDrain (agentIdentifier, group) {
32
32
* @param {* } group - The named "bucket" to be removed from the registry
33
33
*/
34
34
export function deregisterDrain ( agentIdentifier , group ) {
35
- curateRegistry ( agentIdentifier )
35
+ if ( ! agentIdentifier || ! registry [ agentIdentifier ] ) return
36
36
if ( registry [ agentIdentifier ] . get ( group ) ) registry [ agentIdentifier ] . delete ( group )
37
+ drainGroup ( agentIdentifier , group , false )
37
38
if ( registry [ agentIdentifier ] . size ) checkCanDrainAll ( agentIdentifier )
38
39
}
39
40
@@ -86,29 +87,33 @@ function checkCanDrainAll (agentIdentifier) {
86
87
* the subscribed handlers for the group.
87
88
* @param {* } group - The name of a particular feature's event "bucket".
88
89
*/
89
- function drainGroup ( agentIdentifier , group ) {
90
+ function drainGroup ( agentIdentifier , group , activateGroup = true ) {
90
91
const baseEE = agentIdentifier ? ee . get ( agentIdentifier ) : ee
91
92
const handlers = defaultRegister . handlers // other storage in registerHandler
92
93
if ( ! baseEE . backlog || ! handlers ) return
93
94
94
- var bufferedEventsInGroup = baseEE . backlog [ group ]
95
- var groupHandlers = handlers [ group ] // each group in the registerHandler storage
96
- if ( groupHandlers ) {
97
- // We don't cache the length of the buffer while looping because events might still be added while processing.
98
- for ( var i = 0 ; bufferedEventsInGroup && i < bufferedEventsInGroup . length ; ++ i ) { // eslint-disable-line no-unmodified-loop-condition
99
- emitEvent ( bufferedEventsInGroup [ i ] , groupHandlers )
100
- }
95
+ // Only activated features being drained should run queued listeners on buffered events. Deactivated features only need to release memory.
96
+ if ( activateGroup ) {
97
+ const bufferedEventsInGroup = baseEE . backlog [ group ]
98
+ const groupHandlers = handlers [ group ] // each group in the registerHandler storage
99
+ if ( groupHandlers ) {
100
+ // We don't cache the length of the buffer while looping because events might still be added while processing.
101
+ for ( let i = 0 ; bufferedEventsInGroup && i < bufferedEventsInGroup . length ; ++ i ) { // eslint-disable-line no-unmodified-loop-condition
102
+ emitEvent ( bufferedEventsInGroup [ i ] , groupHandlers )
103
+ }
101
104
102
- mapOwn ( groupHandlers , function ( eventType , handlerRegistrationList ) {
103
- mapOwn ( handlerRegistrationList , function ( i , registration ) {
104
- // registration is an array of: [targetEE, eventHandler]
105
- registration [ 0 ] . on ( eventType , registration [ 1 ] )
105
+ mapOwn ( groupHandlers , function ( eventType , handlerRegistrationList ) {
106
+ mapOwn ( handlerRegistrationList , function ( i , registration ) {
107
+ // registration is an array of: [targetEE, eventHandler]
108
+ registration [ 0 ] . on ( eventType , registration [ 1 ] )
109
+ } )
106
110
} )
107
- } )
111
+ }
108
112
}
113
+
109
114
if ( ! baseEE . isolatedBacklog ) delete handlers [ group ]
110
115
baseEE . backlog [ group ] = null
111
- baseEE . emit ( 'drain-' + group , [ ] )
116
+ baseEE . emit ( 'drain-' + group , [ ] ) // TODO: Code exists purely for a unit test and needs to be refined
112
117
}
113
118
114
119
/**
0 commit comments