refactor: move ipc method and types to data-center folder

This commit is contained in:
Lin Onetwo
2023-01-05 14:53:02 +08:00
parent c732520182
commit 20002785e9
10 changed files with 94 additions and 15 deletions

View File

@@ -28,6 +28,7 @@
"dependencies": {
"@blocksuite/blocks": "^0.3.1",
"@blocksuite/store": "^0.3.1",
"@tauri-apps/api": "^1.2.0",
"debug": "^4.3.4",
"encoding": "^0.1.13",
"firebase": "^9.15.0",
@@ -36,7 +37,7 @@
"ky-universal": "^0.11.0",
"lib0": "^0.2.58",
"swr": "^2.0.0",
"yjs": "^13.5.44",
"y-protocols": "^1.0.5"
"y-protocols": "^1.0.5",
"yjs": "^13.5.44"
}
}

View File

@@ -0,0 +1,64 @@
import type { BlobStorage } from '@blocksuite/store';
import assert from 'assert';
import type { ConfigStore, InitialParams } from '../index.js';
import { BaseProvider } from '../base.js';
import { IndexedDBProvider } from './indexeddb.js';
export class LocalProvider extends BaseProvider {
static id = 'local';
private _blobs!: BlobStorage;
private _idb?: IndexedDBProvider = undefined;
constructor() {
super();
}
async init(params: InitialParams) {
super.init(params);
const blobs = await this._workspace.blobs;
assert(blobs);
this._blobs = blobs;
}
async initData() {
assert(this._workspace.room);
this._logger('Loading local data');
this._idb = new IndexedDBProvider(
this._workspace.room,
this._workspace.doc
);
await this._idb.whenSynced;
this._logger('Local data loaded');
await this._globalConfig.set(this._workspace.room, true);
}
async clear() {
await super.clear();
await this._blobs.clear();
await this._idb?.clearData();
await this._globalConfig.delete(this._workspace.room!);
}
async destroy(): Promise<void> {
super.destroy();
await this._idb?.destroy();
}
async getBlob(id: string): Promise<string | null> {
return this._blobs.get(id);
}
async setBlob(blob: Blob): Promise<string> {
return this._blobs.set(blob);
}
static async list(
config: Readonly<ConfigStore<boolean>>
): Promise<Map<string, boolean> | undefined> {
const entries = await config.entries();
return new Map(entries);
}
}

View File

@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
import * as Y from 'yjs';
import { Observable } from 'lib0/observable';
import { DocProvider } from '@blocksuite/store';
import type { Awareness } from 'y-protocols/awareness';
import { invoke } from '@tauri-apps/api';
import { updateYDocument } from './methods';
export class TauriIPCProvider
extends Observable<string>
implements DocProvider
{
#yDocument: Y.Doc;
constructor(
room: string,
yDocument: Y.Doc,
options?: { awareness?: Awareness }
) {
super();
this.#yDocument = yDocument;
this.#yDocument.on(
'update',
async (
update: Uint8Array,
origin: any,
_yDocument: Y.Doc,
_transaction: Y.Transaction
) => {
try {
// TODO: need handle potential data race when update is frequent?
// TODO: update seems too frequent upon each keydown, why no batching?
const success = await updateYDocument({
update: Array.from(update),
room,
});
} catch (error) {
// TODO: write error log to disk, and add button to open them in settings panel
console.error("#yDocument.on('update'", error);
}
}
);
}
connect() {}
destroy() {}
disconnect() {
// do nothing
}
async clearData() {}
}

View File

@@ -0,0 +1,24 @@
import { invoke } from '@tauri-apps/api';
import { YDocumentUpdate } from '../types/ipc/document';
import { CreateWorkspace } from '../types/ipc/workspace';
import { GetBlob, PutBlob } from '../types/ipc/blob';
export const updateYDocument = async (parameters: YDocumentUpdate) =>
await invoke<boolean>('update_y_document', {
parameters,
});
export const createWorkspace = async (parameters: CreateWorkspace) =>
await invoke<boolean>('create_workspace', {
parameters,
});
export const putBlob = async (parameters: PutBlob) =>
await invoke<string>('put_blob', {
parameters,
});
export const getBlob = async (parameters: GetBlob) =>
await invoke<number[]>('get_blob', {
parameters,
});