mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-04 11:09:01 +08:00
refactor: move ipc method and types to data-center folder
This commit is contained in:
@@ -10,16 +10,24 @@ import path from 'path';
|
||||
import { compileFromFile } from 'json-schema-to-typescript';
|
||||
import { cd } from 'zx/core';
|
||||
|
||||
const projectRoot = path.join(__dirname, '..', '..');
|
||||
const tsTypingsFolder = path.join(
|
||||
projectRoot,
|
||||
'packages/data-center/src/provider/tauri-ipc/ipc/types'
|
||||
);
|
||||
|
||||
/**
|
||||
* 1. generate JSONSchema using rs crate `schemars`, this happened on rs side script `src-tauri/examples/generate-jsonschema.rs`
|
||||
*/
|
||||
cd('./src-tauri');
|
||||
try {
|
||||
fs.mkdirSync(tsTypingsFolder);
|
||||
} catch {}
|
||||
await $`cargo run --example generate-jsonschema`;
|
||||
|
||||
/**
|
||||
* 2. generate TS from JSON schema, this is efficient on NodeJS side.
|
||||
*/
|
||||
const tsTypingsFolder = path.join(__dirname, '..', 'src', 'types', 'ipc');
|
||||
const fileNames = fs.readdirSync(tsTypingsFolder);
|
||||
const jsonSchemaFilePaths = fileNames
|
||||
.filter(fileName => fileName.endsWith('.json'))
|
||||
@@ -37,6 +45,7 @@ await Promise.all(
|
||||
/**
|
||||
* 3. fix eslint error on generated ts files
|
||||
*/
|
||||
cd(path.join(projectRoot, 'packages/data-center'));
|
||||
await $`eslint ${tsTypingsFolder} --ext ts --fix`;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use ipc_types::{
|
||||
blob::IBlobParameters, document::YDocumentUpdate, workspace::CreateWorkspace,
|
||||
};
|
||||
use ipc_types::{blob::IBlobParameters, document::YDocumentUpdate, workspace::CreateWorkspace};
|
||||
/**
|
||||
* convert serde to jsonschema: https://imfeld.dev/writing/generating_typescript_types_from_rust
|
||||
* with way to optimize
|
||||
@@ -24,7 +22,12 @@ where
|
||||
|
||||
fn main() {
|
||||
let project_root = &get_project_root().unwrap();
|
||||
generate::<YDocumentUpdate>(Path::join(project_root, "../src/types/ipc/document.json"));
|
||||
generate::<CreateWorkspace>(Path::join(project_root, "../src/types/ipc/workspace.json"));
|
||||
generate::<IBlobParameters>(Path::join(project_root, "../src/types/ipc/blob.json"));
|
||||
let mono_repo_root = Path::join(project_root, "../..");
|
||||
let data_center_ipc_type_folder = Path::join(
|
||||
&mono_repo_root,
|
||||
"packages/data-center/src/provider/tauri-ipc/ipc/types",
|
||||
);
|
||||
generate::<YDocumentUpdate>(Path::join(&data_center_ipc_type_folder, "document.json"));
|
||||
generate::<CreateWorkspace>(Path::join(&data_center_ipc_type_folder, "workspace.json"));
|
||||
generate::<IBlobParameters>(Path::join(&data_center_ipc_type_folder, "blob.json"));
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@ export type IBlobParameters =
|
||||
};
|
||||
|
||||
export interface PutBlob {
|
||||
[k: string]: unknown;
|
||||
blob: number[];
|
||||
workspace_id: number;
|
||||
[k: string]: unknown;
|
||||
}
|
||||
export interface GetBlob {
|
||||
[k: string]: unknown;
|
||||
id: string;
|
||||
workspace_id: number;
|
||||
[k: string]: unknown;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
export interface YDocumentUpdate {
|
||||
[k: string]: unknown;
|
||||
room: string;
|
||||
update: number[];
|
||||
[k: string]: unknown;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
*/
|
||||
|
||||
export interface CreateWorkspace {
|
||||
[k: string]: unknown;
|
||||
avatar: string;
|
||||
id: number;
|
||||
name: string;
|
||||
[k: string]: unknown;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -21,7 +21,7 @@ export class TauriIPCProvider
|
||||
super();
|
||||
this.#yDocument = yDocument;
|
||||
this.#yDocument.on(
|
||||
'updateV2',
|
||||
'update',
|
||||
async (
|
||||
update: Uint8Array,
|
||||
origin: any,
|
||||
@@ -37,7 +37,7 @@ export class TauriIPCProvider
|
||||
});
|
||||
} catch (error) {
|
||||
// TODO: write error log to disk, and add button to open them in settings panel
|
||||
console.error("#yDocument.on('updateV2'", error);
|
||||
console.error("#yDocument.on('update'", error);
|
||||
}
|
||||
}
|
||||
);
|
||||
Generated
+2
@@ -237,6 +237,7 @@ importers:
|
||||
'@blocksuite/blocks': ^0.3.1
|
||||
'@blocksuite/store': ^0.3.1
|
||||
'@playwright/test': ^1.29.1
|
||||
'@tauri-apps/api': ^1.2.0
|
||||
'@types/debug': ^4.1.7
|
||||
debug: ^4.3.4
|
||||
encoding: ^0.1.13
|
||||
@@ -253,6 +254,7 @@ importers:
|
||||
dependencies:
|
||||
'@blocksuite/blocks': 0.3.1_yjs@13.5.44
|
||||
'@blocksuite/store': 0.3.1_yjs@13.5.44
|
||||
'@tauri-apps/api': 1.2.0
|
||||
debug: 4.3.4
|
||||
encoding: 0.1.13
|
||||
firebase: 9.15.0_encoding@0.1.13
|
||||
|
||||
Reference in New Issue
Block a user