refactoring

This commit is contained in:
Tom Moor committed 2020-10-11 23:06:15 -07:00
1 parent a7f2c7edb3
commit 2719321430
5 files changed
+61 -60

No files matched your search

+14 -14
View File
@@ -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, typeof Editor>((props, ref) => (
<EditorWithRouterAndTheme {...props} forwardedRef={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;
// }
+12 -19
View File
@@ -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<string,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;
/**
@@ -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 });
+6 -7
View File
@@ -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<number>} */ (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,
})
);
+27 -20
View File
@@ -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),
});
}