react-native-nitro-sse 2.4.1 → 3.0.0-beta.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.
Files changed (59) hide show
  1. package/README.md +209 -95
  2. package/android/src/main/java/com/margelo/nitro/nitrosse/AndroidSseDispatcher.kt +5 -0
  3. package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +11 -18
  4. package/android/src/main/java/com/margelo/nitro/nitrosse/NetworkInspector.kt +79 -38
  5. package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +194 -100
  6. package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +40 -15
  7. package/android/src/main/java/com/margelo/nitro/nitrosse/SseDispatcher.kt +1 -0
  8. package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +34 -28
  9. package/android/src/main/java/com/margelo/nitro/nitrosse/SseLifecycleManager.kt +8 -1
  10. package/android/src/main/java/com/margelo/nitro/nitrosse/SseNetworkMonitor.kt +7 -4
  11. package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +6 -4
  12. package/ios/NitroSse.swift +244 -190
  13. package/ios/SseConfig+CopyWith.swift +2 -1
  14. package/ios/SseConnectionHandler.swift +59 -29
  15. package/ios/SseDispatchQueueDispatcher.swift +15 -2
  16. package/ios/SseDispatcher.swift +7 -1
  17. package/ios/SseEventBuffer.swift +23 -7
  18. package/ios/SseLifecycleManager.swift +10 -1
  19. package/ios/SseNetworkMonitor.swift +8 -2
  20. package/ios/SseReconnectStrategy.swift +9 -3
  21. package/lib/module/MockSseEngine.js +203 -0
  22. package/lib/module/NitroSseClient.js +411 -214
  23. package/lib/module/NitroSseError.js +61 -0
  24. package/lib/module/SseDriver.js +156 -0
  25. package/lib/module/index.js +6 -3
  26. package/lib/module/useNitroSse.js +359 -0
  27. package/lib/typescript/src/MockSseEngine.d.ts +31 -0
  28. package/lib/typescript/src/NitroSse.nitro.d.ts +25 -2
  29. package/lib/typescript/src/NitroSseClient.d.ts +25 -7
  30. package/lib/typescript/src/NitroSseError.d.ts +38 -0
  31. package/lib/typescript/src/SseDriver.d.ts +80 -0
  32. package/lib/typescript/src/SseInterface.d.ts +25 -7
  33. package/lib/typescript/src/index.d.ts +4 -2
  34. package/lib/typescript/src/useNitroSse.d.ts +69 -0
  35. package/nitrogen/generated/android/c++/JSseConfig.hpp +7 -1
  36. package/nitrogen/generated/android/c++/JSseMockConfig.hpp +2 -0
  37. package/nitrogen/generated/android/c++/JSseMockEvent.hpp +9 -3
  38. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +9 -4
  39. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockEvent.kt +9 -4
  40. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +15 -15
  41. package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +3 -3
  42. package/nitrogen/generated/ios/swift/SseConfig.swift +19 -1
  43. package/nitrogen/generated/ios/swift/SseMockEvent.swift +12 -1
  44. package/nitrogen/generated/shared/c++/SseConfig.hpp +5 -1
  45. package/nitrogen/generated/shared/c++/SseMockEvent.hpp +9 -2
  46. package/package.json +10 -11
  47. package/android/jacoco.gradle +0 -33
  48. package/lib/module/NitroSse.nitro.js.map +0 -1
  49. package/lib/module/NitroSseClient.js.map +0 -1
  50. package/lib/module/SseInterface.js.map +0 -1
  51. package/lib/module/index.js.map +0 -1
  52. package/lib/typescript/src/NitroSse.nitro.d.ts.map +0 -1
  53. package/lib/typescript/src/NitroSseClient.d.ts.map +0 -1
  54. package/lib/typescript/src/SseInterface.d.ts.map +0 -1
  55. package/lib/typescript/src/index.d.ts.map +0 -1
  56. package/src/NitroSse.nitro.ts +0 -58
  57. package/src/NitroSseClient.ts +0 -337
  58. package/src/SseInterface.ts +0 -272
  59. package/src/index.ts +0 -31
@@ -1,21 +1,17 @@
1
1
  package com.margelo.nitro.nitrosse
2
2
 
