From c7bef35a41b781b0586e960acd84321273002179 Mon Sep 17 00:00:00 2001 From: Arun Bose Date: Sat, 30 Oct 2021 20:55:41 +0530 Subject: [PATCH] File/dir create/change watchers. sync of file content across different phoenix tab instances working. https://github.com/aicore/phoenix/issues/3 --- src/phoenix/fslib.js | 45 ++++++++++++++++++++++++++++++++++---- src/phoenix/fslib_watch.js | 30 +++++++++++++------------ 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/phoenix/fslib.js b/src/phoenix/fslib.js index b0de2e16ca..94a75150fd 100644 --- a/src/phoenix/fslib.js +++ b/src/phoenix/fslib.js @@ -131,18 +131,44 @@ const fileSystemLib = { }, mkdir: function (...args) { // (path, mode, callback) let path = args[0]; + function callbackInterceptor(...interceptedArgs) { + let err = interceptedArgs.length >= 1 ? interceptedArgs[0] : null; + if(!err){ + FsWatch.reportCreateEvent(path); + } + if(args.originalCallback){ + args.originalCallback(...interceptedArgs); + } + } + let callbackIndex = _getFirstFunctionIndex(args); + if(callbackIndex !== -1) { + args.originalCallback = args[callbackIndex]; + args[callbackIndex] = callbackInterceptor; + } + if(Mounts.isMountSubPath(path)) { return NativeFS.mkdir(...args); } return filerLib.fs.mkdir(...args); }, rename: function (oldPath, newPath, cb) { + function callbackInterceptor(...args) { + let err = args.length >= 1 ? args[0] : null; + if(!err){ + FsWatch.reportUnlinkEvent(oldPath); + FsWatch.reportCreateEvent(newPath); + } + if(cb){ + cb(...args); + } + } + if(Mounts.isMountPath(oldPath) || Mounts.isMountPath(newPath)) { throw new Errors.EPERM('Mount root directory cannot be deleted.'); } else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) { - return NativeFS.rename(oldPath, newPath, cb); + return NativeFS.rename(oldPath, newPath, callbackInterceptor); } - return filerLib.fs.rename(oldPath, newPath, cb); + return filerLib.fs.rename(oldPath, newPath, callbackInterceptor); }, unlink: function (path, cb) { function callbackInterceptor(...args) { @@ -154,6 +180,7 @@ const fileSystemLib = { cb(...args); } } + if(Mounts.isMountPath(path)) { throw new Errors.EPERM('Mount root directory cannot be deleted.'); } else if(Mounts.isMountSubPath(path)) { @@ -161,9 +188,19 @@ const fileSystemLib = { } return filerShell.rm(path, { recursive: true }, callbackInterceptor); }, - copy: function (src, dst, callback) { + copy: function (src, dst, cb) { + function callbackInterceptor(...args) { + let err = args.length >= 1 ? args[0] : null; + if(!err){ + FsWatch.reportCreateEvent(dst); + } + if(cb){ + cb(...args); + } + } + if(Mounts.isMountSubPath(src) && Mounts.isMountSubPath(dst)) { - return NativeFS.copy(src, dst, callback); + return NativeFS.copy(src, dst, callbackInterceptor); } throw new Errors.ENOSYS('Phoenix fs copy on filer or across filer and native not yet supported'); }, diff --git a/src/phoenix/fslib_watch.js b/src/phoenix/fslib_watch.js index 207bae3550..d5a6d009f9 100644 --- a/src/phoenix/fslib_watch.js +++ b/src/phoenix/fslib_watch.js @@ -32,7 +32,7 @@ let _watchListeners = []; let _globmatch = null; const WATCH_EVENT_NOTIFICATION = 'PHOENIX_WATCH_EVENT_NOTIFICATION'; -// const WATCH_EVENT_CREATED = 'created'; +const WATCH_EVENT_CREATED = 'created'; const WATCH_EVENT_DELETED = 'deleted'; const WATCH_EVENT_CHANGED = 'changed'; @@ -93,6 +93,7 @@ function _processFsWatchEvent(event, broadcast=true) { function _listenToExternalFsWatchEvents() { _setupBroadcastChannel(); _channel.onmessage = async function(event) { + console.log("External fs watch event: ", event.data); _processFsWatchEvent(event.data, false); }; } @@ -108,28 +109,28 @@ function watch(path, ignoreGlobList, changeCallback, callback) { callback(); } -function reportUnlinkEvent(path) { +function _triggerEvent(path, eventType) { let pathLib = window.path; path = pathLib.normalize(path); let event = { - event: WATCH_EVENT_DELETED, - parentDirPath: pathLib.dirname(path), + event: eventType, + parentDirPath: `${pathLib.dirname(path)}/`, entryName: pathLib.basename(path), path: path }; _processFsWatchEvent(event); } +function reportUnlinkEvent(path) { + _triggerEvent(path, WATCH_EVENT_DELETED); +} + function reportChangeEvent(path) { - let pathLib = window.path; - path = pathLib.normalize(path); - let event = { - event: WATCH_EVENT_CHANGED, - parentDirPath: pathLib.dirname(path), - entryName: pathLib.basename(path), - path: path - }; - _processFsWatchEvent(event); + _triggerEvent(path, WATCH_EVENT_CHANGED); +} + +function reportCreateEvent(path) { + _triggerEvent(path, WATCH_EVENT_CREATED); } function unwatch(path, callback) { @@ -151,7 +152,8 @@ const FsWatch = { unwatch, unwatchAll, reportUnlinkEvent, - reportChangeEvent + reportChangeEvent, + reportCreateEvent }; export default FsWatch;