react-native-nitro-sse 2.3.2 → 2.4.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.
- package/README.md +1 -1
- package/android/build.gradle +11 -0
- package/android/jacoco.gradle +33 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/AndroidSseDispatcher.kt +27 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +78 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/NetworkInspector.kt +6 -3
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +309 -656
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSsePackage.kt +4 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +114 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseDispatcher.kt +14 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +102 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseLifecycleManager.kt +52 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseNetworkMonitor.kt +87 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +80 -0
- package/ios/NitroSse.swift +433 -637
- package/ios/SseConfig+CopyWith.swift +46 -0
- package/ios/SseConnectionHandler.swift +114 -0
- package/ios/SseDispatchQueueDispatcher.swift +38 -0
- package/ios/SseDispatcher.swift +18 -0
- package/ios/SseEventBuffer.swift +79 -0
- package/ios/SseLifecycleManager.swift +118 -0
- package/ios/SseNetworkMonitor.swift +84 -0
- package/ios/SseReconnectStrategy.swift +99 -0
- package/lib/module/NitroSseClient.js +38 -3
- package/lib/module/NitroSseClient.js.map +1 -1
- package/lib/typescript/src/NitroSse.nitro.d.ts +5 -1
- package/lib/typescript/src/NitroSse.nitro.d.ts.map +1 -1
- package/lib/typescript/src/NitroSseClient.d.ts +4 -1
- package/lib/typescript/src/NitroSseClient.d.ts.map +1 -1
- package/lib/typescript/src/SseInterface.d.ts +10 -1
- package/lib/typescript/src/SseInterface.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +2 -0
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +9 -0
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.hpp +1 -0
- package/nitrogen/generated/android/c++/JSseEvent.hpp +9 -3
- package/nitrogen/generated/android/c++/JSseEventType.hpp +3 -0
- package/nitrogen/generated/android/c++/JSseState.hpp +76 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.kt +0 -2
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/Func_void_std__vector_SseEvent_.kt +0 -2
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/HybridNitroSseSpec.kt +6 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEvent.kt +9 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEventType.kt +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseState.kt +29 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +3 -0
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +11 -0
- package/nitrogen/generated/ios/swift/HybridNitroSseSpec.swift +1 -0
- package/nitrogen/generated/ios/swift/HybridNitroSseSpec_cxx.swift +12 -0
- package/nitrogen/generated/ios/swift/SseEvent.swift +12 -1
- package/nitrogen/generated/ios/swift/SseEventType.swift +4 -0
- package/nitrogen/generated/ios/swift/SseState.swift +64 -0
- package/nitrogen/generated/shared/c++/HybridNitroSseSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridNitroSseSpec.hpp +4 -0
- package/nitrogen/generated/shared/c++/SseEvent.hpp +9 -2
- package/nitrogen/generated/shared/c++/SseEventType.hpp +4 -0
- package/nitrogen/generated/shared/c++/SseState.hpp +100 -0
- package/package.json +3 -3
- package/src/NitroSse.nitro.ts +6 -1
- package/src/NitroSseClient.ts +42 -2
- package/src/SseInterface.ts +25 -1
|
@@ -24,6 +24,8 @@ namespace margelo::nitro::nitrosse { struct SseMockConfig; }
|
|
|
24
24
|
namespace margelo::nitro::nitrosse { struct SseMockEvent; }
|
|
25
25
|
// Forward declaration of `SseMockMode` to properly resolve imports.
|
|
26
26
|
namespace margelo::nitro::nitrosse { enum class SseMockMode; }
|
|
27
|
+
// Forward declaration of `SseState` to properly resolve imports.
|
|
28
|
+
namespace margelo::nitro::nitrosse { enum class SseState; }
|
|
27
29
|
// Forward declaration of `SseStats` to properly resolve imports.
|
|
28
30
|
namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
29
31
|
|
|
@@ -36,6 +38,7 @@ namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
|
36
38
|
#include "SseMockConfig.hpp"
|
|
37
39
|
#include "SseMockEvent.hpp"
|
|
38
40
|
#include "SseMockMode.hpp"
|
|
41
|
+
#include "SseState.hpp"
|
|
39
42
|
#include "SseStats.hpp"
|
|
40
43
|
#include <NitroModules/AnyMap.hpp>
|
|
41
44
|
#include <NitroModules/Promise.hpp>
|
|
@@ -26,6 +26,8 @@ namespace margelo::nitro::nitrosse { struct SseMockEvent; }
|
|
|
26
26
|
namespace margelo::nitro::nitrosse { enum class SseEventType; }
|
|
27
27
|
// Forward declaration of `SseEvent` to properly resolve imports.
|
|
28
28
|
namespace margelo::nitro::nitrosse { struct SseEvent; }
|
|
29
|
+
// Forward declaration of `SseState` to properly resolve imports.
|
|
30
|
+
namespace margelo::nitro::nitrosse { enum class SseState; }
|
|
29
31
|
// Forward declaration of `SseStats` to properly resolve imports.
|
|
30
32
|
namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
31
33
|
|
|
@@ -43,6 +45,7 @@ namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
|
43
45
|
#include "SseEventType.hpp"
|
|
44
46
|
#include <NitroModules/AnyMap.hpp>
|
|
45
47
|
#include "SseEvent.hpp"
|
|
48
|
+
#include "SseState.hpp"
|
|
46
49
|
#include "SseStats.hpp"
|
|
47
50
|
|
|
48
51
|
#include "NitroSse-Swift-Cxx-Umbrella.hpp"
|
|
@@ -153,6 +156,14 @@ namespace margelo::nitro::nitrosse {
|
|
|
153
156
|
auto __value = std::move(__result.value());
|
|
154
157
|
return __value;
|
|
155
158
|
}
|
|
159
|
+
inline SseState getState() override {
|
|
160
|
+
auto __result = _swiftPart.getState();
|
|
161
|
+
if (__result.hasError()) [[unlikely]] {
|
|
162
|
+
std::rethrow_exception(__result.error());
|
|
163
|
+
}
|
|
164
|
+
auto __value = std::move(__result.value());
|
|
165
|
+
return __value;
|
|
166
|
+
}
|
|
156
167
|
|
|
157
168
|
private:
|
|
158
169
|
NitroSse::HybridNitroSseSpec_cxx _swiftPart;
|
|
@@ -243,4 +243,16 @@ open class HybridNitroSseSpec_cxx {
|
|
|
243
243
|
return bridge.create_Result_bool_(__exceptionPtr)
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
|
+
|
|
247
|
+
@inline(__always)
|
|
248
|
+
public final func getState() -> bridge.Result_SseState_ {
|
|
249
|
+
do {
|
|
250
|
+
let __result = try self.__implementation.getState()
|
|
251
|
+
let __resultCpp = __result
|
|
252
|
+
return bridge.create_Result_SseState_(__resultCpp)
|
|
253
|
+
} catch (let __error) {
|
|
254
|
+
let __exceptionPtr = __error.toCpp()
|
|
255
|
+
return bridge.create_Result_SseState_(__exceptionPtr)
|
|
256
|
+
}
|
|
257
|
+
}
|
|
246
258
|
}
|
|
@@ -18,7 +18,7 @@ public extension SseEvent {
|
|
|
18
18
|
/**
|
|
19
19
|
* Create a new instance of `SseEvent`.
|
|
20
20
|
*/
|
|
21
|
-
init(type: SseEventType, data: String?, parsedData: AnyMap?, id: String?, event: String?, message: String?, statusCode: Double?, retry: Double?) {
|
|
21
|
+
init(type: SseEventType, data: String?, parsedData: AnyMap?, id: String?, event: String?, message: String?, statusCode: Double?, retry: Double?, state: SseState?) {
|
|
22
22
|
self.init(type, { () -> bridge.std__optional_std__string_ in
|
|
23
23
|
if let __unwrappedValue = data {
|
|
24
24
|
return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
|
|
@@ -61,6 +61,12 @@ public extension SseEvent {
|
|
|
61
61
|
} else {
|
|
62
62
|
return .init()
|
|
63
63
|
}
|
|
64
|
+
}(), { () -> bridge.std__optional_SseState_ in
|
|
65
|
+
if let __unwrappedValue = state {
|
|
66
|
+
return bridge.create_std__optional_SseState_(__unwrappedValue)
|
|
67
|
+
} else {
|
|
68
|
+
return .init()
|
|
69
|
+
}
|
|
64
70
|
}())
|
|
65
71
|
}
|
|
66
72
|
|
|
@@ -152,4 +158,9 @@ public extension SseEvent {
|
|
|
152
158
|
}
|
|
153
159
|
}()
|
|
154
160
|
}
|
|
161
|
+
|
|
162
|
+
@inline(__always)
|
|
163
|
+
var state: SseState? {
|
|
164
|
+
return self.__state.value
|
|
165
|
+
}
|
|
155
166
|
}
|
|
@@ -27,6 +27,8 @@ public extension SseEventType {
|
|
|
27
27
|
self = .close
|
|
28
28
|
case "heartbeat":
|
|
29
29
|
self = .heartbeat
|
|
30
|
+
case "state":
|
|
31
|
+
self = .state
|
|
30
32
|
default:
|
|
31
33
|
return nil
|
|
32
34
|
}
|
|
@@ -47,6 +49,8 @@ public extension SseEventType {
|
|
|
47
49
|
return "close"
|
|
48
50
|
case .heartbeat:
|
|
49
51
|
return "heartbeat"
|
|
52
|
+
case .state:
|
|
53
|
+
return "state"
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
56
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// SseState.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Represents the JS union `SseState`, backed by a C++ enum.
|
|
10
|
+
*/
|
|
11
|
+
public typealias SseState = margelo.nitro.nitrosse.SseState
|
|
12
|
+
|
|
13
|
+
public extension SseState {
|
|
14
|
+
/**
|
|
15
|
+
* Get a SseState for the given String value, or
|
|
16
|
+
* return `nil` if the given value was invalid/unknown.
|
|
17
|
+
*/
|
|
18
|
+
init?(fromString string: String) {
|
|
19
|
+
switch string {
|
|
20
|
+
case "open":
|
|
21
|
+
self = .open
|
|
22
|
+
case "idle":
|
|
23
|
+
self = .idle
|
|
24
|
+
case "connecting":
|
|
25
|
+
self = .connecting
|
|
26
|
+
case "stale":
|
|
27
|
+
self = .stale
|
|
28
|
+
case "reconnecting":
|
|
29
|
+
self = .reconnecting
|
|
30
|
+
case "paused":
|
|
31
|
+
self = .paused
|
|
32
|
+
case "closed":
|
|
33
|
+
self = .closed
|
|
34
|
+
case "failed":
|
|
35
|
+
self = .failed
|
|
36
|
+
default:
|
|
37
|
+
return nil
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get the String value this SseState represents.
|
|
43
|
+
*/
|
|
44
|
+
var stringValue: String {
|
|
45
|
+
switch self {
|
|
46
|
+
case .open:
|
|
47
|
+
return "open"
|
|
48
|
+
case .idle:
|
|
49
|
+
return "idle"
|
|
50
|
+
case .connecting:
|
|
51
|
+
return "connecting"
|
|
52
|
+
case .stale:
|
|
53
|
+
return "stale"
|
|
54
|
+
case .reconnecting:
|
|
55
|
+
return "reconnecting"
|
|
56
|
+
case .paused:
|
|
57
|
+
return "paused"
|
|
58
|
+
case .closed:
|
|
59
|
+
return "closed"
|
|
60
|
+
case .failed:
|
|
61
|
+
return "failed"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -23,6 +23,7 @@ namespace margelo::nitro::nitrosse {
|
|
|
23
23
|
prototype.registerHybridMethod("flush", &HybridNitroSseSpec::flush);
|
|
24
24
|
prototype.registerHybridMethod("restart", &HybridNitroSseSpec::restart);
|
|
25
25
|
prototype.registerHybridMethod("isConnected", &HybridNitroSseSpec::isConnected);
|
|
26
|
+
prototype.registerHybridMethod("getState", &HybridNitroSseSpec::getState);
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -19,6 +19,8 @@ namespace margelo::nitro::nitrosse { struct SseConfig; }
|
|
|
19
19
|
namespace margelo::nitro::nitrosse { struct SseEvent; }
|
|
20
20
|
// Forward declaration of `SseStats` to properly resolve imports.
|
|
21
21
|
namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
22
|
+
// Forward declaration of `SseState` to properly resolve imports.
|
|
23
|
+
namespace margelo::nitro::nitrosse { enum class SseState; }
|
|
22
24
|
|
|
23
25
|
#include "SseConfig.hpp"
|
|
24
26
|
#include "SseEvent.hpp"
|
|
@@ -27,6 +29,7 @@ namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
|
27
29
|
#include <string>
|
|
28
30
|
#include <unordered_map>
|
|
29
31
|
#include "SseStats.hpp"
|
|
32
|
+
#include "SseState.hpp"
|
|
30
33
|
|
|
31
34
|
namespace margelo::nitro::nitrosse {
|
|
32
35
|
|
|
@@ -68,6 +71,7 @@ namespace margelo::nitro::nitrosse {
|
|
|
68
71
|
virtual void flush() = 0;
|
|
69
72
|
virtual void restart() = 0;
|
|
70
73
|
virtual bool isConnected() = 0;
|
|
74
|
+
virtual SseState getState() = 0;
|
|
71
75
|
|
|
72
76
|
protected:
|
|
73
77
|
// Hybrid Setup
|
|
@@ -30,11 +30,14 @@
|
|
|
30
30
|
|
|
31
31
|
// Forward declaration of `SseEventType` to properly resolve imports.
|
|
32
32
|
namespace margelo::nitro::nitrosse { enum class SseEventType; }
|
|
33
|
+
// Forward declaration of `SseState` to properly resolve imports.
|
|
34
|
+
namespace margelo::nitro::nitrosse { enum class SseState; }
|
|
33
35
|
|
|
34
36
|
#include "SseEventType.hpp"
|
|
35
37
|
#include <string>
|
|
36
38
|
#include <optional>
|
|
37
39
|
#include <NitroModules/AnyMap.hpp>
|
|
40
|
+
#include "SseState.hpp"
|
|
38
41
|
|
|
39
42
|
namespace margelo::nitro::nitrosse {
|
|
40
43
|
|
|
@@ -51,10 +54,11 @@ namespace margelo::nitro::nitrosse {
|
|
|
51
54
|
std::optional<std::string> message SWIFT_PRIVATE;
|
|
52
55
|
std::optional<double> statusCode SWIFT_PRIVATE;
|
|
53
56
|
std::optional<double> retry SWIFT_PRIVATE;
|
|
57
|
+
std::optional<SseState> state SWIFT_PRIVATE;
|
|
54
58
|
|
|
55
59
|
public:
|
|
56
60
|
SseEvent() = default;
|
|
57
|
-
explicit SseEvent(SseEventType type, std::optional<std::string> data, std::optional<std::shared_ptr<AnyMap>> parsedData, std::optional<std::string> id, std::optional<std::string> event, std::optional<std::string> message, std::optional<double> statusCode, std::optional<double> retry): type(type), data(data), parsedData(parsedData), id(id), event(event), message(message), statusCode(statusCode), retry(retry) {}
|
|
61
|
+
explicit SseEvent(SseEventType type, std::optional<std::string> data, std::optional<std::shared_ptr<AnyMap>> parsedData, std::optional<std::string> id, std::optional<std::string> event, std::optional<std::string> message, std::optional<double> statusCode, std::optional<double> retry, std::optional<SseState> state): type(type), data(data), parsedData(parsedData), id(id), event(event), message(message), statusCode(statusCode), retry(retry), state(state) {}
|
|
58
62
|
|
|
59
63
|
public:
|
|
60
64
|
friend bool operator==(const SseEvent& lhs, const SseEvent& rhs) = default;
|
|
@@ -77,7 +81,8 @@ namespace margelo::nitro {
|
|
|
77
81
|
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "event"))),
|
|
78
82
|
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "message"))),
|
|
79
83
|
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "statusCode"))),
|
|
80
|
-
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retry")))
|
|
84
|
+
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retry"))),
|
|
85
|
+
JSIConverter<std::optional<margelo::nitro::nitrosse::SseState>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "state")))
|
|
81
86
|
);
|
|
82
87
|
}
|
|
83
88
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitrosse::SseEvent& arg) {
|
|
@@ -90,6 +95,7 @@ namespace margelo::nitro {
|
|
|
90
95
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "message"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.message));
|
|
91
96
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "statusCode"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.statusCode));
|
|
92
97
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "retry"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.retry));
|
|
98
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "state"), JSIConverter<std::optional<margelo::nitro::nitrosse::SseState>>::toJSI(runtime, arg.state));
|
|
93
99
|
return obj;
|
|
94
100
|
}
|
|
95
101
|
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
@@ -108,6 +114,7 @@ namespace margelo::nitro {
|
|
|
108
114
|
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "message")))) return false;
|
|
109
115
|
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "statusCode")))) return false;
|
|
110
116
|
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retry")))) return false;
|
|
117
|
+
if (!JSIConverter<std::optional<margelo::nitro::nitrosse::SseState>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "state")))) return false;
|
|
111
118
|
return true;
|
|
112
119
|
}
|
|
113
120
|
};
|
|
@@ -34,6 +34,7 @@ namespace margelo::nitro::nitrosse {
|
|
|
34
34
|
ERROR SWIFT_NAME(error) = 2,
|
|
35
35
|
CLOSE SWIFT_NAME(close) = 3,
|
|
36
36
|
HEARTBEAT SWIFT_NAME(heartbeat) = 4,
|
|
37
|
+
STATE SWIFT_NAME(state) = 5,
|
|
37
38
|
} CLOSED_ENUM;
|
|
38
39
|
|
|
39
40
|
} // namespace margelo::nitro::nitrosse
|
|
@@ -51,6 +52,7 @@ namespace margelo::nitro {
|
|
|
51
52
|
case hashString("error"): return margelo::nitro::nitrosse::SseEventType::ERROR;
|
|
52
53
|
case hashString("close"): return margelo::nitro::nitrosse::SseEventType::CLOSE;
|
|
53
54
|
case hashString("heartbeat"): return margelo::nitro::nitrosse::SseEventType::HEARTBEAT;
|
|
55
|
+
case hashString("state"): return margelo::nitro::nitrosse::SseEventType::STATE;
|
|
54
56
|
default: [[unlikely]]
|
|
55
57
|
throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum SseEventType - invalid value!");
|
|
56
58
|
}
|
|
@@ -62,6 +64,7 @@ namespace margelo::nitro {
|
|
|
62
64
|
case margelo::nitro::nitrosse::SseEventType::ERROR: return JSIConverter<std::string>::toJSI(runtime, "error");
|
|
63
65
|
case margelo::nitro::nitrosse::SseEventType::CLOSE: return JSIConverter<std::string>::toJSI(runtime, "close");
|
|
64
66
|
case margelo::nitro::nitrosse::SseEventType::HEARTBEAT: return JSIConverter<std::string>::toJSI(runtime, "heartbeat");
|
|
67
|
+
case margelo::nitro::nitrosse::SseEventType::STATE: return JSIConverter<std::string>::toJSI(runtime, "state");
|
|
65
68
|
default: [[unlikely]]
|
|
66
69
|
throw std::invalid_argument("Cannot convert SseEventType to JS - invalid value: "
|
|
67
70
|
+ std::to_string(static_cast<int>(arg)) + "!");
|
|
@@ -78,6 +81,7 @@ namespace margelo::nitro {
|
|
|
78
81
|
case hashString("error"):
|
|
79
82
|
case hashString("close"):
|
|
80
83
|
case hashString("heartbeat"):
|
|
84
|
+
case hashString("state"):
|
|
81
85
|
return true;
|
|
82
86
|
default:
|
|
83
87
|
return false;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// SseState.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/NitroHash.hpp>)
|
|
11
|
+
#include <NitroModules/NitroHash.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
16
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
21
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
namespace margelo::nitro::nitrosse {
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An enum which can be represented as a JavaScript union (SseState).
|
|
30
|
+
*/
|
|
31
|
+
enum class SseState {
|
|
32
|
+
OPEN SWIFT_NAME(open) = 0,
|
|
33
|
+
IDLE SWIFT_NAME(idle) = 1,
|
|
34
|
+
CONNECTING SWIFT_NAME(connecting) = 2,
|
|
35
|
+
STALE SWIFT_NAME(stale) = 3,
|
|
36
|
+
RECONNECTING SWIFT_NAME(reconnecting) = 4,
|
|
37
|
+
PAUSED SWIFT_NAME(paused) = 5,
|
|
38
|
+
CLOSED SWIFT_NAME(closed) = 6,
|
|
39
|
+
FAILED SWIFT_NAME(failed) = 7,
|
|
40
|
+
} CLOSED_ENUM;
|
|
41
|
+
|
|
42
|
+
} // namespace margelo::nitro::nitrosse
|
|
43
|
+
|
|
44
|
+
namespace margelo::nitro {
|
|
45
|
+
|
|
46
|
+
// C++ SseState <> JS SseState (union)
|
|
47
|
+
template <>
|
|
48
|
+
struct JSIConverter<margelo::nitro::nitrosse::SseState> final {
|
|
49
|
+
static inline margelo::nitro::nitrosse::SseState fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
50
|
+
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
|
|
51
|
+
switch (hashString(unionValue.c_str(), unionValue.size())) {
|
|
52
|
+
case hashString("open"): return margelo::nitro::nitrosse::SseState::OPEN;
|
|
53
|
+
case hashString("idle"): return margelo::nitro::nitrosse::SseState::IDLE;
|
|
54
|
+
case hashString("connecting"): return margelo::nitro::nitrosse::SseState::CONNECTING;
|
|
55
|
+
case hashString("stale"): return margelo::nitro::nitrosse::SseState::STALE;
|
|
56
|
+
case hashString("reconnecting"): return margelo::nitro::nitrosse::SseState::RECONNECTING;
|
|
57
|
+
case hashString("paused"): return margelo::nitro::nitrosse::SseState::PAUSED;
|
|
58
|
+
case hashString("closed"): return margelo::nitro::nitrosse::SseState::CLOSED;
|
|
59
|
+
case hashString("failed"): return margelo::nitro::nitrosse::SseState::FAILED;
|
|
60
|
+
default: [[unlikely]]
|
|
61
|
+
throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum SseState - invalid value!");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::nitrosse::SseState arg) {
|
|
65
|
+
switch (arg) {
|
|
66
|
+
case margelo::nitro::nitrosse::SseState::OPEN: return JSIConverter<std::string>::toJSI(runtime, "open");
|
|
67
|
+
case margelo::nitro::nitrosse::SseState::IDLE: return JSIConverter<std::string>::toJSI(runtime, "idle");
|
|
68
|
+
case margelo::nitro::nitrosse::SseState::CONNECTING: return JSIConverter<std::string>::toJSI(runtime, "connecting");
|
|
69
|
+
case margelo::nitro::nitrosse::SseState::STALE: return JSIConverter<std::string>::toJSI(runtime, "stale");
|
|
70
|
+
case margelo::nitro::nitrosse::SseState::RECONNECTING: return JSIConverter<std::string>::toJSI(runtime, "reconnecting");
|
|
71
|
+
case margelo::nitro::nitrosse::SseState::PAUSED: return JSIConverter<std::string>::toJSI(runtime, "paused");
|
|
72
|
+
case margelo::nitro::nitrosse::SseState::CLOSED: return JSIConverter<std::string>::toJSI(runtime, "closed");
|
|
73
|
+
case margelo::nitro::nitrosse::SseState::FAILED: return JSIConverter<std::string>::toJSI(runtime, "failed");
|
|
74
|
+
default: [[unlikely]]
|
|
75
|
+
throw std::invalid_argument("Cannot convert SseState to JS - invalid value: "
|
|
76
|
+
+ std::to_string(static_cast<int>(arg)) + "!");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
80
|
+
if (!value.isString()) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
|
|
84
|
+
switch (hashString(unionValue.c_str(), unionValue.size())) {
|
|
85
|
+
case hashString("open"):
|
|
86
|
+
case hashString("idle"):
|
|
87
|
+
case hashString("connecting"):
|
|
88
|
+
case hashString("stale"):
|
|
89
|
+
case hashString("reconnecting"):
|
|
90
|
+
case hashString("paused"):
|
|
91
|
+
case hashString("closed"):
|
|
92
|
+
case hashString("failed"):
|
|
93
|
+
return true;
|
|
94
|
+
default:
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
} // namespace margelo::nitro
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-sse",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.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",
|
|
@@ -91,12 +91,12 @@
|
|
|
91
91
|
"eslint-plugin-prettier": "^5.5.4",
|
|
92
92
|
"jest": "^29.7.0",
|
|
93
93
|
"lefthook": "^2.0.3",
|
|
94
|
-
"nitrogen": "0.36.
|
|
94
|
+
"nitrogen": "0.36.5",
|
|
95
95
|
"prettier": "^2.8.8",
|
|
96
96
|
"react": "19.2.3",
|
|
97
97
|
"react-native": "0.85.3",
|
|
98
98
|
"react-native-builder-bob": "^0.40.17",
|
|
99
|
-
"react-native-nitro-modules": "0.36.
|
|
99
|
+
"react-native-nitro-modules": "0.36.5",
|
|
100
100
|
"release-it": "^19.0.4",
|
|
101
101
|
"turbo": "^2.5.6",
|
|
102
102
|
"typescript": "^5.9.2"
|
package/src/NitroSse.nitro.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
-
import type { SseConfig, SseEvent, SseStats } from './SseInterface';
|
|
2
|
+
import type { SseConfig, SseEvent, SseStats, SseState } from './SseInterface';
|
|
3
3
|
|
|
4
4
|
export interface NitroSse
|
|
5
5
|
extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
|
|
@@ -50,4 +50,9 @@ export interface NitroSse
|
|
|
50
50
|
* Check if the connection is currently active.
|
|
51
51
|
*/
|
|
52
52
|
isConnected(): boolean;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get the current state of the connection.
|
|
56
|
+
*/
|
|
57
|
+
getState(): SseState;
|
|
53
58
|
}
|
package/src/NitroSseClient.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
SseEvent,
|
|
6
6
|
SseListener,
|
|
7
7
|
SseStats,
|
|
8
|
+
SseState,
|
|
8
9
|
} from './SseInterface';
|
|
9
10
|
|
|
10
11
|
declare const __DEV__: boolean | undefined;
|
|
@@ -17,13 +18,25 @@ export class NitroSseClient implements SseClient {
|
|
|
17
18
|
private _mockIntervalId?: any;
|
|
18
19
|
private _mockIndex: number = 0;
|
|
19
20
|
|
|
21
|
+
private _mockState?: SseState;
|
|
22
|
+
|
|
20
23
|
constructor(native: NitroSse) {
|
|
21
24
|
this._native = native;
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
private _setMockState(state: SseState): void {
|
|
28
|
+
if (this._mockState !== state) {
|
|
29
|
+
this._mockState = state;
|
|
30
|
+
const stateEvent: SseEvent = { type: 'state', state, statusCode: 200 };
|
|
31
|
+
this._legacyCallback?.([stateEvent]);
|
|
32
|
+
this._emit('state', stateEvent);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
24
36
|
setup(config: SseConfig, onEvent?: (events: SseEvent[]) => void): void {
|
|
25
37
|
this._config = { ...config };
|
|
26
38
|
this._legacyCallback = onEvent;
|
|
39
|
+
this._mockState = undefined;
|
|
27
40
|
|
|
28
41
|
const isDev = typeof __DEV__ !== 'undefined' ? __DEV__ === true : false;
|
|
29
42
|
if (!isDev) {
|
|
@@ -101,6 +114,7 @@ export class NitroSseClient implements SseClient {
|
|
|
101
114
|
message: rawMock.message,
|
|
102
115
|
statusCode: rawMock.statusCode ?? 200,
|
|
103
116
|
retry: rawMock.retry,
|
|
117
|
+
state: rawMock.state,
|
|
104
118
|
};
|
|
105
119
|
}
|
|
106
120
|
|
|
@@ -156,6 +170,9 @@ export class NitroSseClient implements SseClient {
|
|
|
156
170
|
clearTimeout(this._mockIntervalId);
|
|
157
171
|
this._mockIntervalId = undefined;
|
|
158
172
|
}
|
|
173
|
+
if (mode === 'replace') {
|
|
174
|
+
this._setMockState('closed');
|
|
175
|
+
}
|
|
159
176
|
const closeEvent: SseEvent = { type: 'close', statusCode: 200 };
|
|
160
177
|
this._legacyCallback?.([closeEvent]);
|
|
161
178
|
this._emit('close', closeEvent);
|
|
@@ -165,6 +182,9 @@ export class NitroSseClient implements SseClient {
|
|
|
165
182
|
|
|
166
183
|
// Simulate connection drops
|
|
167
184
|
if (validatedErrorRate && Math.random() < validatedErrorRate) {
|
|
185
|
+
if (mode === 'replace') {
|
|
186
|
+
this._setMockState('reconnecting');
|
|
187
|
+
}
|
|
168
188
|
const errorEvent: SseEvent = {
|
|
169
189
|
type: 'error',
|
|
170
190
|
message: 'Mock Connection Drop (Simulated Error)',
|
|
@@ -202,6 +222,9 @@ export class NitroSseClient implements SseClient {
|
|
|
202
222
|
}
|
|
203
223
|
|
|
204
224
|
if (batch.length > 0) {
|
|
225
|
+
if (mode === 'replace') {
|
|
226
|
+
this._setMockState('open');
|
|
227
|
+
}
|
|
205
228
|
// 1. Call legacy batch callback if provided
|
|
206
229
|
this._legacyCallback?.(batch);
|
|
207
230
|
|
|
@@ -225,6 +248,9 @@ export class NitroSseClient implements SseClient {
|
|
|
225
248
|
: intervalMs;
|
|
226
249
|
|
|
227
250
|
if (mode === 'replace') {
|
|
251
|
+
this._setMockState('connecting');
|
|
252
|
+
this._setMockState('open');
|
|
253
|
+
|
|
228
254
|
// Emit simulated open event
|
|
229
255
|
const openEvent: SseEvent = { type: 'open', statusCode: 200 };
|
|
230
256
|
this._legacyCallback?.([openEvent]);
|
|
@@ -245,6 +271,9 @@ export class NitroSseClient implements SseClient {
|
|
|
245
271
|
clearTimeout(this._mockIntervalId);
|
|
246
272
|
this._mockIntervalId = undefined;
|
|
247
273
|
}
|
|
274
|
+
if (this._config?.mock?.mode === 'replace') {
|
|
275
|
+
this._setMockState('closed');
|
|
276
|
+
}
|
|
248
277
|
this._native.stop();
|
|
249
278
|
}
|
|
250
279
|
|
|
@@ -262,8 +291,12 @@ export class NitroSseClient implements SseClient {
|
|
|
262
291
|
}
|
|
263
292
|
|
|
264
293
|
isConnected(): boolean {
|
|
265
|
-
if (this._config?.mock?.mode === 'replace'
|
|
266
|
-
return
|
|
294
|
+
if (this._config?.mock?.mode === 'replace') {
|
|
295
|
+
return (
|
|
296
|
+
this._mockState === 'open' ||
|
|
297
|
+
this._mockState === 'connecting' ||
|
|
298
|
+
this._mockState === 'reconnecting'
|
|
299
|
+
);
|
|
267
300
|
}
|
|
268
301
|
return this._native.isConnected();
|
|
269
302
|
}
|
|
@@ -278,6 +311,13 @@ export class NitroSseClient implements SseClient {
|
|
|
278
311
|
return this._native.getStats();
|
|
279
312
|
}
|
|
280
313
|
|
|
314
|
+
getState(): SseState {
|
|
315
|
+
if (this._config?.mock?.mode === 'replace') {
|
|
316
|
+
return this._mockState ?? 'idle';
|
|
317
|
+
}
|
|
318
|
+
return this._native.getState();
|
|
319
|
+
}
|
|
320
|
+
|
|
281
321
|
updateHeaders(headers: Record<string, string>): void {
|
|
282
322
|
this._native.updateHeaders(headers);
|
|
283
323
|
}
|
package/src/SseInterface.ts
CHANGED
|
@@ -11,8 +11,28 @@ export type HttpMethod = 'get' | 'post';
|
|
|
11
11
|
* - `error`: Connection error or other failure.
|
|
12
12
|
* - `close`: Connection closed (not typical for SSE, but used for cleanup).
|
|
13
13
|
* - `heartbeat`: Ping/Keep-alive signal.
|
|
14
|
+
* - `state`: Connection state change.
|
|
14
15
|
*/
|
|
15
|
-
export type SseEventType =
|
|
16
|
+
export type SseEventType =
|
|
17
|
+
| 'open'
|
|
18
|
+
| 'message'
|
|
19
|
+
| 'error'
|
|
20
|
+
| 'close'
|
|
21
|
+
| 'heartbeat'
|
|
22
|
+
| 'state';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* State of the SSE connection.
|
|
26
|
+
*/
|
|
27
|
+
export type SseState =
|
|
28
|
+
| 'idle'
|
|
29
|
+
| 'connecting'
|
|
30
|
+
| 'open'
|
|
31
|
+
| 'stale'
|
|
32
|
+
| 'reconnecting'
|
|
33
|
+
| 'paused'
|
|
34
|
+
| 'closed'
|
|
35
|
+
| 'failed';
|
|
16
36
|
|
|
17
37
|
/**
|
|
18
38
|
* Configuration for the SSE connection.
|
|
@@ -184,6 +204,8 @@ export interface SseEvent {
|
|
|
184
204
|
statusCode?: number;
|
|
185
205
|
/** Server-requested retry delay in milliseconds. */
|
|
186
206
|
retry?: number;
|
|
207
|
+
/** The current connection state (only available if type is 'state'). */
|
|
208
|
+
state?: SseState;
|
|
187
209
|
}
|
|
188
210
|
|
|
189
211
|
/**
|
|
@@ -245,4 +267,6 @@ export interface SseClient {
|
|
|
245
267
|
setLastProcessedId(id: string): void;
|
|
246
268
|
/** Manually inject a mock event into the stream (for testing/debugging). */
|
|
247
269
|
injectMockEvent(event: Partial<SseEvent>): void;
|
|
270
|
+
/** Get current connection state. */
|
|
271
|
+
getState(): SseState;
|
|
248
272
|
}
|