@@ -50,32 +50,32 @@ public class NucleusState {
5050 ]
5151
5252 public init ( key: String , passableMetadata: [ String : Any ] , fetchPlayheadTime: @escaping ( ) -> Int ,
53- fetchVideoState: @escaping ( ) -> [ String : Any ] ) {
54- self . key = key
55- self . metadata = passableMetadata [ " data " ] as! [ String : Any ]
56- self . utilMethods = commonMethods
57- self . getVideoData = fetchVideoState
58- self . getCurrentPlayheadTime = fetchPlayheadTime
59- self . playerInitializationTime = commonMethods. now ( )
60- self . isVideoPlaying = false
61- self . isVideoBuffering = false
62- self . isVideoSeeking = false
63- self . isVideoErrorOccured = false
64- self . playerDestroyed = false
65- self . data = [
66- " player_sequence_number " : 1 ,
67- " view_sequence_number " : 1 ,
68- " player_instance_id " : commonMethods. getUUID ( ) . lowercased ( ) ,
69- " beacon_domain " : ( passableMetadata [ " configDomain " ] as? String ) ?? " metrix.ws " ,
70- " workspace_id " : ( self . metadata [ " workspace_id " ] as? String ) ?? " workspaceId "
71- ]
53+ fetchVideoState: @escaping ( ) -> [ String : Any ] ) {
54+ self . key = key
55+ self . metadata = passableMetadata [ " data " ] as! [ String : Any ]
56+ self . utilMethods = commonMethods
57+ self . getVideoData = fetchVideoState
58+ self . getCurrentPlayheadTime = fetchPlayheadTime
59+ self . playerInitializationTime = commonMethods. now ( )
60+ self . isVideoPlaying = false
61+ self . isVideoBuffering = false
62+ self . isVideoSeeking = false
63+ self . isVideoErrorOccured = false
64+ self . playerDestroyed = false
65+ self . data = [
66+ " player_sequence_number " : 1 ,
67+ " view_sequence_number " : 1 ,
68+ " player_instance_id " : commonMethods. getUUID ( ) . lowercased ( ) ,
69+ " beacon_domain " : ( passableMetadata [ " configDomain " ] as? String ) ?? " metrix.ws " ,
70+ " workspace_id " : ( self . metadata [ " workspace_id " ] as? String ) ?? " workspaceId "
71+ ]
7272
73- self . lastCheckedEventTime = 0
74- self . dispatchEvent ( event: " configureView " , eventmetadata: [ : ] )
75-
76- NotificationCenter . default. addObserver ( self , selector: #selector( appWillTerminate) , name: UIApplication . willTerminateNotification, object: nil )
77- NotificationCenter . default. addObserver ( self , selector: #selector( appWillTerminate) , name: UIScreen . didDisconnectNotification, object: nil )
78- }
73+ self . lastCheckedEventTime = 0
74+ self . dispatchEvent ( event: " configureView " , eventmetadata: [ : ] )
75+
76+ NotificationCenter . default. addObserver ( self , selector: #selector( appWillTerminate) , name: UIApplication . willTerminateNotification, object: nil )
77+ NotificationCenter . default. addObserver ( self , selector: #selector( appWillTerminate) , name: UIScreen . didDisconnectNotification, object: nil )
78+ }
7979
8080 func emitEvents( event: String , eventmetadata: [ String : Any ] ) {
8181 let currentTime = commonMethods. now ( )
@@ -245,7 +245,15 @@ public class NucleusState {
245245 func emitPulse( ) {
246246 dispatchEvent ( event: " pulse " , eventmetadata: [ : ] )
247247 }
248-
248+
249+ deinit {
250+ // Remove observer for app termination to avoid potential retain cycles or memory leaks
251+ NotificationCenter . default. removeObserver ( self , name: UIApplication . willTerminateNotification, object: nil )
252+
253+ // Remove observer for screen disconnection
254+ NotificationCenter . default. removeObserver ( self , name: UIScreen . didDisconnectNotification, object: nil )
255+ }
256+
249257 func refreshViewData( ) {
250258 self . data. keys. forEach { key in
251259 if key. hasPrefix ( " view_ " ) {
0 commit comments