smartchok/dist/index.js

76 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-09-22 14:33:44 +00:00
"use strict";
const plugins = require("./smartchok.plugins");
const lik_1 = require("lik");
class Smartchok {
constructor(watchArrayArg, optionsArg = {}) {
this.watchStringmap = new lik_1.Stringmap();
2016-09-22 15:14:36 +00:00
this.status = 'idle';
2016-09-22 14:33:44 +00:00
this.watchingDeferred = plugins.q.defer(); // used to run things when watcher is initialized
this.eventObservablemap = new plugins.lik.Observablemap(); // register one observable per event
this.watchStringmap.addStringArray(watchArrayArg);
this.chokidarOptions = optionsArg;
}
/**
* adds files to the list of watched files
*/
add(pathArrayArg) {
this.watchStringmap.addStringArray(pathArrayArg);
2016-09-22 15:14:36 +00:00
if (this.status !== 'idle') {
this.watchingDeferred.promise.then(() => {
this.watcher.add(pathArrayArg);
});
}
2016-09-22 14:33:44 +00:00
}
/**
* removes files from the list of watched files
*/
remove(pathArg) {
this.watchStringmap.removeString(''); // TODO
this.watchingDeferred.promise.then(() => {
this.watcher.unwatch(pathArg);
});
}
/**
* gets an observable for a certain event
*/
getObservableFor(fsEvent) {
let done = plugins.q.defer();
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() {
let done = plugins.q.defer();
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') {
this.watchingDeferred.promise.then(() => { closeWatcher(); });
}
}
}
exports.Smartchok = Smartchok;
2016-11-18 23:36:48 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjoz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