Skip to content

Commit fc4645e

Browse files
committed
fix: Handle non-status code errors for streaming connection. (#533)
Also the buttons had gotten too tall, so I re-arranged them. ![image](https://github.com/user-attachments/assets/9d3becaa-d643-4a4d-84a6-799d7fb285cf)
1 parent c1490e2 commit fc4645e

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

packages/sdk/react-native/example/src/welcome.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function Welcome() {
2929
<Text>
3030
{flagKey}: {`${flagValue}`}
3131
</Text>
32-
<ScrollView style={{ flexGrow: 0.2, backgroundColor: 'black', maxHeight: 200 }}>
32+
<ScrollView style={{ flexGrow: 0.5, backgroundColor: 'black', maxHeight: 200 }}>
3333
<Text style={{ color: 'orange' }}>Logging: {JSON.stringify(context, null, 2)}</Text>
3434
</ScrollView>
3535
<TextInput
@@ -50,21 +50,23 @@ export default function Welcome() {
5050
value={flagKey}
5151
testID="flagKey"
5252
/>
53-
<TouchableOpacity style={styles.buttonContainer} onPress={() => setConnectionMode('offline')}>
54-
<Text style={styles.buttonText}>Set offline</Text>
55-
</TouchableOpacity>
56-
<TouchableOpacity
57-
style={styles.buttonContainer}
58-
onPress={() => setConnectionMode('streaming')}
59-
>
60-
<Text style={styles.buttonText}>Set streaming</Text>
61-
</TouchableOpacity>
62-
<TouchableOpacity
63-
style={styles.buttonContainer}
64-
onPress={() => setConnectionMode('polling')}
65-
>
66-
<Text style={styles.buttonText}>Set polling</Text>
67-
</TouchableOpacity>
53+
<View style={styles.connectionModeContainer}>
54+
<TouchableOpacity style={styles.buttonContainer} onPress={() => setConnectionMode('offline')}>
55+
<Text style={styles.buttonText}>Set offline</Text>
56+
</TouchableOpacity>
57+
<TouchableOpacity
58+
style={styles.buttonContainer}
59+
onPress={() => setConnectionMode('streaming')}
60+
>
61+
<Text style={styles.buttonText}>Set streaming</Text>
62+
</TouchableOpacity>
63+
<TouchableOpacity
64+
style={styles.buttonContainer}
65+
onPress={() => setConnectionMode('polling')}
66+
>
67+
<Text style={styles.buttonText}>Set polling</Text>
68+
</TouchableOpacity>
69+
</View>
6870
</View>
6971
);
7072
}
@@ -75,6 +77,12 @@ const styles = StyleSheet.create({
7577
alignItems: 'center',
7678
justifyContent: 'center',
7779
},
80+
connectionModeContainer: {
81+
display: 'flex',
82+
flexDirection: 'row',
83+
flexWrap: 'wrap',
84+
justifyContent: 'space-around',
85+
},
7886
input: {
7987
height: 40,
8088
margin: 12,

packages/sdk/react-native/src/fromExternal/react-native-sse/EventSource.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ export default class EventSource<E extends string = never> {
9090

9191
private tryConnect(initialConnection: boolean = false) {
9292
let delay = initialConnection ? 0 : this.getNextRetryDelay();
93-
if(initialConnection) {
93+
if (initialConnection) {
9494
this.logger?.debug(`[EventSource] opening new connection.`)
9595
} else {
9696
this.logger?.debug(`[EventSource] Will open new connection in ${delay} ms.`);
9797
this.dispatch('retry', { type: 'retry', delayMillis: delay });
9898
}
9999

100100
this.connectTimer = setTimeout(() => {
101-
if(!initialConnection) {
101+
if (!initialConnection) {
102102
this.close();
103103
}
104104

@@ -138,8 +138,7 @@ export default class EventSource<E extends string = never> {
138138
}
139139

140140
this.logger?.debug(
141-
`[EventSource][onreadystatechange] ReadyState: ${
142-
XMLReadyStateMap[this.xhr.readyState] || 'Unknown'
141+
`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[this.xhr.readyState] || 'Unknown'
143142
}(${this.xhr.readyState}), status: ${this.xhr.status}`,
144143
);
145144

@@ -165,8 +164,9 @@ export default class EventSource<E extends string = never> {
165164
this.logger?.debug('[EventSource][onreadystatechange][DONE] Operation done.');
166165
this.tryConnect();
167166
}
168-
} else if (this.xhr.status !== 0) {
167+
} else {
169168
this.status = this.ERROR;
169+
170170
this.dispatch('error', {
171171
type: 'error',
172172
message: this.xhr.responseText,
@@ -349,8 +349,8 @@ export default class EventSource<E extends string = never> {
349349
return this.status;
350350
}
351351

352-
onopen() {}
353-
onclose() {}
354-
onerror(_err: any) {}
355-
onretrying(_e: any) {}
352+
onopen() { }
353+
onclose() { }
354+
onerror(_err: any) { }
355+
onretrying(_e: any) { }
356356
}

0 commit comments

Comments
 (0)