Fix text synchronization

This commit is contained in:
Pavel Murygin 2021-01-14 23:57:24 +03:00
parent 60be27d97a
commit 77b70b7c49
4 changed files with 23 additions and 11 deletions

View File

@ -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 = () => {
</div>
<div className="wrapper">
{/* <div>{isInRoom && client && <CollaborativeEditor />}</div> */}
<div>{isInRoom && client && <UserList selfName={nickName} />}</div>
<div>{isInRoom && client && <CollaborativeEditor />}</div>
{/* <div>{isInRoom && client && <UserList selfName={nickName} />}</div> */}
</div>
</div>
</FluenceClientContext.Provider>

View File

@ -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

View File

@ -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);

View File

@ -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(
<React.StrictMode>