Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix: seriailize update as list, not merge then on yrs side, which is broken

linonetwo3ab34de1e149
diff --git a/client-app/src-tauri/src/commands/document.rs b/client-app/src-tauri/src/commands/document.rs
index f34c350fa4..822d1bf690 100644
--- a/client-app/src-tauri/src/commands/document.rs
+++ b/client-app/src-tauri/src/commands/document.rs
@@ -1,10 +1,14 @@
+use std::io::ErrorKind;
+
use ipc_types::document::{
CreateDocumentParameter, GetDocumentParameter, GetDocumentResponse, YDocumentUpdate,
};
use jwst::DocStorage;
use jwst::Workspace as OctoBaseWorkspace;
use lib0::any::Any;
-use yrs::StateVector;
+use tokio::fs::File;
+use tokio::io::AsyncReadExt;
+use yrs::{updates::decoder::Decode, Doc, StateVector, Update};
use crate::state::AppState;
@@ -47,21 +51,33 @@ pub async fn get_doc<'s>(
) -> Result<GetDocumentResponse, String> {
// TODO: check user permission
- if let Some(doc) = &state
+ let doc_file_path = &state
.0
.lock()
.await
.doc_storage
- .get(parameters.id.clone())
- .await
- .ok()
- {
- Ok(GetDocumentResponse {
- update: doc.encode_state_as_update_v1(&StateVector::default()),
- })
- } else {
- Err(format!("Failed to get yDoc from {}", parameters.id))
+ .get_path(parameters.id.clone());
+ let mut file = File::open(doc_file_path).await.unwrap();
+ let mut updates_vector: Vec<Vec<u8>> = Vec::new();
+ loop {
+ let len = file.read_u64_le().await;
+
+ let len = match len {
+ Ok(len) => len,
+ Err(e) if e.kind() == ErrorKind::UnexpectedEof => break,
+ Err(e) => return Err(format!("Failed to get yDoc from {}", parameters.id)),
+ };
+
+ let mut update = vec![0; len as usize];
+ file.read_exact(&mut update).await.unwrap();
+
+ updates_vector.push(update);
+
+ file.read_u64_le().await.unwrap();
}
+ Ok(GetDocumentResponse {
+ updates: updates_vector,
+ })
}
#[tauri::command]
diff --git a/client-app/src-tauri/types/src/document.rs b/client-app/src-tauri/types/src/document.rs
index 50f8f5a509..037c2195ed 100644
--- a/client-app/src-tauri/types/src/document.rs
+++ b/client-app/src-tauri/types/src/document.rs
@@ -18,7 +18,7 @@ pub struct CreateDocumentParameter {
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct GetDocumentResponse {
- pub update: Vec<u8>,
+ pub updates: Vec<Vec<u8>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
diff --git a/packages/data-center/src/provider/tauri-ipc/index.ts b/packages/data-center/src/provider/tauri-ipc/index.ts
index dd99201d9d..00fc3f6274 100644
--- a/packages/data-center/src/provider/tauri-ipc/index.ts
+++ b/packages/data-center/src/provider/tauri-ipc/index.ts
@@ -42,11 +42,17 @@ export class TauriIPCProvider extends LocalProvider {
async #initDocFromIPC(workspaceID: string, doc: Y.Doc) {
this._logger(`Loading ${workspaceID}...`);
- const updates = await ipcMethods.getYDocument({ id: workspaceID });
- if (updates) {
+ const result = await ipcMethods.getYDocument({ id: workspaceID });
+ if (result) {
await new Promise(resolve => {
doc.once('update', resolve);
- Y.applyUpdate(doc, new Uint8Array(updates.update));
+ const updates = result.updates.map(
+ binaryUpdate => new Uint8Array(binaryUpdate)
+ );
+ const mergedUpdate = Y.mergeUpdates(updates);
+ // DEBUG: console mergedUpdate
+ console.log(`mergedUpdate`, mergedUpdate);
+ Y.applyUpdate(doc, new Uint8Array(mergedUpdate));
});
this._logger(`Loaded: ${workspaceID}`);
}
diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/types/document.json b/packages/data-center/src/provider/tauri-ipc/ipc/types/document.json
index eaef4a27ca..bd504bf000 100644
--- a/packages/data-center/src/provider/tauri-ipc/ipc/types/document.json
+++ b/packages/data-center/src/provider/tauri-ipc/ipc/types/document.json
@@ -12,6 +12,16 @@
},
"additionalProperties": false
},
+ {
+ "type": "object",
+ "required": ["CreateDocumentParameter"],
+ "properties": {
+ "CreateDocumentParameter": {
+ "$ref": "#/definitions/CreateDocumentParameter"
+ }
+ },
+ "additionalProperties": false
+ },
{
"type": "object",
"required": ["GetDocumentParameter"],
@@ -34,6 +44,18 @@
}
],
"definitions": {
+ "CreateDocumentParameter": {
+ "type": "object",
+ "required": ["workspace_id", "workspace_name"],
+ "properties": {
+ "workspace_id": {
+ "type": "string"
+ },
+ "workspace_name": {
+ "type": "string"
+ }
+ }
+ },
"GetDocumentParameter": {
"type": "object",
"required": ["id"],
@@ -45,14 +67,17 @@
},
"GetDocumentResponse": {
"type": "object",
- "required": ["update"],
+ "required": ["updates"],
"properties": {
- "update": {
+ "updates": {
"type": "array",
"items": {
- "type": "integer",
- "format": "uint8",
- "minimum": 0.0
+ "type": "array",
+ "items": {
+ "type": "integer",
+ "format": "uint8",
+ "minimum": 0.0
+ }
}
}
}
diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/types/document.ts b/packages/data-center/src/provider/tauri-ipc/ipc/types/document.ts
index 71603490f9..400a8dff4b 100644
--- a/packages/data-center/src/provider/tauri-ipc/ipc/types/document.ts
+++ b/packages/data-center/src/provider/tauri-ipc/ipc/types/document.ts
@@ -9,6 +9,9 @@ export type IDocumentParameters =
| {
YDocumentUpdate: YDocumentUpdate;
}
+ | {
+ CreateDocumentParameter: CreateDocumentParameter;
+ }
| {
GetDocumentParameter: GetDocumentParameter;
}
@@ -21,11 +24,16 @@ export interface YDocumentUpdate {
update: number[];
[k: string]: unknown;
}
+export interface CreateDocumentParameter {
+ workspace_id: string;
+ workspace_name: string;
+ [k: string]: unknown;
+}
export interface GetDocumentParameter {
id: string;
[k: string]: unknown;
}
export interface GetDocumentResponse {
- update: number[];
+ updates: number[][];
[k: string]: unknown;
}