From b84d6777abca10d45c1b0842b2ae89bd4edd74c7 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 10 Jan 2017 22:38:51 +0100 Subject: [PATCH] main: monitor session changes and update clients that are using the same session file --- app/main/callbacks.js | 60 +++++++++++++++++++++++++++++++++-- app/package.json | 3 +- src/browser/js/app/actions.js | 1 + 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/main/callbacks.js b/app/main/callbacks.js index 091ac7d0..f9b19c82 100644 --- a/app/main/callbacks.js +++ b/app/main/callbacks.js @@ -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:'

'+error+'

'}) + } }, + savingSession: function(data, clientId) { + + lastSavingClient = clientId + + }, + sendOsc: function(data) { if (data.syncOnly) return diff --git a/app/package.json b/app/package.json index 77e1c850..a2cb73fb 100644 --- a/app/package.json +++ b/app/package.json @@ -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 ." diff --git a/src/browser/js/app/actions.js b/src/browser/js/app/actions.js index f0b8f5db..bf066cbf 100644 --- a/src/browser/js/app/actions.js +++ b/src/browser/js/app/actions.js @@ -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(){