react-native-nitro-sse 1.6.1 → 2.0.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.
Files changed (32) hide show
  1. package/README.md +1 -0
  2. package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +193 -90
  3. package/android/src/test/java/com/margelo/nitro/nitrosse/NitroSseLogicTest.kt +163 -82
  4. package/ios/NitroSse.swift +49 -12
  5. package/ios/Tests/NitroSseTests.swift +191 -71
  6. package/lib/module/NitroSseClient.js +75 -0
  7. package/lib/module/NitroSseClient.js.map +1 -0
  8. package/lib/module/index.js +9 -1
  9. package/lib/module/index.js.map +1 -1
  10. package/lib/typescript/src/NitroSseClient.d.ts +21 -0
  11. package/lib/typescript/src/NitroSseClient.d.ts.map +1 -0
  12. package/lib/typescript/src/SseInterface.d.ts +64 -1
  13. package/lib/typescript/src/SseInterface.d.ts.map +1 -1
  14. package/lib/typescript/src/index.d.ts +8 -2
  15. package/lib/typescript/src/index.d.ts.map +1 -1
  16. package/nitro.json +8 -2
  17. package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.hpp +2 -2
  18. package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +2 -2
  19. package/nitrogen/generated/android/c++/JHttpMethod.hpp +1 -1
  20. package/nitrogen/generated/android/c++/JHybridNitroSseSpec.hpp +2 -2
  21. package/nitrogen/generated/android/c++/JSseConfig.hpp +18 -2
  22. package/nitrogen/generated/android/c++/JSseEvent.hpp +1 -1
  23. package/nitrogen/generated/android/c++/JSseEventType.hpp +1 -1
  24. package/nitrogen/generated/android/c++/JSseStats.hpp +1 -1
  25. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +16 -4
  26. package/nitrogen/generated/android/nitrosseOnLoad.cpp +2 -2
  27. package/nitrogen/generated/ios/swift/SseConfig.swift +73 -1
  28. package/nitrogen/generated/shared/c++/SseConfig.hpp +17 -1
  29. package/package.json +5 -5
  30. package/src/NitroSseClient.ts +96 -0
  31. package/src/SseInterface.ts +69 -1
  32. package/src/index.ts +10 -2
