From d76c7766cce6ec50acdfd4be0d5d274731da4deb Mon Sep 17 00:00:00 2001 From: George Date: Sun, 7 Jun 2020 23:04:23 +0300 Subject: [PATCH] feat: add onError cb (#15) --- README.md | 1 + packages/client/src/withSocketIO.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 367f03e..cedb201 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Check [detailed example](https://github.com/cudr/slate-collaborative/blob/master cursorData?: any // any data passed to cursor onConnect?: () => void // connect callback onDisconnect?: () => void // disconnect callback + onError?: (reason: string) => void // error callback } ``` diff --git a/packages/client/src/withSocketIO.ts b/packages/client/src/withSocketIO.ts index e06dc80..03c861a 100644 --- a/packages/client/src/withSocketIO.ts +++ b/packages/client/src/withSocketIO.ts @@ -11,6 +11,8 @@ export interface SocketIOPluginOptions { onConnect?: () => void onDisconnect?: () => void + + onError?: (msg: string) => void } export interface WithSocketIOEditor { @@ -35,7 +37,14 @@ const withSocketIO = ( ) => { const e = editor as T & WithSocketIOEditor - const { onConnect, onDisconnect, connectOpts, url, autoConnect } = options + const { + onConnect, + onDisconnect, + onError, + connectOpts, + url, + autoConnect + } = options /** * Connect to Socket. @@ -54,6 +63,10 @@ const withSocketIO = ( }) } + e.socket.on('error', (msg: string) => { + onError && onError(msg) + }) + e.socket.on('msg', (data: CollabAction) => { e.receive(data) })