fixes [Symbol.iterator] typing

This commit is contained in:
Isaiah Becker-Mayer 2022-08-25 22:03:46 -04:00
parent 1e6d5ca42f
commit d025c1111e
9 changed files with 31 additions and 43 deletions

View File

@ -45,8 +45,7 @@ export class BaseMappedList<F,T,R = T> extends BaseObservableList<T> {
return this._mappedValues!.length;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): IterableIterator<T> {
return this._mappedValues!.values();
}
}

View File

@ -86,13 +86,11 @@ export class ConcatList<T> extends BaseObservableList<T> implements IListObserve
return len;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): Iterator<T> {
let sourceListIdx = 0;
let it = this._sourceLists[0][Symbol.iterator]();
return {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
next: () => {
next: (): IteratorResult<T> => {
let result = it.next();
while (result.done) {
sourceListIdx += 1;

View File

@ -75,8 +75,7 @@ export class ObservableArray<T> extends BaseObservableList<T> {
return this._items.length;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): IterableIterator<T> {
return this._items.values();
}
}

View File

@ -112,40 +112,31 @@ export class SortedArray<T> extends BaseObservableList<T> {
return this._items.length;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): Iterator<T> {
return new Iterator(this);
}
}
// iterator that works even if the current value is removed while iterating
class Iterator<T> {
private _sortedArray: SortedArray<T> | null
private _current: T | null | undefined
private _sortedArray: SortedArray<T>;
private _current: T | null | undefined;
private _consumed: boolean = false;
constructor(sortedArray: SortedArray<T>) {
this._sortedArray = sortedArray;
this._current = null;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
next() {
if (this._sortedArray) {
if (this._current) {
this._current = this._sortedArray._getNext(this._current);
} else {
this._current = this._sortedArray.get(0);
}
if (this._current) {
return {value: this._current};
} else {
// cause done below
this._sortedArray = null;
}
next(): IteratorResult<T> {
if (this._consumed) {
return {value: undefined, done: true};
}
if (!this._sortedArray) {
return {done: true};
this._current = this._current? this._sortedArray._getNext(this._current): this._sortedArray.get(0);
if (!this._current) {
this._consumed = true;
}
return { value: this._current, done: this._consumed } as IteratorResult<T>;
}
}

View File

@ -79,7 +79,9 @@ export class ApplyMap<K, V> extends BaseObservableMap<K, V> {
onUnsubscribeLast(): void {
super.onUnsubscribeLast();
if (this._subscription) this._subscription = this._subscription();
if (this._subscription) {
this._subscription = this._subscription();
}
}
onReset(): void {
@ -89,8 +91,7 @@ export class ApplyMap<K, V> extends BaseObservableMap<K, V> {
this.emitReset();
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): Iterator<[K, V]> {
return this._source[Symbol.iterator]();
}

View File

@ -142,8 +142,7 @@ export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
this.emitReset();
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): FilterIterator<K, V> {
return new FilterIterator<K, V>(this._source, this._included);
}
@ -157,7 +156,7 @@ export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
return count;
}
get(key): V | undefined{
get(key: K): V | undefined {
const value = this._source.get(key);
if (value && this._filter(value, key)) {
return value;
@ -173,8 +172,7 @@ class FilterIterator<K, V> {
this._sourceIterator = map[Symbol.iterator]();
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
next() {
next(): IteratorResult<[K, V]> {
// eslint-disable-next-line no-constant-condition
while (true) {
const sourceResult = this._sourceIterator.next();

View File

@ -72,8 +72,7 @@ export class LogMap<K, V> extends BaseObservableMap<K, V> {
this.emitReset();
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): Iterator<[K, V]> {
return this._source[Symbol.iterator]();
}

View File

@ -64,7 +64,9 @@ export class MappedMap<K, V, MappedV> extends BaseObservableMap<K, MappedV> {
onRemove(key: K/*, _value*/): void {
const mappedValue = this._mappedValues.get(key);
if (this._mappedValues.delete(key)) {
if (mappedValue) this.emitRemove(key, mappedValue);
if (mappedValue) {
this.emitRemove(key, mappedValue);
}
}
}
@ -93,7 +95,9 @@ export class MappedMap<K, V, MappedV> extends BaseObservableMap<K, MappedV> {
onUnsubscribeLast(): void {
super.onUnsubscribeLast();
if (this._subscription) this._subscription = this._subscription();
if (this._subscription) {
this._subscription = this._subscription();
}
this._mappedValues.clear();
}
@ -102,8 +106,7 @@ export class MappedMap<K, V, MappedV> extends BaseObservableMap<K, MappedV> {
this.emitReset();
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
[Symbol.iterator]() {
[Symbol.iterator](): IterableIterator<[K, MappedV]> {
return this._mappedValues.entries();
}

View File

@ -97,7 +97,7 @@ export class ObservableMap<K, V> extends BaseObservableMap<K, V> {
keys(): Iterator<K> {
return this._values.keys();
}
};
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function tests() {