-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathevents-reader.ts
More file actions
55 lines (48 loc) · 1.3 KB
/
Copy pathevents-reader.ts
File metadata and controls
55 lines (48 loc) · 1.3 KB
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
import type { Event, SessionMeta } from '@rolldown/debug'
import fs from 'node:fs'
import { parseJsonStreamWithConcatArrays } from '../utils/json-parse-stream'
import { RolldownEventsManager } from './events-manager'
const readers: Map<string, RolldownEventsReader> = new Map()
export class RolldownEventsReader {
lastBytes: number = 0
lastTimestamp: number = 0
manager = new RolldownEventsManager()
meta: SessionMeta | undefined
private constructor(
readonly filepath: string,
) {
}
static get(filepath: string) {
if (readers.has(filepath)) {
return readers.get(filepath)!
}
const reader = new RolldownEventsReader(filepath)
readers.set(filepath, reader)
return reader
}
async read() {
const { mtime, size } = await fs.promises.stat(this.filepath)
if (mtime.getTime() <= this.lastTimestamp) {
return
}
const stream = fs.createReadStream(this.filepath, {
start: this.lastBytes,
})
this.lastTimestamp = mtime.getTime()
this.lastBytes = size
await parseJsonStreamWithConcatArrays<Event>(
stream,
(event) => {
this.manager.handleEvent(event)
return event
},
)
}
dispose() {
readers.delete(this.filepath)
this.manager.dispose()
}
[Symbol.dispose]() {
this.dispose()
}
}