blob: 9a0709570e29b37dbd65fa25a55bd97559885bb4 [file] [log] [blame]
Matteo Scandolo0f77c502016-12-06 16:46:00 -08001/// <reference path="../../../../typings/index.d.ts"/>
2
3import {Injectable} from '@angular/core';
4import {BehaviorSubject} from 'rxjs/Rx';
Matteo Scandolo40f8fa92016-12-07 09:21:35 -08005import {IInstance} from '../../interfaces/models.interface';
Matteo Scandolo0f77c502016-12-06 16:46:00 -08006import {InstanceService} from '../rest/instance.service';
Matteo Scandolo0f77c502016-12-06 16:46:00 -08007import {IWSEvent} from '../../interfaces/ws.interface';
8import {GlobalEvent} from '../websockets/websocket.global';
Matteo Scandolo40f8fa92016-12-07 09:21:35 -08009import {ObservableCollectionHandler} from '../helpers/store.service';
Matteo Scandolo0f77c502016-12-06 16:46:00 -080010
11@Injectable()
12export 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 Scandolo40f8fa92016-12-07 09:21:35 -080017 .filter((e: IWSEvent) => e.model === 'Instance')
Matteo Scandolo0f77c502016-12-06 16:46:00 -080018 .subscribe(
19 (event: IWSEvent) => {
Matteo Scandolo40f8fa92016-12-07 09:21:35 -080020 ObservableCollectionHandler.update(event, this._instances);
Matteo Scandolo0f77c502016-12-06 16:46:00 -080021 }
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}