Skip to content

Commit

Permalink
fix: Handle non-status code errors for streaming connection. (#533)
Browse files Browse the repository at this point in the history
Also the buttons had gotten too tall, so I re-arranged them.

![image](https://github.com/user-attachments/assets/9d3becaa-d643-4a4d-84a6-799d7fb285cf)
  • Loading branch information
kinyoklion committed Aug 1, 2024
1 parent c1490e2 commit fc4645e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
40 changes: 24 additions & 16 deletions packages/sdk/react-native/example/src/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Welcome() {
<Text>
{flagKey}: {`${flagValue}`}
</Text>
<ScrollView style={{ flexGrow: 0.2, backgroundColor: 'black', maxHeight: 200 }}>
<ScrollView style={{ flexGrow: 0.5, backgroundColor: 'black', maxHeight: 200 }}>
<Text style={{ color: 'orange' }}>Logging: {JSON.stringify(context, null, 2)}</Text>
</ScrollView>
<TextInput
Expand All @@ -50,21 +50,23 @@ export default function Welcome() {
value={flagKey}
testID="flagKey"
/>
<TouchableOpacity style={styles.buttonContainer} onPress={() => setConnectionMode('offline')}>
<Text style={styles.buttonText}>Set offline</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => setConnectionMode('streaming')}
>
<Text style={styles.buttonText}>Set streaming</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => setConnectionMode('polling')}
>
<Text style={styles.buttonText}>Set polling</Text>
</TouchableOpacity>
<View style={styles.connectionModeContainer}>
<TouchableOpacity style={styles.buttonContainer} onPress={() => setConnectionMode('offline')}>
<Text style={styles.buttonText}>Set offline</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => setConnectionMode('streaming')}
>
<Text style={styles.buttonText}>Set streaming</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => setConnectionMode('polling')}
>
<Text style={styles.buttonText}>Set polling</Text>
</TouchableOpacity>
</View>
</View>
);
}
Expand All @@ -75,6 +77,12 @@ const styles = StyleSheet.create({
alignItems: 'center',
justifyContent: 'center',
},
connectionModeContainer: {
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-around',
},
input: {
height: 40,
margin: 12,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ export default class EventSource<E extends string = never> {

private tryConnect(initialConnection: boolean = false) {
let delay = initialConnection ? 0 : this.getNextRetryDelay();
if(initialConnection) {
if (initialConnection) {
this.logger?.debug(`[EventSource] opening new connection.`)
} else {
this.logger?.debug(`[EventSource] Will open new connection in ${delay} ms.`);
this.dispatch('retry', { type: 'retry', delayMillis: delay });
}

this.connectTimer = setTimeout(() => {
if(!initialConnection) {
if (!initialConnection) {
this.close();
}

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

this.logger?.debug(
`[EventSource][onreadystatechange] ReadyState: ${
XMLReadyStateMap[this.xhr.readyState] || 'Unknown'
`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[this.xhr.readyState] || 'Unknown'
}(${this.xhr.readyState}), status: ${this.xhr.status}`,
);

Expand All @@ -165,8 +164,9 @@ export default class EventSource<E extends string = never> {
this.logger?.debug('[EventSource][onreadystatechange][DONE] Operation done.');
this.tryConnect();
}
} else if (this.xhr.status !== 0) {
} else {
this.status = this.ERROR;

this.dispatch('error', {
type: 'error',
message: this.xhr.responseText,
Expand Down Expand Up @@ -349,8 +349,8 @@ export default class EventSource<E extends string = never> {
return this.status;
}

onopen() {}
onclose() {}
onerror(_err: any) {}
onretrying(_e: any) {}
onopen() { }
onclose() { }
onerror(_err: any) { }
onretrying(_e: any) { }
}

0 comments on commit fc4645e

Please sign in to comment.