Skip to content

Commit

Permalink
chore: Use error filter in rn event source (#322)
Browse files Browse the repository at this point in the history
Pass `errorFilter` to rn eventSource to specify custom retry logic and
error handling.
  • Loading branch information
yusinto authored Dec 2, 2023
1 parent a343c83 commit 4371870
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
10 changes: 9 additions & 1 deletion packages/sdk/react-native/src/ReactNativeLDClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
base64UrlEncode,
BasicLogger,
LDClientImpl,
type LDContext,
type LDOptions,
Expand All @@ -9,7 +10,14 @@ import platform from './platform';

export default class ReactNativeLDClient extends LDClientImpl {
constructor(sdkKey: string, options: LDOptions = {}) {
super(sdkKey, platform, options);
const logger =
options.logger ??
new BasicLogger({
level: 'info',
// eslint-disable-next-line no-console
destination: console.log,
});
super(sdkKey, platform, { ...options, logger });
}

override createStreamUriPath(context: LDContext) {
Expand Down
6 changes: 4 additions & 2 deletions packages/sdk/react-native/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import RNEventSource from './react-native-sse';

class PlatformRequests implements Requests {
createEventSource(url: string, eventSourceInitDict: EventSourceInitDict): EventSource {
// TODO: add retry logic
return new RNEventSource<EventName>(url, eventSourceInitDict);
return new RNEventSource<EventName>(url, {
headers: eventSourceInitDict.headers,
retryAndHandleError: eventSourceInitDict.errorFilter,
});
}

fetch(url: string, options?: Options): Promise<Response> {
Expand Down
25 changes: 21 additions & 4 deletions packages/sdk/react-native/src/react-native-sse/EventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const defaultOptions: EventSourceOptions = {
method: 'GET',
pollingInterval: 5000,
timeout: 0,
timeoutBeforeConnection: 500,
timeoutBeforeConnection: 0,
withCredentials: false,
retryAndHandleError: undefined,
};

export default class EventSource<E extends string = never> {
Expand Down Expand Up @@ -49,6 +50,7 @@ export default class EventSource<E extends string = never> {
private xhr: XMLHttpRequest = new XMLHttpRequest();
private pollTimer: any;
private pollingInterval: number;
private retryAndHandleError?: (err: any) => boolean;

constructor(url: string, options?: EventSourceOptions) {
const opts = {
Expand All @@ -65,6 +67,7 @@ export default class EventSource<E extends string = never> {
this.body = opts.body;
this.debug = opts.debug!;
this.pollingInterval = opts.pollingInterval!;
this.retryAndHandleError = opts.retryAndHandleError;

this.pollAgain(this.timeoutBeforeConnection, true);
}
Expand Down Expand Up @@ -147,7 +150,21 @@ export default class EventSource<E extends string = never> {

if (this.xhr.readyState === XMLHttpRequest.DONE) {
this.logDebug('[EventSource][onreadystatechange][ERROR] Response status error.');
this.pollAgain(this.pollingInterval, false);

if (!this.retryAndHandleError) {
// default implementation
this.pollAgain(this.pollingInterval, false);
} else {
// custom retry logic
const shouldRetry = this.retryAndHandleError({
status: this.xhr.status,
message: this.xhr.responseText,
});

if (shouldRetry) {
this.pollAgain(this.pollingInterval, true);
}
}
}
}
};
Expand Down Expand Up @@ -290,7 +307,7 @@ export default class EventSource<E extends string = never> {
this.onerror(data);
break;
case 'retry':
this.onretrying();
this.onretrying({ delayMillis: this.pollingInterval });
break;
default:
break;
Expand All @@ -310,5 +327,5 @@ export default class EventSource<E extends string = never> {
onopen() {}
onclose() {}
onerror(_err: any) {}
onretrying() {}
onretrying(_e: any) {}
}
1 change: 1 addition & 0 deletions packages/sdk/react-native/src/react-native-sse/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface EventSourceOptions {
body?: any;
debug?: boolean;
pollingInterval?: number;
retryAndHandleError?: (err: any) => boolean;
}

type BuiltInEventMap = {
Expand Down

0 comments on commit 4371870

Please sign in to comment.