Commit 111c353a authored by Marcos Defendi's avatar Marcos Defendi
Browse files

Trigger onTyping when agent disconnects

parent d6c6fda0
...@@ -33,6 +33,13 @@ export default class LivechatClient extends LivechatRest implements ISocket { ...@@ -33,6 +33,13 @@ export default class LivechatClient extends LivechatRest implements ISocket {
throw new Error(`Invalid Protocol: ${protocol}, valids: ${Object.keys(Protocols).join()}`) throw new Error(`Invalid Protocol: ${protocol}, valids: ${Object.keys(Protocols).join()}`)
} }
} }
private _onAgentStatusChange(cb: ICallback){
this.onStreamData(this.livechatStream, ({ fields: { args: [{ type, status, username }] } }: any) => {
if (type === 'agentStatus') {
cb(null, { status, username });
}
})
}
async connect (options: ISocketOptions, callback?: ICallback): Promise <any> { return (await this.socket as ISocket).connect(options) } async connect (options: ISocketOptions, callback?: ICallback): Promise <any> { return (await this.socket as ISocket).connect(options) }
async disconnect (): Promise<any> { return (await this.socket as ISocket).disconnect() } async disconnect (): Promise<any> { return (await this.socket as ISocket).disconnect() }
async unsubscribe (subscription: ISubscription): Promise<any> { return (await this.socket as ISocket).unsubscribe(subscription) } async unsubscribe (subscription: ISubscription): Promise<any> { return (await this.socket as ISocket).unsubscribe(subscription) }
...@@ -41,7 +48,15 @@ export default class LivechatClient extends LivechatRest implements ISocket { ...@@ -41,7 +48,15 @@ export default class LivechatClient extends LivechatRest implements ISocket {
async subscribeLoggedNotify (): Promise<any> { return (await this.socket as IDriver) .subscribeLoggedNotify() } async subscribeLoggedNotify (): Promise<any> { return (await this.socket as IDriver) .subscribeLoggedNotify() }
async subscribeNotifyUser (): Promise<any> { return (await this.socket as IDriver) .subscribeNotifyUser() } async subscribeNotifyUser (): Promise<any> { return (await this.socket as IDriver) .subscribeNotifyUser() }
async onMessage (cb: ICallback): Promise<any> { return (await this.socket as IDriver).onMessage(cb) } async onMessage (cb: ICallback): Promise<any> { return (await this.socket as IDriver).onMessage(cb) }
async onTyping (cb: ICallback): Promise<any> { return (await this.socket as IDriver).onTyping(cb) } async onTyping (rid: string, cb: ICallback): Promise<any> {
await this.subscribe(this.livechatStream, rid)
this._onAgentStatusChange(async (error, { status, username }: any) => {
if(status === 'offline') {
return (await this.socket as IDriver).onTyping(cb, username, false);
}
})
return (await this.socket as IDriver).onTyping(cb)
}
async onAgentChange (rid: string, cb: ICallback) { async onAgentChange (rid: string, cb: ICallback) {
await this.subscribe(this.livechatStream, rid) await this.subscribe(this.livechatStream, rid)
this.onStreamData(this.livechatStream, ({ fields: { args: [{ type, data }] } }: any) => { this.onStreamData(this.livechatStream, ({ fields: { args: [{ type, data }] } }: any) => {
...@@ -52,10 +67,8 @@ export default class LivechatClient extends LivechatRest implements ISocket { ...@@ -52,10 +67,8 @@ export default class LivechatClient extends LivechatRest implements ISocket {
} }
async onAgentStatusChange(rid: string, cb:ICallback){ async onAgentStatusChange(rid: string, cb:ICallback){
await this.subscribe(this.livechatStream, rid); await this.subscribe(this.livechatStream, rid);
this.onStreamData(this.livechatStream, ({ fields: { args: [{ type, status }] } }: any) => { this._onAgentStatusChange((error, { status }: any) => {
if (type === 'agentStatus') { cb(status)
cb(status)
}
}) })
} }
......
...@@ -570,7 +570,10 @@ export class DDPDriver extends EventEmitter implements ISocket, IDriver { ...@@ -570,7 +570,10 @@ export class DDPDriver extends EventEmitter implements ISocket, IDriver {
this.ddp.on('stream-room-messages', ({ fields: { args: [message] } }: any) => cb(this.ejsonMessage(message))) this.ddp.on('stream-room-messages', ({ fields: { args: [message] } }: any) => cb(this.ejsonMessage(message)))
} }
onTyping = (cb: ICallback): Promise<any > => { onTyping = (cb: ICallback, username?: any, isTyping?: boolean): Promise<any> => {
if(status !== undefined && username) {
cb(username, isTyping)
}
return this.ddp.on('stream-notify-room', ({ fields: { args: [username, isTyping] } }: any) => { return this.ddp.on('stream-notify-room', ({ fields: { args: [username, isTyping] } }: any) => {
cb(username, isTyping) cb(username, isTyping)
}) as any }) as any
......
...@@ -47,7 +47,7 @@ export interface IDriver { ...@@ -47,7 +47,7 @@ export interface IDriver {
subscribeNotifyUser (): Promise<IDriver> subscribeNotifyUser (): Promise<IDriver>
onTyping (cb: ICallback): Promise<any> onTyping (cb: ICallback, username?: any, isTyping?: boolean): Promise<any>
notifyVisitorTyping(rid: string, username: string, typing: boolean, token: string): Promise<any> notifyVisitorTyping(rid: string, username: string, typing: boolean, token: string): Promise<any>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment