Skip to content

fix: event name conflicts with other libs using RCTDeviceEventEmitter #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.2.1

* [FIXED] Fixed event name conflicts with other libs using RCTDeviceEventEmitter

## 1.2.0

* [CHANGED] Remove mutex locks in favor of storing callbacks so onAuthorizer does no longer freeze the app on iOS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pusherwebsocketreactnative

import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.modules.core.DeviceEventManagerModule

class PusherEventEmitter(private val context: ReactApplicationContext) {
companion object {
private const val EVENT_PREFIX = "PusherReactNative"
}

fun emit(eventName: String, params: Any?) {
val jsModule = this.context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
val pusherEventName = "${EVENT_PREFIX}:${eventName}"

if (params is Map<*, *>) {
jsModule.emit(pusherEventName, Arguments.makeNativeMap(params as Map<String, Any>))
}

if (params is String) {
jsModule.emit(pusherEventName, params)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.pusherwebsocketreactnative

import android.util.Log
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.google.gson.Gson
import com.pusher.client.ChannelAuthorizer
import com.pusher.client.Pusher
Expand All @@ -27,22 +26,12 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
private val authorizerMutex = mutableMapOf<String, Semaphore>()
private val authorizerResult = mutableMapOf<String, ReadableMap>()

private val pusherEventEmitter = PusherEventEmitter(reactContext)

override fun getName(): String {
return "PusherWebsocketReactNative"
}

private fun emitEvent(eventName: String, params: Any?) {
val jsModule = this.reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
if (params is Map<*, *>) {
jsModule.emit(eventName, Arguments.makeNativeMap(params as Map<String, Any>))
}

if (params is String) {
jsModule.emit(eventName, params)
}
}

@ReactMethod
fun addListener(eventName: String?) {
// Keep: Required for RN built in Event Emitter Calls.
Expand Down Expand Up @@ -148,7 +137,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
}

override fun authorize(channelName: String, socketId: String): String? {
emitEvent(
pusherEventEmitter.emit(
"onAuthorizer", mapOf(
"channelName" to channelName,
"socketId" to socketId
Expand All @@ -173,7 +162,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :

// Event handlers
override fun onConnectionStateChange(change: ConnectionStateChange) {
emitEvent(
pusherEventEmitter.emit(
"onConnectionStateChange", mapOf(
"previousState" to change.previousState.toString(),
"currentState" to change.currentState.toString()
Expand All @@ -184,7 +173,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
override fun onSubscriptionSucceeded(channelName: String) {
// For presence channels we wait for the onUsersInformationReceived event.
if (!channelName.startsWith("presence-")) {
emitEvent(
pusherEventEmitter.emit(
"onEvent", mapOf(
"channelName" to channelName,
"eventName" to "pusher_internal:subscription_succeeded",
Expand All @@ -195,7 +184,6 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
}

override fun onEvent(event: PusherEvent) {
// Log.i(TAG, "Received event with data: $event")
// The java sdk transforms some events from pusher_internal
// to pusher:... events, we translate them back.
val finalEvent = if (event.eventName === "pusher:subscription_count") {
Expand All @@ -207,7 +195,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
} else {
event
}
emitEvent(
pusherEventEmitter.emit(
"onEvent", mapOf(
"channelName" to finalEvent.channelName,
"eventName" to finalEvent.eventName,
Expand All @@ -218,8 +206,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
}

override fun onAuthenticationFailure(message: String, e: Exception) {
// Log.e(TAG, "Authentication failure due to $message, exception was $e")
emitEvent(
pusherEventEmitter.emit(
"onSubscriptionError", mapOf(
"message" to message,
"error" to e.toString()
Expand All @@ -228,7 +215,6 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
} // Other ChannelEventListener methods

override fun onUsersInformationReceived(channelName: String?, users: MutableSet<User>?) {
// Log.i(TAG, "Users received: $users")
val gson = Gson()
val channel = pusher!!.getPresenceChannel(channelName)
val hash = mutableMapOf<String, Any?>()
Expand All @@ -243,7 +229,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
"hash" to hash
)
)
emitEvent(
pusherEventEmitter.emit(
"onEvent", mapOf(
"channelName" to channelName,
"eventName" to "pusher_internal:subscription_succeeded",
Expand All @@ -254,8 +240,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
}

override fun onDecryptionFailure(event: String?, reason: String?) {
// Log.e(TAG, "Decryption failure due to $event, exception was $reason")
emitEvent(
pusherEventEmitter.emit(
"onDecryptionFailure", mapOf(
"event" to event,
"reason" to reason
Expand All @@ -264,9 +249,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
}

override fun userSubscribed(channelName: String, user: User) {
// Log.i(TAG, "A new user joined channel [$channelName]: ${user.id}, ${user.info}")
val gson = Gson()
emitEvent(
pusherEventEmitter.emit(
"onMemberAdded", mapOf(
"channelName" to channelName,
"user" to mapOf(
Expand All @@ -278,9 +262,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
}

override fun userUnsubscribed(channelName: String, user: User) {
// Log.i(TAG, "A user left channel [$channelName]: ${user.id}, ${user.info}")
val gson = Gson()
emitEvent(
pusherEventEmitter.emit(
"onMemberRemoved", mapOf(
"channelName" to channelName,
"user" to mapOf(
Expand All @@ -292,7 +275,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
} // Other ChannelEventListener methods

override fun onError(message: String, code: String?, e: Exception?) {
emitEvent(
pusherEventEmitter.emit(
"onError", mapOf(
"message" to message,
"code" to code,
Expand Down
Loading