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

Improve polyfill #80

Merged
merged 2 commits into from Jul 18, 2017
Merged

Improve polyfill #80

merged 2 commits into from Jul 18, 2017

Conversation

neftaly
Copy link
Contributor

@neftaly neftaly commented Jun 21, 2017

This PR prevents the polyfill from creating window.EventSourcePolyfill, and adds node support

@@ -1,5 +1,6 @@
var EventSource = require('./eventsource')
if (typeof window === 'object') {
window.EventSource = window.EventSource || EventSource
} else if (typeof global === 'object') {
global.EventSource = global.EventSource || EventSource
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it better to create the window object in node, or attach to global instead?
eg if (typeof window !== object) global.window = {}

new EventSource vs new window.EventSource

Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm… I'd say neither. In node, we'd probably just want to set module.exports. So, module.exports = EventSource. Take a look at the UMD spec: https://github.com/umdjs/umd

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm hoping to not have to do this in a module that's designed for both node & browser:

const EventSource = (typeof window === 'object' && window.EventSource)
  ? window.EventSource
  : require('eventsource');

Are you ok with something like this for eventsource-polyfill.js:

var EventSource = require('./eventsource')
if (typeof window === 'object') {
  if (!window.EventSource) window.EventSource = EventSource
  module.exports = window.EventSource;
} else {
  module.exports = EventSource;
}

That way, eventsource-polyfill becomes a commonjs module (that also polyfills the browser).

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems fine in principle, but you'll probably want to add a check to make sure module is a valid thing before setting it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I guess you have to cover for people doing <script src='eventsource-polyfill.js'> 😒

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wait, no - you wouldn't need to, cause then require wouldn't work!

Copy link
Contributor

@joeybaker joeybaker left a comment

Choose a reason for hiding this comment

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

@rexxars This seems fine to me. I don't see the need for the additional global and this solves things in a nice way.

@piranna
Copy link
Member

piranna commented Jun 21, 2017 via email

@neftaly
Copy link
Contributor Author

neftaly commented Jun 21, 2017

Ok, I'm using this in node on the backend and in webpack on the frontend however (both via ES6 import).

@rexxars
Copy link
Member

rexxars commented Jun 23, 2017

I don't have any problems with the node compatibility, but I'd rather not break backwards compatibility at this point. If you reintroduced window.EventSourcePolyfill I will be happy to merge.

@neftaly
Copy link
Contributor Author

neftaly commented Jul 16, 2017

Ping @rexxars

@rexxars rexxars merged commit 42805b9 into EventSource:master Jul 18, 2017
@rexxars
Copy link
Member

rexxars commented Jul 18, 2017

Thanks for pinging and sorry for the delay. Released as 1.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants