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

Introduce tokens for runnables of specific application events for Open Closed Principle

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-04-27 11:07:16 +03:00
parent bc0b342602
commit 6fe0c2fce5
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
9 changed files with 166 additions and 6 deletions

View File

@ -0,0 +1,11 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
export const beforeApplicationIsReadyInjectionToken =
getInjectionToken<Runnable>({
id: "before-application-is-ready",
});

View File

@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
export const onApplicationActivationInjectionToken = getInjectionToken<
Runnable<{ hasVisibleWindows: boolean }>
>({
id: "on-application-activation",
});

View File

@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../../../../run-many-for";
import type { Event } from "electron";
export const onApplicationQuitInjectionToken = getInjectionToken<
Runnable<{ event: Event }>
>({
id: "on-application-quit",
});

View File

@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
import type { Event } from "electron";
export const onApplicationCloseInjectionToken = getInjectionToken<
Runnable<{ event: Event }>
>({
id: "on-application-close",
});

View File

@ -3,11 +3,8 @@
* 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 { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
export interface Setupable { export const onApplicationIsReadyInjectionToken = getInjectionToken<Runnable>({
runSetup: () => Promise<void> | void; id: "on-application-is-ready",
}
export const setupableInjectionToken = getInjectionToken<Setupable>({
id: "setupable-injection-token",
}); });

View File

@ -0,0 +1,16 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
import type { Event } from "electron";
export const onOpenOfUrlInjectionToken = getInjectionToken<
Runnable<{
event: Event;
url: string;
}>
>({
id: "on-open-of-url",
});

View File

@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
export const onRootFrameRenderInjectionToken = getInjectionToken<Runnable>({
id: "on-root-frame-render",
});

View File

@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../run-many-for";
export const onSecondApplicationInstanceInjectionToken = getInjectionToken<
Runnable<{ commandLineArguments: string[] }>
>({
id: "on-second-application-instance",
});

View File

@ -0,0 +1,76 @@
/**
* 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 electronAppInjectable from "../app-paths/get-electron-app-path/electron-app/electron-app.injectable";
import { beforeApplicationIsReadyInjectionToken } from "./before-application-is-ready/before-application-is-ready-injection-token";
import { onApplicationIsReadyInjectionToken } from "./on-application-is-ready/on-application-is-ready-injection-token";
import { onSecondApplicationInstanceInjectionToken } from "./on-second-application-instance/on-second-application-instance-injection-token";
import { onApplicationActivationInjectionToken } from "./on-application-activation/on-application-activation-injection-token";
import { onOpenOfUrlInjectionToken } from "./on-open-of-url/on-open-of-url-injection-token";
import { runManyFor } from "./run-many-for";
import {
onApplicationCloseInjectionToken,
} from "./on-application-close/on-application-close-injection-token";
const startMainApplicationInjectable = getInjectable({
id: "start-main-application",
instantiate: (di) => {
const runMany = runManyFor(di);
const app = di.inject(electronAppInjectable);
const runManyBeforeApplicationIsReady = runMany(
beforeApplicationIsReadyInjectionToken,
);
const runManyAfterApplicationIsReady = runMany(
onApplicationIsReadyInjectionToken,
);
const runManyForSecondApplicationInstance = runMany(
onSecondApplicationInstanceInjectionToken,
);
const runManyForApplicationActivation = runMany(
onApplicationActivationInjectionToken,
);
const runManyForApplicationClose = runMany(onApplicationCloseInjectionToken);
const runManyForOpenOfUrl = runMany(onOpenOfUrlInjectionToken);
return async () => {
app.on("second-instance", async (_, commandLineArguments) => {
await runManyForSecondApplicationInstance({ commandLineArguments });
});
app.on("activate", async (_, hasVisibleWindows) => {
await runManyForApplicationActivation({ hasVisibleWindows });
});
app.on("will-quit", async (event) => {
await runManyForApplicationClose({ event });
});
app.on("open-url", async (event, url) => {
await runManyForOpenOfUrl({ event, url });
});
await runManyBeforeApplicationIsReady();
await app.whenReady();
await runManyAfterApplicationIsReady();
};
},
});
export default startMainApplicationInjectable;