Skip to content
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

make timeout before connection configurable #16

Merged
merged 2 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ new EventSource(url: string | URL, options?: EventSourceOptions);
const options: EventSourceOptions = {
method: 'GET'; // Request method. Default: GET
timeout: 0; // Time after which the connection will expire without any activity: Default: 0 (no timeout)
timeoutBeforeConnection: 500; // Time to wait before initial connection is made: Default: 500ms
headers: {}; // Your request headers. Default: {}
body: undefined; // Your request body sent on connection: Default: undefined
debug: false; // Show console.debug messages for debugging purpose. Default: false
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface EventSourceOptions {
body?: any;
debug?: boolean;
pollingInterval?: number;
timeoutBeforeConnection?: number;
}

export type EventSourceEvent = MessageEvent | OpenEvent | CloseEvent | TimeoutEvent | ErrorEvent | ExceptionEvent;
Expand Down
3 changes: 2 additions & 1 deletion src/EventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class EventSource {
this.headers = options.headers || {};
this.body = options.body || undefined;
this.debug = options.debug || false;
this.timeoutBeforeConnection = options.timeoutBeforeConnection ?? 500;

this._xhr = null;
this._pollTimer = null;
Expand All @@ -37,7 +38,7 @@ class EventSource {
this.url = url;
}

this._pollAgain(500);
this._pollAgain(this.timeoutBeforeConnection);
}

_pollAgain(time) {
Expand Down