react-native-nitro-sse 2.4.0 → 2.4.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/src/main/java/com/margelo/nitro/nitrosse/NetworkInspector.kt +0 -2
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +41 -23
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +2 -2
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +0 -3
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +4 -0
- package/ios/NitroSse.swift +39 -23
- package/ios/SseConnectionHandler.swift +26 -3
- package/ios/SseNetworkMonitor.swift +3 -2
- package/ios/SseReconnectStrategy.swift +5 -0
- package/lib/module/MockSseEngine.js +212 -0
- package/lib/module/MockSseEngine.js.map +1 -0
- package/lib/module/NitroSseClient.js +40 -216
- package/lib/module/NitroSseClient.js.map +1 -1
- package/lib/module/SseDriver.js +125 -0
- package/lib/module/SseDriver.js.map +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/MockSseEngine.d.ts +31 -0
- package/lib/typescript/src/MockSseEngine.d.ts.map +1 -0
- package/lib/typescript/src/NitroSseClient.d.ts +5 -5
- package/lib/typescript/src/NitroSseClient.d.ts.map +1 -1
- package/lib/typescript/src/SseDriver.d.ts +72 -0
- package/lib/typescript/src/SseDriver.d.ts.map +1 -0
- package/lib/typescript/src/SseInterface.d.ts +2 -0
- package/lib/typescript/src/SseInterface.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JSseConfig.hpp +2 -0
- package/nitrogen/generated/android/c++/JSseMockConfig.hpp +2 -0
- package/nitrogen/generated/android/c++/JSseMockEvent.hpp +9 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockEvent.kt +9 -4
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +15 -15
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +3 -3
- package/nitrogen/generated/ios/swift/SseMockEvent.swift +12 -1
- package/nitrogen/generated/shared/c++/SseMockEvent.hpp +9 -2
- package/package.json +5 -7
- package/src/MockSseEngine.ts +254 -0
- package/src/NitroSseClient.ts +54 -237
- package/src/SseDriver.ts +161 -0
- package/src/SseInterface.ts +2 -0
- package/src/index.ts +1 -1
package/src/NitroSseClient.ts
CHANGED
|
@@ -7,77 +7,75 @@ import type {
|
|
|
7
7
|
SseStats,
|
|
8
8
|
SseState,
|
|
9
9
|
} from './SseInterface';
|
|
10
|
+
import { MockSseEngine } from './MockSseEngine';
|
|
11
|
+
import {
|
|
12
|
+
type SseDriver,
|
|
13
|
+
NativeDriver,
|
|
14
|
+
MockReplaceDriver,
|
|
15
|
+
MockInjectDriver,
|
|
16
|
+
} from './SseDriver';
|
|
10
17
|
|
|
11
18
|
declare const __DEV__: boolean | undefined;
|
|
12
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Public facade and typed event emitter for NitroSse, delegating streaming execution to an SseDriver strategy.
|
|
22
|
+
*/
|
|
13
23
|
export class NitroSseClient implements SseClient {
|
|
14
24
|
private _native: NitroSse;
|
|
25
|
+
private _driver: SseDriver;
|
|
15
26
|
private _listeners: Map<string, Set<SseListener>> = new Map();
|
|
16
27
|
private _legacyCallback?: (events: SseEvent[]) => void;
|
|
17
28
|
private _config?: SseConfig;
|
|
18
|
-
private _mockIntervalId?: any;
|
|
19
|
-
private _mockIndex: number = 0;
|
|
20
|
-
|
|
21
|
-
private _mockState?: SseState;
|
|
22
29
|
|
|
23
30
|
constructor(native: NitroSse) {
|
|
24
31
|
this._native = native;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
}
|
|
32
|
+
this._driver = new NativeDriver(native, (events) =>
|
|
33
|
+
this._dispatchEvents(events)
|
|
34
|
+
);
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
setup(config: SseConfig, onEvent?: (events: SseEvent[]) => void): void {
|
|
38
|
+
this._driver?.stop();
|
|
37
39
|
this._config = { ...config };
|
|
38
40
|
this._legacyCallback = onEvent;
|
|
39
|
-
this._mockState = undefined;
|
|
40
41
|
|
|
41
42
|
const isDev = typeof __DEV__ !== 'undefined' ? __DEV__ === true : false;
|
|
43
|
+
const mockConfig = isDev ? this._config.mock : undefined;
|
|
42
44
|
if (!isDev) {
|
|
43
45
|
delete this._config.mock;
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
if (
|
|
48
|
+
if (mockConfig) {
|
|
47
49
|
console.warn(
|
|
48
50
|
'\n' +
|
|
49
51
|
'=================================================================\n' +
|
|
50
52
|
'⚠️ [react-native-nitro-sse] WARNING: MOCK STREAMING IS ENABLED! ⚠️\n' +
|
|
51
|
-
` Mode: ${
|
|
52
|
-
|
|
53
|
+
` Mode: ${mockConfig.mode.toUpperCase()} | Speed: ${
|
|
54
|
+
mockConfig.eventsPerSecond ?? 1
|
|
53
55
|
} events/sec\n` +
|
|
54
56
|
' Please ensure mocking is disabled before building for production!\n' +
|
|
55
57
|
'=================================================================\n'
|
|
56
58
|
);
|
|
57
|
-
|
|
59
|
+
const mockEngine = new MockSseEngine(mockConfig, (events) => {
|
|
60
|
+
this._dispatchEvents(events);
|
|
61
|
+
});
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
this._driver =
|
|
64
|
+
mockConfig.mode === 'replace'
|
|
65
|
+
? new MockReplaceDriver(mockEngine)
|
|
66
|
+
: new MockInjectDriver(this._native, mockEngine);
|
|
67
|
+
} else {
|
|
68
|
+
this._driver = new NativeDriver(this._native, (events) =>
|
|
69
|
+
this._dispatchEvents(events)
|
|
70
|
+
);
|
|
62
71
|
}
|
|
63
72
|
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
for (const event of events) {
|
|
71
|
-
// Dispatch basic types: 'open', 'message', 'error', 'close', 'heartbeat'
|
|
72
|
-
this._emit(event.type, event);
|
|
73
|
-
|
|
74
|
-
// Dispatch by SSE event name if it's different from the type
|
|
75
|
-
// e.g., if type is 'message' but event name is 'update'
|
|
76
|
-
if (event.event && event.event !== event.type) {
|
|
77
|
-
this._emit(event.event, event);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
});
|
|
73
|
+
// Wrap the native setup to dispatch events to typed listeners when native streaming is active
|
|
74
|
+
if (mockConfig?.mode !== 'replace') {
|
|
75
|
+
this._native.setup(this._config, (events) => {
|
|
76
|
+
this._dispatchEvents(events);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
addEventListener(type: string, listener: SseListener): void {
|
|
@@ -104,234 +102,53 @@ export class NitroSseClient implements SseClient {
|
|
|
104
102
|
}
|
|
105
103
|
}
|
|
106
104
|
|
|
107
|
-
private
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
event: rawMock.event,
|
|
114
|
-
message: rawMock.message,
|
|
115
|
-
statusCode: rawMock.statusCode ?? 200,
|
|
116
|
-
retry: rawMock.retry,
|
|
117
|
-
state: rawMock.state,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
start(): void {
|
|
122
|
-
if (this._mockIntervalId) {
|
|
123
|
-
clearTimeout(this._mockIntervalId);
|
|
124
|
-
this._mockIntervalId = undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (this._config?.mock) {
|
|
128
|
-
const {
|
|
129
|
-
mode,
|
|
130
|
-
data,
|
|
131
|
-
eventsPerSecond = 1,
|
|
132
|
-
loop = false,
|
|
133
|
-
errorRate = 0,
|
|
134
|
-
} = this._config.mock;
|
|
135
|
-
this._mockIndex = 0;
|
|
136
|
-
|
|
137
|
-
// Validate and normalize eventsPerSecond (must be a finite positive number, default to 1)
|
|
138
|
-
let validatedEventsPerSecond = Number(eventsPerSecond);
|
|
139
|
-
if (
|
|
140
|
-
Number.isNaN(validatedEventsPerSecond) ||
|
|
141
|
-
!Number.isFinite(validatedEventsPerSecond) ||
|
|
142
|
-
validatedEventsPerSecond <= 0
|
|
143
|
-
) {
|
|
144
|
-
validatedEventsPerSecond = 1;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Validate and normalize errorRate (must be a finite number within [0,1], default to 0)
|
|
148
|
-
let validatedErrorRate = Number(errorRate);
|
|
149
|
-
if (
|
|
150
|
-
Number.isNaN(validatedErrorRate) ||
|
|
151
|
-
!Number.isFinite(validatedErrorRate)
|
|
152
|
-
) {
|
|
153
|
-
validatedErrorRate = 0;
|
|
154
|
-
} else {
|
|
155
|
-
validatedErrorRate = Math.max(0, Math.min(1, validatedErrorRate));
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const delayMs = 1000 / validatedEventsPerSecond;
|
|
159
|
-
// Safeguard: if eventsPerSecond is huge (e.g. 1000), setInterval/setTimeout is too slow.
|
|
160
|
-
// We batch events together in chunks if the calculated interval is under 10ms.
|
|
161
|
-
const batchSize = Math.max(1, Math.round(validatedEventsPerSecond / 100)); // chunk size for 10ms intervals
|
|
162
|
-
const intervalMs = Math.max(10, delayMs * batchSize);
|
|
163
|
-
|
|
164
|
-
const scheduleNext = () => {
|
|
165
|
-
if (this._mockIndex >= data.length) {
|
|
166
|
-
if (loop && data.length > 0) {
|
|
167
|
-
this._mockIndex = 0;
|
|
168
|
-
} else {
|
|
169
|
-
if (this._mockIntervalId) {
|
|
170
|
-
clearTimeout(this._mockIntervalId);
|
|
171
|
-
this._mockIntervalId = undefined;
|
|
172
|
-
}
|
|
173
|
-
if (mode === 'replace') {
|
|
174
|
-
this._setMockState('closed');
|
|
175
|
-
}
|
|
176
|
-
const closeEvent: SseEvent = { type: 'close', statusCode: 200 };
|
|
177
|
-
this._legacyCallback?.([closeEvent]);
|
|
178
|
-
this._emit('close', closeEvent);
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// Simulate connection drops
|
|
184
|
-
if (validatedErrorRate && Math.random() < validatedErrorRate) {
|
|
185
|
-
if (mode === 'replace') {
|
|
186
|
-
this._setMockState('reconnecting');
|
|
187
|
-
}
|
|
188
|
-
const errorEvent: SseEvent = {
|
|
189
|
-
type: 'error',
|
|
190
|
-
message: 'Mock Connection Drop (Simulated Error)',
|
|
191
|
-
statusCode: 500,
|
|
192
|
-
};
|
|
193
|
-
this._legacyCallback?.([errorEvent]);
|
|
194
|
-
this._emit('error', errorEvent);
|
|
195
|
-
|
|
196
|
-
// Retry / reconnect delay simulator (e.g. 2000ms)
|
|
197
|
-
this._mockIntervalId = setTimeout(scheduleNext, 2000);
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const currentItem = data[this._mockIndex];
|
|
202
|
-
const hasCustomDelay =
|
|
203
|
-
currentItem && typeof currentItem.delayMs === 'number';
|
|
204
|
-
|
|
205
|
-
const batch: SseEvent[] = [];
|
|
206
|
-
if (hasCustomDelay) {
|
|
207
|
-
const rawMock = data[this._mockIndex++];
|
|
208
|
-
if (rawMock) {
|
|
209
|
-
batch.push(this._createSseEvent(rawMock));
|
|
210
|
-
}
|
|
211
|
-
} else {
|
|
212
|
-
for (let i = 0; i < batchSize && this._mockIndex < data.length; i++) {
|
|
213
|
-
const nextItem = data[this._mockIndex];
|
|
214
|
-
if (nextItem && typeof nextItem.delayMs === 'number') {
|
|
215
|
-
break;
|
|
216
|
-
}
|
|
217
|
-
const rawMock = data[this._mockIndex++];
|
|
218
|
-
if (rawMock) {
|
|
219
|
-
batch.push(this._createSseEvent(rawMock));
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (batch.length > 0) {
|
|
225
|
-
if (mode === 'replace') {
|
|
226
|
-
this._setMockState('open');
|
|
227
|
-
}
|
|
228
|
-
// 1. Call legacy batch callback if provided
|
|
229
|
-
this._legacyCallback?.(batch);
|
|
230
|
-
|
|
231
|
-
// 2. Dispatch individual events to registered listeners
|
|
232
|
-
for (const event of batch) {
|
|
233
|
-
this._emit(event.type, event);
|
|
234
|
-
if (event.event && event.event !== event.type) {
|
|
235
|
-
this._emit(event.event, event);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const nextDelay = hasCustomDelay ? currentItem.delayMs! : intervalMs;
|
|
241
|
-
this._mockIntervalId = setTimeout(scheduleNext, nextDelay);
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
const firstItem = data[0];
|
|
245
|
-
const firstDelay =
|
|
246
|
-
firstItem && typeof firstItem.delayMs === 'number'
|
|
247
|
-
? firstItem.delayMs
|
|
248
|
-
: intervalMs;
|
|
249
|
-
|
|
250
|
-
if (mode === 'replace') {
|
|
251
|
-
this._setMockState('connecting');
|
|
252
|
-
this._setMockState('open');
|
|
253
|
-
|
|
254
|
-
// Emit simulated open event
|
|
255
|
-
const openEvent: SseEvent = { type: 'open', statusCode: 200 };
|
|
256
|
-
this._legacyCallback?.([openEvent]);
|
|
257
|
-
this._emit('open', openEvent);
|
|
258
|
-
|
|
259
|
-
this._mockIntervalId = setTimeout(scheduleNext, firstDelay);
|
|
260
|
-
return; // Skip native start
|
|
261
|
-
} else if (mode === 'inject') {
|
|
262
|
-
this._mockIntervalId = setTimeout(scheduleNext, firstDelay);
|
|
105
|
+
private _dispatchEvents(events: SseEvent[]): void {
|
|
106
|
+
this._legacyCallback?.(events);
|
|
107
|
+
for (const event of events) {
|
|
108
|
+
this._emit(event.type, event);
|
|
109
|
+
if (event.event && event.event !== event.type) {
|
|
110
|
+
this._emit(event.event, event);
|
|
263
111
|
}
|
|
264
112
|
}
|
|
113
|
+
}
|
|
265
114
|
|
|
266
|
-
|
|
115
|
+
start(): void {
|
|
116
|
+
this._driver.start();
|
|
267
117
|
}
|
|
268
118
|
|
|
269
119
|
stop(): void {
|
|
270
|
-
|
|
271
|
-
clearTimeout(this._mockIntervalId);
|
|
272
|
-
this._mockIntervalId = undefined;
|
|
273
|
-
}
|
|
274
|
-
if (this._config?.mock?.mode === 'replace') {
|
|
275
|
-
this._setMockState('closed');
|
|
276
|
-
}
|
|
277
|
-
this._native.stop();
|
|
120
|
+
this._driver.stop();
|
|
278
121
|
}
|
|
279
122
|
|
|
280
123
|
restart(): void {
|
|
281
|
-
|
|
282
|
-
this.stop();
|
|
283
|
-
this.start();
|
|
284
|
-
} else {
|
|
285
|
-
this._native.restart();
|
|
286
|
-
}
|
|
124
|
+
this._driver.restart();
|
|
287
125
|
}
|
|
288
126
|
|
|
289
127
|
flush(): void {
|
|
290
|
-
this.
|
|
128
|
+
this._driver.flush();
|
|
291
129
|
}
|
|
292
130
|
|
|
293
131
|
isConnected(): boolean {
|
|
294
|
-
|
|
295
|
-
return (
|
|
296
|
-
this._mockState === 'open' ||
|
|
297
|
-
this._mockState === 'connecting' ||
|
|
298
|
-
this._mockState === 'reconnecting'
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
return this._native.isConnected();
|
|
132
|
+
return this._driver.isConnected();
|
|
302
133
|
}
|
|
303
134
|
|
|
304
135
|
getStats(): SseStats {
|
|
305
|
-
|
|
306
|
-
return {
|
|
307
|
-
totalBytesReceived: this._mockIndex * 150,
|
|
308
|
-
reconnectCount: 0,
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
return this._native.getStats();
|
|
136
|
+
return this._driver.getStats();
|
|
312
137
|
}
|
|
313
138
|
|
|
314
139
|
getState(): SseState {
|
|
315
|
-
|
|
316
|
-
return this._mockState ?? 'idle';
|
|
317
|
-
}
|
|
318
|
-
return this._native.getState();
|
|
140
|
+
return this._driver.getState();
|
|
319
141
|
}
|
|
320
142
|
|
|
321
143
|
updateHeaders(headers: Record<string, string>): void {
|
|
322
|
-
this.
|
|
144
|
+
this._driver.updateHeaders(headers);
|
|
323
145
|
}
|
|
324
146
|
|
|
325
147
|
setLastProcessedId(id: string): void {
|
|
326
|
-
this.
|
|
148
|
+
this._driver.setLastProcessedId(id);
|
|
327
149
|
}
|
|
328
150
|
|
|
329
151
|
injectMockEvent(event: Partial<SseEvent>): void {
|
|
330
|
-
|
|
331
|
-
this._legacyCallback?.([sseEvent]);
|
|
332
|
-
this._emit(sseEvent.type, sseEvent);
|
|
333
|
-
if (sseEvent.event && sseEvent.event !== sseEvent.type) {
|
|
334
|
-
this._emit(sseEvent.event, sseEvent);
|
|
335
|
-
}
|
|
152
|
+
this._driver.injectMockEvent(event);
|
|
336
153
|
}
|
|
337
154
|
}
|
package/src/SseDriver.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { NitroSse } from './NitroSse.nitro';
|
|
2
|
+
import type { SseEvent, SseState, SseStats } from './SseInterface';
|
|
3
|
+
import { MockSseEngine } from './MockSseEngine';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Internal execution strategy driving the SSE lifecycle.
|
|
7
|
+
*/
|
|
8
|
+
export interface SseDriver {
|
|
9
|
+
start(): void;
|
|
10
|
+
stop(): void;
|
|
11
|
+
restart(): void;
|
|
12
|
+
flush(): void;
|
|
13
|
+
isConnected(): boolean;
|
|
14
|
+
getStats(): SseStats;
|
|
15
|
+
getState(): SseState;
|
|
16
|
+
updateHeaders(headers: Record<string, string>): void;
|
|
17
|
+
setLastProcessedId(id: string): void;
|
|
18
|
+
injectMockEvent(event: Partial<SseEvent>): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Driver delegating all operations to the native JSI NitroSse module.
|
|
23
|
+
*/
|
|
24
|
+
export class NativeDriver implements SseDriver {
|
|
25
|
+
constructor(
|
|
26
|
+
private _native: NitroSse,
|
|
27
|
+
private _dispatchEvents: (events: SseEvent[]) => void
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
start(): void {
|
|
31
|
+
this._native.start();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
stop(): void {
|
|
35
|
+
this._native.stop();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
restart(): void {
|
|
39
|
+
this._native.restart();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
flush(): void {
|
|
43
|
+
this._native.flush();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
isConnected(): boolean {
|
|
47
|
+
return this._native.isConnected();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getStats(): SseStats {
|
|
51
|
+
return this._native.getStats();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getState(): SseState {
|
|
55
|
+
return this._native.getState();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
updateHeaders(headers: Record<string, string>): void {
|
|
59
|
+
this._native.updateHeaders(headers);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
setLastProcessedId(id: string): void {
|
|
63
|
+
this._native.setLastProcessedId(id);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
injectMockEvent(event: Partial<SseEvent>): void {
|
|
67
|
+
const sseEvent = MockSseEngine.createSseEvent(event);
|
|
68
|
+
this._dispatchEvents([sseEvent]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Driver simulating the entire SSE stream in JavaScript without native networking.
|
|
74
|
+
*/
|
|
75
|
+
export class MockReplaceDriver implements SseDriver {
|
|
76
|
+
constructor(private _mockEngine: MockSseEngine) {}
|
|
77
|
+
|
|
78
|
+
start(): void {
|
|
79
|
+
this._mockEngine.start();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
stop(): void {
|
|
83
|
+
this._mockEngine.stop();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
restart(): void {
|
|
87
|
+
this._mockEngine.restart();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
flush(): void {}
|
|
91
|
+
|
|
92
|
+
isConnected(): boolean {
|
|
93
|
+
return this._mockEngine.isConnected();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getStats(): SseStats {
|
|
97
|
+
return this._mockEngine.getStats();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
getState(): SseState {
|
|
101
|
+
return this._mockEngine.getState();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
updateHeaders(_headers: Record<string, string>): void {}
|
|
105
|
+
|
|
106
|
+
setLastProcessedId(_id: string): void {}
|
|
107
|
+
|
|
108
|
+
injectMockEvent(event: Partial<SseEvent>): void {
|
|
109
|
+
this._mockEngine.injectEvent(event);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Driver running native networking in parallel with mock event injection.
|
|
115
|
+
*/
|
|
116
|
+
export class MockInjectDriver implements SseDriver {
|
|
117
|
+
constructor(private _native: NitroSse, private _mockEngine: MockSseEngine) {}
|
|
118
|
+
|
|
119
|
+
start(): void {
|
|
120
|
+
this._mockEngine.start();
|
|
121
|
+
this._native.start();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
stop(): void {
|
|
125
|
+
this._mockEngine.stop();
|
|
126
|
+
this._native.stop();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
restart(): void {
|
|
130
|
+
this._mockEngine.restart();
|
|
131
|
+
this._native.restart();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
flush(): void {
|
|
135
|
+
this._native.flush();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
isConnected(): boolean {
|
|
139
|
+
return this._native.isConnected();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getStats(): SseStats {
|
|
143
|
+
return this._native.getStats();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getState(): SseState {
|
|
147
|
+
return this._native.getState();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
updateHeaders(headers: Record<string, string>): void {
|
|
151
|
+
this._native.updateHeaders(headers);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
setLastProcessedId(id: string): void {
|
|
155
|
+
this._native.setLastProcessedId(id);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
injectMockEvent(event: Partial<SseEvent>): void {
|
|
159
|
+
this._mockEngine.injectEvent(event);
|
|
160
|
+
}
|
|
161
|
+
}
|
package/src/SseInterface.ts
CHANGED
|
@@ -151,6 +151,8 @@ export interface SseMockEvent {
|
|
|
151
151
|
retry?: number;
|
|
152
152
|
/** Custom delay in milliseconds before this event is dispatched. */
|
|
153
153
|
delayMs?: number;
|
|
154
|
+
/** The connection state if this event represents a state change. */
|
|
155
|
+
state?: SseState;
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
/**
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
-
import type { NitroSse } from './NitroSse.nitro';
|
|
3
2
|
import { NitroSseClient } from './NitroSseClient';
|
|
3
|
+
import type { NitroSse } from './NitroSse.nitro';
|
|
4
4
|
import type { SseClient } from './SseInterface';
|
|
5
5
|
|
|
6
6
|
export * from './SseInterface';
|