react-native-nitro-sse 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,41 +66,43 @@ class NitroSse : HybridNitroSseSpec() {
66
66
  this.config = config
67
67
  this.onEventsCallback = onEvent
68
68
 
69
- this.client = OkHttpClient.Builder()
70
- .connectTimeout(15, TimeUnit.SECONDS)
71
- .readTimeout(35, TimeUnit.SECONDS)
72
- .addNetworkInterceptor { chain ->
73
- val response = chain.proceed(chain.request())
74
- val responseBody = response.body
75
- if (responseBody != null) {
76
- val countingBody = object : ResponseBody() {
77
- override fun contentType() = responseBody.contentType()
78
- override fun contentLength() = responseBody.contentLength()
79
- override fun source() = (object : okio.ForwardingSource(responseBody.source()) {
80
- override fun read(sink: okio.Buffer, byteCount: Long): Long {
81
- val bytesRead = super.read(sink, byteCount)
82
- if (bytesRead != -1L) {
83
- totalBytesReceived.addAndGet(bytesRead)
84
-
85
- try {
86
- val snapshot = sink.snapshot()
87
- if (snapshot.size > 0 && snapshot.get(0) == ':'.toByte()) {
88
- pushEventToBuffer(SseEvent(SseEventType.HEARTBEAT, null, null, null, "keep-alive"))
69
+ if (this.client == null) {
70
+ this.client = OkHttpClient.Builder()
71
+ .connectTimeout(15, TimeUnit.SECONDS)
72
+ .readTimeout(35, TimeUnit.SECONDS)
73
+ .addNetworkInterceptor { chain ->
74
+ val response = chain.proceed(chain.request())
75
+ val responseBody = response.body
76
+ if (responseBody != null) {
77
+ val countingBody = object : ResponseBody() {
78
+ override fun contentType() = responseBody.contentType()
79
+ override fun contentLength() = responseBody.contentLength()
80
+ override fun source() = (object : okio.ForwardingSource(responseBody.source()) {
81
+ override fun read(sink: okio.Buffer, byteCount: Long): Long {
82
+ val bytesRead = super.read(sink, byteCount)
83
+ if (bytesRead != -1L) {
84
+ totalBytesReceived.addAndGet(bytesRead)
85
+
86
+ try {
87
+ val snapshot = sink.snapshot()
88
+ if (snapshot.size > 0 && snapshot.get(0) == ':'.toByte()) {
89
+ pushEventToBuffer(SseEvent(SseEventType.HEARTBEAT, null, null, null, "keep-alive"))
90
+ }
91
+ } catch (e: Exception) {
92
+ // Silent catch for interceptor parsing
89
93
  }
90
- } catch (e: Exception) {
91
- // Silent catch for interceptor parsing
92
94
  }
95
+ return bytesRead
93
96
  }
94
- return bytesRead
95
- }
96
- }).buffer()
97
+ }).buffer()
98
+ }
99
+ response.newBuilder().body(countingBody).build()
100
+ } else {
101
+ response
97
102
  }
98
- response.newBuilder().body(countingBody).build()
99
- } else {
100
- response
101
103
  }
102
- }
103
- .build()
104
+ .build()
105
+ }
104
106
 
105
107
  if (sseHandlerThread == null) {
106
108
  sseHandlerThread = android.os.HandlerThread("NitroSseThread").apply { start() }
@@ -163,7 +165,7 @@ class NitroSse : HybridNitroSseSpec() {
163
165
  }
164
166
 
165
167
  override fun start() {
166
- if (config == null) return
168
+ if (config == null || isRunning.get()) return
167
169
  isRunning.set(true)
168
170
  backoffCounter = 0
169
171
  sseHandler?.post { performConnection() }
@@ -172,6 +174,10 @@ class NitroSse : HybridNitroSseSpec() {
172
174
  private fun performConnection() {
173
175
  if (!isRunning.get()) return
174
176
 
177
+ // Cancel existing event source if any before starting new one
178
+ eventSource?.cancel()
179
+ eventSource = null
180
+
175
181
  val requestBuilder = Request.Builder()
176
182
  .url(config!!.url)
177
183
  .header("Accept", "text/event-stream")
@@ -43,6 +43,7 @@ class NitroSse: HybridNitroSseSpec, EventHandler {
43
43
  self.config = config
44
44
  self.onEventsCallback = onEvent
45
45
 
46
+ NotificationCenter.default.removeObserver(self)
46
47
  NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
47
48
  NotificationCenter.default.addObserver(self, selector: #selector(handleAppWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
48
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-sse",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "High-performance Server-Sent Events (SSE) for React Native, powered by Nitro Modules.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",