Skip to content

Commit

Permalink
Added client side directory VFS watch (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Aug 17, 2020
1 parent 983395f commit 66cf034
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
2 changes: 2 additions & 0 deletions __tests__/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ describe('utils.vfs#basename', () => {
describe('utils.vfs#pathname', () => {
test('Should resolve', () => {
expect(vfs.pathname('home:/foo/bar')).toBe('home:/foo');
expect(vfs.pathname('home:/foo')).toBe('home:/');
expect(vfs.pathname('home:/')).toBe('home:/');
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export const defaultConfiguration = {
},

vfs: {
watch: false,
watch: true,
defaultPath: 'home:/',
defaultAdapter: 'system',
adapters: {},
Expand Down
13 changes: 10 additions & 3 deletions src/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import * as VFS from './vfs';
import {EventEmitter} from '@osjs/event-emitter';
import {parseMontpointPrefix, filterMountByGroups} from './utils/vfs';
import {parseMontpointPrefix, filterMountByGroups, createWatchEvents} from './utils/vfs';
import defaultAdapter from './adapters/vfs/null';
import systemAdapter from './adapters/vfs/system';
import appsAdapter from './adapters/vfs/apps';
Expand Down Expand Up @@ -233,7 +233,14 @@ export default class Filesystem extends EventEmitter {
_request(method, ...args) {
const ev = `osjs/vfs:${method}`;

const done = () => this.core.emit(`${ev}:done`, ...args);
const done = (error) => {
this.core.emit(`${ev}:done`, ...args);

if (!error && this.core.config('vfs.watch')) {
const eva = createWatchEvents(method, args);
eva.forEach(([e, a]) => this.core.emit(e, a));
}
};

this.core.emit(ev, ...args);

Expand All @@ -243,7 +250,7 @@ export default class Filesystem extends EventEmitter {
return result;
})
.catch(error => {
done();
done(error);
throw error;
});
}
Expand Down
43 changes: 42 additions & 1 deletion src/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,12 @@ export const basename = path => path.split('/').reverse()[0];
*/
export const pathname = path => {
const split = path.split('/');
split.splice(split.length - 1, 1);
if (split.length === 2) {
split[1] = '';
} else {
split.splice(split.length - 1, 1);
}

return split.join('/');
};

Expand Down Expand Up @@ -341,3 +346,39 @@ export const filterMountByGroups = userGroups => (mountGroups, strict = true) =>
? mountGroups[strict ? 'every' : 'some'](g => userGroups.indexOf(g) !== -1)
: true;


/**
* Creates a list of VFS events to simulate server-side
* file watching
* @return {object[]}
*/
export const createWatchEvents = (method, args) => {
const events = [];
const options = args[args.length - 1] || {};

const movement = ['move', 'rename', 'copy'].indexOf(method) !== -1;
const invalid = ['readdir', 'download', 'url', 'exists', 'readfile', 'search', 'stat'].indexOf(method) !== -1;
const path = i => typeof i === 'string' ? i : i.path;

if (!invalid) {
const obj = {
method,
source: path(args[0]),
pid: options.pid
};

events.push(['osjs/vfs:directoryChanged', {
...obj,
path: pathname(path(args[0])),
}]);

if (movement) {
events.push(['osjs/vfs:directoryChanged', {
...obj,
path: pathname(path(args[1]))
}]);
}
}

return events;
};

0 comments on commit 66cf034

Please sign in to comment.