@@ -30,7 +30,7 @@ namespace margelo::nitro::nitrosse {
30
30
  */
31
31
  struct JSseConfig final: public jni::JavaClass<JSseConfig> {
32
32
  public:
33
- static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseConfig;";
33
+ static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseConfig;";
34
34
 
35
35
  public:
36
36
  /**
@@ -58,6 +58,14 @@ namespace margelo::nitro::nitrosse {
58
58
  jni::local_ref<jni::JDouble> connectionTimeoutMs = this->getFieldValue(fieldConnectionTimeoutMs);
59
59
  static const auto fieldReadTimeoutMs = clazz->getField<jni::JDouble>("readTimeoutMs");
60
60
  jni::local_ref<jni::JDouble> readTimeoutMs = this->getFieldValue(fieldReadTimeoutMs);
61
+ static const auto fieldRetryIntervalMs = clazz->getField<jni::JDouble>("retryIntervalMs");
62
+ jni::local_ref<jni::JDouble> retryIntervalMs = this->getFieldValue(fieldRetryIntervalMs);
63
+ static const auto fieldMaxRetryIntervalMs = clazz->getField<jni::JDouble>("maxRetryIntervalMs");
64
+ jni::local_ref<jni::JDouble> maxRetryIntervalMs = this->getFieldValue(fieldMaxRetryIntervalMs);
65
+ static const auto fieldJitterFactor = clazz->getField<jni::JDouble>("jitterFactor");
66
+ jni::local_ref<jni::JDouble> jitterFactor = this->getFieldValue(fieldJitterFactor);
67
+ static const auto fieldMaxReconnectAttempts = clazz->getField<jni::JDouble>("maxReconnectAttempts");
68
+ jni::local_ref<jni::JDouble> maxReconnectAttempts = this->getFieldValue(fieldMaxReconnectAttempts);
61
69
  static const auto fieldOnBeforeRequest = clazz->getField<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____::javaobject>("onBeforeRequest");
62
70
  jni::local_ref<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____::javaobject> onBeforeRequest = this->getFieldValue(fieldOnBeforeRequest);
63
71
  return SseConfig(
@@ -77,6 +85,10 @@ namespace margelo::nitro::nitrosse {
77
85
  maxBufferSize != nullptr ? std::make_optional(maxBufferSize->value()) : std::nullopt,
78
86
  connectionTimeoutMs != nullptr ? std::make_optional(connectionTimeoutMs->value()) : std::nullopt,
79
87
  readTimeoutMs != nullptr ? std::make_optional(readTimeoutMs->value()) : std::nullopt,
88
+ retryIntervalMs != nullptr ? std::make_optional(retryIntervalMs->value()) : std::nullopt,
89
+ maxRetryIntervalMs != nullptr ? std::make_optional(maxRetryIntervalMs->value()) : std::nullopt,
90
+ jitterFactor != nullptr ? std::make_optional(jitterFactor->value()) : std::nullopt,
91
+ maxReconnectAttempts != nullptr ? std::make_optional(maxReconnectAttempts->value()) : std::nullopt,
80
92
  onBeforeRequest != nullptr ? std::make_optional([&]() -> std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()> {
81
93
  if (onBeforeRequest->isInstanceOf(JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string______cxx::javaClassStatic())) [[likely]] {
82
94
  auto downcast = jni::static_ref_cast<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string______cxx::javaobject>(onBeforeRequest);
@@ -95,7 +107,7 @@ namespace margelo::nitro::nitrosse {
95
107
  */
96
108
  [[maybe_unused]]
97
109
  static jni::local_ref<JSseConfig::javaobject> fromCpp(const SseConfig& value) {
98
- using JSignature = JSseConfig(jni::alias_ref<jni::JString>, jni::alias_ref<JHttpMethod>, jni::alias_ref<jni::JMap<jni::JString, jni::JString>>, jni::alias_ref<jni::JString>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____::javaobject>);
110
+ using JSignature = JSseConfig(jni::alias_ref<jni::JString>, jni::alias_ref<JHttpMethod>, jni::alias_ref<jni::JMap<jni::JString, jni::JString>>, jni::alias_ref<jni::JString>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____::javaobject>);
99
111
  static const auto clazz = javaClassStatic();
100
112
  static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
101
113
  return create(
@@ -115,6 +127,10 @@ namespace margelo::nitro::nitrosse {
115
127
  value.maxBufferSize.has_value() ? jni::JDouble::valueOf(value.maxBufferSize.value()) : nullptr,
116
128
  value.connectionTimeoutMs.has_value() ? jni::JDouble::valueOf(value.connectionTimeoutMs.value()) : nullptr,
117
129
  value.readTimeoutMs.has_value() ? jni::JDouble::valueOf(value.readTimeoutMs.value()) : nullptr,
130
+ value.retryIntervalMs.has_value() ? jni::JDouble::valueOf(value.retryIntervalMs.value()) : nullptr,
131
+ value.maxRetryIntervalMs.has_value() ? jni::JDouble::valueOf(value.maxRetryIntervalMs.value()) : nullptr,
132
+ value.jitterFactor.has_value() ? jni::JDouble::valueOf(value.jitterFactor.value()) : nullptr,
133
+ value.maxReconnectAttempts.has_value() ? jni::JDouble::valueOf(value.maxReconnectAttempts.value()) : nullptr,
118
134
  value.onBeforeRequest.has_value() ? JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string______cxx::fromCpp(value.onBeforeRequest.value()) : nullptr
119
135
  );
120
136
  }
@@ -24,7 +24,7 @@ namespace margelo::nitro::nitrosse {
24
24
  */
25
25
  struct JSseEvent final: public jni::JavaClass<JSseEvent> {
26
26
  public:
27
- static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseEvent;";
27
+ static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseEvent;";
28
28
 
29
29
  public:
30
30
  /**
@@ -19,7 +19,7 @@ namespace margelo::nitro::nitrosse {
19
19
  */
20
20
  struct JSseEventType final: public jni::JavaClass<JSseEventType> {
21
21
  public:
22
- static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseEventType;";
22
+ static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseEventType;";
23
23
 
24
24
  public:
25
25
  /**
@@ -22,7 +22,7 @@ namespace margelo::nitro::nitrosse {
22
22
  */
23
23
  struct JSseStats final: public jni::JavaClass<JSseStats> {
24
24
  public:
25
- static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseStats;";
25
+ static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/SseStats;";
26
26
 
27
27
  public:
28
28
  /**
@@ -46,13 +46,25 @@ data class SseConfig(
46
46
  val readTimeoutMs: Double?,
47
47
  @DoNotStrip
48
48
  @Keep
49
+ val retryIntervalMs: Double?,
50
+ @DoNotStrip
51
+ @Keep
52
+ val maxRetryIntervalMs: Double?,
53
+ @DoNotStrip
54
+ @Keep
55
+ val jitterFactor: Double?,
56
+ @DoNotStrip
57
+ @Keep
58
+ val maxReconnectAttempts: Double?,
59
+ @DoNotStrip
60
+ @Keep
49
61
  val onBeforeRequest: Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____?
50
62
  ) {
51
63
  /**
52
64
  * Create a new instance of SseConfig from Kotlin
53
65
  */
54
- constructor(url: String, method: HttpMethod?, headers: Map<String, String>?, body: String?, backgroundExecution: Boolean?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, onBeforeRequest: (() -> Promise<Promise<Map<String, String>>>)?):
55
- this(url, method, headers, body, backgroundExecution, batchingIntervalMs, maxBufferSize, connectionTimeoutMs, readTimeoutMs, onBeforeRequest?.let { Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string______java(it) })
66
+ constructor(url: String, method: HttpMethod?, headers: Map<String, String>?, body: String?, backgroundExecution: Boolean?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, retryIntervalMs: Double?, maxRetryIntervalMs: Double?, jitterFactor: Double?, maxReconnectAttempts: Double?, onBeforeRequest: (() -> Promise<Promise<Map<String, String>>>)?):
67
+ this(url, method, headers, body, backgroundExecution, batchingIntervalMs, maxBufferSize, connectionTimeoutMs, readTimeoutMs, retryIntervalMs, maxRetryIntervalMs, jitterFactor, maxReconnectAttempts, onBeforeRequest?.let { Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string______java(it) })
56
68
 
57
69
  companion object {
58
70
  /**
@@ -62,8 +74,8 @@ data class SseConfig(
62
74
  @Keep
63
75
  @Suppress("unused")
64
76
  @JvmStatic
65
- private fun fromCpp(url: String, method: HttpMethod?, headers: Map<String, String>?, body: String?, backgroundExecution: Boolean?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, onBeforeRequest: Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____?): SseConfig {
66
- return SseConfig(url, method, headers, body, backgroundExecution, batchingIntervalMs, maxBufferSize, connectionTimeoutMs, readTimeoutMs, onBeforeRequest)
77
+ private fun fromCpp(url: String, method: HttpMethod?, headers: Map<String, String>?, body: String?, backgroundExecution: Boolean?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, retryIntervalMs: Double?, maxRetryIntervalMs: Double?, jitterFactor: Double?, maxReconnectAttempts: Double?, onBeforeRequest: Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____?): SseConfig {
78
+ return SseConfig(url, method, headers, body, backgroundExecution, batchingIntervalMs, maxBufferSize, connectionTimeoutMs, readTimeoutMs, retryIntervalMs, maxRetryIntervalMs, jitterFactor, maxReconnectAttempts, onBeforeRequest)
67
79
  }
68
80
  }
69
81
  }
@@ -29,9 +29,9 @@ int initialize(JavaVM* vm) {
29
29
  }
30
30
 
31
31
  struct JHybridNitroSseSpecImpl: public jni::JavaClass<JHybridNitroSseSpecImpl, JHybridNitroSseSpec::JavaPart> {
32
- static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/NitroSse;";
32
+ static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitrosse/NitroSse;";
33
33
  static std::shared_ptr<JHybridNitroSseSpec> create() {
34
- static auto constructorFn = javaClassStatic()->getConstructor<JHybridNitroSseSpecImpl::javaobject()>();
34
+ static const auto constructorFn = javaClassStatic()->getConstructor<JHybridNitroSseSpecImpl::javaobject()>();
35
35
  jni::local_ref<JHybridNitroSseSpec::JavaPart> javaPart = javaClassStatic()->newObject(constructorFn);
36
36
  return javaPart->getJHybridNitroSseSpec();
37
37
  }
@@ -18,7 +18,7 @@ public extension SseConfig {
18
18
  /**
19
19
  * Create a new instance of `SseConfig`.
20
20
  */
21
- init(url: String, method: HttpMethod?, headers: Dictionary<String, String>?, body: String?, backgroundExecution: Bool?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, onBeforeRequest: (() -> Promise<Promise<Dictionary<String, String>>>)?) {
21
+ init(url: String, method: HttpMethod?, headers: Dictionary<String, String>?, body: String?, backgroundExecution: Bool?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, retryIntervalMs: Double?, maxRetryIntervalMs: Double?, jitterFactor: Double?, maxReconnectAttempts: Double?, onBeforeRequest: (() -> Promise<Promise<Dictionary<String, String>>>)?) {
22
22
  self.init(std.string(url), { () -> bridge.std__optional_HttpMethod_ in
23
23
  if let __unwrappedValue = method {
24
24
  return bridge.create_std__optional_HttpMethod_(__unwrappedValue)
@@ -73,6 +73,30 @@ public extension SseConfig {
73
73
  } else {
74
74
  return .init()
75
75
  }
76
+ }(), { () -> bridge.std__optional_double_ in
77
+ if let __unwrappedValue = retryIntervalMs {
78
+ return bridge.create_std__optional_double_(__unwrappedValue)
79
+ } else {
80
+ return .init()
81
+ }
82
+ }(), { () -> bridge.std__optional_double_ in
83
+ if let __unwrappedValue = maxRetryIntervalMs {
84
+ return bridge.create_std__optional_double_(__unwrappedValue)
85
+ } else {
86
+ return .init()
87
+ }
88
+ }(), { () -> bridge.std__optional_double_ in
89
+ if let __unwrappedValue = jitterFactor {
90
+ return bridge.create_std__optional_double_(__unwrappedValue)
91
+ } else {
92
+ return .init()
93
+ }
94
+ }(), { () -> bridge.std__optional_double_ in
95
+ if let __unwrappedValue = maxReconnectAttempts {
96
+ return bridge.create_std__optional_double_(__unwrappedValue)
97
+ } else {
98
+ return .init()
99
+ }
76
100
  }(), { () -> bridge.std__optional_std__function_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_________ in
77
101
  if let __unwrappedValue = onBeforeRequest {
78
102
  return bridge.create_std__optional_std__function_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_________({ () -> bridge.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____ in
@@ -187,6 +211,54 @@ public extension SseConfig {
187
211
  }()
188
212
  }
189
213
 
214
+ @inline(__always)
215
+ var retryIntervalMs: Double? {
216
+ return { () -> Double? in
217
+ if bridge.has_value_std__optional_double_(self.__retryIntervalMs) {
218
+ let __unwrapped = bridge.get_std__optional_double_(self.__retryIntervalMs)
219
+ return __unwrapped
220
+ } else {
221
+ return nil
222
+ }
223
+ }()
224
+ }
225
+
226
+ @inline(__always)
227
+ var maxRetryIntervalMs: Double? {
228
+ return { () -> Double? in
229
+ if bridge.has_value_std__optional_double_(self.__maxRetryIntervalMs) {
230
+ let __unwrapped = bridge.get_std__optional_double_(self.__maxRetryIntervalMs)
231
+ return __unwrapped
232
+ } else {
233
+ return nil
234
+ }
235
+ }()
236
+ }
237
+
238
+ @inline(__always)
239
+ var jitterFactor: Double? {
240
+ return { () -> Double? in
241
+ if bridge.has_value_std__optional_double_(self.__jitterFactor) {
242
+ let __unwrapped = bridge.get_std__optional_double_(self.__jitterFactor)
243
+ return __unwrapped
244
+ } else {
245
+ return nil
246
+ }
247
+ }()
248
+ }
249
+
250
+ @inline(__always)
251
+ var maxReconnectAttempts: Double? {
252
+ return { () -> Double? in
253
+ if bridge.has_value_std__optional_double_(self.__maxReconnectAttempts) {
254
+ let __unwrapped = bridge.get_std__optional_double_(self.__maxReconnectAttempts)
255
+ return __unwrapped
256
+ } else {
257
+ return nil
258
+ }
259
+ }()
260
+ }
261
+
190
262
  @inline(__always)
191
263
  var onBeforeRequest: (() -> Promise<Promise<Dictionary<String, String>>>)? {
192
264
  return { () -> (() -> Promise<Promise<Dictionary<String, String>>>)? in
@@ -54,11 +54,15 @@ namespace margelo::nitro::nitrosse {
54
54
  std::optional<double> maxBufferSize SWIFT_PRIVATE;
55
55
  std::optional<double> connectionTimeoutMs SWIFT_PRIVATE;
56
56
  std::optional<double> readTimeoutMs SWIFT_PRIVATE;
57
+ std::optional<double> retryIntervalMs SWIFT_PRIVATE;
58
+ std::optional<double> maxRetryIntervalMs SWIFT_PRIVATE;
59
+ std::optional<double> jitterFactor SWIFT_PRIVATE;
60
+ std::optional<double> maxReconnectAttempts SWIFT_PRIVATE;
57
61
  std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest SWIFT_PRIVATE;
58
62
 
59
63
  public:
60
64
  SseConfig() = default;
61
- explicit SseConfig(std::string url, std::optional<HttpMethod> method, std::optional<std::unordered_map<std::string, std::string>> headers, std::optional<std::string> body, std::optional<bool> backgroundExecution, std::optional<double> batchingIntervalMs, std::optional<double> maxBufferSize, std::optional<double> connectionTimeoutMs, std::optional<double> readTimeoutMs, std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest): url(url), method(method), headers(headers), body(body), backgroundExecution(backgroundExecution), batchingIntervalMs(batchingIntervalMs), maxBufferSize(maxBufferSize), connectionTimeoutMs(connectionTimeoutMs), readTimeoutMs(readTimeoutMs), onBeforeRequest(onBeforeRequest) {}
65
+ explicit SseConfig(std::string url, std::optional<HttpMethod> method, std::optional<std::unordered_map<std::string, std::string>> headers, std::optional<std::string> body, std::optional<bool> backgroundExecution, std::optional<double> batchingIntervalMs, std::optional<double> maxBufferSize, std::optional<double> connectionTimeoutMs, std::optional<double> readTimeoutMs, std::optional<double> retryIntervalMs, std::optional<double> maxRetryIntervalMs, std::optional<double> jitterFactor, std::optional<double> maxReconnectAttempts, std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest): url(url), method(method), headers(headers), body(body), backgroundExecution(backgroundExecution), batchingIntervalMs(batchingIntervalMs), maxBufferSize(maxBufferSize), connectionTimeoutMs(connectionTimeoutMs), readTimeoutMs(readTimeoutMs), retryIntervalMs(retryIntervalMs), maxRetryIntervalMs(maxRetryIntervalMs), jitterFactor(jitterFactor), maxReconnectAttempts(maxReconnectAttempts), onBeforeRequest(onBeforeRequest) {}
62
66
 
63
67
  public:
64
68
  // SseConfig is not equatable because these properties are not equatable: onBeforeRequest
@@ -83,6 +87,10 @@ namespace margelo::nitro {
83
87
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxBufferSize"))),
84
88
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "connectionTimeoutMs"))),
85
89
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "readTimeoutMs"))),
90
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retryIntervalMs"))),
91
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxRetryIntervalMs"))),
92
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "jitterFactor"))),
93
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxReconnectAttempts"))),
86
94
  JSIConverter<std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "onBeforeRequest")))
87
95
  );
88
96
  }
@@ -97,6 +105,10 @@ namespace margelo::nitro {
97
105
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxBufferSize"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.maxBufferSize));
98
106
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "connectionTimeoutMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.connectionTimeoutMs));
99
107
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "readTimeoutMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.readTimeoutMs));
108
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "retryIntervalMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.retryIntervalMs));
109
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxRetryIntervalMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.maxRetryIntervalMs));
110
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "jitterFactor"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.jitterFactor));
111
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxReconnectAttempts"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.maxReconnectAttempts));
100
112
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "onBeforeRequest"), JSIConverter<std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>>::toJSI(runtime, arg.onBeforeRequest));
101
113
  return obj;
