You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
5 years ago
|
import { AnalyticUnit, AnalyticUnitId } from './analytic_unit';
|
||
|
import { createAnalyticUnit } from './utils';
|
||
|
|
||
|
|
||
|
export class AnalyticUnitsSet {
|
||
|
|
||
|
private _mapIdIndex: Map<AnalyticUnitId, number>;
|
||
|
private _items: AnalyticUnit[];
|
||
|
|
||
|
constructor(private _serverObject: any[]) {
|
||
|
if (_serverObject === undefined) {
|
||
|
throw new Error('server object can`t be undefined');
|
||
|
}
|
||
|
this._mapIdIndex = new Map<AnalyticUnitId, number>();
|
||
|
this._items = _serverObject.map(p => createAnalyticUnit(p));
|
||
|
this._rebuildIndex();
|
||
|
}
|
||
|
|
||
|
get items() { return this._items; }
|
||
|
|
||
|
addItem(item: AnalyticUnit) {
|
||
|
this._serverObject.push(item.serverObject);
|
||
|
this._mapIdIndex[item.id] = this._items.length;
|
||
|
this._items.push(item);
|
||
|
}
|
||
|
|
||
|
removeItem(id: AnalyticUnitId) {
|
||
|
var index = this._mapIdIndex[id];
|
||
|
this._serverObject.splice(index, 1);
|
||
|
this._items.splice(index, 1);
|
||
|
this._rebuildIndex();
|
||
|
}
|
||
|
|
||
|
_rebuildIndex() {
|
||
|
this._items.forEach((a, i) => {
|
||
|
this._mapIdIndex[a.id] = i;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
byId(id: AnalyticUnitId): AnalyticUnit {
|
||
|
return this._items[this._mapIdIndex[id]];
|
||
|
}
|
||
|
|
||
|
byIndex(index: number): AnalyticUnit {
|
||
|
return this._items[index];
|
||
|
}
|
||
|
}
|