react-native-nitro-sse 1.0.1 → 1.1.0

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.
@@ -117,4 +117,5 @@ dependencies {
117
117
  implementation project(":react-native-nitro-modules")
118
118
  implementation "com.squareup.okhttp3:okhttp:4.12.0"
119
119
  implementation "com.squareup.okhttp3:okhttp-sse:4.12.0"
120
+ implementation "androidx.lifecycle:lifecycle-process:2.8.7"
120
121
  }
@@ -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) {
@@ -56,7 +56,7 @@ namespace margelo::nitro::nitrosse::bridge::swift {
56
56
  return optional.has_value();
57
57
  }
58
58
  inline HttpMethod get_std__optional_HttpMethod_(const std::optional<HttpMethod>& optional) noexcept {
59
- return *optional;
59
+ return optional.value();
60
60
  }
61
61
 
62
62
  // pragma MARK: std::unordered_map<std::string, std::string>
@@ -96,7 +96,7 @@ namespace margelo::nitro::nitrosse::bridge::swift {
96
96
  return optional.has_value();
97
97
  }
98
98
  inline std::unordered_map<std::string, std::string> get_std__optional_std__unordered_map_std__string__std__string__(const std::optional<std::unordered_map<std::string, std::string>>& optional) noexcept {
99
- return *optional;
99
+ return optional.value();
100
100
  }
101
101
 
102
102
  // pragma MARK: std::optional<std::string>
@@ -111,7 +111,7 @@ namespace margelo::nitro::nitrosse::bridge::swift {
111
111
  return optional.has_value();
112
112
  }
113
113
  inline std::string get_std__optional_std__string_(const std::optional<std::string>& optional) noexcept {
114
- return *optional;
114
+ return optional.value();
115
115
  }
116
116
 
117
117
  // pragma MARK: std::optional<bool>
@@ -126,7 +126,7 @@ namespace margelo::nitro::nitrosse::bridge::swift {
126
126
  return optional.has_value();
127
127
  }
128
128
  inline bool get_std__optional_bool_(const std::optional<bool>& optional) noexcept {
129
- return *optional;
129
+ return optional.value();
130
130
  }
131
131
 
132
132
  // pragma MARK: std::optional<double>
@@ -141,7 +141,7 @@ namespace margelo::nitro::nitrosse::bridge::swift {
141
141
  return optional.has_value();
142
142
  }
143
143
  inline double get_std__optional_double_(const std::optional<double>& optional) noexcept {
144
- return *optional;
144
+ return optional.value();
145
145
  }
146
146
 
147
147
  // pragma MARK: std::vector<SseEvent>
@@ -120,11 +120,25 @@ public extension SseConfig {
120
120
 
121
121
  @inline(__always)
122
122
  var batchingIntervalMs: Double? {
123
- return self.__batchingIntervalMs.value
123
+ return { () -> Double? in
124
+ if bridge.has_value_std__optional_double_(self.__batchingIntervalMs) {
125
+ let __unwrapped = bridge.get_std__optional_double_(self.__batchingIntervalMs)
126
+ return __unwrapped
127
+ } else {
128
+ return nil
129
+ }
130
+ }()
124
131
  }
125
132
 
126
133
  @inline(__always)
127
134
  var maxBufferSize: Double? {
128
- return self.__maxBufferSize.value
135
+ return { () -> Double? in
136
+ if bridge.has_value_std__optional_double_(self.__maxBufferSize) {
137
+ let __unwrapped = bridge.get_std__optional_double_(self.__maxBufferSize)
138
+ return __unwrapped
139
+ } else {
140
+ return nil
141
+ }
142
+ }()
129
143
  }
130
144
  }
@@ -46,7 +46,14 @@ public extension SseStats {
46
46
 
47
47
  @inline(__always)
48
48
  var lastErrorTime: Double? {
49
- return self.__lastErrorTime.value
49
+ return { () -> Double? in
50
+ if bridge.has_value_std__optional_double_(self.__lastErrorTime) {
51
+ let __unwrapped = bridge.get_std__optional_double_(self.__lastErrorTime)
52
+ return __unwrapped
53
+ } else {
54
+ return nil
55
+ }
56
+ }()
50
57
  }
51
58
 
52
59
  @inline(__always)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-sse",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
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": "0.33.8",
86
+ "nitrogen": "0.33.9",
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": "0.33.8",
91
+ "react-native-nitro-modules": "0.33.9",
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": "0.33.8"
99
+ "react-native-nitro-modules": "0.33.9"
100
100
  },
101
101
  "workspaces": [
102
102
  "example"
@@ -176,4 +176,4 @@
176
176
  ],
177
177
  "version": "0.57.0"
178
178
  }
179
- }
179
+ }