Skip to content

Commit

Permalink
Small watch mode event refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
osyrisrblx committed Sep 25, 2020
1 parent 0255914 commit ec89e49
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/Project/functions/setupProjectWatchProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,29 @@ export function setupProjectWatchProgram(data: ProjectData, usePolling: boolean)
reportEmitResult(emitResult);
}

function collectEvent(type: "add" | "change" | "remove", fsPath: string) {
if (type === "add") {
filesToAdd.add(fsPath);
} else if (type === "change") {
filesToChange.add(fsPath);
} else {
filesToDelete.add(fsPath);
}
function openEventCollection() {
if (!collecting) {
collecting = true;
reportText("File change detected. Starting incremental compilation...");
setTimeout(closeEventCollection, 100);
}
}

function collectAddEvent(fsPath: string) {
filesToAdd.add(fsPath);
openEventCollection();
}

function collectChangeEvent(fsPath: string) {
filesToChange.add(fsPath);
openEventCollection();
}

function collectDeleteEvent(fsPath: string) {
filesToDelete.add(fsPath);
openEventCollection();
}

chokidar
.watch(getRootDirs(options), {
awaitWriteFinish: {
Expand All @@ -174,11 +182,11 @@ export function setupProjectWatchProgram(data: ProjectData, usePolling: boolean)
disableGlobbing: true,
usePolling,
})
.on("add", fsPath => collectEvent("add", fsPath))
.on("addDir", fsPath => collectEvent("add", fsPath))
.on("change", fsPath => collectEvent("change", fsPath))
.on("unlink", fsPath => collectEvent("remove", fsPath))
.on("unlinkDir", fsPath => collectEvent("remove", fsPath));
.on("add", collectAddEvent)
.on("addDir", collectAddEvent)
.on("change", collectChangeEvent)
.on("unlink", collectDeleteEvent)
.on("unlinkDir", collectDeleteEvent);

reportText("Starting compilation in watch mode...");
reportEmitResult(runInitialCompile());
Expand Down

0 comments on commit ec89e49

Please sign in to comment.