102
114
  }
@@ -117,6 +129,10 @@ namespace margelo::nitro {
117
129
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxBufferSize")))) return false;
118
130
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "connectionTimeoutMs")))) return false;
119
131
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "readTimeoutMs")))) return false;
132
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retryIntervalMs")))) return false;
133
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxRetryIntervalMs")))) return false;
134
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "jitterFactor")))) return false;
135
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxReconnectAttempts")))) return false;
120
136
  if (!JSIConverter<std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "onBeforeRequest")))) return false;
121
137
  return true;
122
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-sse",
3
- "version": "1.6.1",
3
+ "version": "2.0.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",
@@ -84,12 +84,12 @@
84
84
  "eslint-plugin-prettier": "^5.5.4",
85
85
  "jest": "^29.7.0",
86
86
  "lefthook": "^2.0.3",
87
- "nitrogen": "0.35.0",
87
+ "nitrogen": "0.35.2",
88
88
  "prettier": "^2.8.8",
89
89
  "react": "19.2.0",
90
90
  "react-native": "0.83.0",
91
91
  "react-native-builder-bob": "^0.40.17",
92
- "react-native-nitro-modules": "0.35.0",
92
+ "react-native-nitro-modules": "0.35.2",
93
93
  "release-it": "^19.0.4",
