Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
| 2 | |
| 3 | import {Injectable} from '@angular/core'; |
| 4 | import {BehaviorSubject} from 'rxjs/Rx'; |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 5 | import {IInstance} from '../../interfaces/models.interface'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 6 | import {InstanceService} from '../rest/instance.service'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 7 | import {IWSEvent} from '../../interfaces/ws.interface'; |
| 8 | import {GlobalEvent} from '../websockets/websocket.global'; |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 9 | import {ObservableCollectionHandler} from '../helpers/store.service'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 10 | |
| 11 | @Injectable() |
| 12 | export class InstanceStore { |
| 13 | private _instances: BehaviorSubject<IInstance[]> = new BehaviorSubject([]); |
| 14 | constructor(private instanceService: InstanceService, private globalEvent: GlobalEvent) { |
| 15 | this.loadInitialData(); |
| 16 | this.globalEvent.list() |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 17 | .filter((e: IWSEvent) => e.model === 'Instance') |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 18 | .subscribe( |
| 19 | (event: IWSEvent) => { |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 20 | ObservableCollectionHandler.update(event, this._instances); |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 21 | } |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | loadInitialData() { |
| 26 | this.instanceService.query() |
| 27 | .subscribe( |
| 28 | res => { |
| 29 | this._instances.next(res); |
| 30 | }, |
| 31 | err => console.log('Error retrieving Instances', err) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | query() { |
| 36 | return this._instances.asObservable(); |
| 37 | } |
| 38 | |
| 39 | } |