@@ -18,6 +18,7 @@ class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) :
1818 ReactContextBaseJavaModule (reactContext) {
1919
2020 private var locateSubscription: HyperTrack .Cancellable ? = null
21+ private var subscriptions: List <HyperTrack .Cancellable >? = null
2122
2223 override fun getName (): String {
2324 return NAME
@@ -30,6 +31,13 @@ class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) :
3031 @ReactMethod
3132 fun addListener (eventName : String? ) {
3233 // called when RN app subscribes to an event
34+ /* *
35+ * We init listeners lazily to avoid calling any SDK method before dynamic publishable key
36+ * is set.
37+ */
38+ if (subscriptions == null ) {
39+ subscriptions = initListeners()
40+ }
3341 when (eventName) {
3442 EVENT_ERRORS -> {
3543 emitEvent(EVENT_ERRORS , serializeErrors(HyperTrack .errors).toWriteableArray())
@@ -48,11 +56,6 @@ class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) :
4856 // Keep: Required for RN built in Event Emitter Calls.
4957 }
5058
51- override fun initialize () {
52- super .initialize()
53- initListeners()
54- }
55-
5659 @ReactMethod
5760 fun removeListeners (type : Int? ) {
5861 // Keep: Required for RN built in Event Emitter Calls.
@@ -131,21 +134,31 @@ class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) :
131134 HyperTrackSdkWrapper .setName(args.toHashMap())
132135 }
133136
134- private fun initListeners () {
135- HyperTrack .subscribeToErrors {
136- emitEvent(EVENT_ERRORS , serializeErrors(it).toWriteableArray())
137- }
137+ private fun initListeners (): List <HyperTrack .Cancellable > {
138+ return mutableListOf<HyperTrack .Cancellable >().also { result ->
139+ HyperTrack .subscribeToErrors {
140+ emitEvent(EVENT_ERRORS , serializeErrors(it).toWriteableArray())
141+ }.also {
142+ result.add(it)
143+ }
138144
139- HyperTrack .subscribeToIsAvailable {
140- emitEvent(EVENT_IS_AVAILABLE , serializeIsAvailable(it).toWritableMap())
141- }
145+ HyperTrack .subscribeToIsAvailable {
146+ emitEvent(EVENT_IS_AVAILABLE , serializeIsAvailable(it).toWritableMap())
147+ }.also {
148+ result.add(it)
149+ }
142150
143- HyperTrack .subscribeToIsTracking {
144- emitEvent(EVENT_IS_TRACKING , serializeIsTracking(it).toWritableMap())
145- }
151+ HyperTrack .subscribeToIsTracking {
152+ emitEvent(EVENT_IS_TRACKING , serializeIsTracking(it).toWritableMap())
153+ }.also {
154+ result.add(it)
155+ }
146156
147- HyperTrack .subscribeToLocation {
148- emitEvent(EVENT_LOCATION , serializeLocationResult(it).toWritableMap())
157+ HyperTrack .subscribeToLocation {
158+ emitEvent(EVENT_LOCATION , serializeLocationResult(it).toWritableMap())
159+ }.also {
160+ result.add(it)
161+ }
149162 }
150163 }
151164
0 commit comments