-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Use error filter in rn event source #322
Changes from all commits
b049c49
d228357
cd075af
a6845fc
0c521e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,9 @@ const defaultOptions: EventSourceOptions = { | |
method: 'GET', | ||
pollingInterval: 5000, | ||
timeout: 0, | ||
timeoutBeforeConnection: 500, | ||
timeoutBeforeConnection: 0, | ||
withCredentials: false, | ||
retryAndHandleError: undefined, | ||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I set |
||
}; | ||
|
||
export default class EventSource<E extends string = never> { | ||
|
@@ -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 = { | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
@@ -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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a bugfix. The StreamingProcessor expects this argument. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so in the future we could add jitter/backoff here. By calculating it and passing it as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I also created a ticket for this work. |
||
break; | ||
default: | ||
break; | ||
|
@@ -310,5 +327,5 @@ export default class EventSource<E extends string = never> { | |
onopen() {} | ||
onclose() {} | ||
onerror(_err: any) {} | ||
onretrying() {} | ||
onretrying(_e: any) {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use console.log instead of error to remove annoying error messages on the simulator during development.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, we may want to make a different client-side basic logger. One that just uses the levels of the error.
Being as, in browser, we generally have these levels and they are more meaningful than stdout,stderr.
Doesn't need to be in this PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a ticket to capture this for the future.