From 2719321430d1ac3c2529104fdfd5f40c7830ab2c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 11 Oct 2020 23:06:15 -0700 Subject: [PATCH] refactoring --- app/components/Editor.js | 28 +++++------ app/multiplayer/websocket.js | 31 +++++------- .../Document/components/SocketPresence.js | 2 + server/multiplayer/WSSharedDoc.js | 13 +++-- server/multiplayer/utils.js | 47 +++++++++++-------- 5 files changed, 61 insertions(+), 60 deletions(-) diff --git a/app/components/Editor.js b/app/components/Editor.js index 35b4044c8e..5f3c4844ef 100644 --- a/app/components/Editor.js +++ b/app/components/Editor.js @@ -122,20 +122,6 @@ const StyledEditor = styled(RichMarkdownEditor)` padding-right: 2px; } } - - > .ProseMirror-yjs-cursor:first-child { - margin-top: 16px; - } - - p:first-child, - h1:first-child, - h2:first-child, - h3:first-child, - h4:first-child, - h5:first-child, - h6:first-child { - margin-top: 16px; - } } p { @@ -168,3 +154,17 @@ const EditorWithRouterAndTheme = withRouter(withTheme(Editor)); export default React.forwardRef((props, ref) => ( )); + +// > .ProseMirror-yjs-cursor:first-child { +// margin-top: 16px; +// } + +// p:first-child, +// h1:first-child, +// h2:first-child, +// h3:first-child, +// h4:first-child, +// h5:first-child, +// h6:first-child { +// margin-top: 16px; +// } diff --git a/app/multiplayer/websocket.js b/app/multiplayer/websocket.js index 7d75b3711e..4f62f3b98f 100644 --- a/app/multiplayer/websocket.js +++ b/app/multiplayer/websocket.js @@ -5,11 +5,9 @@ import * as bc from "lib0/broadcastchannel.js"; import * as decoding from "lib0/decoding.js"; import * as encoding from "lib0/encoding.js"; -import * as math from "lib0/math.js"; import * as mutex from "lib0/mutex.js"; import { Observable } from "lib0/observable.js"; import * as time from "lib0/time.js"; -import * as url from "lib0/url.js"; import * as awarenessProtocol from "y-protocols/awareness.js"; import * as syncProtocol from "y-protocols/sync.js"; import * as Y from "yjs"; @@ -99,7 +97,7 @@ const setupWS = (provider) => { if (!buff) return; provider.socket.binary(true).emit("presence", { - documentId: provider.roomname, + documentId: provider.documentId, userId: provider.userId, data: buff, }); @@ -175,7 +173,7 @@ const setupWS = (provider) => { export class WebsocketProvider extends Observable { /** * @param {string} serverUrl - * @param {string} roomname + * @param {string} documentId * @param {Y.Doc} doc * @param {object} [opts] * @param {boolean} [opts.connect] @@ -186,24 +184,25 @@ export class WebsocketProvider extends Observable { */ constructor( socket, - roomname, - userId, - doc, + documentId: string, + userId: string, + doc: Y.Doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), - resyncInterval = 10 * 1000, + resyncInterval = 15 * 1000, + }: { + connect: boolean, + awareness: awarenessProtocol.Awareness, + resyncInterval: number, } = {} ) { super(); this.socket = socket; - this.bcChannel = roomname; - this.roomname = roomname; + this.bcChannel = documentId; + this.documentId = documentId; this.userId = userId; this.doc = doc; - /** - * @type {Object} - */ this._localAwarenessState = {}; this.awareness = awareness; this.wsconnected = false; @@ -211,13 +210,7 @@ export class WebsocketProvider extends Observable { this.bcconnected = false; this.wsUnsuccessfulReconnects = 0; this.mux = mutex.createMutex(); - /** - * @type {boolean} - */ this._synced = false; - /** - * @type {WebSocket?} - */ this.ws = null; this.wsLastMessageReceived = 0; /** diff --git a/app/scenes/Document/components/SocketPresence.js b/app/scenes/Document/components/SocketPresence.js index 4d93042f7d..3a74ef4f4e 100644 --- a/app/scenes/Document/components/SocketPresence.js +++ b/app/scenes/Document/components/SocketPresence.js @@ -18,6 +18,8 @@ export default function SocketPresence(props: Props) { ); React.useEffect(() => { + if (!context) return; + const emitJoin = () => { if (!context) return; context.emit("join", { documentId: props.documentId }); diff --git a/server/multiplayer/WSSharedDoc.js b/server/multiplayer/WSSharedDoc.js index 30dfec467d..0d106a7c1a 100644 --- a/server/multiplayer/WSSharedDoc.js +++ b/server/multiplayer/WSSharedDoc.js @@ -7,9 +7,9 @@ import * as Y from "yjs"; import { MESSAGE_AWARENESS, MESSAGE_SYNC } from "../../shared/constants"; export default class WSSharedDoc extends Y.Doc { - constructor(name: string) { + constructor(documentId: string) { super({ gc: true }); - this.name = name; + this.documentId = documentId; this.mux = mutex.createMutex(); this.conns = new Map(); this.awareness = new awarenessProtocol.Awareness(this); @@ -18,9 +18,8 @@ export default class WSSharedDoc extends Y.Doc { this.awareness.on("update", ({ added, updated, removed }, conn) => { const changedClients = added.concat(updated, removed); if (conn !== null) { - const connControlledIDs = /** @type {Set} */ (this.conns.get( - conn - )); + const connControlledIDs = this.conns.get(conn); + if (connControlledIDs !== undefined) { added.forEach((clientID) => { connControlledIDs.add(clientID); @@ -41,7 +40,7 @@ export default class WSSharedDoc extends Y.Doc { const buff = encoding.toUint8Array(encoder); this.conns.forEach((_, conn) => { conn.binary(true).emit("user.presence", { - documentId: this.name, + documentId: this.documentId, data: buff, }); }); @@ -55,7 +54,7 @@ export default class WSSharedDoc extends Y.Doc { doc.conns.forEach((_, conn) => conn.binary(true).emit("user.presence", { - documentId: this.name, + documentId: this.documentId, data: buff, }) ); diff --git a/server/multiplayer/utils.js b/server/multiplayer/utils.js index c0622cdf0f..330076a0a1 100644 --- a/server/multiplayer/utils.js +++ b/server/multiplayer/utils.js @@ -13,15 +13,13 @@ const messageListener = (conn, doc, message) => { const decoder = decoding.createDecoder(message); const messageType = decoding.readVarUint(decoder); - console.log("messageListener", message); - switch (messageType) { case MESSAGE_SYNC: encoding.writeVarUint(encoder, MESSAGE_SYNC); syncProtocol.readSyncMessage(decoder, encoder, doc, null); if (encoding.length(encoder) > 1) { conn.binary(true).emit({ - documentId: doc.name, + documentId: doc.documentId, data: encoding.toUint8Array(encoder), }); } @@ -39,22 +37,25 @@ const messageListener = (conn, doc, message) => { }; const cleanup = (doc, conn) => { - if (doc.conns.has(conn)) { - const controlledIds = doc.conns.get(conn); - doc.conns.delete(conn); + if (!doc || !doc.conns.has(conn)) { + return; + } + const controlledIds = doc.conns.get(conn); + doc.conns.delete(conn); - awarenessProtocol.removeAwarenessStates( - doc.awareness, - Array.from(controlledIds), - null - ); + awarenessProtocol.removeAwarenessStates( + doc.awareness, + Array.from(controlledIds), + null + ); - // last person has left this editing session - if (doc.conns.size === 0) { - // TODO: Write document state to database - doc.destroy(); - docs.delete(doc.name); - } + // last person has left this editing session + if (doc.conns.size === 0) { + // TODO: Write document state to database + console.log("everyone left, write to database here"); + + doc.destroy(); + docs.delete(doc.documentId); } }; @@ -68,6 +69,7 @@ export const setupConnection = (conn, documentId: string) => { doc = new WSSharedDoc(documentId); // TODO: Grab state from database + console.log("new session, load from database"); docs.set(documentId, doc); } @@ -79,7 +81,12 @@ export const setupConnection = (conn, documentId: string) => { messageListener(conn, doc, new Uint8Array(event.data)) ); - conn.on("close", cleanup); + conn.on("disconnecting", () => cleanup(doc, conn)); + conn.on("leave", (event) => { + if (event.documentId === doc.documentId) { + cleanup(doc, conn); + } + }); // send sync step 1 const encoder = encoding.createEncoder(); @@ -87,7 +94,7 @@ export const setupConnection = (conn, documentId: string) => { syncProtocol.writeSyncStep1(encoder, doc); conn.binary(true).emit("user.presence", { - documentId: doc.name, + documentId: doc.documentId, data: encoding.toUint8Array(encoder), }); @@ -105,7 +112,7 @@ export const setupConnection = (conn, documentId: string) => { ); conn.binary(true).emit("user.presence", { - documentId: doc.name, + documentId: doc.documentId, data: encoding.toUint8Array(encoder), }); }