94
94
  "turbo": "^2.5.6",
95
95
  "typescript": "^5.9.2"
@@ -97,7 +97,7 @@
97
97
  "peerDependencies": {
98
98
  "react": "*",
99
99
  "react-native": "*",
100
- "react-native-nitro-modules": "0.35.0"
100
+ "react-native-nitro-modules": ">= 0.35.2"
101
101
  },
102
102
  "workspaces": [
103
103
  "example"
@@ -177,4 +177,4 @@
177
177
  ],
178
178
  "version": "0.57.0"
179
179
  }
180
- }
180
+ }
@@ -0,0 +1,96 @@
1
+ import type { NitroSse } from './NitroSse.nitro';
2
+ import type {
3
+ SseClient,
4
+ SseConfig,
5
+ SseEvent,
6
+ SseListener,
7
+ SseStats,
8
+ } from './SseInterface';
9
+
10
+ export class NitroSseClient implements SseClient {
11
+ private _native: NitroSse;
12
+ private _listeners: Map<string, Set<SseListener>> = new Map();
13
+ private _legacyCallback?: (events: SseEvent[]) => void;
14
+
15
+ constructor(native: NitroSse) {
16
+ this._native = native;
17
+ }
18
+
19
+ setup(config: SseConfig, onEvent?: (events: SseEvent[]) => void): void {
20
+ this._legacyCallback = onEvent;
21
+
22
+ // We wrap the native setup to dispatch events to typed listeners
23
+ this._native.setup(config, (events) => {
24
+ // 1. Call legacy batch callback if provided
25
+ this._legacyCallback?.(events);
26
+
27
+ // 2. Dispatch individual events to registered listeners
28
+ for (const event of events) {
29
+ // Dispatch basic types: 'open', 'message', 'error', 'close', 'heartbeat'
30
+ this._emit(event.type, event);
31
+
32
+ // Dispatch by SSE event name if it's different from the type
33
+ // e.g., if type is 'message' but event name is 'update'
34
+ if (event.event && event.event !== event.type) {
35
+ this._emit(event.event, event);
36
+ }
37
+ }
38
+ });
39
+ }
40
+
41
+ addEventListener(type: string, listener: SseListener): void {
42
+ if (!this._listeners.has(type)) {
43
+ this._listeners.set(type, new Set());
44
+ }
45
+ this._listeners.get(type)!.add(listener);
46
+ }
47
+
48
+ removeEventListener(type: string, listener: SseListener): void {
49
+ this._listeners.get(type)?.delete(listener);
50
+ }
51
+
52
+ private _emit(type: string, event: SseEvent): void {
53
+ const listeners = this._listeners.get(type);
54
+ if (listeners) {
55
+ listeners.forEach((listener) => {
56
+ try {
57
+ listener(event);
58
+ } catch (e) {
59
+ console.error(`[NitroSse] Error in event listener for "${type}":`, e);
60
+ }
61
+ });
62
+ }
63
+ }
64
+
65
+ start(): void {
66
+ this._native.start();
67
+ }
68
+
69
+ stop(): void {
70
+ this._native.stop();
71
+ }
72
+
73
+ restart(): void {
74
+ this._native.restart();
75
+ }
76
+
77
+ flush(): void {
78
+ this._native.flush();
79
+ }
80
+
81
+ isConnected(): boolean {
82
+ return this._native.isConnected();
83
+ }
84
+
85
+ getStats(): SseStats {
86
+ return this._native.getStats();
87
+ }
88
+
89
+ updateHeaders(headers: Record<string, string>): void {
90
+ this._native.updateHeaders(headers);
91
+ }
92
+
93
+ setLastProcessedId(id: string): void {
94
+ this._native.setLastProcessedId(id);
95
+ }
96
+ }
@@ -46,7 +46,7 @@ export interface SseConfig {
46
46
  maxBufferSize?: number;
47
47
  /**
48
48
  * Maximum time (in ms) to wait for the initial server connection and handshake to complete.
49
- * Effectively the "Connect Timeout".
49
+ * Effectively the \"Connect Timeout\".
50
50
  * @default 15000
51
51
  */
52
52
  connectionTimeoutMs?: number;
@@ -57,6 +57,29 @@ export interface SseConfig {
57
57
  * @default 300000
58
58
  */
59
59
  readTimeoutMs?: number;
60
+ /**
61
+ * Initial delay (in ms) for reconnection attempts.
62
+ * Subsequent attempts use exponential backoff.
63
+ * @default 1000
64
+ */
65
+ retryIntervalMs?: number;
66
+ /**
67
+ * Maximum delay (in ms) for reconnection attempts.
68
+ * @default 30000
69
+ */
70
+ maxRetryIntervalMs?: number;
71
+ /**
72
+ * Factor to randomize reconnection attempts (0.0 to 1.0).
73
+ * 0 means no jitter, 1.0 means up to 100% randomization.
74
+ * @default 0.5
75
+ */
76
+ jitterFactor?: number;
77
+ /**
78
+ * Maximum number of reconnection attempts before giving up.
79
+ * Use -1 for infinite (default), 0 to disable auto-reconnection.
80
+ * @default -1
81
+ */
82
+ maxReconnectAttempts?: number;
60
83
  /**
61
84
  * Async interceptor called before every connection attempt (including auto-reconnects).
62
85
  * Use this to refresh tokens or calculate dynamic headers.
@@ -98,3 +121,48 @@ export interface SseStats {
98
121
  /** Error code of the last error. */
99
122
  lastErrorCode?: string;
100
123
  }
124
+
125
+ /**
126
+ * Listener for a specific SSE event.
127
+ */
128
+ export type SseListener = (event: SseEvent) => void;
129
+
130
+ /**
131
+ * Public interface for the NitroSse client, supporting typed event listeners.
132
+ */
133
+ export interface SseClient {
134
+ /**
135
+ * Configure SSE and setup event callback.
136
+ * @param config The SSE configuration.
137
+ * @param onEvent Optional legacy batch callback for all events.
138
+ */
139
+ setup(config: SseConfig, onEvent?: (events: SseEvent[]) => void): void;
140
+
141
+ /**
142
+ * Register a listener for a specific event type ('message', 'open', etc.)
143
+ * or a custom SSE event name (from the 'event:' field).
144
+ */
145
+ addEventListener(type: string, listener: SseListener): void;
146
+
147
+ /**
148
+ * Unregister a listener.
149
+ */
150
+ removeEventListener(type: string, listener: SseListener): void;
151
+
152
+ /** Start the connection. */
153
+ start(): void;
154
+ /** Stop the connection. */
155
+ stop(): void;
156
+ /** Restart the connection (stop + start). */
157
+ restart(): void;
158
+ /** Force flush buffered events to JS. */
159
+ flush(): void;
160
+ /** Check if active. */
161
+ isConnected(): boolean;
162
+ /** Get stats. */
163
+ getStats(): SseStats;
164
+ /** Manually update headers. */
165
+ updateHeaders(headers: Record<string, string>): void;
166
+ /** Set last event ID. */
167
+ setLastProcessedId(id: string): void;
168
+ }
package/src/index.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import { NitroModules } from 'react-native-nitro-modules';
2
2
  import type { NitroSse } from './NitroSse.nitro';
3
+ import { NitroSseClient } from './NitroSseClient';
4
+ import type { SseClient } from './SseInterface';
3
5
 
4
6
  export * from './SseInterface';
5
7
  export * from './NitroSse.nitro';
6
8
 
7
- export function createNitroSse(): NitroSse {
9
+ /**
10
+ * Creates a high-performance SSE client that supports typed event listeners (`addEventListener`) and legacy batching.
11
+ *
12
+ * @returns An SseClient instance wrapping the native NitroSse implementation.
13
+ * @throws If the native NitroSse module cannot be found. Ensure the library is linked and the app is built for iOS/Android.
14
+ */
15
+ export function createNitroSse(): SseClient {
8
16
  let nativeSse: NitroSse | undefined;
9
17
  try {
10
18
  nativeSse = NitroModules.createHybridObject<NitroSse>('NitroSse');
@@ -19,5 +27,5 @@ export function createNitroSse(): NitroSse {
19
27
  'NitroSse: Native module not found. Ensure you have linked the library and built the app for iOS/Android.'
20
28
  );
21
29
  }
22
- return nativeSse;
30
+ return new NitroSseClient(nativeSse);
23
31
  }