Skip to content

Commit

Permalink
Improve ability of downstream apps to control file watching
Browse files Browse the repository at this point in the history
Signed-off-by: Nigel Westbury <nigelipse@miegel.org>
  • Loading branch information
Nigel Westbury authored and westbury committed Mar 9, 2021
1 parent 926d71f commit aa702f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
20 changes: 12 additions & 8 deletions packages/filesystem/src/node/filesystem-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ContainerModule, interfaces } from 'inversify';
import { ConnectionHandler, JsonRpcConnectionHandler, ILogger } from '@theia/core/lib/common';
import { FileSystemWatcherServer, FileSystemWatcherService } from '../common/filesystem-watcher-protocol';
import { FileSystemWatcherServerClient } from './filesystem-watcher-client';
import { NsfwFileSystemWatcherService } from './nsfw-watcher/nsfw-filesystem-service';
import { NsfwFileSystemWatcherService, NsfwFileSystemWatcherServerOptions } from './nsfw-watcher/nsfw-filesystem-service';
import { MessagingService } from '@theia/core/lib/node/messaging/messaging-service';
import { NodeFileUploadService } from './node-file-upload-service';
import { NsfwOptions } from './nsfw-watcher/nsfw-options';
Expand All @@ -37,7 +37,7 @@ const SINGLE_THREADED = process.argv.indexOf('--no-cluster') !== -1;
const NSFW_WATCHER_VERBOSE = process.argv.indexOf('--nsfw-watcher-verbose') !== -1;

export function bindFileSystemWatcherServer(bind: interfaces.Bind, { singleThreaded }: { singleThreaded: boolean } = { singleThreaded: SINGLE_THREADED }): void {
bind<NsfwOptions>(NsfwOptions).toConstantValue({});
bind<NsfwOptions>(NsfwOptions).toConstantValue({ nsfw: {} });

bind(FileSystemWatcherServiceDispatcher).toSelf().inSingletonScope();

Expand All @@ -46,16 +46,20 @@ export function bindFileSystemWatcherServer(bind: interfaces.Bind, { singleThrea

if (singleThreaded) {
// Bind and run the watch server in the current process:
bind<FileSystemWatcherService>(FileSystemWatcherService).toDynamicValue(ctx => {
bind(NsfwFileSystemWatcherServerOptions).toDynamicValue(ctx => {
const logger = ctx.container.get<ILogger>(ILogger);
const nsfwOptions = ctx.container.get<NsfwOptions>(NsfwOptions);
const dispatcher = ctx.container.get<FileSystemWatcherServiceDispatcher>(FileSystemWatcherServiceDispatcher);
const server = new NsfwFileSystemWatcherService({
return {
nsfwOptions,
verbose: NSFW_WATCHER_VERBOSE,
info: (message, ...args) => logger.info(message, ...args),
error: (message, ...args) => logger.error(message, ...args)
});
} as NsfwFileSystemWatcherServerOptions;
}).inSingletonScope();
bind<FileSystemWatcherService>(FileSystemWatcherService).toDynamicValue(ctx => {
const watcherOptions = ctx.container.get<NsfwFileSystemWatcherServerOptions>(NsfwFileSystemWatcherServerOptions);
const dispatcher = ctx.container.get<FileSystemWatcherServiceDispatcher>(FileSystemWatcherServiceDispatcher);
const server = new NsfwFileSystemWatcherService(watcherOptions);
server.setClient(dispatcher);
return server;
}).inSingletonScope();
Expand All @@ -73,14 +77,14 @@ export function bindFileSystemWatcherServer(bind: interfaces.Bind, { singleThrea
// We need to call `.setClient` before listening, else the JSON-RPC calls won't go through.
serverProxy.setClient(dispatcher);
const args: string[] = [
`--nsfwOptions=${JSON.stringify(nsfwOptions)}`
`--nsfwOptions=${JSON.stringify(nsfwOptions.nsfw)}`
];
if (NSFW_WATCHER_VERBOSE) {
args.push('--verbose');
}
ipcConnectionProvider.listen({
serverName,
entryPoint: path.resolve(__dirname, serverName),
entryPoint: nsfwOptions.entryPoint || path.resolve(__dirname, 'nsfw-watcher'),
errorHandler: new ConnectionErrorHandler({
serverName,
logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface NsfwWatcherOptions {
ignored: IMinimatch[]
}

export const NsfwFileSystemWatcherServerOptions = Symbol('NsfwFileSystemWatcherServerOptions');
export interface NsfwFileSystemWatcherServerOptions {
verbose: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -422,11 +423,7 @@ export class NsfwFileSystemWatcherService implements FileSystemWatcherService {
let watcher = this.watchers.get(watcherKey);
if (watcher === undefined) {
const fsPath = FileUri.fsPath(uri);
const watcherOptions: NsfwWatcherOptions = {
ignored: resolvedOptions.ignored
.map(pattern => new Minimatch(pattern, { dot: true })),
};
watcher = new NsfwWatcher(clientId, fsPath, watcherOptions, this.options, this.maybeClient);
watcher = this.createWatcher(clientId, fsPath, resolvedOptions);
watcher.whenDisposed.then(() => this.watchers.delete(watcherKey));
this.watchers.set(watcherKey, watcher);
} else {
Expand All @@ -438,6 +435,14 @@ export class NsfwFileSystemWatcherService implements FileSystemWatcherService {
return watcherId;
}

protected createWatcher(clientId: number, fsPath: string, resolvedOptions: WatchOptions): NsfwWatcher {
const watcherOptions: NsfwWatcherOptions = {
ignored: resolvedOptions.ignored
.map(pattern => new Minimatch(pattern, { dot: true })),
};
return new NsfwWatcher(clientId, fsPath, watcherOptions, this.options, this.maybeClient);
}

async unwatchFileChanges(watcherId: number): Promise<void> {
const handle = this.watcherHandles.get(watcherId);
if (handle === undefined) {
Expand Down
5 changes: 4 additions & 1 deletion packages/filesystem/src/node/nsfw-watcher/nsfw-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ import * as nsfw from 'nsfw';
* Inversify service identifier allowing extensions to override options passed to nsfw by the file watcher.
*/
export const NsfwOptions = Symbol('NsfwOptions');
export type NsfwOptions = nsfw.Options;
export interface NsfwOptions {
nsfw: nsfw.Options;
entryPoint?: string;
}

0 comments on commit aa702f1

Please sign in to comment.