Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected onUpdateEvent(index: number, newValue: Value, key: Value): void {
newValue = this.listWillUpdate(index, newValue);
const oldValue = this._state.get(index) || Value.absent();
this._state.set(index, newValue);
this.listDidUpdate(index, newValue, oldValue);
}
insert(index: number, newValue: Value, key?: Value): this {
if (index < 0 || index > this._state.length) {
throw new RangeError("" + index);
}
newValue = this.listWillUpdate(index, newValue);
this._state.insert(index, newValue, key);
const newEntry = this._state.getEntry(index)!;
this.listDidUpdate(index, newValue, Value.absent());
const header = Record.create(2).slot("key", newEntry[0]).slot("index", index);
this.command(Attr.of("update", header).concat(newValue));
return this;
}
protected onMoveEvent(fromIndex: number, toIndex: number, key: Value): void {
toIndex = Math.min(Math.max(0, toIndex), this._state.length);
if (fromIndex !== toIndex) {
const value = this._state.get(fromIndex) || Value.absent();
this.listWillMove(fromIndex, toIndex, value);
this._state.remove(fromIndex).insert(toIndex, value, key);
this.listDidMove(fromIndex, toIndex, value);
}
}
protected onInsertEvent(index: number, newValue: Value, key: Value): void {
newValue = this.listWillUpdate(index, newValue);
this._state.insert(index, newValue, key);
this.listDidUpdate(index, newValue, Value.absent());
}
absent(): Value {
return Value.absent();
}
get(index: number, key?: Value): Value {
return this._state.get(index, key) || Value.absent();
}
unshift(...newValues: Value[]): number {
for (let i = newValues.length - 1; i >= 0; i -= 1) {
const newValue = this.listWillUpdate(0, newValues[i]);
this._state.insert(0, newValue);
const newEntry = this._state.getEntry(0)!;
this.listDidUpdate(0, newValue, Value.absent());
const header = Record.create(2).slot("key", newEntry[0]).slot("index", 0);
this.command(Attr.of("update", header).concat(newValue));
}
return this._state.length;
}
protected onRemoveEvent(index: number, key: Value): void {
this.listWillRemove(index);
const oldValue = this._state.get(index) || Value.absent();
this._state.remove(index);
this.listDidRemove(index, oldValue);
}
pop(): Value {
const index = this._state.length - 1;
if (index >= 0) {
this.listWillRemove(index);
const oldEntry = this._state.getEntry(index)!;
this._state.remove(index);
this.listDidRemove(index, oldEntry[1]);
const header = Record.create(2).slot("key", oldEntry[0]).slot("index", index);
this.command(Record.create(1).attr("remove", header));
return oldEntry[1];
} else {
return Value.absent();
}
}