Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
| 2 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 3 | import {Subject, Observable} from 'rxjs/Rx'; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 4 | import {IWSEvent, IWSEventService} from '../websocket/global'; |
| 5 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 6 | export interface IStoreService { |
| 7 | query(): Observable<any>; |
| 8 | } |
| 9 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 10 | export class SynchronizerStore { |
| 11 | static $inject = ['WebSocket']; |
| 12 | private _notifications: Subject<IWSEvent> = new Subject(); |
| 13 | constructor( |
| 14 | private webSocket: IWSEventService |
| 15 | ) { |
| 16 | this.webSocket.list() |
| 17 | .filter((e: IWSEvent) => { |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 18 | if (!e.msg || !e.msg.changed_fields) { |
| 19 | return false; |
| 20 | } |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 21 | return e.msg.changed_fields.indexOf('backend_status') > -1; |
| 22 | }) |
| 23 | .subscribe( |
| 24 | (event: IWSEvent) => { |
| 25 | this._notifications.next(event); |
| 26 | } |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | query() { |
| 31 | return this._notifications.asObservable(); |
| 32 | } |
| 33 | } |