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