blob: 950d441c2f57e15b222f8e4c9bf28df95af3af52 [file] [log] [blame]
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08001/// <reference path="../../../../typings/index.d.ts"/>
Matteo Scandolo86bc26a2017-01-18 11:06:47 -08002import * as _ from 'lodash';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08003import {BehaviorSubject, Observable} from 'rxjs/Rx';
4import {IWSEvent, IWSEventService} from '../websocket/global';
5import {IXosResourceService} from '../rest/model.rest';
6import {IStoreHelpersService} from '../helpers/store.helpers';
Matteo Scandoloba0d92e2017-03-02 16:47:46 -08007import {IXosDebouncer} from '../../core/services/helpers/debounce.helper';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08008
Matteo Scandolo47860fe2017-02-02 12:05:55 -08009export interface IXosModelStoreService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080010 query(model: string, apiUrl?: string): Observable<any>;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080011 search(modelName: string): any[];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080012}
13
Matteo Scandolo1aee1982017-02-17 08:33:23 -080014export class XosModelStore implements IXosModelStoreService {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080015 static $inject = ['$log', 'WebSocket', 'StoreHelpers', 'ModelRest', 'XosDebouncer'];
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080016 private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080017 private efficientNext: any; // NOTE debounce next
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080018 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080019 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080020 private webSocket: IWSEventService,
21 private storeHelpers: IStoreHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080022 private ModelRest: IXosResourceService,
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080023 private XosDebouncer: IXosDebouncer
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080024 ) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080025 this._collections = {};
Matteo Scandolo968e7f22017-03-03 11:49:18 -080026 this.efficientNext = this.XosDebouncer.debounce(this.next, 500, this, false);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080027 }
28
Matteo Scandolo1aee1982017-02-17 08:33:23 -080029 public query(modelName: string, apiUrl: string): Observable<any> {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080030 // if there isn't already an observable for that item
Matteo Scandolo042ea632017-03-01 19:02:34 -080031 // create a new one and .next() is called by this.loadInitialData once data are received
Matteo Scandolo1aee1982017-02-17 08:33:23 -080032 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 Scandolof9dd4d02016-12-22 15:17:01 -080035 }
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070036 // else manually trigger the next with the last know value to trigger the subscribe method of who's requesting this data
Matteo Scandolo042ea632017-03-01 19:02:34 -080037 else {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080038 this.efficientNext(this._collections[modelName]);
Matteo Scandolo042ea632017-03-01 19:02:34 -080039 }
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080040
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080041 this.webSocket.list()
Matteo Scandolo1aee1982017-02-17 08:33:23 -080042 .filter((e: IWSEvent) => e.model === modelName)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080043 .subscribe(
44 (event: IWSEvent) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080045 this.storeHelpers.updateCollection(event, this._collections[modelName]);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080046 },
Matteo Scandolo38e94a82017-03-02 12:17:27 -080047 err => this.$log.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080048 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080049
Matteo Scandolo1aee1982017-02-17 08:33:23 -080050 return this._collections[modelName].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080051 }
52
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080053 public search(modelName: string): any[] {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080054 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 Scandolo86bc26a2017-01-18 11:06:47 -080070 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080071 partialRes.map(m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080072 m.modelName = k;
73 return m;
74 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080075 return results.concat(partialRes);
76 }, []);
77 return res;
78 } catch (e) {
79 return [];
80 }
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080081 }
82
Matteo Scandolo04964232017-01-07 12:53:46 -080083 public get(model: string, id: number) {
84 // TODO implement a get method
85 }
86
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080087 private next(subject: BehaviorSubject<any>): void {
88 subject.next(subject.value);
89 }
90
Matteo Scandolo1aee1982017-02-17 08:33:23 -080091 private loadInitialData(model: string, apiUrl?: string) {
Matteo Scandolo042ea632017-03-01 19:02:34 -080092 // TODO provide always the apiUrl togheter with the query() params
Matteo Scandolo1aee1982017-02-17 08:33:23 -080093 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 Scandolof2c3ed62016-12-15 14:32:50 -080098 .then(
99 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800100 this._collections[model].next(res);
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800101 })
102 .catch(
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800103 err => {
104 this._collections[model].error(err);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800105 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800106 );
107 }
108}