Skip to content

Commit

Permalink
fix: fixed clean-up of set-root-folder listener and recreation of fil…
Browse files Browse the repository at this point in the history
…e monitor
  • Loading branch information
olensmar committed Nov 22, 2021
1 parent eb2f8a1 commit cf6cca0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
3 changes: 3 additions & 0 deletions src/components/organisms/FileTreePane/FileTreePane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ const FileTreePane = () => {
setFolder(data);
}
});
return () => {
ipcRenderer.removeListener('set-root-folder', onExecutedFrom);
};
}, []);

useEffect(() => {
Expand Down
100 changes: 49 additions & 51 deletions src/redux/services/fileMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,55 @@ let watcher: FSWatcher;

export function monitorRootFolder(folder: string, appConfig: AppConfig, dispatch: AppDispatch) {
if (watcher) {
const watched = Object.keys(watcher.getWatched());
watcher.unwatch(watched);
watcher.add(folder);
} else {
watcher = watch(folder, {
ignored: appConfig.scanExcludes,
ignoreInitial: true,
persistent: true,
usePolling: true,
interval: 1000,
});
watcher.close();
}

watcher
.on(
'add',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsAdded({paths, appConfig}));
}, 1000)
)
.on(
'addDir',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsAdded({paths, appConfig}));
}, 1000)
)
.on(
'change',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multipleFilesChanged({paths, appConfig}));
}, 1000)
)
.on(
'unlink',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsRemoved(paths));
}, 1000)
)
.on(
'unlinkDir',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsRemoved(paths));
}, 1000)
);
watcher = watch(folder, {
ignored: appConfig.scanExcludes,
ignoreInitial: true,
persistent: true,
usePolling: true,
interval: 1000,
});

watcher
/* eslint-disable no-console */
.on('error', error => console.log(`Watcher error: ${error}`));
}
watcher
.on(
'add',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsAdded({paths, appConfig}));
}, 1000)
)
.on(
'addDir',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsAdded({paths, appConfig}));
}, 1000)
)
.on(
'change',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multipleFilesChanged({paths, appConfig}));
}, 1000)
)
.on(
'unlink',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsRemoved(paths));
}, 1000)
)
.on(
'unlinkDir',
debounceWithPreviousArgs((args: any[]) => {
const paths: Array<string> = args.map(arg => arg[0]);
dispatch(multiplePathsRemoved(paths));
}, 1000)
);

watcher
/* eslint-disable no-console */
.on('error', error => console.log(`Watcher error: ${error}`));
}

0 comments on commit cf6cca0

Please sign in to comment.