1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

close watch for user resource templates on quit

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2023-04-05 14:30:25 -04:00
parent 696c026f2d
commit 30796eccff
3 changed files with 45 additions and 4 deletions

View File

@ -15,7 +15,7 @@ const createResourceTemplatesInjectable = getInjectable({
instantiate: async (di) => { instantiate: async (di) => {
const lensResourceTemplates = await di.inject(lensCreateResourceTemplatesInjectable); const lensResourceTemplates = await di.inject(lensCreateResourceTemplatesInjectable);
const userResourceTemplates = di.inject(userCreateResourceTemplatesInjectable); const [ userResourceTemplates, ] = di.inject(userCreateResourceTemplatesInjectable);
return computed((): GroupBase<{ label: string; value: string }>[] => { return computed((): GroupBase<{ label: string; value: string }>[] => {
const res = [ const res = [

View File

@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { beforeQuitOfBackEndInjectionToken } from "../../../../main/start-main-application/runnable-tokens/before-quit-of-back-end-injection-token";
import userTemplatesInjectable from "./user-templates.injectable";
const stopWatchingUserTemplatesOnQuitInjectable = getInjectable({
id: "stop-watching-user-templates-on-quit",
instantiate: (di) => {
return {
id: "stop-watching-user-templates-on-quit",
run: () => {
const [, disposer] = di.inject(userTemplatesInjectable);
disposer();
},
};
},
injectionToken: beforeQuitOfBackEndInjectionToken,
});
export default stopWatchingUserTemplatesOnQuitInjectable;

View File

@ -3,6 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { IComputedValue } from "mobx";
import { computed, observable } from "mobx"; import { computed, observable } from "mobx";
import { delay, getOrInsert, isErrnoException, waitForPath } from "../../../utils"; import { delay, getOrInsert, isErrnoException, waitForPath } from "../../../utils";
import { readFile } from "fs/promises"; import { readFile } from "fs/promises";
@ -10,15 +11,19 @@ import { hasCorrectExtension } from "./has-correct-extension";
import type { RawTemplates } from "./create-resource-templates.injectable"; import type { RawTemplates } from "./create-resource-templates.injectable";
import joinPathsInjectable from "../../../../common/path/join-paths.injectable"; import joinPathsInjectable from "../../../../common/path/join-paths.injectable";
import watchInjectable from "../../../../common/fs/watch/watch.injectable"; import watchInjectable from "../../../../common/fs/watch/watch.injectable";
import type { Watcher } from "../../../../common/fs/watch/watch.injectable";
import getRelativePathInjectable from "../../../../common/path/get-relative-path.injectable"; import getRelativePathInjectable from "../../../../common/path/get-relative-path.injectable";
import homeDirectoryPathInjectable from "../../../../common/os/home-directory-path.injectable"; import homeDirectoryPathInjectable from "../../../../common/os/home-directory-path.injectable";
import getDirnameOfPathInjectable from "../../../../common/path/get-dirname.injectable"; import getDirnameOfPathInjectable from "../../../../common/path/get-dirname.injectable";
import loggerInjectable from "../../../../common/logger.injectable"; import loggerInjectable from "../../../../common/logger.injectable";
import parsePathInjectable from "../../../../common/path/parse.injectable"; import parsePathInjectable from "../../../../common/path/parse.injectable";
import type { Disposer } from "../../../../common/utils";
export type ResourceTemplates = [IComputedValue<RawTemplates[]>, Disposer];
const userCreateResourceTemplatesInjectable = getInjectable({ const userCreateResourceTemplatesInjectable = getInjectable({
id: "user-create-resource-templates", id: "user-create-resource-templates",
instantiate: (di) => { instantiate: (di): ResourceTemplates => {
const joinPaths = di.inject(joinPathsInjectable); const joinPaths = di.inject(joinPathsInjectable);
const watch = di.inject(watchInjectable); const watch = di.inject(watchInjectable);
const getRelativePath = di.inject(getRelativePathInjectable); const getRelativePath = di.inject(getRelativePathInjectable);
@ -70,6 +75,8 @@ const userCreateResourceTemplatesInjectable = getInjectable({
templates.delete(filePath); templates.delete(filePath);
}; };
let watcher: Watcher<false>|undefined;
(async () => { (async () => {
for (let i = 1;; i *= 2) { for (let i = 1;; i *= 2) {
try { try {
@ -84,7 +91,7 @@ const userCreateResourceTemplatesInjectable = getInjectable({
/** /**
* NOTE: There is technically a race condition here of the form "time-of-check to time-of-use" * NOTE: There is technically a race condition here of the form "time-of-check to time-of-use"
*/ */
watch(userTemplatesFolder, { watcher = watch<false>(userTemplatesFolder, {
disableGlobbing: true, disableGlobbing: true,
ignorePermissionErrors: true, ignorePermissionErrors: true,
usePolling: false, usePolling: false,
@ -95,6 +102,8 @@ const userCreateResourceTemplatesInjectable = getInjectable({
ignoreInitial: false, ignoreInitial: false,
atomic: 150, // for "atomic writes" atomic: 150, // for "atomic writes"
}) })
watcher
.on("add", onAddOrChange) .on("add", onAddOrChange)
.on("change", onAddOrChange) .on("change", onAddOrChange)
.on("unlink", onUnlink) .on("unlink", onUnlink)
@ -103,7 +112,11 @@ const userCreateResourceTemplatesInjectable = getInjectable({
}); });
})(); })();
return computed(() => groupTemplates(templates)); return [computed(() => groupTemplates(templates)), () => {
logger.info("[USER-CREATE-RESOURCE-TEMPLATES]: stopping watch");
watcher?.close()
}];
}, },
}); });