From 8a68dcabae0233cbeb0d25853a1ddbf3f40afcc5 Mon Sep 17 00:00:00 2001 From: Eric Maciel Date: Wed, 28 Oct 2020 15:58:26 -0400 Subject: [PATCH] fix: socket stored on editor throwing circular error --- packages/client/src/withSocketIO.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/client/src/withSocketIO.ts b/packages/client/src/withSocketIO.ts index 7e8c2d3..62976ba 100644 --- a/packages/client/src/withSocketIO.ts +++ b/packages/client/src/withSocketIO.ts @@ -36,6 +36,7 @@ const withSocketIO = ( options: SocketIOPluginOptions ) => { const e = editor as T & WithSocketIOEditor + let socket: SocketIOClient.Socket const { onConnect, @@ -51,11 +52,11 @@ const withSocketIO = ( */ e.connect = () => { - if (!e.socket) { - e.socket = io(url, { ...connectOpts }) + if (!socket) { + socket = io(url, { ...connectOpts }) - e.socket.on('connect', () => { - e.clientId = e.socket.id + socket.on('connect', () => { + e.clientId = socket.id e.openConnection() @@ -63,21 +64,21 @@ const withSocketIO = ( }) } - e.socket.on('error', (msg: string) => { + socket.on('error', (msg: string) => { onError && onError(msg) }) - e.socket.on('msg', (data: CollabAction) => { + socket.on('msg', (data: CollabAction) => { e.receive(data) }) - e.socket.on('disconnect', () => { + socket.on('disconnect', () => { e.gabageCursor() onDisconnect && onDisconnect() }) - e.socket.connect() + socket.connect() return e } @@ -87,9 +88,9 @@ const withSocketIO = ( */ e.disconnect = () => { - e.socket.removeListener('msg') + socket.removeListener('msg') - e.socket.close() + socket.close() e.closeConnection() @@ -114,7 +115,7 @@ const withSocketIO = ( */ e.send = (msg: CollabAction) => { - e.socket.emit('msg', msg) + socket.emit('msg', msg) } /** @@ -122,7 +123,7 @@ const withSocketIO = ( */ e.destroy = () => { - e.socket.close() + socket.close() e.closeConnection() }