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

Add missing self variable if not defined and when running under node environment II #7787

Closed
wants to merge 6 commits into from

Conversation

simonThiele
Copy link
Contributor

Hi again,

PR #7747 had an issue. You need to check if node is running separately and this is done by this PR. The reasons are the same. Sry for that. Please don't merge this atm because I'm checking how this will work with webworks to get clean with it.

@simonThiele
Copy link
Contributor Author

So, the trick is to use an own scope variable to store the valid scope. In the browser, it's window, on node it's global and in webworker it is self. I validated it in the browser, in the raytracer webworker sandbox example and on node runtime and all passed the test and are working.

BufferGeometryLoader is using scope as well as it get's merge after the scope definition in the final build file.

But please review it with care and maybe try it out. It's always a mess to handle all these different environments.

@bhouston
Copy link
Contributor

I would prefer to have comments in the code and a clear organization so that we can maintain this code easily. Something like:

var scope = null;
// if in a browser window
if ....
   scope = window;
// if in a node.js module
else if ...
   scope = global
// if in a web worker
else if ...
   scope = self
else 
   // and then error out if none of these work
   throw new Error( "can not identify global scope." );

The code as it is right now isn't that clear or well commented. It took me a while to figure it out.

Also you missed some usages of "self" -- at least in Clock and in the Font stuff.

@mrdoob
Copy link
Owner

mrdoob commented Dec 14, 2015

Agree with @bhouston.

@simonThiele
Copy link
Contributor Author

Thx for annotations! I added a few comments on that, I hope thinks are a little clearer now.

Also you missed some usages of "self" -- at least in Clock and in the Font stuff.

True, but these classes are not part of the build file so the scope is not defined. I'm afraid that users have to define the scope by their own.

// the code is running in the browser, so window is defined as the global scope
} else {

scope = window;
Copy link
Owner

Choose a reason for hiding this comment

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

I think calling the variable scope may create even more problems. Maybe this?

if ( typeof module !== 'undefined' && module.exports ) {

    // the code is running under node environment

    if ( typeof self === 'undefined' ) {

        // if self wasn't defined before, set to nodes global scope
        self = global;

    }

} else if ( typeof self !== 'undefined' ) {

    // if the code is running under other environments e.g. webworker, self is defined

} else {

    // the code is running in the browser, so window is defined as the global scope
    self = window;

}

Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to be a bit cleaner, one could create a function or variable on THREE rather than setting a global. It is less likely to get clubbered by other code. THREE.getSelf() or THREE.self or something like that. Just an idea.

Copy link
Owner

Choose a reason for hiding this comment

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

It's just that the variable scope is used in many other places. Using the same variable is very prone to confusion and bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mrdoob Yes, calling it "self" works too (on node, browser and ww). Will refactor it.

@mrdoob
Copy link
Owner

mrdoob commented Dec 17, 2015

How about we turn https://github.com/mrdoob/three.js/blob/dev/src/Three.js#L9-L17 into this instead?

if ( typeof define === 'function' && define.amd ) {

    define( 'three', THREE );

} else if ( 'undefined' !== typeof exports && 'undefined' !== typeof module ) {

    module.exports = THREE;
    self = global;

} else {

    self = window;

}

@simonThiele
Copy link
Contributor Author

Doesn't work on node, since line https://github.com/mrdoob/three.js/blob/dev/src/Three.js#L9 is either true in node and L14/15 need to be called too so there must be at least an else if -> if. Furthermore, it seems you need to check for self because 1. I could be defined earlier and 2. Browser webpack test won't work https://github.com/brianchirls/webpack-test/blob/three.js/index.html (see #7783)

Only thing you can shorten is:

if ( typeof define === 'function' && define.amd ) {

    define( 'three', THREE );

}

if ( typeof module !== 'undefined' && module.exports ) {
    // the code is running under node environment

    module.exports = THREE;

  if ( typeof self === 'undefined' ) {

      self = global;

  }

} else if ( typeof self !== 'undefined' ) {

    // if the code is running under other environments e.g. webworker, self is defined

} else {

    // the code is running in the browser, so window is defined as the global scope
    self = window;

}

@bripkens
Copy link
Contributor

Any update on this?

@mrdoob
Copy link
Owner

mrdoob commented Jan 14, 2016

Here's another solution. How about we remove all the self instances from core?

We have mainly 3:

  1. if ( self.requestAnimationFrame === undefined || self.cancelAnimationFrame === undefined ) ...
  2. if ( self.performance === undefined ) ...
  3. var typedArray = new self[ attribute.type ]( attribute.array );

As far as I know, 1 and 2 will still work when removing self.. For 3 we can just do an object.

@mrdoob
Copy link
Owner

mrdoob commented Jan 14, 2016

@mrdoob
Copy link
Owner

mrdoob commented Jan 14, 2016

Ok, done. We shouldn't have to worry about self anymore.

@bripkens
Copy link
Contributor

thanks @mrdoob!

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.

4 participants