3
3
  import android.util.Log
4
- import com.margelo.nitro.core.AnyMap
5
- import org.json.JSONArray
6
- import org.json.JSONObject
7
4
  import java.util.concurrent.atomic.AtomicBoolean
8
5
 
9
6
  /**
10
7
  * Thread-safe event buffer that accumulates incoming SSE events and flushes them in batches.
11
8
  *
12
9
  * Batching reduces cross-bridge serialization calls to the JavaScript engine during high-frequency
13
- * streaming, while ensuring events are delivered on the main UI thread via [mainDispatcher].
10
+ * streaming, executing directly on the background [dispatcher] to prevent blocking the Android Main UI thread.
14
11
  */
15
12
  class SseEventBuffer(
16
13
  onFlush: (Array<SseEvent>) -> Unit,
17
- private val dispatcher: SseDispatcher?,
18
- private val mainDispatcher: SseDispatcher? = null
14
+ private val dispatcher: SseDispatcher?
19
15
  ) {
20
16
  @Volatile
21
17
  private var onFlush: (Array<SseEvent>) -> Unit = onFlush
@@ -42,6 +38,23 @@ class SseEventBuffer(
42
38
  }
43
39
 
44
40
  fun push(event: SseEvent) {
41
+ if (batchingIntervalMs <= 0.0) {
42
+ if (dispatcher == null || dispatcher.isCurrentDispatcher()) {
43
+ synchronized(eventBuffer) {
44
+ eventBuffer.add(event)
45
+ }
46
+ flush()
47
+ } else {
48
+ dispatcher.post {
49
+ synchronized(eventBuffer) {
50
+ eventBuffer.add(event)
51
+ }
52
+ flush()
53
+ }
54
+ }
55
+ return
56
+ }
57
+
45
58
  var shouldFlush = false
46
59
  synchronized(eventBuffer) {
47
60
  eventBuffer.add(event)
@@ -50,22 +63,23 @@ class SseEventBuffer(
50
63
  }
51
64
  }
52
65
 
53
- dispatcher?.post {
54
- if (batchingIntervalMs <= 0 || shouldFlush) {
55
- dispatcher.removeCallbacks(flushRunnable)
56
- flush()
57
- } else if (!isFlushPending.getAndSet(true)) {
58
- dispatcher.postDelayed(flushRunnable, batchingIntervalMs.toLong())
59
- }
60
- } ?: run {
61
- // Direct synchronous flush fallback when background dispatcher is absent
62
- if (shouldFlush || batchingIntervalMs <= 0) {
66
+ if (shouldFlush) {
67
+ if (dispatcher == null || dispatcher.isCurrentDispatcher()) {
68
+ dispatcher?.removeCallbacks(flushRunnable)
63
69
  flush()
70
+ } else {
71
+ dispatcher.post {
72
+ dispatcher.removeCallbacks(flushRunnable)
73
+ flush()
74
+ }
64
75
  }
76
+ } else if (!isFlushPending.getAndSet(true)) {
77
+ dispatcher?.postDelayed(flushRunnable, batchingIntervalMs.toLong())
65
78
  }
66
79
  }
67
80
 
68
81
  fun flush() {
82
+ dispatcher?.removeCallbacks(flushRunnable)
69
83
  val eventsToEmit: Array<SseEvent>
70
84
  synchronized(eventBuffer) {
71
85
  if (eventBuffer.isEmpty()) {
@@ -77,18 +91,10 @@ class SseEventBuffer(
77
91
  isFlushPending.set(false)
78
92
  }
79
93
 
80
- mainDispatcher?.post {
81
- try {
82
- onFlush(eventsToEmit)
83
- } catch (e: Exception) {
84
- Log.e("SseEventBuffer", "Error invoking onFlush: ${e.message}")
85
- }
86
- } ?: run {
87
- try {
88
- onFlush(eventsToEmit)
89
- } catch (e: Exception) {
90
- Log.e("SseEventBuffer", "Error invoking onFlush (fallback): ${e.message}")
91
- }
94
+ try {
95
+ onFlush(eventsToEmit)
96
+ } catch (e: Exception) {
97
+ Log.e("SseEventBuffer", "Error invoking onFlush: ${e.message}")
92
98
  }
93
99
  }
94
100
 
@@ -1,6 +1,7 @@
1
1
  package com.margelo.nitro.nitrosse
2
2
 
3
3
  import androidx.lifecycle.DefaultLifecycleObserver
4
+ import androidx.lifecycle.Lifecycle
4
5
  import androidx.lifecycle.LifecycleOwner
5
6
  import java.util.concurrent.atomic.AtomicBoolean
6
7
 
@@ -11,7 +12,7 @@ import java.util.concurrent.atomic.AtomicBoolean
11
12
  * is explicitly configured) to conserve mobile network bandwidth and battery.
12
13
  */
13
14
  class SseLifecycleManager(
14
- private val lifecycleProvider: () -> androidx.lifecycle.Lifecycle,
15
+ private val lifecycleProvider: () -> Lifecycle,
15
16
  private val mainDispatcher: SseDispatcher,
16
17
  private val sseDispatcher: SseDispatcher,
17
18
  private val onBackground: () -> Unit,
@@ -24,6 +25,9 @@ class SseLifecycleManager(
24
25
 
25
26
  private val _hasSubscribed = AtomicBoolean(false)
26
27
 
28
+ /**
29
+ * Registers the lifecycle observer with the application lifecycle provider on the main thread.
30
+ */
27
31
  fun startObserving() {
28
32
  if (_hasSubscribed.compareAndSet(false, true)) {
29
33
  mainDispatcher.post {
@@ -32,6 +36,9 @@ class SseLifecycleManager(
32
36
  }
33
37
  }
34
38
 
39
+ /**
40
+ * Unregisters the lifecycle observer from the application lifecycle provider.
41
+ */
35
42
  fun stopObserving() {
36
43
  if (_hasSubscribed.compareAndSet(true, false)) {
37
44
  mainDispatcher.post {
@@ -38,17 +38,20 @@ class SseNetworkMonitor(
38
38
  }
39
39
 
40
40
  val callback = object : ConnectivityManager.NetworkCallback() {
41
+ private fun dispatchChange(isAvailable: Boolean, capabilities: NetworkCapabilities?) {
42
+ dispatcher?.post { onChange(isAvailable, capabilities) } ?: onChange(isAvailable, capabilities)
43
+ }
44
+
41
45
  override fun onAvailable(network: Network) {
42
- val capabilities = connectivityManager.getNetworkCapabilities(network)
43
- dispatcher?.post { onChange(true, capabilities) } ?: onChange(true, capabilities)
46
+ dispatchChange(true, connectivityManager.getNetworkCapabilities(network))
44
47
  }
45
48
 
46
49
  override fun onLost(network: Network) {
47
- dispatcher?.post { onChange(false, null) } ?: onChange(false, null)
50
+ dispatchChange(false, null)
48
51
  }
49
52
 
50
53
  override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) {
51
- dispatcher?.post { onChange(true, capabilities) } ?: onChange(true, capabilities)
54
+ dispatchChange(true, capabilities)
52
55
  }
53
56
  }
54
57
 
@@ -7,10 +7,8 @@ import kotlin.math.pow
7
7
  import kotlin.random.Random
8
8
 
9
9
  /**
10
- * Calculates retry backoff intervals for reconnecting failed SSE connections.
11
- *
12
- * Employs exponential backoff with randomized jitter to prevent thundering herd pressure on backend
13
- * servers during outage recovery, and parses standard HTTP `Retry-After` headers.
10
+ * Calculates retry backoff intervals using randomized jitter exponential backoff.
11
+ * Maintains mathematical parity with iOS SseReconnectStrategy.
14
12
  */
15
13
  class SseReconnectStrategy {
16
14
  private var retryIntervalMs: Double = 1000.0
@@ -59,6 +57,10 @@ class SseReconnectStrategy {
59
57
  return _currentReconnectAttempts.get() >= maxReconnectAttempts
60
58
  }
61
59
 
60
+ fun recordAttempt(): Int {
61
+ return _currentReconnectAttempts.incrementAndGet()
62
+ }
63
+
62
64
  fun reset() {
63
65
  backoffCounter = 0
64
66
  _currentReconnectAttempts.set(0)