clean up watching
This commit is contained in:
parent
925366e79e
commit
15a4600c48
2 changed files with 29 additions and 5 deletions
|
@ -56,6 +56,11 @@ export async function renderView(
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function provideViews(v: typeof views, s: typeof scripts) {
|
||||||
|
views = v;
|
||||||
|
scripts = s;
|
||||||
|
}
|
||||||
|
|
||||||
export function joinScripts(scriptSources: string[]) {
|
export function joinScripts(scriptSources: string[]) {
|
||||||
const { length } = scriptSources;
|
const { length } = scriptSources;
|
||||||
if (length === 0) return "";
|
if (length === 0) return "";
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
const debounceMilliseconds = 25;
|
const debounceMilliseconds = 25;
|
||||||
|
|
||||||
export async function main() {
|
export async function main() {
|
||||||
// Catch up state
|
// Catch up state by running a main build.
|
||||||
const { incr } = await generate.main();
|
const { incr } = await generate.main();
|
||||||
|
// ...and watch the files that cause invals.
|
||||||
// Initialize a watch
|
|
||||||
const watch = new Watch(rebuild);
|
const watch = new Watch(rebuild);
|
||||||
|
watch.add(...incr.invals.keys());
|
||||||
statusLine();
|
statusLine();
|
||||||
|
|
||||||
function rebuild(files: string[]) {
|
function rebuild(files: string[]) {
|
||||||
|
@ -21,7 +21,7 @@ export async function main() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
withSpinner<Record<string, unknown>, any>({
|
withSpinner<Record<string, unknown>, any>({
|
||||||
text: "Recovering State",
|
text: "Rebuilding",
|
||||||
successText: generate.successText,
|
successText: generate.successText,
|
||||||
failureText: () => "sitegen FAIL",
|
failureText: () => "sitegen FAIL",
|
||||||
}, async (spinner) => {
|
}, async (spinner) => {
|
||||||
|
@ -34,6 +34,10 @@ export async function main() {
|
||||||
);
|
);
|
||||||
const result = await generate.sitegen(spinner, incr);
|
const result = await generate.sitegen(spinner, incr);
|
||||||
incr.toDisk(); // Allows picking up this state again
|
incr.toDisk(); // Allows picking up this state again
|
||||||
|
for (const file of watch.files) {
|
||||||
|
const relative = path.relative(hot.projectRoot, file);
|
||||||
|
if (!incr.invals.has(file)) watch.remove(file);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error(util.inspect(err));
|
console.error(util.inspect(err));
|
||||||
|
@ -41,7 +45,6 @@ export async function main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function statusLine() {
|
function statusLine() {
|
||||||
watch.add(...incr.invals.keys());
|
|
||||||
console.info(
|
console.info(
|
||||||
`Watching ${incr.invals.size} files \x1b[36m[last change: ${
|
`Watching ${incr.invals.size} files \x1b[36m[last change: ${
|
||||||
new Date().toLocaleTimeString()
|
new Date().toLocaleTimeString()
|
||||||
|
@ -104,6 +107,22 @@ class Watch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remove(...files: string[]) {
|
||||||
|
for (const file of files) this.files.delete(path.resolve(file));
|
||||||
|
// Find watches that are covering no files
|
||||||
|
const { roots, watchers } = this;
|
||||||
|
const existingFiles = Array.from(this.files);
|
||||||
|
let i = roots.length;
|
||||||
|
while (i > 0) {
|
||||||
|
i -= 1;
|
||||||
|
const root = roots[i];
|
||||||
|
if (!existingFiles.some((file) => file.startsWith(root))) {
|
||||||
|
watchers.splice(i, 1)[0].close();
|
||||||
|
roots.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
for (const w of this.watchers) w.close();
|
for (const w of this.watchers) w.close();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue