blob: 3b07ecad266a3d188ec5350a29fdbc31837cb758 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo035c5932016-12-14 09:55:15 -080019import * as io from 'socket.io-client';
Matteo Scandolo98b5f5d2017-03-17 17:09:05 -070020import * as _ from 'lodash';
Matteo Scandolo035c5932016-12-14 09:55:15 -080021import {Subject, Observable} from 'rxjs/Rx';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080022import {IXosAppConfig} from '../../../index';
Matteo Scandolo035c5932016-12-14 09:55:15 -080023
24export interface IWSEvent {
25 model: string;
26 msg: {
27 changed_fields: string[],
28 object?: any,
29 pk?: number
30 };
31}
32
33export interface IWSEventService {
34 list(): Observable<IWSEvent>;
35}
36
37export class WebSocketEvent {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080038
Matteo Scandolo98b5f5d2017-03-17 17:09:05 -070039 static $inject = [
40 'AppConfig',
41 '$log'
42 ];
Matteo Scandolo828d1e82017-01-17 14:49:38 -080043
Matteo Scandoloe015fa92017-05-10 09:52:36 -070044
Matteo Scandolo035c5932016-12-14 09:55:15 -080045 private _events: Subject<IWSEvent> = new Subject<IWSEvent>();
Matteo Scandoloe015fa92017-05-10 09:52:36 -070046 private socket;
47 constructor(
48 private AppConfig: IXosAppConfig,
49 private $log: ng.ILogService
50 ) {
51 // NOTE list of field that are not useful to the UI
52 const ignoredFields: string[] = ['created', 'updated', 'backend_register'];
Matteo Scandolo98b5f5d2017-03-17 17:09:05 -070053
Matteo Scandoloe015fa92017-05-10 09:52:36 -070054 this.socket = io(this.AppConfig.websocketClient);
55 this.socket.on('event', (data: IWSEvent): void => {
56 this.$log.debug(`[WebSocket] Received Event for: ${data.model} [${data.msg.pk}]`);
Matteo Scandolo98b5f5d2017-03-17 17:09:05 -070057
Matteo Scandoloe015fa92017-05-10 09:52:36 -070058 if (data.msg.changed_fields.length === 0 || _.intersection(data.msg.changed_fields, ignoredFields).length === data.msg.changed_fields.length) {
59 // NOTE means that the only updated fields does not change anything in the UI, so don't send events around
60 this.$log.debug(`[WebSocket] Ignoring Event for: ${data.model} [${data.msg.pk}]`);
61 return;
62 }
63
64 this._events.next(data);
65
66 // NOTE update observers of parent classes
67 if (data.msg.object.class_names && angular.isString(data.msg.object.class_names)) {
68 const models = data.msg.object.class_names.split(',');
69 _.forEach(models, (m: string) => {
70 data.model = m;
71 this._events.next(data);
72 });
73 }
74
75 });
Matteo Scandolo035c5932016-12-14 09:55:15 -080076 }
77 list() {
78 return this._events.asObservable();
79 }
80}