blob: 4c6c09d28ea48967dd6f082fa816542c45f9cc38 [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 Scandolo04964232017-01-07 12:53:46 -080014 static $inject = ['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(
17 private webSocket: IWSEventService,
18 private storeHelpers: IStoreHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080019 private ModelRest: IXosResourceService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080020 ) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080021 this._collections = {};
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080022 }
23
Matteo Scandolo04964232017-01-07 12:53:46 -080024 public query(model: string) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080025 // if there isn't already an observable for that item
26 if (!this._collections[model]) {
27 this._collections[model] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource
28 this.loadInitialData(model);
29 }
30
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031 this.webSocket.list()
32 .filter((e: IWSEvent) => e.model === model)
33 .subscribe(
34 (event: IWSEvent) => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080035 this.storeHelpers.updateCollection(event, this._collections[model]);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080036 },
37 err => console.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080038 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080039
40 return this._collections[model].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080041 }
42
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080043 public search(modelName: string): any[] {
44 return _.reduce(Object.keys(this._collections), (results, k) => {
45 // console.log(k, this._collections[k].value)
46 const partialRes = _.filter(this._collections[k].value, i => {
47 if (i.humanReadableName) {
48 return i.humanReadableName.toLowerCase().indexOf(modelName) > -1;
49 }
50 else if (i.name) {
51 return i.name.toLowerCase().indexOf(modelName) > -1;
52 }
53 return false;
54 })
55 .map(m => {
56 m.modelName = k;
57 return m;
58 });
59 return results.concat(partialRes);
60 }, []);
61 }
62
Matteo Scandolo04964232017-01-07 12:53:46 -080063 public get(model: string, id: number) {
64 // TODO implement a get method
65 }
66
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080067 private loadInitialData(model: string) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080068 // NOTE check what is the correct pattern to pluralize this
Matteo Scandolo04964232017-01-07 12:53:46 -080069 const endpoint = this.storeHelpers.urlFromCoreModel(model);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080070 this.ModelRest.getResource(endpoint).query().$promise
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080071 .then(
72 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080073 this._collections[model].next(res);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080074 },
75 err => console.log(`Error retrieving ${model}`, err)
76 );
77 }
78}