Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix: potential race condition on app load when migration (#2977)

Peng Xiao901fc877166c
diff --git a/apps/web/src/bootstrap/index.ts b/apps/web/src/bootstrap/index.ts
index 45a26d0aed..ee4084e91f 100644
--- a/apps/web/src/bootstrap/index.ts
+++ b/apps/web/src/bootstrap/index.ts
@@ -129,16 +129,10 @@ if (environment.isBrowser) {
})
.finally(() => {
window.dispatchEvent(new CustomEvent('migration-done'));
+ window.$migrationDone = true;
});
} catch (e) {
console.error('error when migrating data', e);
}
}
}
-
-declare global {
- // global Events
- interface WindowEventMap {
- 'migration-done': CustomEvent;
- }
-}
diff --git a/packages/env/src/global.ts b/packages/env/src/global.ts
index 4f37638161..e6d09ec207 100644
--- a/packages/env/src/global.ts
+++ b/packages/env/src/global.ts
@@ -35,6 +35,12 @@ declare global {
events: any;
}
+ interface WindowEventMap {
+ 'migration-done': CustomEvent;
+ }
+
+ // eslint-disable-next-line no-var
+ var $migrationDone: boolean;
// eslint-disable-next-line no-var
var platform: Platform | undefined;
// eslint-disable-next-line no-var
diff --git a/packages/workspace/src/atom.ts b/packages/workspace/src/atom.ts
index 3fc2b6c367..b7035ad6dc 100644
--- a/packages/workspace/src/atom.ts
+++ b/packages/workspace/src/atom.ts
@@ -118,7 +118,10 @@ const rootWorkspacesMetadataPromiseAtom = atom<
}
// migration step, only data in `METADATA_STORAGE_KEY` will be migrated
- if (metadata.some(meta => !('version' in meta))) {
+ if (
+ metadata.some(meta => !('version' in meta)) &&
+ !globalThis.$migrationDone
+ ) {
await new Promise<void>((resolve, reject) => {
signal.addEventListener('abort', () => reject(), { once: true });
window.addEventListener('migration-done', () => resolve(), {