-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEventBridgeModule.kt
More file actions
57 lines (45 loc) · 1.74 KB
/
Copy pathEventBridgeModule.kt
File metadata and controls
57 lines (45 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.reactnativesmallcasegateway
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.modules.core.DeviceEventManagerModule
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import com.smallcase.gateway.portal.EventPublisher
import android.util.Log
class EventBridgeModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
companion object {
const val TAG = "EventBridgeModule"
}
override fun getName(): String {
return "EventBridge"
}
init {
observeEvents()
}
private fun observeEvents() {
scope.launch {
EventPublisher.eventFlow.collect { event ->
sendEventToReactNative(event)
}
}
}
private fun sendEventToReactNative(event: Map<String, Any>) {
val eventName = event["eventName"] as? String // Safe cast to String?
if (eventName != null) { // Check if the eventName is not null
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, event)
} else {
// Handle the case where the eventName is null, if necessary
Log.e("EventBridgeModule", "Event name is null!")
}
}
override fun onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy()
scope.cancel() // Clean up coroutine scope
}
}