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 { dev } from '@fluencelabs/fluence-network-environment';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
@ -69,8 +69,8 @@ const App = () => {
</div> </div>
<div className="wrapper"> <div className="wrapper">
{/* <div>{isInRoom && client && <CollaborativeEditor />}</div> */} <div>{isInRoom && client && <CollaborativeEditor />}</div>
<div>{isInRoom && client && <UserList selfName={nickName} />}</div> {/* <div>{isInRoom && client && <UserList selfName={nickName} />}</div> */}
</div> </div>
</div> </div>
</FluenceClientContext.Provider> </FluenceClientContext.Provider>

View File

@ -42,16 +42,16 @@ const getUpdatedDocFromText = (oldDoc: TextDoc | null, newText: string) => {
return newDoc; return newDoc;
}; };
const parseState = (entry: calls.Entry) => { const parseState = (entry: string) => {
try { try {
const obj = JSON.parse(entry.body); const obj = JSON.parse(entry);
if (obj.fluentPadState) { if (obj.fluentPadState) {
return Automerge.load(obj.fluentPadState) as TextDoc; return Automerge.load(obj.fluentPadState) as TextDoc;
} }
return null; return null;
} catch (e) { } catch (e) {
console.log('couldnt parse state format: ' + entry.body); console.log('couldnt parse state format: ' + entry);
return null; return null;
} }
}; };
@ -59,7 +59,7 @@ const parseState = (entry: calls.Entry) => {
const applyStates = (startingDoc: TextDoc | null, entries: calls.Entry[]) => { const applyStates = (startingDoc: TextDoc | null, entries: calls.Entry[]) => {
let res = startingDoc; let res = startingDoc;
for (let entry of entries) { for (let entry of entries) {
const state = parseState(entry) as TextDoc; const state = parseState(entry.body) as TextDoc;
if (state) { if (state) {
if (!res) { if (!res) {
res = state; 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; return res;
}; };
@ -78,8 +84,12 @@ export const CollaborativeEditor = () => {
useEffect(() => { useEffect(() => {
const unsub1 = subscribeToEvent(client, fluentPadServiceId, notifyTextUpdateFnName, (args, tetraplets) => { const unsub1 = subscribeToEvent(client, fluentPadServiceId, notifyTextUpdateFnName, (args, tetraplets) => {
console.log(args, tetraplets); const [stateStr, isAuthorized] = args;
// TODO const state = parseState(stateStr);
if (state && text) {
const newDoc = Automerge.merge(text, state);
setText(newDoc);
}
}); });
// don't block // don't block

View File

@ -257,7 +257,7 @@ export const addEntry = async (client: FluenceClient, entry: string) => {
(seq (seq
(call userlistNode (userlist "get_users") [] allUsers) (call userlistNode (userlist "get_users") [] allUsers)
(seq (seq
(call node (history "add") [entry token.$.["is_authenticated"]]) (call historyNode (history "add") [entry token.$.["is_authenticated"]])
(fold allUsers.$.users! u (fold allUsers.$.users! u
(par (par
(seq (seq
@ -284,6 +284,7 @@ export const addEntry = async (client: FluenceClient, entry: string) => {
fluentPadServiceId: fluentPadServiceId, fluentPadServiceId: fluentPadServiceId,
notifyTextUpdate: notifyTextUpdateFnName, notifyTextUpdate: notifyTextUpdateFnName,
}, },
99999999,
); );
await sendParticle(client, particle); await sendParticle(client, particle);

View File

@ -4,7 +4,8 @@ import './index.scss';
import App from './app/App'; import App from './app/App';
import log from 'loglevel'; import log from 'loglevel';
log.setLevel(2); // log.setLevel('trace');
log.setLevel('error');
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>