File tree Expand file tree Collapse file tree 3 files changed +26
-18
lines changed
packages/signals/signals/src/core Expand file tree Collapse file tree 3 files changed +26
-18
lines changed Original file line number Diff line number Diff line change @@ -8,19 +8,29 @@ export interface EmitSignal {
88
99export class SignalEmitter implements EmitSignal {
1010 private emitter = new Emitter < { add : [ Signal ] } > ( )
11+ private listeners = new Set < ( signal : Signal ) => void > ( )
1112
1213 emit ( signal : Signal ) {
1314 logger . debug ( 'New signal:' , signal . type , signal . data )
1415 this . emitter . emit ( 'add' , signal )
1516 }
1617
17- subscribe ( broadcaster : ( signal : Signal ) => void ) {
18- logger . debug ( 'subscribed' )
19- this . emitter . on ( 'add' , broadcaster )
18+ subscribe ( listener : ( signal : Signal ) => void ) {
19+ // Prevent duplicate subscriptions
20+ if ( ! this . listeners . has ( listener ) ) {
21+ logger . debug ( 'subscribed' )
22+ this . listeners . add ( listener )
23+ }
24+ this . emitter . on ( 'add' , listener )
2025 }
2126
2227 unsubscribe ( listener : ( signal : Signal ) => void ) {
28+ this . listeners . delete ( listener )
2329 logger . debug ( 'unsubscribed' )
2430 this . emitter . off ( 'add' , listener )
2531 }
32+
33+ once ( listener : ( signal : Signal ) => void ) {
34+ this . emitter . once ( 'add' , listener )
35+ }
2636}
Original file line number Diff line number Diff line change @@ -114,6 +114,7 @@ export class SignalsDebugSettings {
114114 private storageType = 'sessionStorage' as const
115115 private static redactionKey = 'segment_signals_debug_redaction_disabled'
116116 private static ingestionKey = 'segment_signals_debug_ingestion_enabled'
117+ private static logSignals = 'segment_signals_debug_log_signals_enabled'
117118
118119 constructor ( disableRedaction ?: boolean , enableIngestion ?: boolean ) {
119120 if ( typeof disableRedaction === 'boolean' ) {
@@ -133,6 +134,7 @@ export class SignalsDebugSettings {
133134 setAllDebugging = ( boolean : boolean ) => {
134135 this . setDebugKey ( SignalsDebugSettings . redactionKey , boolean )
135136 this . setDebugKey ( SignalsDebugSettings . ingestionKey , boolean )
137+ this . setDebugKey ( SignalsDebugSettings . logSignals , boolean )
136138 }
137139
138140 private setDebugKey = ( key : string , enable : boolean ) : void => {
@@ -168,4 +170,8 @@ export class SignalsDebugSettings {
168170 getEnableSignalsIngestion = ( ) : boolean => {
169171 return this . getDebugKey ( SignalsDebugSettings . ingestionKey )
170172 }
173+
174+ getEnableLogSignals = ( ) : boolean => {
175+ return this . getDebugKey ( SignalsDebugSettings . logSignals )
176+ }
171177}
Original file line number Diff line number Diff line change @@ -129,24 +129,16 @@ export class Signals implements ISignals {
129129 void this . buffer . clear ( )
130130 }
131131
132- /**
133- * Disable redaction and enable ingestion of signals.
134- */
135- debug ( ) : void {
136- this . globalSettings . signalsDebug . setAllDebugging ( true )
132+ // create a reference so we prevent duplicate subscriptions
133+ private logSignal = ( signal : Signal ) => {
134+ logger . log ( signal . type , signal . data , signal . metadata )
137135 }
138-
139136 /**
140- * Log signals to the console .
137+ * Disable redaction, ingestion of signals, and other debug logging .
141138 */
142- enableDebugLogging ( { signalsOnly } = { signalsOnly : true } ) : void {
143- if ( signalsOnly ) {
144- this . signalEmitter . subscribe ( ( signal ) => {
145- logger . log ( signal . type , signal . data )
146- } )
147- } else {
148- logger . enableDebugLogging ( )
149- }
139+ debug ( boolean = true ) : void {
140+ this . globalSettings . signalsDebug . setAllDebugging ( boolean )
141+ this . signalEmitter . subscribe ( this . logSignal )
150142 }
151143
152144 /**
You can’t perform that action at this time.
0 commit comments