This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-iterator.ts
66 lines (60 loc) · 2.48 KB
/
push-iterator.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { PushIterable } from './push-iterable';
/**
* Iterator implementing push iteration protocol.
*
* @typeParam T - Iterated elements type.
*/
export interface PushIterator<T> extends IterableIterator<T>, PushIterable<T> {
[Symbol.iterator](): PushIterator<T>;
/**
* Checks whether iteration is over.
*
* @returns `true` is there is nothing to iterate any more, or `false` if iteration is still possible.
*/
isOver(): boolean;
}
export namespace PushIterator {
/**
* A signature of iterated elements pusher function conforming to push iteration protocol.
*
* @typeParam T - Iterated elements type.
* @param accept - A function to push iterated elements to. Accepts iterated element as its only parameter. May return
* `false` to stop iteration.
*
* @returns `true` if further iteration is possible, or `false` if there is no more elements left to iterate.
* The latter is possible only when iteration aborted, i.e. `accept` returned `false`.
*/
export type Pusher<T> = (this: void, accept: Acceptor<T>) => boolean;
/**
* A signature of a function accepting iterated elements.
*
* It is able to suspend iteration by returning `true`, or to stop it by returning `false`.
*
* @typeParam T - Iterated elements type.
*/
export type Acceptor<T> = EachAcceptor<T> | StoppingAcceptor<T>;
/**
* A signature of a function accepting each iterated element unconditionally.
*
* @typeParam T - Iterated elements type.
* @param element - Iterated element.
*/
export type EachAcceptor<T> = (this: void, element: T) => void;
/**
* A signature of a function accepting iterated elements and able to suspend or stop further iteration.
*
* When this function returns `true`, the iteration is suspended. I.e. the no more elements would be pushed to this
* function, but the iteration method (`[PushIterator__symbol]`) would return an iterator that can be used to resume
* iteration.
*
* When this function returns `false`, the iteration is stopped. I.e. the no more elements would be pushed to this
* function, and the iteration method (`[PushIterator__symbol]`) would return an empty iterator. I.e. the one with
* its {@link PushIterator.isOver} method always returning `true`.
*
* @typeParam T - Iterated elements type.
* @param element - Iterated element.
*
* @returns `true` to suspend iteration, or `false` to stop it.
*/
export type StoppingAcceptor<T> = (this: void, element: T) => boolean;
}