Automatically refresh the drive when a change occurs in a shared folder

pull/1/head
yflory 7 years ago
parent 4706b8a9de
commit b5c2eb8a9c

@ -28,6 +28,7 @@ define([
var postMessage = function () {}; var postMessage = function () {};
var broadcast = function () {}; var broadcast = function () {};
var sendDriveEvent = function () {}; var sendDriveEvent = function () {};
var registerProxyEvents = function () {};
var storeHash; var storeHash;
@ -1168,6 +1169,9 @@ define([
store.manager.addProxy(id, rt.proxy, info.leave); store.manager.addProxy(id, rt.proxy, info.leave);
cb(rt, info.metadata); cb(rt, info.metadata);
}); });
if (store.driveEvents) {
registerProxyEvents(rt.proxy, id);
}
return rt; return rt;
}; };
Store.addSharedFolder = function (clientId, data, cb) { Store.addSharedFolder = function (clientId, data, cb) {
@ -1231,32 +1235,41 @@ define([
// Special events // Special events
var driveEventInit = false;
sendDriveEvent = function (q, data, sender) { sendDriveEvent = function (q, data, sender) {
driveEventClients.forEach(function (cId) { driveEventClients.forEach(function (cId) {
if (cId === sender) { return; } if (cId === sender) { return; }
postMessage(cId, q, data); 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) { Store._subscribeToDrive = function (clientId) {
if (driveEventClients.indexOf(clientId) === -1) { if (driveEventClients.indexOf(clientId) === -1) {
driveEventClients.push(clientId); driveEventClients.push(clientId);
} }
if (!driveEventInit) { if (!store.driveEvents) {
store.proxy.on('change', [], function (o, n, p) { store.driveEvents = true;
sendDriveEvent('DRIVE_CHANGE', { registerProxyEvents(store.proxy);
old: o, Object.keys(store.manager.folders).forEach(function (fId) {
new: n, var proxy = store.manager.folders[fId].proxy;
path: p registerProxyEvents(proxy, fId);
});
});
store.proxy.on('remove', [], function (o, p) {
sendDriveEvent(clientId, 'DRIVE_REMOVE', {
old: o,
path: p
});
}); });
driveEventInit = true;
} }
}; };
@ -1313,7 +1326,6 @@ define([
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
var loadSharedFolders = function (waitFor) { var loadSharedFolders = function (waitFor) {
var w = waitFor();
store.sharedFolders = {}; store.sharedFolders = {};
var shared = Util.find(store.proxy, ['drive', UserObject.SHARED_FOLDERS]) || {}; var shared = Util.find(store.proxy, ['drive', UserObject.SHARED_FOLDERS]) || {};
// Check if any of our shared folder is expired or deleted by its owner. // Check if any of our shared folder is expired or deleted by its owner.
@ -1345,14 +1357,12 @@ define([
delete shared[fId]; delete shared[fId];
}); });
})); }));
}).nThen(function () { }).nThen(function (waitFor) {
Object.keys(shared).forEach(function (id) { Object.keys(shared).forEach(function (id) {
var sf = shared[id]; var sf = shared[id];
loadSharedFolder(id, sf, function () { loadSharedFolder(id, sf, waitFor());
w();
});
}); });
}); }).nThen(waitFor());
}; };
var onReady = function (clientId, returned, cb) { var onReady = function (clientId, returned, cb) {

@ -681,9 +681,11 @@ define([
}; };
}; };
/* /* =============================================================================
Inner only * =============================================================================
*/ * Inner only
* =============================================================================
* ============================================================================= */
var renameInner = function (Env, path, newName, cb) { var renameInner = function (Env, path, newName, cb) {
return void Env.sframeChan.query("Q_DRIVE_USEROBJECT", { return void Env.sframeChan.query("Q_DRIVE_USEROBJECT", {
@ -752,6 +754,7 @@ define([
var findChannels = _findChannels; var findChannels = _findChannels;
var getFileData = _getFileData; var getFileData = _getFileData;
var getUserObjectPath = _getUserObjectPath;
var find = function (Env, path) { var find = function (Env, path) {
var resolved = _resolvePath(Env, path); var resolved = _resolvePath(Env, path);
@ -925,6 +928,7 @@ define([
findChannels: callWithEnv(findChannels), findChannels: callWithEnv(findChannels),
getSharedFolderData: callWithEnv(getSharedFolderData), getSharedFolderData: callWithEnv(getSharedFolderData),
isInSharedFolder: callWithEnv(isInSharedFolder), isInSharedFolder: callWithEnv(isInSharedFolder),
getUserObjectPath: callWithEnv(getUserObjectPath),
// Generic // Generic
isFile: callWithEnv(isFile), isFile: callWithEnv(isFile),
isFolder: callWithEnv(isFolder), isFolder: callWithEnv(isFolder),

@ -237,6 +237,7 @@ define([
}); });
_getFiles['hrefArray'] = function () { _getFiles['hrefArray'] = function () {
var ret = []; var ret = [];
if (sharedFolder) { return ret; }
getHrefArray().forEach(function (c) { getHrefArray().forEach(function (c) {
ret = ret.concat(_getFiles[c]()); ret = ret.concat(_getFiles[c]());
}); });

@ -3134,32 +3134,60 @@ define([
sframeChan.on('EV_DRIVE_CHANGE', function (data) { sframeChan.on('EV_DRIVE_CHANGE', function (data) {
if (history.isHistoryMode) { return; } 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; } if (path[0] !== 'drive') { return false; }
path = path.slice(1); path = path.slice(1);
if (originalPath[0] === 'drive') { originalPath = originalPath.slice(1); }
var cPath = currentPath.slice(); 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))) { (path.length >= cPath.length && manager.isSubpath(path, cPath))) {
// Reload after a few ms to make sure all the change events have been received // Reload after a few ms to make sure all the change events have been received
onRefresh.refresh(); onRefresh.refresh();
} else if (path.length && path[0] === FILES_DATA) { } else {
onRefresh.refresh(); APP.resetTree();
} }
APP.resetTree();
return false; return false;
}); });
sframeChan.on('EV_DRIVE_REMOVE', function (data) { sframeChan.on('EV_DRIVE_REMOVE', function (data) {
if (history.isHistoryMode) { return; } 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; } if (path[0] !== 'drive') { return false; }
path = path.slice(1); path = path.slice(1);
var cPath = currentPath.slice(); var cPath = currentPath.slice();
if ((manager.isPathIn(cPath, ['hrefArray', TRASH]) && cPath[0] === path[0]) || if ((manager.isPathIn(cPath, ['hrefArray', TRASH]) && cPath[0] === path[0]) ||
(path.length >= cPath.length && manager.isSubpath(path, cPath))) { (path.length >= cPath.length && manager.isSubpath(path, cPath))) {
// Reload after a few to make sure all the change events have been received // Reload after a few to make sure all the change events have been received
onRefresh.refresh(); onRefresh.refresh();
} else {
APP.resetTree();
} }
APP.resetTree();
return false; return false;
}); });
@ -3192,7 +3220,6 @@ define([
var title = manager.getTitle(id); var title = manager.getTitle(id);
titles.push(title); titles.push(title);
var paths = manager.findFile(id); var paths = manager.findFile(id);
console.log(title, id, paths);
manager.delete(paths, refresh); manager.delete(paths, refresh);
}); });
if (!titles.length) { return; } if (!titles.length) { return; }

Loading…
Cancel
Save