From 77b70b7c49a36ddee13381c5f4e4500559a70a95 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 14 Jan 2021 23:57:24 +0300 Subject: [PATCH] Fix text synchronization --- client/src/app/App.tsx | 6 +++--- client/src/app/CollaborativeEditor.tsx | 22 ++++++++++++++++------ client/src/fluence/calls.ts | 3 ++- client/src/index.tsx | 3 ++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/client/src/app/App.tsx b/client/src/app/App.tsx index 055d13a..bc1d829 100644 --- a/client/src/app/App.tsx +++ b/client/src/app/App.tsx @@ -1,4 +1,4 @@ -import { createClient, FluenceClient } from '@fluencelabs/fluence'; +import { createClient, FluenceClient, subscribeToEvent } from '@fluencelabs/fluence'; import { dev } from '@fluencelabs/fluence-network-environment'; import React, { useEffect, useState } from 'react'; @@ -69,8 +69,8 @@ const App = () => {
- {/*
{isInRoom && client && }
*/} -
{isInRoom && client && }
+
{isInRoom && client && }
+ {/*
{isInRoom && client && }
*/}
diff --git a/client/src/app/CollaborativeEditor.tsx b/client/src/app/CollaborativeEditor.tsx index b59c5b8..233f8e2 100644 --- a/client/src/app/CollaborativeEditor.tsx +++ b/client/src/app/CollaborativeEditor.tsx @@ -42,16 +42,16 @@ const getUpdatedDocFromText = (oldDoc: TextDoc | null, newText: string) => { return newDoc; }; -const parseState = (entry: calls.Entry) => { +const parseState = (entry: string) => { try { - const obj = JSON.parse(entry.body); + const obj = JSON.parse(entry); if (obj.fluentPadState) { return Automerge.load(obj.fluentPadState) as TextDoc; } return null; } catch (e) { - console.log('couldnt parse state format: ' + entry.body); + console.log('couldnt parse state format: ' + entry); return null; } }; @@ -59,7 +59,7 @@ const parseState = (entry: calls.Entry) => { const applyStates = (startingDoc: TextDoc | null, entries: calls.Entry[]) => { let res = startingDoc; for (let entry of entries) { - const state = parseState(entry) as TextDoc; + const state = parseState(entry.body) as TextDoc; if (state) { if (!res) { res = state; @@ -69,6 +69,12 @@ const applyStates = (startingDoc: TextDoc | null, entries: calls.Entry[]) => { } } + if (res === null) { + res = Automerge.from({ + value: new Automerge.Text(), + }); + } + return res; }; @@ -78,8 +84,12 @@ export const CollaborativeEditor = () => { useEffect(() => { const unsub1 = subscribeToEvent(client, fluentPadServiceId, notifyTextUpdateFnName, (args, tetraplets) => { - console.log(args, tetraplets); - // TODO + const [stateStr, isAuthorized] = args; + const state = parseState(stateStr); + if (state && text) { + const newDoc = Automerge.merge(text, state); + setText(newDoc); + } }); // don't block diff --git a/client/src/fluence/calls.ts b/client/src/fluence/calls.ts index ae6488a..cb7c72c 100644 --- a/client/src/fluence/calls.ts +++ b/client/src/fluence/calls.ts @@ -257,7 +257,7 @@ export const addEntry = async (client: FluenceClient, entry: string) => { (seq (call userlistNode (userlist "get_users") [] allUsers) (seq - (call node (history "add") [entry token.$.["is_authenticated"]]) + (call historyNode (history "add") [entry token.$.["is_authenticated"]]) (fold allUsers.$.users! u (par (seq @@ -284,6 +284,7 @@ export const addEntry = async (client: FluenceClient, entry: string) => { fluentPadServiceId: fluentPadServiceId, notifyTextUpdate: notifyTextUpdateFnName, }, + 99999999, ); await sendParticle(client, particle); diff --git a/client/src/index.tsx b/client/src/index.tsx index c31f257..c6c29d1 100644 --- a/client/src/index.tsx +++ b/client/src/index.tsx @@ -4,7 +4,8 @@ import './index.scss'; import App from './app/App'; import log from 'loglevel'; -log.setLevel(2); +// log.setLevel('trace'); +log.setLevel('error'); ReactDOM.render(