Skip to content

Handle cases where self isn't defined - #253

Merged
mislav merged 1 commit into
JakeChampion:masterfrom
billinghamj:polyfill-self
Jan 19, 2016
Merged

mislav merged 1 commit into
JakeChampion:masterfrom
billinghamj:polyfill-self

Conversation

@billinghamj

Copy link
Copy Markdown
Contributor

As per: #125 (comment)

Resolves #252, #238, #165, #125, #124, and probably a few others.

There are a lot of different ways this code could be written. If you have style preferences contrary to the way I've done this, I'd be very happy to change it.

@mislav

mislav commented Jan 9, 2016

Copy link
Copy Markdown
Contributor

Looks good!

@dgraham Any objections?

@dgraham

dgraham commented Jan 9, 2016

Copy link
Copy Markdown
Contributor

Invoking the wrapper function with this resolves to window, which is how we started all the way back in c1af6de. This looks good to me. :shipit:

@chrisirhc

Copy link
Copy Markdown

Wouldn't this still throw a ReferenceError exception where self isn't defined? How about doing a typeof check like typeof self !== 'undefined' ? self : this ?

@mislav

mislav commented Jan 10, 2016

Copy link
Copy Markdown
Contributor

Good point @chrisirhc. Someone should test this out in an isomorphic environment and report back. I have no idea how it would handle the self || this construct.

@billinghamj

Copy link
Copy Markdown
Contributor Author

Hmm, yes that is true. Another option could be:

this.self || this

Which works exactly the same way but should not throw.

@billinghamj

Copy link
Copy Markdown
Contributor Author

I have updated the code as per the above comment.

@mislav

mislav commented Jan 11, 2016

Copy link
Copy Markdown
Contributor

Can someone test this out in an isomorphic environment, as per people's complaints on aforementioned threads?

@billinghamj

Copy link
Copy Markdown
Contributor Author

Node - master branch:

$ node
> var a = require('./fetch')
ReferenceError: self is not defined
    at /Users/james/Projects/fetch/fetch.js:4:7
    at Object.<anonymous> (/Users/james/Projects/fetch/fetch.js:381:3)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at repl:1:9
    at REPLServer.defaultEval (repl.js:254:27)

Node - this PR:

$ node
> var a = require('./fetch')
undefined
> a
{ Headers: [Function: Headers],
  Request: [Function: Request],
  Response: { [Function: Response] error: [Function], redirect: [Function] },
  fetch: { [Function] polyfill: true } }

React Native - master
React Native - this PR (this new error is a separate issue from isomorphic-fetch)

I can't speak for other environments though

billinghamj added a commit to billinghamj/isomorphic-fetch that referenced this pull request Jan 11, 2016
billinghamj added a commit to billinghamj/isomorphic-fetch that referenced this pull request Jan 11, 2016
Comment thread fetch.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel like I would like to revert to @chrisirhc's idea by doing

typeof self !== 'undefined' ? self : this

simply because that approach has been better battle-tested by compilers such as Browserify.

@billinghamj

Copy link
Copy Markdown
Contributor Author

@mislav updated as-per your comment

mislav added a commit that referenced this pull request Jan 19, 2016
Handle cases where `self` isn't defined
@mislav
mislav merged commit f4fc29f into JakeChampion:master Jan 19, 2016
@mislav

mislav commented Jan 19, 2016

Copy link
Copy Markdown
Contributor

Thank you!

@mislav

mislav commented Jan 19, 2016

Copy link
Copy Markdown
Contributor

@yorkie Something's wrong with our build system and auto-publish on npm. Stay tuned.

@mislav

mislav commented Jan 19, 2016 via email

Copy link
Copy Markdown
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possibility for a 'self' polyfill

4 participants