-
Notifications
You must be signed in to change notification settings - Fork 17
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
remove global window and localStorage dependecy #74
Conversation
window and localStorage are not available in all environments. For example service workers don't have those available. The globalThis property provides a standard way of accessing the global this value across environments. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
@@ -195,7 +195,11 @@ export default class LNC { | |||
); | |||
|
|||
// add an event listener to disconnect if the page is unloaded | |||
window.addEventListener('unload', this.wasm.wasmClientDisconnect); | |||
if (typeof window !== "undefined") { | |||
window.addEventListener('unload', this.wasm.wasmClientDisconnect); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we can add an eventListener only when the window
is available. other contexts might need to manually register some custom code to disconnect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call on this one
@@ -65,7 +65,7 @@ export default class LNC { | |||
} | |||
|
|||
private get wasm() { | |||
return window[this._namespace] as WasmGlobal; | |||
return globalThis[this._namespace] as WasmGlobal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this actually looks a bit dangerous because this could overwrite something. if this._namespace
is already a defined property.
tACK - tried with the connect demo |
Do you have an update on this? will it get in a release? ;) We use a git dependency for this right now which seems to cause some npm install issues. |
@bumi apologies for the delay. Cutting a new release today. |
window and localStorage are not available in all environments. For example service workers don't have those available (afaik).
The globalThis property provides a standard way of accessing the global this value across environments.
The wasm_exec.js code already checks if window is available.
reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis