Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: FileWatcher #1

Open
bugs181 opened this issue Mar 11, 2019 · 0 comments
Open

Proposal: FileWatcher #1

bugs181 opened this issue Mar 11, 2019 · 0 comments
Assignees
Labels
Blueprint Request enhancement New feature or request

Comments

@bugs181
Copy link
Collaborator

bugs181 commented Mar 11, 2019

Example Mockup of what this would consist of. Being able to pass other FileWatch services into this Blueprint would be ideal but not a requirement.

class FileWatcher {
	constructor ( file, data, callback, chokidarOptions, dispose ) {
		const handleWatchEvent = (event) => {
			if ( event === 'rename' || event === 'unlink' ) {
				this.fsWatcher.close();
				dispose();
				callback();
			} else {
				// this is necessary because we get duplicate events...
				const contents = fs.readFileSync( file, 'utf-8' );
				if ( contents !== data ) {
					data = contents;
					callback();
				}
			}
		};

		try {
			if (chokidarOptions) {
				this.fsWatcher = chokidar.watch(file, chokidarOptions).on('all', handleWatchEvent);
			} else {
				this.fsWatcher = fs.watch( file, opts, handleWatchEvent);
			}

			this.fileExists = true;
		} catch ( err ) {
			if ( err.code === 'ENOENT' ) {
				// can't watch files that don't exist (e.g. injected
				// by plugins somehow)
				this.fileExists = false;
			} else {
				throw err;
			}
		}
	}

	close () {
		this.fsWatcher.close();
	}
}
@bugs181 bugs181 added the enhancement New feature or request label Mar 11, 2019
@bugs181 bugs181 self-assigned this Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blueprint Request enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant