Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 19 | import {BehaviorSubject} from 'rxjs'; |
| 20 | import * as _ from 'lodash'; |
| 21 | import {IWSEvent} from '../websocket/global'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 22 | import {IXosResourceService} from '../rest/model.rest'; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 23 | import {IXosModeldefsCache} from './modeldefs.service'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 24 | |
| 25 | export interface IStoreHelpersService { |
| 26 | updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>; |
| 27 | } |
| 28 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 29 | export class StoreHelpers implements IStoreHelpersService { |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 30 | static $inject = [ |
| 31 | 'ModelRest', |
| 32 | 'XosModeldefsCache' |
| 33 | ]; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 34 | |
| 35 | constructor ( |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 36 | private modelRest: IXosResourceService, |
| 37 | private XosModeldefsCache: IXosModeldefsCache |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 38 | ) { |
| 39 | } |
| 40 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 41 | public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> { |
| 42 | const collection: any[] = subject.value; |
| 43 | const index: number = _.findIndex(collection, (i) => { |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 44 | // NOTE evaluate to use event.msg.pk |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 45 | return i.id === event.msg.object.id; |
| 46 | }); |
| 47 | const exist: boolean = index > -1; |
| 48 | const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted'); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 49 | |
| 50 | // generate a resource for the model |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 51 | const modelDef = this.XosModeldefsCache.get(event.model); // get the model definition |
| 52 | const endpoint = this.XosModeldefsCache.getApiUrlFromModel(modelDef); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 53 | const resource = this.modelRest.getResource(endpoint); |
| 54 | const model = new resource(event.msg.object); |
| 55 | |
| 56 | // remove |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 57 | if (exist && isDeleted) { |
| 58 | _.remove(collection, {id: event.msg.object.id}); |
| 59 | } |
| 60 | // Replace item at index using native splice |
| 61 | else if (exist && !isDeleted) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 62 | collection.splice(index, 1, model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 63 | } |
| 64 | // if the element is not deleted add it |
| 65 | else if (!exist && !isDeleted) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 66 | collection.push(model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | subject.next(collection); |
| 70 | |
| 71 | return subject; |
| 72 | } |
| 73 | } |