@@ -2,7 +2,6 @@ package com.pusherwebsocketreactnative
2
2
3
3
import android.util.Log
4
4
import com.facebook.react.bridge.*
5
- import com.facebook.react.modules.core.DeviceEventManagerModule
6
5
import com.google.gson.Gson
7
6
import com.pusher.client.ChannelAuthorizer
8
7
import com.pusher.client.Pusher
@@ -27,22 +26,12 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
27
26
private val authorizerMutex = mutableMapOf<String , Semaphore >()
28
27
private val authorizerResult = mutableMapOf<String , ReadableMap >()
29
28
29
+ private val pusherEventEmitter = PusherEventEmitter (reactContext)
30
+
30
31
override fun getName (): String {
31
32
return " PusherWebsocketReactNative"
32
33
}
33
34
34
- private fun emitEvent (eventName : String , params : Any? ) {
35
- val jsModule = this .reactApplicationContext
36
- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
37
- if (params is Map <* , * >) {
38
- jsModule.emit(eventName, Arguments .makeNativeMap(params as Map <String , Any >))
39
- }
40
-
41
- if (params is String ) {
42
- jsModule.emit(eventName, params)
43
- }
44
- }
45
-
46
35
@ReactMethod
47
36
fun addListener (eventName : String? ) {
48
37
// Keep: Required for RN built in Event Emitter Calls.
@@ -148,7 +137,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
148
137
}
149
138
150
139
override fun authorize (channelName : String , socketId : String ): String? {
151
- emitEvent (
140
+ pusherEventEmitter.emit (
152
141
" onAuthorizer" , mapOf (
153
142
" channelName" to channelName,
154
143
" socketId" to socketId
@@ -173,7 +162,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
173
162
174
163
// Event handlers
175
164
override fun onConnectionStateChange (change : ConnectionStateChange ) {
176
- emitEvent (
165
+ pusherEventEmitter.emit (
177
166
" onConnectionStateChange" , mapOf (
178
167
" previousState" to change.previousState.toString(),
179
168
" currentState" to change.currentState.toString()
@@ -184,7 +173,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
184
173
override fun onSubscriptionSucceeded (channelName : String ) {
185
174
// For presence channels we wait for the onUsersInformationReceived event.
186
175
if (! channelName.startsWith(" presence-" )) {
187
- emitEvent (
176
+ pusherEventEmitter.emit (
188
177
" onEvent" , mapOf (
189
178
" channelName" to channelName,
190
179
" eventName" to " pusher_internal:subscription_succeeded" ,
@@ -195,7 +184,6 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
195
184
}
196
185
197
186
override fun onEvent (event : PusherEvent ) {
198
- // Log.i(TAG, "Received event with data: $event")
199
187
// The java sdk transforms some events from pusher_internal
200
188
// to pusher:... events, we translate them back.
201
189
val finalEvent = if (event.eventName == = " pusher:subscription_count" ) {
@@ -207,7 +195,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
207
195
} else {
208
196
event
209
197
}
210
- emitEvent (
198
+ pusherEventEmitter.emit (
211
199
" onEvent" , mapOf (
212
200
" channelName" to finalEvent.channelName,
213
201
" eventName" to finalEvent.eventName,
@@ -218,8 +206,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
218
206
}
219
207
220
208
override fun onAuthenticationFailure (message : String , e : Exception ) {
221
- // Log.e(TAG, "Authentication failure due to $message, exception was $e")
222
- emitEvent(
209
+ pusherEventEmitter.emit(
223
210
" onSubscriptionError" , mapOf (
224
211
" message" to message,
225
212
" error" to e.toString()
@@ -228,7 +215,6 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
228
215
} // Other ChannelEventListener methods
229
216
230
217
override fun onUsersInformationReceived (channelName : String? , users : MutableSet <User >? ) {
231
- // Log.i(TAG, "Users received: $users")
232
218
val gson = Gson ()
233
219
val channel = pusher!! .getPresenceChannel(channelName)
234
220
val hash = mutableMapOf<String , Any ?>()
@@ -243,7 +229,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
243
229
" hash" to hash
244
230
)
245
231
)
246
- emitEvent (
232
+ pusherEventEmitter.emit (
247
233
" onEvent" , mapOf (
248
234
" channelName" to channelName,
249
235
" eventName" to " pusher_internal:subscription_succeeded" ,
@@ -254,8 +240,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
254
240
}
255
241
256
242
override fun onDecryptionFailure (event : String? , reason : String? ) {
257
- // Log.e(TAG, "Decryption failure due to $event, exception was $reason")
258
- emitEvent(
243
+ pusherEventEmitter.emit(
259
244
" onDecryptionFailure" , mapOf (
260
245
" event" to event,
261
246
" reason" to reason
@@ -264,9 +249,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
264
249
}
265
250
266
251
override fun userSubscribed (channelName : String , user : User ) {
267
- // Log.i(TAG, "A new user joined channel [$channelName]: ${user.id}, ${user.info}")
268
252
val gson = Gson ()
269
- emitEvent (
253
+ pusherEventEmitter.emit (
270
254
" onMemberAdded" , mapOf (
271
255
" channelName" to channelName,
272
256
" user" to mapOf (
@@ -278,9 +262,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
278
262
}
279
263
280
264
override fun userUnsubscribed (channelName : String , user : User ) {
281
- // Log.i(TAG, "A user left channel [$channelName]: ${user.id}, ${user.info}")
282
265
val gson = Gson ()
283
- emitEvent (
266
+ pusherEventEmitter.emit (
284
267
" onMemberRemoved" , mapOf (
285
268
" channelName" to channelName,
286
269
" user" to mapOf (
@@ -292,7 +275,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
292
275
} // Other ChannelEventListener methods
293
276
294
277
override fun onError (message : String , code : String? , e : Exception ? ) {
295
- emitEvent (
278
+ pusherEventEmitter.emit (
296
279
" onError" , mapOf (
297
280
" message" to message,
298
281
" code" to code,
0 commit comments