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

remove global window and localStorage dependecy #74

Merged
merged 1 commit into from
Feb 1, 2023

Conversation

bumi
Copy link
Contributor

@bumi bumi commented Jan 9, 2023

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

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);
Copy link
Contributor Author

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.

Copy link
Contributor

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;
Copy link
Contributor Author

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.

@kaloudis
Copy link
Contributor

tACK - tried with the connect demo

@bumi
Copy link
Contributor Author

bumi commented Jan 29, 2023

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.

@kaloudis
Copy link
Contributor

kaloudis commented Feb 1, 2023

@bumi apologies for the delay. Cutting a new release today.

@kaloudis kaloudis merged commit 033ec52 into lightninglabs:main Feb 1, 2023
@bumi bumi deleted the remove-window-dependency branch February 1, 2023 19:51
@bumi bumi restored the remove-window-dependency branch February 1, 2023 20:06
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.

2 participants