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

replace window with 'self' in workers automatically #1769

Closed
wants to merge 1 commit into from

Conversation

tjenkinson
Copy link
Member

This PR will...

use the webpack imports loader to automatically replace window with self when running in a worker.

Why is this Pull Request needed?

It removes some duplicated code, and also caches window or self, so might have slightly better performance.

E.g in the build

/*** IMPORTS FROM imports-loader ***/
var demuxer_window = __webpack_require__(0);

is injected at the top of each module, and in this built module window has been rewritten as demuxer_window.

@tjenkinson
Copy link
Member Author

Although if users build hlsjs themselves from source, this is another thing they will have to handle.

}
// see https://stackoverflow.com/a/11237259/589493
/* eslint-disable-next-line no-undef */
export default typeof window === 'undefined' ? self : window;
Copy link
Collaborator

Choose a reason for hiding this comment

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

oh ok i guess i understand now

Copy link
Collaborator

@tchakabam tchakabam left a comment

Choose a reason for hiding this comment

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

nice one, saves quite a few codelines 👍 :)

@tchakabam tchakabam modified the milestones: 0.10.1, 0.10.2 Jun 13, 2018
@tjenkinson tjenkinson mentioned this pull request Jun 15, 2018
4 tasks
@tchakabam
Copy link
Collaborator

Closing this, as we have solved it via here: #1841

@tchakabam tchakabam closed this Jul 25, 2018
@tjenkinson
Copy link
Member Author

I think this is still valid. How is it related to the other issue? It is a way of not needing getSelfScope() #1756

Not actually sure if it's better being more explicit though instead of replacing window

@friday
Copy link
Contributor

friday commented Jul 27, 2018

Yes, it's not really the same problem. #1841 could have been solved by passing the global for the current environment (self for browsers or global in node) to a closure. This is fairly common for modules, but isn't possible simply using webpack.BannerPlugin, would require a different architecture, and would also forfeit the performance gain of #1841.

I ended up here before and decided not to make myself even less popular with my opinions, but I'm wondering, why not just use self in the code? It works in both WindowScope and ServiceScope, and in WindowScope properties that aren't supported in ServiceScope still seems to work (like self.document).

Maybe it's too hard to ensure that people won't add window to the code later or shadow self? I don't know any good eslint-rules for this. id-blacklist could be used, but that would also complain about using window as a property.

At least get-self-scope.js could be simplified to export default self; I think?

Also, in case you want set the environment to specific files with eslint, their comments should work /* eslint-env worker */ (see https://eslint.org/docs/user-guide/configuring#specifying-environments). I'm not sure this solves anything for you, but it looks related to what you're doing in the other PR.

@tjenkinson
Copy link
Member Author

Great point I didn't realise 'self' was an alias of 'window'. If it really does work like this in all browsers, and typescript understands this, I think we should just switch to 'self'

The Mozilla docs also mention this https://developer.mozilla.org/en-US/docs/Web/API/Window/self

@friday
Copy link
Contributor

friday commented Jul 28, 2018

I'm late to the game on this and can't claim to know by experience. As I understand it, it was there way before WebWorkers (maybe since the start), similar to this also being an alias of window in the global scope (although the implementation differ).

Found an old stack overflow question indicating it's been supported at least since April 2010 (IE8):
https://stackoverflow.com/questions/2725188/why-is-window-not-identical-to-window-self-in-internet-explorer

I tried the typescript online REPL and it seems to alias self with window

self.document;
self.localStorage;
self.asdf;

The last row got me "Property 'asdf' does not exist on type 'Window'."

You may have to watch out for shadowing though. But maybe typescript catches it?

parse: function (data) {
let self = this;
// If there is no data then we won't decode it, but will just try to parse
// whatever is in buffer already. This may occur in circumstances, for
// example when flush() is called.
if (data) {
// Try to decode the data that we received.
self.buffer += self.decoder.decode(data, { stream: true });
}
function collectNextLine () {
let buffer = self.buffer;
let pos = 0;
buffer = fixLineBreaks(buffer);
while (pos < buffer.length && buffer[pos] !== '\r' && buffer[pos] !== '\n') {
++pos;
}
let line = buffer.substr(0, pos);
// Advance the buffer early in case we fail below.
if (buffer[pos] === '\r') {
++pos;
}

(this particular place might not be a problem)

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