-
Notifications
You must be signed in to change notification settings - Fork 33
/
StackItemIterator.ts
48 lines (39 loc) · 1.68 KB
/
StackItemIterator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// <reference types="@reactivex/ix-es2015-cjs" />
import { AsyncIterableX } from '@reactivex/ix-es2015-cjs/asynciterable/asynciterablex';
import { concat } from '@reactivex/ix-es2015-cjs/asynciterable/concat';
import { map } from '@reactivex/ix-es2015-cjs/asynciterable/pipe/map';
import { InvalidStorageStackItemIteratorError } from './errors';
import { StackItemBase } from './StackItemBase';
import { StackItemEnumerator } from './StackItemEnumerator';
interface Value {
readonly key: StackItemBase;
readonly value: StackItemBase;
}
export class StackItemIterator extends StackItemEnumerator<Value> {
public key(): StackItemBase {
const { mutableCurrent } = this;
if (mutableCurrent === undefined) {
throw new InvalidStorageStackItemIteratorError();
}
return mutableCurrent.key;
}
public keys(): StackItemEnumerator {
const iterable = AsyncIterableX.from(this.enumerator as AsyncIterableIterator<Value>).pipe<{
value: StackItemBase;
}>(map(({ key }) => ({ value: key })));
return new StackItemEnumerator(iterable[Symbol.asyncIterator]());
}
public values(): StackItemEnumerator {
const iterable = AsyncIterableX.from(this.enumerator as AsyncIterableIterator<Value>).pipe<{
value: StackItemBase;
}>(map(({ value }) => ({ value })));
return new StackItemEnumerator(iterable[Symbol.asyncIterator]());
}
public concatIterator(other: StackItemIterator): StackItemIterator {
const iterable = concat(
AsyncIterableX.from(this.enumerator as AsyncIterableIterator<Value>),
AsyncIterableX.from(other.enumerator as AsyncIterableIterator<Value>),
);
return new StackItemIterator(iterable[Symbol.asyncIterator]());
}
}