main: monitor session changes and update clients that are using the same session file

This commit is contained in:
jean-emmanuel 2017-01-10 22:38:51 +01:00
parent 433aefccb6
commit b84d6777ab
3 changed files with 61 additions and 3 deletions

View file

@ -3,7 +3,11 @@ var vm = require('vm'),
fs = require('fs'),
settings = require('./settings'),
osc = require('./osc'),
ipc = require('./server').ipc
ipc = require('./server').ipc,
chokidar = require('chokidar')
var openedSessions = {},
lastSavingClient
module.exports = {
@ -69,13 +73,65 @@ module.exports = {
if (!session) error= 'No session object returned'
if (!error) {
if (data.path) this.sessionAddToHistory(data.path)
ipc.send('sessionOpen',JSON.stringify(session),clientId)
for (var i in openedSessions) {
if (openedSessions[i].indexOf(clientId) != -1) {
openedSessions[i].splice(openedSessions[i].indexOf(clientId), 1)
}
}
if (data.path) {
this.sessionAddToHistory(data.path)
fs.lstat(data.path, (err, stats)=>{
if (err || !stats.isFile()) return
if (!openedSessions[data.path]) {
openedSessions[data.path] = []
var watchFile = ()=>{
var watcher = chokidar.watch(data.path)
watcher.on('change',()=>{
for (var k in openedSessions[data.path]) {
if (openedSessions[data.path][k] != lastSavingClient) {
module.exports.sessionOpen({path:data.path}, openedSessions[data.path][k])
}
}
watcher.close()
watchFile()
})
}
watchFile()
}
openedSessions[data.path].push(clientId)
})
}
} else {
ipc.send('error',{title:'Error: invalid session file',text:'<p>'+error+'</p>'})
}
},
savingSession: function(data, clientId) {
lastSavingClient = clientId
},
sendOsc: function(data) {
if (data.syncOnly) return

View file

@ -16,7 +16,8 @@
"socket.io": "1.7.2",
"socketio-wildcard": "0.3.0",
"express": "4.14.0",
"jquery-slim": "3.0.0"
"jquery-slim": "3.0.0",
"chokidar": "1.6.1"
},
"scripts": {
"start": "electron ."

View file

@ -114,6 +114,7 @@ module.exports = {
down.attr('href', window.URL.createObjectURL(blob))
var event = new MouseEvent("click")
down[0].dispatchEvent(event)
ipc.send('savingSession')
},
editorEnable: function(){