blob: a070436d0ab752256be479c0fbcd413aefe135e4 [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';
7
8export interface IModelStoreService {
9 query(model: string): Observable<any>;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080010 search(modelName: string): any[];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080011}
12
13export class ModelStore {
Matteo Scandolo17bf8242017-01-23 17:30:39 -080014 static $inject = ['$log', 'WebSocket', 'StoreHelpers', 'ModelRest'];
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080015 private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080016 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080017 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080018 private webSocket: IWSEventService,
19 private storeHelpers: IStoreHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080020 private ModelRest: IXosResourceService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080021 ) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080022 this._collections = {};
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080023 }
24
Matteo Scandolo04964232017-01-07 12:53:46 -080025 public query(model: string) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080026 // 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 Scandolof2c3ed62016-12-15 14:32:50 -080032 this.webSocket.list()
33 .filter((e: IWSEvent) => e.model === model)
34 .subscribe(
35 (event: IWSEvent) => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080036 this.storeHelpers.updateCollection(event, this._collections[model]);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080037 },
38 err => console.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080039 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080040
41 return this._collections[model].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080042 }
43
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080044 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 Scandolo04964232017-01-07 12:53:46 -080064 public get(model: string, id: number) {
65 // TODO implement a get method
66 }
67
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080068 private loadInitialData(model: string) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080069 // NOTE check what is the correct pattern to pluralize this
Matteo Scandolo04964232017-01-07 12:53:46 -080070 const endpoint = this.storeHelpers.urlFromCoreModel(model);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080071 this.ModelRest.getResource(endpoint).query().$promise
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080072 .then(
73 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080074 this._collections[model].next(res);
Matteo Scandolo17bf8242017-01-23 17:30:39 -080075 })
76 .catch(
77 err => this.$log.log(`Error retrieving ${model}`, err)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080078 );
79 }
80}