From b5c2eb8a9c35f8dfc68d5f4a6a8098ce451d6ef9 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 13 Jul 2018 19:24:49 +0200 Subject: [PATCH] Automatically refresh the drive when a change occurs in a shared folder --- www/common/outer/async-store.js | 52 ++++++++++++++++++++------------- www/common/proxy-manager.js | 10 +++++-- www/common/userObject.js | 1 + www/drive/inner.js | 43 ++++++++++++++++++++++----- 4 files changed, 74 insertions(+), 32 deletions(-) diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 2ada4ecc6..c009459ab 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -28,6 +28,7 @@ define([ var postMessage = function () {}; var broadcast = function () {}; var sendDriveEvent = function () {}; + var registerProxyEvents = function () {}; var storeHash; @@ -1168,6 +1169,9 @@ define([ store.manager.addProxy(id, rt.proxy, info.leave); cb(rt, info.metadata); }); + if (store.driveEvents) { + registerProxyEvents(rt.proxy, id); + } return rt; }; Store.addSharedFolder = function (clientId, data, cb) { @@ -1231,32 +1235,41 @@ define([ // Special events - var driveEventInit = false; sendDriveEvent = function (q, data, sender) { driveEventClients.forEach(function (cId) { if (cId === sender) { return; } postMessage(cId, q, data); }); }; + registerProxyEvents = function (proxy, fId) { + proxy.on('change', [], function (o, n, p) { + sendDriveEvent('DRIVE_CHANGE', { + id: fId, + old: o, + new: n, + path: p + }); + }); + proxy.on('remove', [], function (o, p) { + sendDriveEvent('DRIVE_REMOVE', { + id: fId, + old: o, + path: p + }); + }); + }; + Store._subscribeToDrive = function (clientId) { if (driveEventClients.indexOf(clientId) === -1) { driveEventClients.push(clientId); } - if (!driveEventInit) { - store.proxy.on('change', [], function (o, n, p) { - sendDriveEvent('DRIVE_CHANGE', { - old: o, - new: n, - path: p - }); - }); - store.proxy.on('remove', [], function (o, p) { - sendDriveEvent(clientId, 'DRIVE_REMOVE', { - old: o, - path: p - }); + if (!store.driveEvents) { + store.driveEvents = true; + registerProxyEvents(store.proxy); + Object.keys(store.manager.folders).forEach(function (fId) { + var proxy = store.manager.folders[fId].proxy; + registerProxyEvents(proxy, fId); }); - driveEventInit = true; } }; @@ -1313,7 +1326,6 @@ define([ ////////////////////////////////////////////////////////////////// var loadSharedFolders = function (waitFor) { - var w = waitFor(); store.sharedFolders = {}; var shared = Util.find(store.proxy, ['drive', UserObject.SHARED_FOLDERS]) || {}; // Check if any of our shared folder is expired or deleted by its owner. @@ -1345,14 +1357,12 @@ define([ delete shared[fId]; }); })); - }).nThen(function () { + }).nThen(function (waitFor) { Object.keys(shared).forEach(function (id) { var sf = shared[id]; - loadSharedFolder(id, sf, function () { - w(); - }); + loadSharedFolder(id, sf, waitFor()); }); - }); + }).nThen(waitFor()); }; var onReady = function (clientId, returned, cb) { diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index f7bd3eb85..bfd3fc89e 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -681,9 +681,11 @@ define([ }; }; - /* - Inner only - */ + /* ============================================================================= + * ============================================================================= + * Inner only + * ============================================================================= + * ============================================================================= */ var renameInner = function (Env, path, newName, cb) { return void Env.sframeChan.query("Q_DRIVE_USEROBJECT", { @@ -752,6 +754,7 @@ define([ var findChannels = _findChannels; var getFileData = _getFileData; + var getUserObjectPath = _getUserObjectPath; var find = function (Env, path) { var resolved = _resolvePath(Env, path); @@ -925,6 +928,7 @@ define([ findChannels: callWithEnv(findChannels), getSharedFolderData: callWithEnv(getSharedFolderData), isInSharedFolder: callWithEnv(isInSharedFolder), + getUserObjectPath: callWithEnv(getUserObjectPath), // Generic isFile: callWithEnv(isFile), isFolder: callWithEnv(isFolder), diff --git a/www/common/userObject.js b/www/common/userObject.js index d373e89d6..9d3cc738a 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -237,6 +237,7 @@ define([ }); _getFiles['hrefArray'] = function () { var ret = []; + if (sharedFolder) { return ret; } getHrefArray().forEach(function (c) { ret = ret.concat(_getFiles[c]()); }); diff --git a/www/drive/inner.js b/www/drive/inner.js index 1fa7e5eb0..b8a95d8b0 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -3134,32 +3134,60 @@ define([ sframeChan.on('EV_DRIVE_CHANGE', function (data) { if (history.isHistoryMode) { return; } - var path = data.path; + + var path = data.path.slice(); + var originalPath = data.path.slice(); + + // Fix the path if this is about a shared folder + if (data.id && manager.folders[data.id]) { + var uoPath = manager.getUserObjectPath(manager.folders[data.id].userObject); + if (uoPath) { + Array.prototype.unshift.apply(path, uoPath); + path.unshift('drive'); + } + } + if (path[0] !== 'drive') { return false; } path = path.slice(1); + if (originalPath[0] === 'drive') { originalPath = originalPath.slice(1); } + var cPath = currentPath.slice(); - if ((manager.isPathIn(cPath, ['hrefArray', TRASH]) && cPath[0] === path[0]) || + if (originalPath.length && originalPath[0] === FILES_DATA) { + onRefresh.refresh(); + } else if ((manager.isPathIn(cPath, ['hrefArray', TRASH]) && cPath[0] === path[0]) || (path.length >= cPath.length && manager.isSubpath(path, cPath))) { // Reload after a few ms to make sure all the change events have been received onRefresh.refresh(); - } else if (path.length && path[0] === FILES_DATA) { - onRefresh.refresh(); + } else { + APP.resetTree(); } - APP.resetTree(); return false; }); sframeChan.on('EV_DRIVE_REMOVE', function (data) { if (history.isHistoryMode) { return; } - var path = data.path; + + var path = data.path.slice(); + + // Fix the path if this is about a shared folder + if (data.id && manager.folders[data.id]) { + var uoPath = manager.getUserObjectPath(manager.folders[data.id].userObject); + if (uoPath) { + Array.prototype.unshift.apply(path, uoPath); + path.unshift('drive'); + } + } + if (path[0] !== 'drive') { return false; } path = path.slice(1); + var cPath = currentPath.slice(); if ((manager.isPathIn(cPath, ['hrefArray', TRASH]) && cPath[0] === path[0]) || (path.length >= cPath.length && manager.isSubpath(path, cPath))) { // Reload after a few to make sure all the change events have been received onRefresh.refresh(); + } else { + APP.resetTree(); } - APP.resetTree(); return false; }); @@ -3192,7 +3220,6 @@ define([ var title = manager.getTitle(id); titles.push(title); var paths = manager.findFile(id); - console.log(title, id, paths); manager.delete(paths, refresh); }); if (!titles.length) { return; }