mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-11 14:08:55 +08:00
@@ -4,7 +4,7 @@ import type {
|
|||||||
LocalIndexedDBDownloadProvider,
|
LocalIndexedDBDownloadProvider,
|
||||||
WorkspaceAdapter,
|
WorkspaceAdapter,
|
||||||
} from '@affine/env/workspace';
|
} from '@affine/env/workspace';
|
||||||
import { WorkspaceFlavour } from '@affine/env/workspace';
|
import { WorkspaceFlavour, WorkspaceVersion } from '@affine/env/workspace';
|
||||||
import type { RootWorkspaceMetadata } from '@affine/workspace/atom';
|
import type { RootWorkspaceMetadata } from '@affine/workspace/atom';
|
||||||
import { workspaceAdaptersAtom } from '@affine/workspace/atom';
|
import { workspaceAdaptersAtom } from '@affine/workspace/atom';
|
||||||
import {
|
import {
|
||||||
@@ -123,6 +123,7 @@ if (environment.isBrowser) {
|
|||||||
try {
|
try {
|
||||||
const metadata = JSON.parse(value) as RootWorkspaceMetadata[];
|
const metadata = JSON.parse(value) as RootWorkspaceMetadata[];
|
||||||
const promises: Promise<void>[] = [];
|
const promises: Promise<void>[] = [];
|
||||||
|
const newMetadata = [...metadata];
|
||||||
metadata.forEach(oldMeta => {
|
metadata.forEach(oldMeta => {
|
||||||
if (!('version' in oldMeta)) {
|
if (!('version' in oldMeta)) {
|
||||||
const adapter = WorkspaceAdapters[oldMeta.flavour];
|
const adapter = WorkspaceAdapters[oldMeta.flavour];
|
||||||
@@ -160,6 +161,13 @@ if (environment.isBrowser) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await adapter.CRUD.delete(workspace as any);
|
await adapter.CRUD.delete(workspace as any);
|
||||||
|
console.log('migrated', oldMeta.id, newId);
|
||||||
|
const index = newMetadata.findIndex(meta => meta.id === oldMeta.id);
|
||||||
|
newMetadata[index] = {
|
||||||
|
...oldMeta,
|
||||||
|
id: newId,
|
||||||
|
version: WorkspaceVersion.SubDoc,
|
||||||
|
};
|
||||||
await migrateLocalBlobStorage(workspace.id, newId);
|
await migrateLocalBlobStorage(workspace.id, newId);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -176,6 +184,7 @@ if (environment.isBrowser) {
|
|||||||
console.error('migration failed');
|
console.error('migration failed');
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
localStorage.setItem('jotai-workspaces', JSON.stringify(newMetadata));
|
||||||
window.dispatchEvent(new CustomEvent('migration-done'));
|
window.dispatchEvent(new CustomEvent('migration-done'));
|
||||||
window.$migrationDone = true;
|
window.$migrationDone = true;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -81,6 +81,19 @@ const rootWorkspacesMetadataPromiseAtom = atom<
|
|||||||
// fixme(himself65): we might not need step 1
|
// fixme(himself65): we might not need step 1
|
||||||
// step 1: try load metadata from localStorage
|
// step 1: try load metadata from localStorage
|
||||||
{
|
{
|
||||||
|
// migration step, only data in `METADATA_STORAGE_KEY` will be migrated
|
||||||
|
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(), {
|
||||||
|
once: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// don't change this key,
|
// don't change this key,
|
||||||
// otherwise it will cause the data loss in the production
|
// otherwise it will cause the data loss in the production
|
||||||
const primitiveMetadata = localStorage.getItem(METADATA_STORAGE_KEY);
|
const primitiveMetadata = localStorage.getItem(METADATA_STORAGE_KEY);
|
||||||
@@ -95,19 +108,6 @@ const rootWorkspacesMetadataPromiseAtom = atom<
|
|||||||
console.error('cannot parse worksapce', e);
|
console.error('cannot parse worksapce', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// migration step, only data in `METADATA_STORAGE_KEY` will be migrated
|
|
||||||
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(), {
|
|
||||||
once: true,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// step 2: fetch from adapters
|
// step 2: fetch from adapters
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user