react-native-nitro-sse 0.1.0-beta.3 → 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
|
|
70
|
-
.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
override fun
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
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")
|
package/ios/NitroSse.swift
CHANGED
|
@@ -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": "
|
|
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",
|
|
@@ -83,12 +83,12 @@
|
|
|
83
83
|
"eslint-plugin-prettier": "^5.5.4",
|
|
84
84
|
"jest": "^29.7.0",
|
|
85
85
|
"lefthook": "^2.0.3",
|
|
86
|
-
"nitrogen": "
|
|
86
|
+
"nitrogen": "0.33.8",
|
|
87
87
|
"prettier": "^2.8.8",
|
|
88
88
|
"react": "19.2.0",
|
|
89
89
|
"react-native": "0.83.0",
|
|
90
90
|
"react-native-builder-bob": "^0.40.17",
|
|
91
|
-
"react-native-nitro-modules": "
|
|
91
|
+
"react-native-nitro-modules": "0.33.8",
|
|
92
92
|
"release-it": "^19.0.4",
|
|
93
93
|
"turbo": "^2.5.6",
|
|
94
94
|
"typescript": "^5.9.2"
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"peerDependencies": {
|
|
97
97
|
"react": "*",
|
|
98
98
|
"react-native": "*",
|
|
99
|
-
"react-native-nitro-modules": "
|
|
99
|
+
"react-native-nitro-modules": "0.33.8"
|
|
100
100
|
},
|
|
101
101
|
"workspaces": [
|
|
102
102
|
"example"
|