added start / stop events; starting / stopping folder watcher.

This commit is contained in:
Alberto Miranda
2017-03-22 23:21:07 -03:00
parent a6c53d06d2
commit 7f817241ed
5 changed files with 53 additions and 22 deletions

View File

@@ -6,9 +6,19 @@ class FolderWatcher {
this.window = window
this.state = state
this.torrentsFolderPath
this.watching = false
}
init () {
isEnabled () {
return this.state.saved.prefs.autoAddTorrents
}
start () {
log('--- FOLDER WATCHER --> START')
// Stop watching previous folder before
// start watching a new one.
if (this.watching) this.stop()
const torrentsFolderPath = this.state.saved.prefs.torrentsFolderPath
this.torrentsFolderPath = torrentsFolderPath
if (!torrentsFolderPath) return
@@ -23,30 +33,15 @@ class FolderWatcher {
log('-- torrent added: ', path)
this.window.dispatch('addTorrent', path)
})
}
start (torrentsFolderPath) {
// Stop watching previous folder before
// start watching a new one.
if (this.torrentsFolderPath) {
this.stop()
}
const glob = `${torrentsFolderPath}/**/*.torrent`
log('Folder Watcher: watching: ', glob)
const options = {ignoreInitial: true}
this.watcher = chokidar.watch(glob, options)
this.watcher
.on('add', (path) => {
log('-- torrent added: ', path)
this.window.dispatch('addTorrent', path)
})
this.watching = true
}
stop () {
if (!this.watcher) return
log('--- FOLDER WATCHER --> STOP')
if (!this.watching) return
this.watcher.close()
this.watching = false
}
}

View File

@@ -136,7 +136,11 @@ function delayedInit (state) {
announcement.init()
dock.init()
updater.init()
folderWatcher.init()
ipc.setModule('folderWatcher', folderWatcher)
if (folderWatcher.isEnabled()) {
folderWatcher.start()
}
if (process.platform === 'win32') {
const userTasks = require('./user-tasks')

View File

@@ -1,5 +1,6 @@
module.exports = {
init
init,
setModule
}
const electron = require('electron')
@@ -13,6 +14,14 @@ const windows = require('./windows')
// Messages from the main process, to be sent once the WebTorrent process starts
const messageQueueMainToWebTorrent = []
// Will hold modules injected from the app that will be used on fired
// IPC events.
const modules = {}
function setModule (name, module) {
modules[name] = module
}
function init () {
const ipc = electron.ipcMain
@@ -106,6 +115,16 @@ function init () {
thumbar.onPlayerPause()
})
ipc.on('startFolderWatcher', function () {
log('--- Torrent Watcher started')
modules['folderWatcher'].start()
})
ipc.on('stopFolderWatcher', function () {
log('--- Torrent Watcher stop')
modules['folderWatcher'].stop()
})
/**
* Shell
*/