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

Check for a global EventSource before deciding what to use. #389

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Changes from 1 commit
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
Next Next commit
Check for a global EventSource before deciding what to use.
This approach gives you the flexibility to inject polyfills in other environments like react-native.
  • Loading branch information
fnando committed Jul 26, 2019
commit 2a116858ef0ddce5983cdce0ced27af9317c8b78
13 changes: 9 additions & 4 deletions src/call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ export interface EventSourceOptions<T> {
}

let EventSource: Constructable<EventSource>;
const anyGlobal = global as any;

if (isNode) {
if (anyGlobal.EventSource) {
EventSource = anyGlobal.EventSource;
} else if (isNode) {
/* tslint:disable-next-line:no-var-requires */
EventSource = require("eventsource");
} else {
/* tslint:disable-next-line:variable-name */
EventSource = (global as any).window.EventSource;
EventSource = anyGlobal.window.EventSource;
}

/**
Expand Down Expand Up @@ -149,7 +151,10 @@ export class CallBuilder<
createEventSource();
return function close() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed before that this function keeps trying to write to es even if the assignment fails. I feel like it should return in the catch rather than continuing with the function. Can you make that fix too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely! :)

clearTimeout(timeout);
es.close();

if (es) {
es.close();
}
};
}

Expand Down