fix: socket stored on editor throwing circular error

This commit is contained in:
Eric Maciel 2020-10-28 15:58:26 -04:00
parent 1bb805958f
commit 8a68dcabae

View File

@ -36,6 +36,7 @@ const withSocketIO = <T extends AutomergeEditor>(
options: SocketIOPluginOptions options: SocketIOPluginOptions
) => { ) => {
const e = editor as T & WithSocketIOEditor const e = editor as T & WithSocketIOEditor
let socket: SocketIOClient.Socket
const { const {
onConnect, onConnect,
@ -51,11 +52,11 @@ const withSocketIO = <T extends AutomergeEditor>(
*/ */
e.connect = () => { e.connect = () => {
if (!e.socket) { if (!socket) {
e.socket = io(url, { ...connectOpts }) socket = io(url, { ...connectOpts })
e.socket.on('connect', () => { socket.on('connect', () => {
e.clientId = e.socket.id e.clientId = socket.id
e.openConnection() e.openConnection()
@ -63,21 +64,21 @@ const withSocketIO = <T extends AutomergeEditor>(
}) })
} }
e.socket.on('error', (msg: string) => { socket.on('error', (msg: string) => {
onError && onError(msg) onError && onError(msg)
}) })
e.socket.on('msg', (data: CollabAction) => { socket.on('msg', (data: CollabAction) => {
e.receive(data) e.receive(data)
}) })
e.socket.on('disconnect', () => { socket.on('disconnect', () => {
e.gabageCursor() e.gabageCursor()
onDisconnect && onDisconnect() onDisconnect && onDisconnect()
}) })
e.socket.connect() socket.connect()
return e return e
} }
@ -87,9 +88,9 @@ const withSocketIO = <T extends AutomergeEditor>(
*/ */
e.disconnect = () => { e.disconnect = () => {
e.socket.removeListener('msg') socket.removeListener('msg')
e.socket.close() socket.close()
e.closeConnection() e.closeConnection()
@ -114,7 +115,7 @@ const withSocketIO = <T extends AutomergeEditor>(
*/ */
e.send = (msg: CollabAction) => { e.send = (msg: CollabAction) => {
e.socket.emit('msg', msg) socket.emit('msg', msg)
} }
/** /**
@ -122,7 +123,7 @@ const withSocketIO = <T extends AutomergeEditor>(
*/ */
e.destroy = () => { e.destroy = () => {
e.socket.close() socket.close()
e.closeConnection() e.closeConnection()
} }