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

#4058: fix race condition on tab load #4061

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/contentScript/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// IMPORTANT: do not import anything that has a transitive dependency of the messenger.
// See for more information: https://github.com/pixiebrix/pixiebrix-extension/issues/4058
import "./contentScript.scss";
import { uuidv4 } from "@/types/helpers";
import { onContextInvalidated } from "@/errors/contextInvalidated";
import {
isInstalledInThisSession,
isReadyInThisDocument,
Expand Down Expand Up @@ -47,17 +48,12 @@ async function initContentScript() {

setInstalledInThisSession();

// eslint-disable-next-line promise/prefer-await-to-then -- It's an unrelated event listener
void onContextInvalidated().then(() => {
console.debug("contentScript: invalidated", uuid);
});

// Keeping the import separate ensures that no side effects are run until this point
const { init } = await logPromiseDuration(
"contentScript: imported", // "imported" timing includes the parsing of the file, which can take 500-1000ms
import(/* webpackChunkName: "contentScriptCore" */ "./contentScriptCore")
);
await init();
await init(uuid);
setReadyInThisDocument(uuid);
}

Expand Down
11 changes: 10 additions & 1 deletion src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import { initPartnerIntegrations } from "@/contentScript/partnerIntegrations";
import {
isContextInvalidatedError,
notifyContextInvalidated,
onContextInvalidated,
} from "@/errors/contextInvalidated";
import { uncaughtErrorHandlers } from "@/telemetry/reportUncaughtErrors";
import { UUID } from "@/core";

function ignoreContextInvalidatedErrors(
errorEvent: ErrorEvent | PromiseRejectionEvent
Expand All @@ -51,7 +53,7 @@ function ignoreContextInvalidatedErrors(
// Must come before the default handler for ignoring errors. Otherwise, this handler might not be run
uncaughtErrorHandlers.unshift(ignoreContextInvalidatedErrors);

export async function init(): Promise<void> {
export async function init(uuid: UUID): Promise<void> {
registerMessenger();
registerExternalMessenger();
registerBuiltinBlocks();
Expand All @@ -72,4 +74,11 @@ export async function init(): Promise<void> {

// Let the partner page know
initPartnerIntegrations();

// Put here instead of in the sync contentScript because contextInvalidated has a transitive dependency on the
// messenger (which causes a race on messenger initialization)
// eslint-disable-next-line promise/prefer-await-to-then -- It's an unrelated event listener
void onContextInvalidated().then(() => {
console.debug("contentScript: invalidated", uuid);
});
}