Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import * as io from 'socket.io-client'; |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 2 | import * as _ from 'lodash'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 3 | import {Subject, Observable} from 'rxjs/Rx'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 4 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 5 | |
| 6 | export interface IWSEvent { |
| 7 | model: string; |
| 8 | msg: { |
| 9 | changed_fields: string[], |
| 10 | object?: any, |
| 11 | pk?: number |
| 12 | }; |
| 13 | } |
| 14 | |
| 15 | export interface IWSEventService { |
| 16 | list(): Observable<IWSEvent>; |
| 17 | } |
| 18 | |
| 19 | export class WebSocketEvent { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 20 | |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 21 | static $inject = [ |
| 22 | 'AppConfig', |
| 23 | '$log' |
| 24 | ]; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 25 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 26 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 27 | private _events: Subject<IWSEvent> = new Subject<IWSEvent>(); |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 28 | private socket; |
| 29 | constructor( |
| 30 | private AppConfig: IXosAppConfig, |
| 31 | private $log: ng.ILogService |
| 32 | ) { |
| 33 | // NOTE list of field that are not useful to the UI |
| 34 | const ignoredFields: string[] = ['created', 'updated', 'backend_register']; |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 35 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 36 | this.socket = io(this.AppConfig.websocketClient); |
| 37 | this.socket.on('event', (data: IWSEvent): void => { |
| 38 | this.$log.debug(`[WebSocket] Received Event for: ${data.model} [${data.msg.pk}]`); |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 39 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 40 | if (data.msg.changed_fields.length === 0 || _.intersection(data.msg.changed_fields, ignoredFields).length === data.msg.changed_fields.length) { |
| 41 | // NOTE means that the only updated fields does not change anything in the UI, so don't send events around |
| 42 | this.$log.debug(`[WebSocket] Ignoring Event for: ${data.model} [${data.msg.pk}]`); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | this._events.next(data); |
| 47 | |
| 48 | // NOTE update observers of parent classes |
| 49 | if (data.msg.object.class_names && angular.isString(data.msg.object.class_names)) { |
| 50 | const models = data.msg.object.class_names.split(','); |
| 51 | _.forEach(models, (m: string) => { |
| 52 | data.model = m; |
| 53 | this._events.next(data); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | }); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 58 | } |
| 59 | list() { |
| 60 | return this._events.asObservable(); |
| 61 | } |
| 62 | } |