refactor(electron): sqlite db data workflow (remove symlink & fs watcher) (#2491)

This commit is contained in:
Peng Xiao
2023-05-29 12:53:15 +08:00
committed by GitHub
parent f3ac12254c
commit 20cf45270d
58 changed files with 1078 additions and 896 deletions

View File

@@ -5,7 +5,7 @@ import {
TableHead,
TableRow,
} from '@affine/component';
import { DEFAULT_SORT_KEY } from "@affine/env/constant";
import { DEFAULT_SORT_KEY } from '@affine/env/constant';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { ArrowDownBigIcon, ArrowUpBigIcon } from '@blocksuite/icons';
import { useMediaQuery, useTheme } from '@mui/material';

View File

@@ -1,5 +1,4 @@
module.exports.createFSWatcher = function createFSWatcher() {
// require it in the function level so that it won't break the `generate-main-exposed-meta.mjs`
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { FsWatcher } = require('./index');
return FsWatcher;

View File

@@ -22,7 +22,7 @@ export const createSQLiteStorage = (workspaceId: string): BlobStorage => {
return apis.db.deleteBlob(workspaceId, key);
},
list: async () => {
return apis.db.getPersistedBlobs(workspaceId);
return apis.db.getBlobKeys(workspaceId);
},
},
};

View File

@@ -14,7 +14,9 @@ let provider: SQLiteProvider;
let offlineYdoc: YType.Doc;
let triggerDBUpdate: ((_: string) => void) | null = null;
let triggerDBUpdate:
| Parameters<typeof window.events.db.onExternalUpdate>[0]
| null = null;
const mockedAddBlob = vi.fn();
@@ -27,7 +29,7 @@ vi.stubGlobal('window', {
applyDocUpdate: async (id: string, update: Uint8Array) => {
Y.applyUpdate(offlineYdoc, update, 'sqlite');
},
getPersistedBlobs: async () => {
getBlobKeys: async () => {
// todo: may need to hack the way to get hash keys of blobs
return [];
},
@@ -36,20 +38,12 @@ vi.stubGlobal('window', {
},
events: {
db: {
onDBFileUpdate: (fn: (id: string) => void) => {
onExternalUpdate: fn => {
triggerDBUpdate = fn;
return () => {
triggerDBUpdate = null;
};
},
// not used in this test
onDBFileMissing: () => {
return () => {};
},
onDBFilePathChange: () => {
return () => {};
},
},
} satisfies Partial<NonNullable<typeof window.events>>,
});
@@ -111,23 +105,26 @@ describe('SQLite provider', () => {
});
test('on db update', async () => {
vi.useFakeTimers();
await provider.connect();
offlineYdoc.getText('text').insert(0, 'sqlite-world');
triggerDBUpdate?.(id);
triggerDBUpdate?.({
workspaceId: id + '-another-id',
update: Y.encodeStateAsUpdate(offlineYdoc),
});
// not yet updated
expect(workspace.doc.getText('text').toString()).toBe('sqlite-hello');
// wait for the update to be sync'ed
await vi.advanceTimersByTimeAsync(1000);
triggerDBUpdate?.({
workspaceId: id,
update: Y.encodeStateAsUpdate(offlineYdoc),
});
expect(workspace.doc.getText('text').toString()).toBe(
'sqlite-worldsqlite-hello'
);
vi.useRealTimers();
});
test('disconnect handlers', async () => {

View File

@@ -176,9 +176,7 @@ const createSQLiteProvider = (
}
async function syncBlobIntoSQLite(bs: BlobManager) {
const persistedKeys = await apis.db.getPersistedBlobs(
blockSuiteWorkspace.id
);
const persistedKeys = await apis.db.getBlobKeys(blockSuiteWorkspace.id);
const allKeys = await bs.list();
const keysToPersist = allKeys.filter(k => !persistedKeys.includes(k));
@@ -242,20 +240,9 @@ const createSQLiteProvider = (
blockSuiteWorkspace.doc.on('update', handleUpdate);
let timer = 0;
unsubscribe = events.db.onDBFileUpdate(workspaceId => {
unsubscribe = events.db.onExternalUpdate(({ update, workspaceId }) => {
if (workspaceId === blockSuiteWorkspace.id) {
// throttle
logger.debug('on db update', workspaceId);
if (timer) {
clearTimeout(timer);
}
// @ts-expect-error ignore the type
timer = setTimeout(() => {
syncUpdates();
timer = 0;
}, 1000);
Y.applyUpdate(blockSuiteWorkspace.doc, update, sqliteOrigin);
}
});