-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncStreamWinston.js
58 lines (47 loc) · 1.17 KB
/
asyncStreamWinston.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
import { Writable } from "stream";
export class AsyncStreamWinston extends Writable {
constructor(opts) {
super({
objectMode: true,
...opts,
});
this.callsInProgress = new Map();
this.index = 0;
this.name = "async-transport";
this.on("drain", () => {
console.log("drain", this.index);
});
}
async _final(callback) {
console.log("final", this.callsInProgress.size);
await Promise.all(this.callsInProgress.values());
callback();
}
_write({ message }, _, callback) {
const i = this.index++;
const promise = new Promise((resolve) => {
setTimeout(() => {
// console.log("logg:: result", message);
resolve();
}, 1000);
});
this.callsInProgress.set(
i,
promise.finally(() => {
console.count("logged");
this.callsInProgress.delete(i);
})
);
// It works but the last chunk is missing
setImmediate(() => {
callback();
});
// callback();
}
log(data, callback = () => {}) {
this.write(data, encoding, callback);
}
// log(chunk, callback = () => {}) {
// this.write(chunk, null, callback);
// }
}