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

ReferenceError: window is not defined when using Workers #153

Closed
Darker opened this issue Oct 8, 2015 · 3 comments
Closed

ReferenceError: window is not defined when using Workers #153

Darker opened this issue Oct 8, 2015 · 3 comments

Comments

@Darker
Copy link

Darker commented Oct 8, 2015

If I do this in web worker:

importScripts("http://127.0.0.1/lines/eventemitter2.js");

I get an error as in title. I thought this library was supposed to be compatible with node.js. So why use window? Actually, why does it execute anything on window when just being loaded? Shouldn't such calls be lazy?

@RangerMauve
Copy link
Contributor

It's doing that because of the AMD wrapper so that it'd work with various dependency managers without any issues. Try using it with browserify as a quick fix

@Darker
Copy link
Author

Darker commented Oct 9, 2015

I fixed it like this:

    var window = {};
    importScripts("eventemitter2.js");
    self.EventEmitter2 = window.EventEmitter2;

You could use self for best compatibility, it allways reffers to global scope unless overriden. And in case of workers, I don't think there's other global scope refference than self and this (which is overriden in class methods).

@RangerMauve
Copy link
Contributor

A PR would be very much appreciated for this.

DigitalBrainJS added a commit to DigitalBrainJS/EventEmitter2 that referenced this issue Mar 11, 2020
RangerMauve pushed a commit that referenced this issue Mar 27, 2020
#247)

* Added waitFor method

* fixed README.md code example for waitFor method

* Closes #153 by using the current global scope instead of window

* added getMaxListeners method and defaultMaxListeners property

* added missing semicolons

* added the ability for defaultMaxListeners to set default max listeners for previously created instances;
added TypeScript typings;
added documentation for getMaxListeners() & defaultMaxListeners;
added documentation for emitter.listeners();
RangerMauve added a commit that referenced this issue Mar 28, 2020
* Added waitFor method

* fixed README.md code example for waitFor method

* Closes #153 by using the current global scope instead of window

* added EventEmitter2.once method;
added internal resolveOptions helper;
added internal toCancelablePromise helper;

* reworked waitFor method to use shared internal helpers;
updated README.md;
updated typings;
fixed listening cancellation bug in case of using generic EventTarget interface;

* updated README.md;

* updated README.md features section;

* refactored toCancelablePromise to makeCancelablePromise;
removed TODO;

Co-authored-by: RangerMauve <RangerMauve@hotmail.com>
RangerMauve pushed a commit that referenced this issue Apr 1, 2020
* Added waitFor method

* fixed README.md code example for waitFor method

* Closes #153 by using the current global scope instead of window

* Added universal test loader for nodeunit and mocha test suites;
Updated dependencies;
Fixed coverage script;
Reworked repo scripts set;
Added coveralls integration;
Added travis build integration;
Updated README.md badges;

* Extended integration with assert module;

* Added prepublishOnly & postversion scripts

* Reduced required branches coverage to 75%

* added return value from test function just in case

* allowed to skip null tests

* updated package description

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

No branches or pull requests

2 participants