fix: tauri loading and method loading

This commit is contained in:
linonetwo 2023-01-11 12:14:07 +08:00
parent 6aa679365a
commit cdc196322c
2 changed files with 11 additions and 8 deletions

View File

@ -40,7 +40,11 @@ export class DataCenter {
}; };
}; };
// TODO: switch different provider // TODO: switch different provider
if (typeof window !== 'undefined' && window.CLIENT_APP) { if (
typeof window !== 'undefined' &&
window.CLIENT_APP &&
typeof window.__TAURI_IPC__ === 'function'
) {
const { TauriIPCProvider } = await import('./provider/tauri-ipc'); const { TauriIPCProvider } = await import('./provider/tauri-ipc');
dc.registerProvider(new TauriIPCProvider(getInitParams())); dc.registerProvider(new TauriIPCProvider(getInitParams()));
} }

View File

@ -13,7 +13,6 @@ import { setDefaultAvatar } from '../utils.js';
export class TauriIPCProvider extends LocalProvider { export class TauriIPCProvider extends LocalProvider {
static id = 'tauri-ipc'; static id = 'tauri-ipc';
#ipc = ipcMethods;
private _workspacesCache: Map<string, BlocksuiteWorkspace> = new Map(); private _workspacesCache: Map<string, BlocksuiteWorkspace> = new Map();
constructor(params: ProviderConstructorParams) { constructor(params: ProviderConstructorParams) {
@ -27,7 +26,7 @@ export class TauriIPCProvider extends LocalProvider {
async #initDocFromIPC(workspaceID: string, doc: Y.Doc) { async #initDocFromIPC(workspaceID: string, doc: Y.Doc) {
this._logger(`Loading ${workspaceID}...`); this._logger(`Loading ${workspaceID}...`);
const updates = await this.#ipc.getYDocument({ id: workspaceID }); const updates = await ipcMethods.getYDocument({ id: workspaceID });
if (updates) { if (updates) {
await new Promise(resolve => { await new Promise(resolve => {
doc.once('update', resolve); doc.once('update', resolve);
@ -43,7 +42,7 @@ export class TauriIPCProvider extends LocalProvider {
try { try {
// TODO: need handle potential data race when update is frequent? // TODO: need handle potential data race when update is frequent?
// TODO: update seems too frequent upon each keydown, why no batching? // TODO: update seems too frequent upon each keydown, why no batching?
const success = await this.#ipc.updateYDocument({ const success = await ipcMethods.updateYDocument({
update: Array.from(update), update: Array.from(update),
id: workspaceID, id: workspaceID,
}); });
@ -64,7 +63,7 @@ export class TauriIPCProvider extends LocalProvider {
public override async createWorkspaceInfo( public override async createWorkspaceInfo(
meta: WorkspaceMeta meta: WorkspaceMeta
): Promise<WorkspaceInfo> { ): Promise<WorkspaceInfo> {
const { id } = await this.#ipc.createWorkspace({ const { id } = await ipcMethods.createWorkspace({
name: meta.name, name: meta.name,
// TODO: get userID here // TODO: get userID here
user_id: 0, user_id: 0,
@ -125,7 +124,7 @@ export class TauriIPCProvider extends LocalProvider {
override async loadWorkspaces() { override async loadWorkspaces() {
// TODO: get user id here // TODO: get user id here
const { workspaces: workspacesList } = await this.#ipc.getWorkspaces({ const { workspaces: workspacesList } = await ipcMethods.getWorkspaces({
user_id: 0, user_id: 0,
}); });
const workspaces: WorkspaceInfo[] = workspacesList.map(w => { const workspaces: WorkspaceInfo[] = workspacesList.map(w => {
@ -145,7 +144,7 @@ export class TauriIPCProvider extends LocalProvider {
this._workspacesCache.set(id, workspace); this._workspacesCache.set(id, workspace);
if (workspace) { if (workspace) {
return new Promise<BlocksuiteWorkspace>(resolve => { return new Promise<BlocksuiteWorkspace>(resolve => {
this.#ipc.getYDocument({ id }).then(({ update }) => { ipcMethods.getYDocument({ id }).then(({ update }) => {
Y.applyUpdate(workspace.doc, new Uint8Array(update)); Y.applyUpdate(workspace.doc, new Uint8Array(update));
resolve(workspace); resolve(workspace);
}); });
@ -176,7 +175,7 @@ export class TauriIPCProvider extends LocalProvider {
| null; | null;
}> => { }> => {
const { id, permission, created_at } = workspaceWithPermission; const { id, permission, created_at } = workspaceWithPermission;
const { workspace } = await this.#ipc.getWorkspace({ id }); const { workspace } = await ipcMethods.getWorkspace({ id });
return { return {
id, id,
detail: { detail: {