2018-07-15 14:04:27 +00:00
|
|
|
import * as plugins from './lik.plugins';
|
2016-07-31 12:36:28 +00:00
|
|
|
|
2016-08-08 15:29:44 +00:00
|
|
|
export interface IObjectmapForEachFunction<T> {
|
2018-01-27 17:11:11 +00:00
|
|
|
(itemArg: T): void;
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-08-08 14:00:14 +00:00
|
|
|
|
2016-08-08 15:29:44 +00:00
|
|
|
export interface IObjectmapFindFunction<T> {
|
2018-01-27 17:11:11 +00:00
|
|
|
(itemArg: T): boolean;
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-07-31 12:36:28 +00:00
|
|
|
|
2016-07-30 22:54:46 +00:00
|
|
|
/**
|
|
|
|
* allows keeping track of objects
|
|
|
|
*/
|
2016-08-08 15:29:44 +00:00
|
|
|
export class Objectmap<T> {
|
2018-01-27 17:11:11 +00:00
|
|
|
private objectArray: T[] = [];
|
2017-06-17 12:44:55 +00:00
|
|
|
|
2019-11-27 23:15:13 +00:00
|
|
|
// events
|
|
|
|
public eventSubject = new plugins.smartrx.rxjs.Subject<any>();
|
|
|
|
|
2017-06-17 12:44:55 +00:00
|
|
|
/**
|
|
|
|
* returns a new instance
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
constructor() {
|
2017-06-17 12:44:55 +00:00
|
|
|
// nothing here
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add object to Objectmap
|
2017-07-05 12:29:08 +00:00
|
|
|
* returns false if the object is already in the map
|
|
|
|
* returns true if the object was added successfully
|
2017-06-17 12:44:55 +00:00
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public add(objectArg: T): boolean {
|
2017-07-05 12:29:08 +00:00
|
|
|
if (this.checkForObject(objectArg)) {
|
|
|
|
// the object is already in the objectmap
|
2018-01-27 17:11:11 +00:00
|
|
|
return false;
|
2017-07-05 12:29:08 +00:00
|
|
|
} else {
|
|
|
|
// the object is not yet in the objectmap
|
2018-01-27 17:11:11 +00:00
|
|
|
this.objectArray.push(objectArg);
|
2019-11-27 23:15:13 +00:00
|
|
|
this.eventSubject.next('add');
|
2018-01-27 17:11:11 +00:00
|
|
|
return true;
|
2017-07-05 12:29:08 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* like .add but adds an whole array of objects
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public addArray(objectArrayArg: T[]) {
|
2017-06-17 12:44:55 +00:00
|
|
|
for (let item of objectArrayArg) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.add(item);
|
2016-08-08 14:00:14 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if object is in Objectmap
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public checkForObject(objectArg: T) {
|
2018-01-27 17:11:11 +00:00
|
|
|
return this.objectArray.indexOf(objectArg) !== -1;
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find object
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public find(findFunction: IObjectmapFindFunction<T>) {
|
|
|
|
const resultArray = this.objectArray.filter(findFunction);
|
2017-06-17 12:44:55 +00:00
|
|
|
if (resultArray.length > 0) {
|
2018-01-27 17:11:11 +00:00
|
|
|
return resultArray[0];
|
2017-06-17 12:44:55 +00:00
|
|
|
} else {
|
2018-01-27 17:11:11 +00:00
|
|
|
return null;
|
2016-11-19 22:08:54 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* finds a specific element and then removes it
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T {
|
|
|
|
const foundElement = this.find(findFunction);
|
2017-06-17 12:44:55 +00:00
|
|
|
if (foundElement) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.remove(foundElement);
|
2016-07-30 22:54:46 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
return foundElement;
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run function for each item in Objectmap
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public async forEach(functionArg: IObjectmapForEachFunction<T>) {
|
2017-08-27 18:01:06 +00:00
|
|
|
for (let object of this.objectArray) {
|
2018-01-27 17:11:11 +00:00
|
|
|
await functionArg(object);
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets an object in the Observablemap and removes it, so it can't be retrieved again
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public getOneAndRemove(): T {
|
2019-11-27 23:15:13 +00:00
|
|
|
const removedItem = this.objectArray.shift();
|
|
|
|
this.eventSubject.next('remove');
|
|
|
|
return removedItem;
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a cloned array of all the objects currently in the Objectmap
|
|
|
|
*/
|
2019-03-15 22:50:39 +00:00
|
|
|
public getArray(): T[] {
|
2018-11-23 19:33:44 +00:00
|
|
|
const returnArray: any[] = [];
|
|
|
|
for (const objectItem of this.objectArray) {
|
|
|
|
returnArray.push(objectItem);
|
|
|
|
}
|
|
|
|
return returnArray;
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if Objectmap ist empty
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public isEmpty(): boolean {
|
2017-06-17 12:44:55 +00:00
|
|
|
if (this.objectArray.length === 0) {
|
2018-01-27 17:11:11 +00:00
|
|
|
return true;
|
2017-06-17 12:44:55 +00:00
|
|
|
} else {
|
2018-01-27 17:11:11 +00:00
|
|
|
return false;
|
2016-09-29 20:05:20 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove object from Objectmap
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public remove(objectArg: T) {
|
2018-01-27 17:11:11 +00:00
|
|
|
let replacementArray = [];
|
2017-06-17 12:44:55 +00:00
|
|
|
for (let item of this.objectArray) {
|
|
|
|
if (item !== objectArg) {
|
2018-01-27 17:11:11 +00:00
|
|
|
replacementArray.push(item);
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
2016-11-19 22:08:54 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
this.objectArray = replacementArray;
|
2019-11-27 23:15:13 +00:00
|
|
|
this.eventSubject.next('remove');
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wipe Objectmap
|
|
|
|
*/
|
2018-11-23 19:33:44 +00:00
|
|
|
public wipe() {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.objectArray = [];
|
2019-11-27 23:15:13 +00:00
|
|
|
this.eventSubject.next('wiped');
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|