smartchok/dist/smartchok.classes.smartchok.js

77 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-06-30 16:05:55 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartchok.plugins");
const lik_1 = require("lik");
/**
* Smartchok allows easy wathcing of files
*/
class Smartchok {
/**
* constructor of class smartchok
*/
constructor(watchArrayArg, optionsArg = {}) {
this.watchStringmap = new lik_1.Stringmap();
this.status = 'idle';
2017-06-30 18:32:35 +00:00
this.watchingDeferred = plugins.smartq.defer(); // used to run things when watcher is initialized
2018-02-28 23:08:08 +00:00
this.eventObservablemap = new plugins.smartrx.Observablemap(); // register one observable per event
2017-06-30 16:05:55 +00:00
this.watchStringmap.addStringArray(watchArrayArg);
this.chokidarOptions = optionsArg;
}
/**
* adds files to the list of watched files
*/
add(pathArrayArg) {
this.watchStringmap.addStringArray(pathArrayArg);
}
/**
* removes files from the list of watched files
*/
remove(pathArg) {
this.watchStringmap.removeString(pathArg);
}
/**
* gets an observable for a certain event
*/
getObservableFor(fsEvent) {
2017-06-30 18:32:35 +00:00
let done = plugins.smartq.defer();
2017-06-30 16:05:55 +00:00
this.watchingDeferred.promise.then(() => {
let eventObservable = this.eventObservablemap.getObservableForEmitterEvent(this.watcher, fsEvent);
done.resolve(eventObservable);
});
return done.promise;
}
/**
* starts the watcher
* @returns Promise<void>
*/
start() {
2017-06-30 18:32:35 +00:00
let done = plugins.smartq.defer();
2017-06-30 16:05:55 +00:00
this.status = 'starting';
this.watcher = plugins.chokidar.watch(this.watchStringmap.getStringArray(), this.chokidarOptions);
this.watcher.on('ready', () => {
this.status = 'watching';
this.watchingDeferred.resolve();
done.resolve();
});
return done.promise;
}
/**
* stop the watcher process if watching
*/
stop() {
let closeWatcher = () => {
this.watcher.close();
};
if (this.status === 'watching') {
console.log('closing while watching');
closeWatcher();
}
else if (this.status === 'starting') {
2018-02-28 23:08:08 +00:00
this.watchingDeferred.promise.then(() => {
closeWatcher();
});
2017-06-30 16:05:55 +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