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 global scope situation with util and lint update #1273

Merged
merged 1 commit into from
Aug 31, 2023

Conversation

peaBerberian
Copy link
Collaborator

Some years back, we were notified of an issue when the RxPlayer was imported in a Node.JS environment - which has mostly no chance to bring any result unless that environment implements MSE, EME and DOM API - but which was still done due to some application logic linked to server-side-rendering.

That issue lead to an error, and was due to the RxPlayer refering to the window (the global scope accessible in JavaScript in a main thread environment) at the top level (generally done there to check once at script evaluation if various browser API are present), because window does not exist in Node.JS environments.

What we did at the time is just to check (poorly) if we were in a Node.js environment, and to not rely on window if that was the case at those various places. However that was error-prone (we could forget that check) and the only thing preventing mistakes was a script in scripts/check_nodejs_import_compatibility.js which wouldn't be able to catch all cases.


Because this became again a problem when PoCing worker environments, I profited from this project to work on a better solution.

Now the global scope is directly provided by an util function of our compat code, which works for NodeJS, JS main thread and JS WebWorker environments, and its usage is enforced by a linter rule which should hopefully catch most window usage in the code.

I also chose to erase all usage of the term window or the shortened win alias in code to better communicate the point that we're specifically mean the global scope, regardless of how it is referred to in the current environment.

Some years back, we were notified of an issue when the RxPlayer was
imported in a Node.JS environment - which has mostly no chance to bring
any result unless that environment implements MSE, EME and DOM API - but
which was still done due to some logic linked to server-side-rendering.

That issue lead to an error, and was due to the RxPlayer refering to the
`window` (the global scope accessible in JavaScript in a main thread
environment) at the top level (generally done there to check once at
script evaluation if various browser API are present), because `window`
does not exist in Node.JS environments.

What we did at the time is just to check (poorly) if we were in a
Node.js environment, and to not rely on `window` if that was the case at
those various places. However that was error-prone (we could forget that
check) and the only thing preventing mistakes was a script in
`scripts/check_nodejs_import_compatibility.js` which wouldn't be able
to catch all cases.

---

Because this became again a problem when PoCing worker environments, I
profited from this project to work on a better solution.

Now the global scope is directly provided by an util function of our
compat code, which works for NodeJS, JS main thread and JS WebWorker
environments, and its usage is enforced by a linter rule which should
hopefully catch all `window` usage in the code.

I also chose to erase all usage of the term `window` or the shortened
`win` alias in code to better communicate the point that we're
specifically mean the global scope, regardless of how it is referred to
in the current environment.
@peaBerberian peaBerberian added this to the 4.0.0-beta.3 milestone Aug 31, 2023
@peaBerberian peaBerberian merged commit 9b48bc6 into next-v4 Aug 31, 2023
3 checks passed
peaBerberian added a commit that referenced this pull request Aug 31, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Aug 31, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Aug 31, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 15, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 15, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 22, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 26, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 26, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 27, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 27, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 27, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Sep 29, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Oct 13, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Oct 13, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Oct 13, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Oct 19, 2023
Improve global scope situation with util and lint update
peaBerberian added a commit that referenced this pull request Oct 26, 2023
Improve global scope situation with util and lint update
@peaBerberian peaBerberian deleted the misc/global-scope-improvement branch February 7, 2024 16:30
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.

1 participant