mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
feat: create workspace from loading existing exported file (#2122)
Co-authored-by: Himself65 <himself65@outlook.com>
This commit is contained in:
@@ -21,7 +21,7 @@ const mockedAddBlob = vi.fn();
|
||||
vi.stubGlobal('window', {
|
||||
apis: {
|
||||
db: {
|
||||
getDoc: async () => {
|
||||
getDocAsUpdates: async () => {
|
||||
return Y.encodeStateAsUpdate(offlineYdoc);
|
||||
},
|
||||
applyDocUpdate: async (id: string, update: Uint8Array) => {
|
||||
@@ -31,15 +31,24 @@ vi.stubGlobal('window', {
|
||||
// todo: may need to hack the way to get hash keys of blobs
|
||||
return [];
|
||||
},
|
||||
onDBUpdate: (fn: (id: string) => void) => {
|
||||
addBlob: mockedAddBlob,
|
||||
} satisfies Partial<NonNullable<typeof window.apis>['db']>,
|
||||
},
|
||||
events: {
|
||||
db: {
|
||||
onDbFileUpdate: (fn: (id: string) => void) => {
|
||||
triggerDBUpdate = fn;
|
||||
return () => {
|
||||
triggerDBUpdate = null;
|
||||
};
|
||||
},
|
||||
addBlob: mockedAddBlob,
|
||||
} satisfies Partial<typeof window.apis.db>,
|
||||
},
|
||||
|
||||
// not used in this test
|
||||
onDbFileMissing: () => {
|
||||
return () => {};
|
||||
},
|
||||
},
|
||||
} satisfies Partial<NonNullable<typeof window.events>>,
|
||||
});
|
||||
|
||||
vi.stubGlobal('environment', {
|
||||
|
||||
@@ -162,18 +162,21 @@ const createSQLiteProvider = (
|
||||
blockSuiteWorkspace: BlockSuiteWorkspace
|
||||
): SQLiteProvider => {
|
||||
const sqliteOrigin = Symbol('sqlite-provider-origin');
|
||||
const apis = window.apis!;
|
||||
const events = window.events!;
|
||||
// make sure it is being used in Electron with APIs
|
||||
assertExists(environment.isDesktop && window.apis);
|
||||
assertExists(apis);
|
||||
assertExists(events);
|
||||
|
||||
function handleUpdate(update: Uint8Array, origin: unknown) {
|
||||
if (origin === sqliteOrigin) {
|
||||
return;
|
||||
}
|
||||
window.apis.db.applyDocUpdate(blockSuiteWorkspace.id, update);
|
||||
apis.db.applyDocUpdate(blockSuiteWorkspace.id, update);
|
||||
}
|
||||
|
||||
async function syncBlobIntoSQLite(bs: BlobManager) {
|
||||
const persistedKeys = await window.apis.db.getPersistedBlobs(
|
||||
const persistedKeys = await apis.db.getPersistedBlobs(
|
||||
blockSuiteWorkspace.id
|
||||
);
|
||||
|
||||
@@ -188,7 +191,7 @@ const createSQLiteProvider = (
|
||||
logger.warn('blob not found for', k);
|
||||
return;
|
||||
}
|
||||
return window.apis.db.addBlob(
|
||||
return window.apis?.db.addBlob(
|
||||
blockSuiteWorkspace.id,
|
||||
k,
|
||||
new Uint8Array(await blob.arrayBuffer())
|
||||
@@ -199,7 +202,7 @@ const createSQLiteProvider = (
|
||||
|
||||
async function syncUpdates() {
|
||||
logger.info('syncing updates from sqlite', blockSuiteWorkspace.id);
|
||||
const updates = await window.apis.db.getDoc(blockSuiteWorkspace.id);
|
||||
const updates = await apis.db.getDocAsUpdates(blockSuiteWorkspace.id);
|
||||
|
||||
if (updates) {
|
||||
Y.applyUpdate(blockSuiteWorkspace.doc, updates, sqliteOrigin);
|
||||
@@ -208,7 +211,7 @@ const createSQLiteProvider = (
|
||||
const mergeUpdates = Y.encodeStateAsUpdate(blockSuiteWorkspace.doc);
|
||||
|
||||
// also apply updates to sqlite
|
||||
window.apis.db.applyDocUpdate(blockSuiteWorkspace.id, mergeUpdates);
|
||||
apis.db.applyDocUpdate(blockSuiteWorkspace.id, mergeUpdates);
|
||||
|
||||
const bs = blockSuiteWorkspace.blobs;
|
||||
|
||||
@@ -240,7 +243,7 @@ const createSQLiteProvider = (
|
||||
blockSuiteWorkspace.doc.on('update', handleUpdate);
|
||||
|
||||
let timer = 0;
|
||||
unsubscribe = window.apis.db.onDBUpdate(workspaceId => {
|
||||
unsubscribe = events.db.onDbFileUpdate(workspaceId => {
|
||||
if (workspaceId === blockSuiteWorkspace.id) {
|
||||
// throttle
|
||||
logger.debug('on db update', workspaceId);
|
||||
|
||||
Reference in New Issue
Block a user