-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookaheadGenerator.js
99 lines (97 loc) · 3.39 KB
/
lookaheadGenerator.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// export default class LookaheadGenerator {
// constructor(baseGenerator) {
// this.baseGenerator = baseGenerator;
// const {value, done} = await baseGenerator.next();
// this.next = value;
// this.hasNext = !done;
// this.generator = function*() {
// for await (const data of this.baseGenerator) {
// yield this.next;
// this.next = data;
// }
// this.next = undefined;
// this.hasNext = false;
// }();
// }
// }
export default async function lookaheadGenerator(baseGenerator) {
// console.log(baseGenerator);
const baseIterator = ('next' in baseGenerator) ? baseGenerator : baseGenerator[Symbol.asyncIterator]();
// console.log(baseIterator);
var lookAhead;
lookAhead = (await baseIterator.next()).value;
return {
baseGenerator,
get lookAhead() {return lookAhead},
get hasNext() {return lookAhead !== undefined;},
[Symbol.asyncIterator]: async function*() {
// console.log('creating generator...');
while (true) {
const {value, done} = await baseIterator.next();
if (done) break;
// console.log('next record:')
// console.log(value);
// console.log('record:')
// console.log(lookAhead);
const result = lookAhead;
lookAhead = value;
yield result;
}
// for await (const data of baseIterator) {
// console.log('next record:')
// console.log(data);
// console.log('record:')
// console.log(lookAhead);
// const result = lookAhead;
// lookAhead = data;
// yield result;
// }
if (lookAhead !== undefined) {
const result = lookAhead;
lookAhead = undefined;
yield result;
}
}
// generator: async function*() {
// // console.log('creating generator...');
// for await (const data of baseIterator) {
// console.log('next record:')
// console.log(data);
// console.log('record:')
// console.log(next);
// const result = next;
// next = data;
// yield result;
// }
// next = undefined;
// }()
// async * generatorFunction() {
// for await (const data of baseGenerator) {
// yield next;
// next = data;
// }
// next = undefined;
// hasNext = false;
// }(),
// generator:
// generator: async function*() {
// for await (const data of this.baseGenerator) {
// yield this.next;
// this.next = data;
// }
// this.next = undefined;
// this.hasNext = false;
// }()
}
// this.baseGenerator = baseGenerator;
// this.next = value;
// this.hasNext = !done;
// this.generator = function*() {
// for await (const data of this.baseGenerator) {
// yield this.next;
// this.next = data;
// }
// this.next = undefined;
// this.hasNext = false;
// }();
}