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