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