@@ -29,18 +29,31 @@ internal object NativeCustomerIOModuleImpl {
2929 private val logger: Logger
3030 get() = SDKComponent .logger
3131
32- private fun customerIO (): CustomerIO ? = runCatching {
32+ // Returns CustomerIO instance if initialized, null otherwise, with configurable failure handling.
33+ private inline fun getSDKInstanceOrNull (
34+ onFailure : (exception: Throwable ) -> Unit = {}
35+ ): CustomerIO ? = runCatching {
3336 // If the SDK is not initialized, `CustomerIO.instance()` throws an exception
3437 CustomerIO .instance()
35- }.onFailure {
36- logger.error(" Customer.io instance not initialized" )
37- }.getOrNull()
38+ }.onFailure(onFailure).getOrNull()
39+
40+ // Returns CustomerIO instance if initialized, null otherwise, logging error on failure.
41+ private fun requireSDKInstance (): CustomerIO ? = getSDKInstanceOrNull {
42+ logger.error(" CustomerIO SDK is not initialized. Please call initialize() first." )
43+ }
3844
3945 fun initialize (
4046 reactContext : ReactApplicationContext ,
4147 sdkConfig : ReadableMap ? ,
4248 promise : Promise ?
4349 ) {
50+ // Skip initialization if already initialized
51+ if (getSDKInstanceOrNull() != null ) {
52+ logger.info(" CustomerIO SDK is already initialized. Skipping initialization." )
53+ promise?.resolve(true )
54+ return
55+ }
56+
4457 try {
4558 val packageConfig = sdkConfig.toMap()
4659 val cdpApiKey = packageConfig.getTypedValue<String >(
@@ -98,7 +111,7 @@ internal object NativeCustomerIOModuleImpl {
98111 }
99112
100113 fun clearIdentify () {
101- customerIO ()?.clearIdentify()
114+ requireSDKInstance ()?.clearIdentify()
102115 }
103116
104117 fun identify (params : ReadableMap ? ) {
@@ -111,39 +124,39 @@ internal object NativeCustomerIOModuleImpl {
111124 }
112125
113126 userId?.let {
114- customerIO ()?.identify(userId, traits.toMap())
127+ requireSDKInstance ()?.identify(userId, traits.toMap())
115128 } ? : run {
116- customerIO ()?.profileAttributes = traits.toMap()
129+ requireSDKInstance ()?.profileAttributes = traits.toMap()
117130 }
118131 }
119132
120133 fun track (name : String? , properties : ReadableMap ? ) {
121134 val eventName = assertNotNull(name) ? : return
122135
123- customerIO ()?.track(eventName, properties.toMap())
136+ requireSDKInstance ()?.track(eventName, properties.toMap())
124137 }
125138
126139 fun setDeviceAttributes (attributes : ReadableMap ? ) {
127- customerIO ()?.deviceAttributes = attributes.toMap()
140+ requireSDKInstance ()?.deviceAttributes = attributes.toMap()
128141 }
129142
130143 fun setProfileAttributes (attributes : ReadableMap ? ) {
131- customerIO ()?.profileAttributes = attributes.toMap()
144+ requireSDKInstance ()?.profileAttributes = attributes.toMap()
132145 }
133146
134147 fun screen (title : String? , properties : ReadableMap ? ) {
135148 val screenTitle = assertNotNull(title) ? : return
136149
137- customerIO ()?.screen(screenTitle, properties.toMap())
150+ requireSDKInstance ()?.screen(screenTitle, properties.toMap())
138151 }
139152
140153 fun registerDeviceToken (token : String? ) {
141154 val deviceToken = assertNotNull(token) ? : return
142155
143- customerIO ()?.registerDeviceToken(deviceToken)
156+ requireSDKInstance ()?.registerDeviceToken(deviceToken)
144157 }
145158
146159 fun deleteDeviceToken () {
147- customerIO ()?.deleteDeviceToken()
160+ requireSDKInstance ()?.deleteDeviceToken()
148161 }
149162}
0 commit comments