Skip to content

Commit

Permalink
define Detector class to enable incremental detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjor committed Jan 11, 2019
1 parent 001cf3a commit 65dfadc
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ export class Changes {
}

/** Options */
export interface Options {
export interface DetectorOptions {
/** If true, watcher checks the symlinked files/directories too. */
followSymlink?: boolean;
/** Ignores something like .gitignore, .vscode, etc. */
ignoreDotFiles?: boolean;
/** Path to search in regex (ex. "\.(ts|css)$") */
test?: RegExp | string;
/** Path to ignore in regex. */
ignore?: RegExp | string;
}
export interface Options extends DetectorOptions {
/** The minimum interval[ms] of checking loop.
* The next checking can be delayed until user program ends.
*
Expand All @@ -53,14 +63,6 @@ export interface Options {
* |<--- user program --->|
*/
interval?: number;
/** If true, watcher checks the symlinked files/directories too. */
followSymlink?: boolean;
/** Ignores something like .gitignore, .vscode, etc. */
ignoreDotFiles?: boolean;
/** Path to search in regex (ex. "\.(ts|css)$") */
test?: RegExp | string;
/** Path to ignore in regex. */
ignore?: RegExp | string;
}

/** The watcher */
Expand Down Expand Up @@ -134,37 +136,59 @@ async function* run(
timeout: null
}
) {
const { interval, followSymlink } = options;
const filter = makeFilter(options);
let lastStartTime = Date.now();
let files = {};
collect(files, targets, followSymlink, filter);

const detector = new Detector(targets, options);
const { startTime } = detector.init();
let lastStartTime = startTime;
while (!state.abort) {
let waitTime = Math.max(0, interval - (Date.now() - lastStartTime));
let waitTime = Math.max(0, options.interval - (Date.now() - lastStartTime));
await new Promise(resolve => {
state.timeout = setTimeout(resolve, waitTime);
});
state.timeout = null;
lastStartTime = Date.now();
let changes = new Changes();
changes.startTime = lastStartTime;
const changes = await detector.detectChanges();
lastStartTime = changes.startTime;
if (changes.length) {
yield changes;
}
}
}

/** This object detects changes for one step */
export class Detector {
public files = {};
constructor(public targets: string[], public options: DetectorOptions) {}
/** Call this function first to collect initial files.
* Otherwise, all files existing at first will be marked as "ADDED" next time.
*/
init(): { startTime: number; endTime: number; fileCount: number } {
const filter = makeFilter(this.options);
const changes = new Changes();
changes.startTime = Date.now();
collect(this.files, this.targets, this.options.followSymlink, filter);
changes.fileCount = Object.keys(this.files).length;
changes.endTime = Date.now();
return changes;
}
/** Traverse all files and detect changes. */
async detectChanges(): Promise<Changes> {
const changes = new Changes();
changes.startTime = Date.now();
const newFiles = {};
await detectChanges(
files,
const filter = makeFilter(this.options);
await walk(
this.files,
newFiles,
targets,
followSymlink,
this.targets,
this.options.followSymlink,
filter,
changes
);
files = newFiles;
Array.prototype.push.apply(changes.deleted, Object.keys(this.files));
this.files = newFiles;
changes.fileCount = Object.keys(newFiles).length;
changes.endTime = Date.now();

if (changes.length) {
yield changes;
}
return changes;
}
}

Expand All @@ -191,18 +215,6 @@ function makeFilter({ test, ignore, ignoreDotFiles }: Options) {
};
}

async function detectChanges(
prev: any,
curr: any,
targets: string[],
followSymlink: boolean,
filter: (f: FileInfo, path: string) => boolean,
changes: Changes
): Promise<void> {
await walk(prev, curr, targets, followSymlink, filter, changes);
Array.prototype.push.apply(changes.deleted, Object.keys(prev));
}

async function walk(
prev: any,
curr: any,
Expand Down

0 comments on commit 65dfadc

Please sign in to comment.