-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite the read tracking functionality; completely different api and…
… much cleaner simpler code and explanation of how it works
- Loading branch information
1 parent
1a0c14a
commit f2a884c
Showing
4 changed files
with
65 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Each time a file is explicitly read from that we haven't | ||
seen before, we write its name to readTrackingFile | ||
followed by a null byte. | ||
Everything is reset when readTrackingFile gets deleted from | ||
disk by some external process (do that to indicate need for | ||
a reset). | ||
*/ | ||
|
||
import { appendFile, writeFile, stat } from "fs/promises"; | ||
import debug from "debug"; | ||
const log = debug("websocketfs:read-tracking"); | ||
|
||
export default class ReadTracking { | ||
private readTrackingFile: string; | ||
private history = new Set<string>(); | ||
|
||
constructor(readTrackingFile) { | ||
this.readTrackingFile = readTrackingFile; | ||
this.init(); | ||
} | ||
|
||
private init = async () => { | ||
try { | ||
await writeFile(this.readTrackingFile, ""); | ||
} catch (err) { | ||
log("error clearing read tracking file", this.readTrackingFile, err); | ||
} | ||
}; | ||
|
||
trackRead = async (filename: string) => { | ||
log(`fileWasRead`, { filename }); | ||
try { | ||
await stat(this.readTrackingFile); | ||
} catch (_) { | ||
// file doesn't exist, so reset history | ||
this.history.clear(); | ||
} | ||
if (this.history.has(filename)) { | ||
return; | ||
} | ||
await appendFile(this.readTrackingFile, `${filename.slice(1)}\0`); | ||
this.history.add(filename); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters