update dependencies
This commit is contained in:
142
ts/index.ts
142
ts/index.ts
@ -8,84 +8,84 @@ export type TFsEvent = 'add' | 'addDir' | 'change' | 'error' | 'unlink' | 'unlin
|
||||
* Smartchok allows easy wathcing of files
|
||||
*/
|
||||
export class Smartchok {
|
||||
watchStringmap = new Stringmap()
|
||||
chokidarOptions: plugins.chokidar.WatchOptions
|
||||
status: TSmartchokStatus = 'idle'
|
||||
private watcher
|
||||
private watchingDeferred = plugins.q.defer<void>() // used to run things when watcher is initialized
|
||||
private eventObservablemap = new plugins.lik.Observablemap() // register one observable per event
|
||||
watchStringmap = new Stringmap()
|
||||
chokidarOptions: plugins.chokidar.WatchOptions
|
||||
status: TSmartchokStatus = 'idle'
|
||||
private watcher
|
||||
private watchingDeferred = plugins.q.defer<void>() // used to run things when watcher is initialized
|
||||
private eventObservablemap = new plugins.lik.Observablemap() // register one observable per event
|
||||
|
||||
/**
|
||||
* constructor of class smartchok
|
||||
*/
|
||||
constructor(watchArrayArg: string[], optionsArg: plugins.chokidar.WatchOptions = {}) {
|
||||
this.watchStringmap.addStringArray(watchArrayArg)
|
||||
this.chokidarOptions = optionsArg
|
||||
/**
|
||||
* constructor of class smartchok
|
||||
*/
|
||||
constructor(watchArrayArg: string[], optionsArg: plugins.chokidar.WatchOptions = {}) {
|
||||
this.watchStringmap.addStringArray(watchArrayArg)
|
||||
this.chokidarOptions = optionsArg
|
||||
}
|
||||
|
||||
/**
|
||||
* adds files to the list of watched files
|
||||
*/
|
||||
add(pathArrayArg: string[]) {
|
||||
this.watchStringmap.addStringArray(pathArrayArg)
|
||||
if (this.status !== 'idle') {
|
||||
this.watchingDeferred.promise.then(() => {
|
||||
this.watcher.add(pathArrayArg)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* adds files to the list of watched files
|
||||
*/
|
||||
add(pathArrayArg: string[]) {
|
||||
this.watchStringmap.addStringArray(pathArrayArg)
|
||||
if (this.status !== 'idle') {
|
||||
this.watchingDeferred.promise.then(() => {
|
||||
this.watcher.add(pathArrayArg)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* removes files from the list of watched files
|
||||
*/
|
||||
remove(pathArg: string) {
|
||||
this.watchStringmap.removeString('') // TODO
|
||||
this.watchingDeferred.promise.then(() => {
|
||||
this.watcher.unwatch(pathArg)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* removes files from the list of watched files
|
||||
*/
|
||||
remove(pathArg: string) {
|
||||
this.watchStringmap.removeString('') // TODO
|
||||
this.watchingDeferred.promise.then(() => {
|
||||
this.watcher.unwatch(pathArg)
|
||||
})
|
||||
}
|
||||
/**
|
||||
* gets an observable for a certain event
|
||||
*/
|
||||
getObservableFor(fsEvent: TFsEvent): plugins.q.Promise<plugins.rx.Observable<any>> {
|
||||
let done = plugins.q.defer<plugins.rx.Observable<any>>()
|
||||
this.watchingDeferred.promise.then(() => {
|
||||
let eventObservable = this.eventObservablemap.getObservableForEmitterEvent(this.watcher, fsEvent)
|
||||
done.resolve(eventObservable)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* gets an observable for a certain event
|
||||
*/
|
||||
getObservableFor(fsEvent: TFsEvent): plugins.q.Promise<plugins.rx.Observable<any>> {
|
||||
let done = plugins.q.defer<plugins.rx.Observable<any>>()
|
||||
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(): plugins.q.Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* starts the watcher
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
start(): plugins.q.Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
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()
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() })
|
||||
}
|
||||
if (this.status === 'watching') {
|
||||
console.log('closing while watching')
|
||||
closeWatcher()
|
||||
} else if (this.status === 'starting') {
|
||||
this.watchingDeferred.promise.then(() => { closeWatcher() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user