Skip to content

Commit

Permalink
fix(fsevents): rework fsevent gathering to better support linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed May 24, 2020
1 parent 5e44ffa commit 003058d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
51 changes: 37 additions & 14 deletions src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,7 @@ export class Daemon implements AsyncIterable<DenonEvent> {
this.#config = denon.config; // just as a shortcut
}

/**
* Restart current process.
*/
private async reload() {
if (this.#config.logger.fullscreen) {
log.debug("clearing screen");
console.clear();
}

log.info(`watching path(s): ${this.#config.watcher.match.join(" ")}`);
log.info(`watching extensions: ${this.#config.watcher.exts.join(",")}`);
log.info("restarting due to changes...");

private killAll() {
// kill all processes spawned
let pcopy = Object.assign({}, this.#processes);
this.#processes = {};
Expand All @@ -49,6 +37,22 @@ export class Daemon implements AsyncIterable<DenonEvent> {
Deno.kill(p.pid, Deno.Signal.SIGKILL);
}
}
}

/**
* Restart current process.
*/
private async reload() {
if (this.#config.logger.fullscreen) {
log.debug("clearing screen");
console.clear();
}

log.info(`watching path(s): ${this.#config.watcher.match.join(" ")}`);
log.info(`watching extensions: ${this.#config.watcher.exts.join(",")}`);
log.info("restarting due to changes...");

this.killAll();

await this.start();
}
Expand Down Expand Up @@ -87,13 +91,32 @@ export class Daemon implements AsyncIterable<DenonEvent> {
}
}

private async onExit() {
if (Deno.build.os !== "windows") {
const signs = [
Deno.Signal.SIGHUP,
Deno.Signal.SIGINT,
Deno.Signal.SIGTERM,
Deno.Signal.SIGTSTP,
];
signs.forEach((s) => {
(async () => {
await Deno.signal(s);
this.killAll();
Deno.exit(0);
})();
});
}
}

async *iterate(): AsyncIterator<DenonEvent> {
yield {
type: "start",
};
this.start();
this.onExit();
for await (const watchE of this.#denon.watcher) {
if (watchE.some((_) => _.type === "modify" || _.type === "access")) {
if (watchE.some((_) => _.type.includes("modify"))) {
log.debug(`reload event detected, starting the reload procedure...`);
yield {
type: "reload",
Expand Down
14 changes: 7 additions & 7 deletions src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface FileEvent {
/** The path of the changed file */
path: string;
/** The type of change that occurred */
type: FileAction;
type: FileAction[];
}

/** All of the options for the `watch` generator */
Expand Down Expand Up @@ -55,7 +55,7 @@ export interface WatcherConfig {
*/
export class Watcher implements AsyncIterable<FileEvent[]> {
#signal = deferred();
#changes: { [key: string]: FileAction } = {};
#changes: { [key: string]: FileAction[] } = {};
#exts?: string[] = undefined;
#match?: RegExp[] = undefined;
#skip?: RegExp[] = undefined;
Expand Down Expand Up @@ -89,7 +89,6 @@ export class Watcher implements AsyncIterable<FileEvent[]> {

isWatched(path: string): boolean {
path = this.verifyPath(path);
log.debug(`evaluating path ${path}`);
if (
extname(path) && this.#exts?.length &&
this.#exts?.every((ext) => !path.endsWith(ext))
Expand Down Expand Up @@ -156,7 +155,8 @@ export class Watcher implements AsyncIterable<FileEvent[]> {
const { kind, paths } = event;
for (const path of paths) {
if (this.isWatched(path)) {
this.#changes[path] = kind;
if (!this.#changes[path]) this.#changes[path] = [];
this.#changes[path].push(kind);
debounce();
}
}
Expand Down Expand Up @@ -206,19 +206,19 @@ export class Watcher implements AsyncIterable<FileEvent[]> {
const pre = previous[path];
const post = current[path];
if (pre && !post) {
this.#changes[path] = "remove";
this.#changes[path].push("remove");
} else if (
pre &&
post &&
pre.getTime() !== post.getTime()
) {
this.#changes[path] = "modify";
this.#changes[path].push("modify");
}
}

for (const path in current) {
if (!previous[path] && current[path]) {
this.#changes[path] = "create";
this.#changes[path].push("create");
}
}

Expand Down

0 comments on commit 003058d

Please sign in to comment.