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

fix: allow dynamically import atmosphere.js for SW context #2901

Open
wants to merge 5 commits into
base: fix/sw-context-access
Choose a base branch
from

Conversation

krissvaa
Copy link
Contributor

Fixes #2867

@krissvaa krissvaa force-pushed the fix/sw-context-dynamic-import branch from 5c613c4 to 33b8cc7 Compare November 13, 2024 14:06
Env should be optional for other frameworks
Fixes #2867
Copy link

sonarcloud bot commented Nov 13, 2024

Copy link

codecov bot commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 92.52%. Comparing base (cf3a028) to head (3ba43b0).

Files with missing lines Patch % Lines
packages/ts/frontend/src/FluxConnection.ts 80.00% 1 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                    @@
##           fix/sw-context-access    #2901      +/-   ##
=========================================================
- Coverage                  92.54%   92.52%   -0.03%     
=========================================================
  Files                         83       83              
  Lines                       2832     2836       +4     
  Branches                     732      733       +1     
=========================================================
+ Hits                        2621     2624       +3     
- Misses                       156      157       +1     
  Partials                      55       55              
Flag Coverage Δ
unittests 92.52% <80.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -0,0 +1,10 @@
// / <reference types="vite/client" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// / <reference types="vite/client" />
/// <reference types="vite/client" />

Comment on lines +76 to +82
(async () => {
if (!import.meta.env?.VITE_SW_CONTEXT) {
atmosphere = await import('atmosphere.js').then((module) => module.default);
}
})().catch((e) => {
console.error('Failed to load atmosphere.js', e);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(async () => {
if (!import.meta.env?.VITE_SW_CONTEXT) {
atmosphere = await import('atmosphere.js').then((module) => module.default);
}
})().catch((e) => {
console.error('Failed to load atmosphere.js', e);
});
if (!import.meta.env?.VITE_SW_CONTEXT) {
try {
atmosphere = await import('atmosphere.js').then((module) => module.default);
} catch (e: unknown) {
console.error('Failed to load atmosphere.js', e);
}
}

I'd suggest using top-level await here. Is it possible?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, we can probably check the presence of the document in globalThis here, instead of VITE_SW_CONTEXT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

top-level await is not allowed according to our project configuration.

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's avoid top-level await for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

But that creates a possible race condition, and in some cases the FluxConnection can possibly be created before the Atmosphere is loaded.

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.

3 participants