react-native-nitro-sse 1.0.1 → 1.0.2
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.
package/android/build.gradle
CHANGED
|
@@ -13,6 +13,9 @@ import okhttp3.sse.EventSourceListener
|
|
|
13
13
|
import okio.Buffer
|
|
14
14
|
import okio.ForwardingSource
|
|
15
15
|
import okio.buffer
|
|
16
|
+
import androidx.lifecycle.DefaultLifecycleObserver
|
|
17
|
+
import androidx.lifecycle.LifecycleOwner
|
|
18
|
+
import androidx.lifecycle.ProcessLifecycleOwner
|
|
16
19
|
import java.util.concurrent.TimeUnit
|
|
17
20
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
18
21
|
import java.util.concurrent.atomic.AtomicLong
|
|
@@ -30,9 +33,11 @@ import kotlin.random.Random
|
|
|
30
33
|
* prevent DoS-ing the server while ensuring resilient reconnections.
|
|
31
34
|
* 4. Heartbeat: Since OkHttp-SSE obscures comments, we use a Network Interceptor to manually
|
|
32
35
|
* detect ':' bytes, enabling JS-side watchdog timers.
|
|
36
|
+
* 5. Lifecycle Management: Implements the Hibernation pattern. When the app enters the background,
|
|
37
|
+
* we stop the stream to save battery. It automatically resumes when returning to foreground.
|
|
33
38
|
*/
|
|
34
39
|
@DoNotStrip
|
|
35
|
-
class NitroSse : HybridNitroSseSpec() {
|
|
40
|
+
class NitroSse : HybridNitroSseSpec(), DefaultLifecycleObserver {
|
|
36
41
|
private var client: OkHttpClient? = null
|
|
37
42
|
private var eventSource: EventSource? = null
|
|
38
43
|
private var config: SseConfig? = null
|
|
@@ -53,6 +58,8 @@ class NitroSse : HybridNitroSseSpec() {
|
|
|
53
58
|
private var reconnectCount = 0
|
|
54
59
|
private var lastErrorTime: Double? = null
|
|
55
60
|
private var lastErrorCode: String? = null
|
|
61
|
+
|
|
62
|
+
private var hasSubscribedToLifecycle = false
|
|
56
63
|
|
|
57
64
|
private val defaultRetryDelayMs = 3000L
|
|
58
65
|
private val baseBackoffDelayMs = 2000L
|
|
@@ -108,6 +115,29 @@ class NitroSse : HybridNitroSseSpec() {
|
|
|
108
115
|
sseHandlerThread = android.os.HandlerThread("NitroSseThread").apply { start() }
|
|
109
116
|
sseHandler = Handler(sseHandlerThread!!.looper)
|
|
110
117
|
}
|
|
118
|
+
|
|
119
|
+
if (!hasSubscribedToLifecycle) {
|
|
120
|
+
Handler(Looper.getMainLooper()).post {
|
|
121
|
+
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
|
122
|
+
hasSubscribedToLifecycle = true
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
override fun onStart(owner: LifecycleOwner) {
|
|
128
|
+
if (wasRunningBeforePaused) {
|
|
129
|
+
Log.d(TAG, "App foregrounded. Resuming NitroSse stream.")
|
|
130
|
+
wasRunningBeforePaused = false
|
|
131
|
+
start()
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
override fun onStop(owner: LifecycleOwner) {
|
|
136
|
+
if (isRunning.get()) {
|
|
137
|
+
Log.d(TAG, "App backgrounded. Hibernating NitroSse connection.")
|
|
138
|
+
wasRunningBeforePaused = true
|
|
139
|
+
stop()
|
|
140
|
+
}
|
|
111
141
|
}
|
|
112
142
|
|
|
113
143
|
private fun pushEventToBuffer(event: SseEvent) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-sse",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
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",
|