File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,31 @@ function includeIf(predicate, elem) {
22
22
return predicate ? [ elem ] : [ ] ;
23
23
}
24
24
25
+ function asyncTimeout ( millis ) {
26
+ return new Promise ( ( resolve ) => {
27
+ setTimeout ( ( ) => resolve ( ) , millis ) ;
28
+ } ) ;
29
+ }
30
+
31
+ function debounce ( func , millis , initial = false ) {
32
+ let count = 0 ;
33
+ const loop = async ( ...args ) => {
34
+ if ( ! count && initial ) {
35
+ func ( ...args ) ;
36
+ }
37
+ count ++ ;
38
+ const id = count ;
39
+ await asyncTimeout ( millis ) ;
40
+ if ( id === count ) {
41
+ count = 0 ;
42
+ if ( id > 1 || ! initial ) {
43
+ func ( ...args ) ;
44
+ }
45
+ }
46
+ } ;
47
+ return loop ;
48
+ }
49
+
25
50
async function execPrint ( command ) {
26
51
try {
27
52
const { stdout, stderr } = await execAsync ( command ) ;
@@ -75,20 +100,22 @@ function processArgs() {
75
100
}
76
101
77
102
function startWatch ( args ) {
103
+ const debouncedCompile = debounce ( compile , 500 ) ;
78
104
const watcher = watch (
79
105
[
80
106
'mangareader/**/*.py' ,
81
107
'mangareader/**/*.ts' ,
82
108
'mangareader/**/*.scss' ,
83
109
'mangareader/**/*.html' ,
110
+ 'reader.py' ,
84
111
] ,
85
112
{
86
113
persistent : true ,
87
114
} ,
88
115
) ;
89
116
watcher . on ( 'change' , async ( path ) => {
90
117
console . log ( 'File changed:' , path ) ;
91
- await compile ( args ) ;
118
+ await debouncedCompile ( args ) ;
92
119
} ) ;
93
120
}
94
121
You can’t perform that action at this time.
0 commit comments