-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadAchievementsPage.ts
34 lines (30 loc) · 970 Bytes
/
readAchievementsPage.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
export function readAchievementsPage(
stream: NodeJS.ReadableStream,
onPage: (achievements: string[]) => void,
onDone: () => void,
onError: (error: Error) => void,
): void {
let achievementsBuffer: string[] = [];
let residualData = '';
stream.setEncoding('utf8');
stream.on('data', (chunk: string) => {
const achievements = (residualData + chunk).split(';');
residualData = achievements.pop() || '';
achievementsBuffer = achievementsBuffer.concat(achievements);
while (achievementsBuffer.length >= 10) {
onPage(achievementsBuffer.splice(0, 10));
}
});
stream.on('end', () => {
if (residualData.length > 0) {
achievementsBuffer.push(residualData);
}
if (achievementsBuffer.length > 0) {
onPage(achievementsBuffer);
}
onDone();
});
stream.on('error', (error: Error) => {
onError(error);
});
}