Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
| 2 | |
| 3 | import {BehaviorSubject, Observable} from 'rxjs/Rx'; |
| 4 | import {IWSEvent, IWSEventService} from '../websocket/global'; |
| 5 | import {IXosResourceService} from '../rest/model.rest'; |
| 6 | import {IStoreHelpersService} from '../helpers/store.helpers'; |
| 7 | |
| 8 | export interface IModelStoreService { |
| 9 | query(model: string): Observable<any>; |
| 10 | } |
| 11 | |
| 12 | export class ModelStore { |
| 13 | static $inject = ['WebSocket', 'StoreHelpers', 'ModelRest']; |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 14 | private _collections: any; // NOTE contains a map of {model: BehaviourSubject} |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 15 | constructor( |
| 16 | private webSocket: IWSEventService, |
| 17 | private storeHelpers: IStoreHelpersService, |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 18 | private ModelRest: IXosResourceService |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 19 | ) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 20 | this._collections = {}; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | query(model: string) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 24 | |
| 25 | // if there isn't already an observable for that item |
| 26 | if (!this._collections[model]) { |
| 27 | this._collections[model] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource |
| 28 | this.loadInitialData(model); |
| 29 | } |
| 30 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 31 | this.webSocket.list() |
| 32 | .filter((e: IWSEvent) => e.model === model) |
| 33 | .subscribe( |
| 34 | (event: IWSEvent) => { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 35 | this.storeHelpers.updateCollection(event, this._collections[model]); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 36 | }, |
| 37 | err => console.error |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 38 | ); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 39 | |
| 40 | return this._collections[model].asObservable(); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | private loadInitialData(model: string) { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 44 | // NOTE check what is the correct pattern to pluralize this |
| 45 | const endpoint = `/core/${model.toLowerCase()}s`; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 46 | this.ModelRest.getResource(endpoint).query().$promise |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 47 | .then( |
| 48 | res => { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 49 | this._collections[model].next(res); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | }, |
| 51 | err => console.log(`Error retrieving ${model}`, err) |
| 52 | ); |
| 53 | } |
| 54 | } |