Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 2 | import * as _ from 'lodash'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 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 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 8 | export interface IXosModelStoreService { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 9 | query(model: string): Observable<any>; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 10 | search(modelName: string): any[]; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 11 | } |
| 12 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 13 | export class ModelStore implements IXosModelStoreService { |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 14 | static $inject = ['$log', 'WebSocket', 'StoreHelpers', 'ModelRest']; |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 15 | private _collections: any; // NOTE contains a map of {model: BehaviourSubject} |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 16 | constructor( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 17 | private $log: ng.ILogService, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 18 | private webSocket: IWSEventService, |
| 19 | private storeHelpers: IStoreHelpersService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 20 | private ModelRest: IXosResourceService, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 21 | ) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 22 | this._collections = {}; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 25 | public query(model: string): Observable<any> { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 26 | // if there isn't already an observable for that item |
| 27 | if (!this._collections[model]) { |
| 28 | this._collections[model] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource |
| 29 | this.loadInitialData(model); |
| 30 | } |
| 31 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 32 | this.webSocket.list() |
| 33 | .filter((e: IWSEvent) => e.model === model) |
| 34 | .subscribe( |
| 35 | (event: IWSEvent) => { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 36 | this.storeHelpers.updateCollection(event, this._collections[model]); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 37 | }, |
| 38 | err => console.error |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 39 | ); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 40 | |
| 41 | return this._collections[model].asObservable(); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 44 | public search(modelName: string): any[] { |
| 45 | return _.reduce(Object.keys(this._collections), (results, k) => { |
| 46 | // console.log(k, this._collections[k].value) |
| 47 | const partialRes = _.filter(this._collections[k].value, i => { |
| 48 | if (i.humanReadableName) { |
| 49 | return i.humanReadableName.toLowerCase().indexOf(modelName) > -1; |
| 50 | } |
| 51 | else if (i.name) { |
| 52 | return i.name.toLowerCase().indexOf(modelName) > -1; |
| 53 | } |
| 54 | return false; |
| 55 | }) |
| 56 | .map(m => { |
| 57 | m.modelName = k; |
| 58 | return m; |
| 59 | }); |
| 60 | return results.concat(partialRes); |
| 61 | }, []); |
| 62 | } |
| 63 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 64 | public get(model: string, id: number) { |
| 65 | // TODO implement a get method |
| 66 | } |
| 67 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 68 | private loadInitialData(model: string) { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 69 | // NOTE check what is the correct pattern to pluralize this |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 70 | const endpoint = this.storeHelpers.urlFromCoreModel(model); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 71 | this.ModelRest.getResource(endpoint).query().$promise |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 72 | .then( |
| 73 | res => { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 74 | this._collections[model].next(res); |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 75 | }) |
| 76 | .catch( |
| 77 | err => this.$log.log(`Error retrieving ${model}`, err) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 78 | ); |
| 79 | } |
| 80 | } |