Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import * as io from 'socket.io-client'; |
| 2 | import {Subject, Observable} from 'rxjs/Rx'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 3 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 4 | |
| 5 | export interface IWSEvent { |
| 6 | model: string; |
| 7 | msg: { |
| 8 | changed_fields: string[], |
| 9 | object?: any, |
| 10 | pk?: number |
| 11 | }; |
| 12 | } |
| 13 | |
| 14 | export interface IWSEventService { |
| 15 | list(): Observable<IWSEvent>; |
| 16 | } |
| 17 | |
| 18 | export class WebSocketEvent { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 19 | |
| 20 | static $inject = ['AppConfig']; |
| 21 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 22 | private _events: Subject<IWSEvent> = new Subject<IWSEvent>(); |
| 23 | private socket; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 24 | constructor( |
| 25 | private AppConfig: IXosAppConfig |
| 26 | ) { |
| 27 | this.socket = io(this.AppConfig.websocketClient); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 28 | this.socket.on('event', (data: IWSEvent): void => { |
| 29 | this._events.next(data); |
| 30 | }); |
| 31 | } |
| 32 | list() { |
| 33 | return this._events.asObservable(); |
| 34 | } |
| 35 | } |