17
17
import chokidar from "chokidar"
18
18
import { basename } from "path"
19
19
20
- import { readdir } from "fs/promises"
21
20
import { Profiles } from "madwizard"
22
21
23
22
import UpdateFunction from "../../update"
@@ -31,7 +30,7 @@ export const RUNS_ERROR = "No runs found"
31
30
*/
32
31
export default class ProfileRunWatcher {
33
32
/** Our model */
34
- private _runs : string [ ] = [ ]
33
+ private _runs : { runId : string ; timestamp : number } [ ] = [ ]
35
34
36
35
/** Have we already performed the on-time init? */
37
36
private _initDone = false
@@ -57,7 +56,6 @@ export default class ProfileRunWatcher {
57
56
/** Initialize `this._runs` model */
58
57
public async init ( ) : Promise < ProfileRunWatcher > {
59
58
if ( ! this . _initDone ) {
60
- // await this.readOnce() no need, since chokidar gives us an initial read
61
59
this . initWatcher ( )
62
60
this . _initDone = true
63
61
}
@@ -66,18 +64,18 @@ export default class ProfileRunWatcher {
66
64
67
65
/** Initialize the filesystem watcher to notify us of new or removed profiles */
68
66
private initWatcher ( ) {
69
- this . watcher . on ( "addDir" , async ( path ) => {
67
+ this . watcher . on ( "addDir" , async ( path , stats ) => {
70
68
const runId = basename ( path )
71
- const idx = this . runs . findIndex ( ( _ ) => _ === runId )
69
+ const idx = this . runs . findIndex ( ( _ ) => _ . runId === runId )
72
70
if ( idx < 0 ) {
73
- this . _runs . push ( runId )
71
+ this . _runs . push ( { runId, timestamp : stats ?. mtimeMs || stats ?. ctimeMs || 0 } )
74
72
this . updateFn ( )
75
73
}
76
74
} )
77
75
78
76
this . watcher . on ( "unlink" , ( path ) => {
79
77
const runId = basename ( path )
80
- const idx = this . runs . findIndex ( ( _ ) => _ === runId )
78
+ const idx = this . runs . findIndex ( ( _ ) => _ . runId === runId )
81
79
if ( idx >= 0 ) {
82
80
this . _runs . splice ( idx , 1 )
83
81
this . updateFn ( )
@@ -91,19 +89,7 @@ export default class ProfileRunWatcher {
91
89
}
92
90
93
91
/** Overwrite the run model state */
94
- private set runs ( runs : string [ ] ) {
92
+ private set runs ( runs : { runId : string ; timestamp : number } [ ] ) {
95
93
this . _runs = runs
96
94
}
97
-
98
- /** @return files of the directory of job runs for a given profile */
99
- private async readOnce ( ) {
100
- try {
101
- // TODO do a "full" read with Dirents, so that we have filesystem
102
- // timestamps, and sort, so that the `.slice(0, 10)` below pulls
103
- // out the most recent runs
104
- this . runs = await readdir ( ProfileRunWatcher . path ( this . profile ) )
105
- } catch ( err ) {
106
- this . runs = [ RUNS_ERROR ]
107
- }
108
- }
109
95
}
0 commit comments