@@ -2,54 +2,13 @@ import CioMessagingInApp
22import Foundation
33import React
44
5- // Core in-app messaging implementation shared between new and old architecture
6- class NativeMessagingInAppImplementation {
7- private let inAppEventCallback : ( _ body: [ String : Any ] ) -> Void
8-
9- init ( inAppEventCallback: @escaping ( _ body: [ String : Any ] ) -> Void ) {
10- self . inAppEventCallback = inAppEventCallback
11- }
12-
13- // Initialize in-app event listener - called by React Native
14- func initialize( ) {
15- ReactInAppEventListener . shared. setEventEmitter { [ weak self] data in
16- guard let self else { return }
17-
18- // Filter out nil values to convert [String: Any?] to [String: Any]
19- let body = data. compactMapValues { $0 }
20- inAppEventCallback ( body)
21- }
22- // If MessagingInApp module has already been initialized, this sets the listener directly.
23- // If this method is called early, accessing ReactInAppEventListener.shared will also register
24- // it into the DI graph, making it available for access in Expo during auto-initialization.
25- MessagingInApp . shared. setEventListener ( ReactInAppEventListener . shared)
26- }
27-
28- // Clears the in-app event listener to prevent leaks when module is deallocated or invalidated
29- func clearInAppEventListener( ) {
30- ReactInAppEventListener . shared. clearEventEmitter ( )
31- }
32-
33- // Clear in-app event listener to prevent leaks
34- func invalidate( ) {
35- clearInAppEventListener ( )
36- }
37- }
38-
395// In-app messaging module for new React Native architecture (TurboModule)
406@objc ( NativeMessagingInApp)
417public class NativeMessagingInApp : NSObject {
42- private var implementation : NativeMessagingInAppImplementation !
438 // Reference to the ObjC event emitter for new architecture (TurboModule)
449 private weak var objcEventEmitter : AnyObject ?
45-
46- @objc
47- override public init ( ) {
48- super. init ( )
49-
50- self . implementation = . init( inAppEventCallback: { [ weak self] body in
51- self ? . sendEvent ( body: body)
52- } )
10+ private lazy var inAppEventCallback : ( _ body: [ String : Any ] ) -> Void = { [ weak self] body in
11+ self ? . sendEvent ( body: body)
5312 }
5413
5514 // Set ObjC event emitter reference for new architecture
@@ -61,13 +20,25 @@ public class NativeMessagingInApp: NSObject {
6120 // Initialize in-app event listener - called by React Native
6221 @objc
6322 public func initialize( ) {
64- implementation. initialize ( )
23+ // Initialize in-app event listener - called by React Native
24+ ReactInAppEventListener . shared. setEventEmitter { [ weak self] data in
25+ guard let self else { return }
26+
27+ // Filter out nil values to convert [String: Any?] to [String: Any]
28+ let body = data. compactMapValues { $0 }
29+ inAppEventCallback ( body)
30+ }
31+ // If MessagingInApp module has already been initialized, this sets the listener directly.
32+ // If this method is called early, accessing ReactInAppEventListener.shared will also register
33+ // it into the DI graph, making it available for access in Expo during auto-initialization.
34+ MessagingInApp . shared. setEventListener ( ReactInAppEventListener . shared)
6535 }
6636
6737 // Clear in-app event listener to prevent memory leaks - called by React Native
6838 @objc
6939 public func invalidate( ) {
70- implementation. invalidate ( )
40+ // Clear in-app event listener to prevent leaks
41+ clearInAppEventListener ( )
7142 }
7243
7344 /**
@@ -78,6 +49,11 @@ public class NativeMessagingInApp: NSObject {
7849 MessagingInApp . shared. dismissMessage ( )
7950 }
8051
52+ // Clears the in-app event listener to prevent leaks when module is deallocated or invalidated
53+ func clearInAppEventListener( ) {
54+ ReactInAppEventListener . shared. clearEventEmitter ( )
55+ }
56+
8157 // Send in-app event to React Native layer using ObjC event emitter
8258 private func sendEvent( body: [ String : Any ] ) {
8359 guard let emitter = objcEventEmitter else {
@@ -94,52 +70,3 @@ public class NativeMessagingInApp: NSObject {
9470 _ = emitter. perform ( selector, with: body as NSDictionary )
9571 }
9672}
97-
98- // Legacy in-app messaging module for old React Native architecture (pre-TurboModule)
99- @objc ( NativeMessagingInAppLegacy)
100- public class NativeMessagingInAppLegacy : RCTEventEmitter {
101- private var implementation : NativeMessagingInAppImplementation !
102-
103- @objc
104- override public init ( ) {
105- super. init ( )
106-
107- self . implementation = . init( inAppEventCallback: { [ weak self] body in
108- guard let self else { return }
109-
110- // Old architecture: use self as RCTEventEmitter
111- sendEvent ( withName: CustomerioConstants . inAppEventListener, body: body)
112- } )
113- initialize ( )
114- }
115-
116- deinit {
117- invalidate ( )
118- }
119-
120- // Initialize in-app event listener - called by React Native
121- @objc
122- public func initialize( ) {
123- implementation. initialize ( )
124- }
125-
126- @objc
127- override public func invalidate( ) {
128- implementation. invalidate ( )
129- super. invalidate ( )
130- }
131-
132- // Returns array of supported event names for RCTEventEmitter
133- // All in-app events are combined under a single event name
134- override public func supportedEvents( ) -> [ String ] ! {
135- [ CustomerioConstants . inAppEventListener]
136- }
137-
138- /**
139- * Dismisses any currently displayed in-app message
140- */
141- @objc
142- public func dismissMessage( ) {
143- MessagingInApp . shared. dismissMessage ( )
144- }
145- }
0 commit comments