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 | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 9 | query(model: string, apiUrl?: 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 | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 13 | export class XosModelStore 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 | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 25 | public query(modelName: string, apiUrl: string): Observable<any> { |
Matteo Scandolo | 38e94a8 | 2017-03-02 12:17:27 -0800 | [diff] [blame^] | 26 | if (modelName === 'XOSGuiExtension') { |
| 27 | this.$log.log(performance.now(), `QUERY Model ${modelName}` ); |
| 28 | } |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 29 | // if there isn't already an observable for that item |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 30 | // create a new one and .next() is called by this.loadInitialData once data are received |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 31 | if (!this._collections[modelName]) { |
| 32 | this._collections[modelName] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource |
| 33 | this.loadInitialData(modelName, apiUrl); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 34 | } |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 35 | // else manually trigger the next with the last know value to trigger the subscribe method of who's requestiong this data |
| 36 | else { |
| 37 | this._collections[modelName].next(this._collections[modelName].value); |
| 38 | } |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 39 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 40 | this.webSocket.list() |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 41 | .filter((e: IWSEvent) => e.model === modelName) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 42 | .subscribe( |
| 43 | (event: IWSEvent) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 44 | this.storeHelpers.updateCollection(event, this._collections[modelName]); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 45 | }, |
Matteo Scandolo | 38e94a8 | 2017-03-02 12:17:27 -0800 | [diff] [blame^] | 46 | err => this.$log.error |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 47 | ); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 48 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 49 | return this._collections[modelName].asObservable(); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 52 | public search(modelName: string): any[] { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 53 | try { |
| 54 | const res = _.reduce(Object.keys(this._collections), (results, k) => { |
| 55 | let partialRes; |
| 56 | // NOTE wrapped in a try catch as some subject may be errored, due to not available REST endpoint |
| 57 | try { |
| 58 | partialRes = _.filter(this._collections[k].value, i => { |
| 59 | if (i && i.humanReadableName) { |
| 60 | return i.humanReadableName.toLowerCase().indexOf(modelName) > -1; |
| 61 | } |
| 62 | else if (i && i.name) { |
| 63 | return i.name.toLowerCase().indexOf(modelName) > -1; |
| 64 | } |
| 65 | return false; |
| 66 | }); |
| 67 | } catch (e) { |
| 68 | partialRes = []; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 69 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 70 | partialRes.map(m => { |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 71 | m.modelName = k; |
| 72 | return m; |
| 73 | }); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 74 | return results.concat(partialRes); |
| 75 | }, []); |
| 76 | return res; |
| 77 | } catch (e) { |
| 78 | return []; |
| 79 | } |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 82 | public get(model: string, id: number) { |
| 83 | // TODO implement a get method |
| 84 | } |
| 85 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 86 | private loadInitialData(model: string, apiUrl?: string) { |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 87 | // TODO provide always the apiUrl togheter with the query() params |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 88 | if (!angular.isDefined(apiUrl)) { |
| 89 | // NOTE check what is the correct pattern to pluralize this |
| 90 | apiUrl = this.storeHelpers.urlFromCoreModel(model); |
| 91 | } |
| 92 | this.ModelRest.getResource(apiUrl).query().$promise |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 93 | .then( |
| 94 | res => { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 95 | this._collections[model].next(res); |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 96 | }) |
| 97 | .catch( |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 98 | err => { |
| 99 | this._collections[model].error(err); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 100 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 101 | ); |
| 102 | } |
| 103 | } |