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

Introduce electron-main specific timeslots

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-03-01 11:21:14 +02:00
parent c3bb9cfb15
commit e1b3bfea88
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
14 changed files with 1548 additions and 1157 deletions

2417
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@ import { beforeApplicationIsLoadingInjectionToken } from "./timeslots/before-app
import { onLoadOfApplicationInjectionToken } from "./timeslots/on-load-of-application-injection-token";
import { afterApplicationIsLoadedInjectionToken } from "./timeslots/after-application-is-loaded-injection-token";
export type StartApplication = () => void;
export type StartApplication = () => Promise<void>;
export const startApplicationInjectionToken =
getInjectionToken<StartApplication>({

View File

@ -1 +1,8 @@
import { overrideSideEffectsWithFakes } from "./src/override-side-effects-with-fakes";
export { beforeElectronIsReadyInjectionToken } from "./src/start-application/timeslots/before-electron-is-ready-injection-token";
export { beforeAnythingInjectionToken } from "./src/start-application/timeslots/before-anything-injection-token";
export { feature } from "./src/feature";
export const testUtils = { overrideSideEffectsWithFakes }

View File

@ -30,14 +30,15 @@
},
"peerDependencies": {
"@k8slens/feature-core": "^0.0.1",
"@ogre-tools/fp": "^15.1.1",
"@k8slens/application": "6.4.0-beta.13",
"@ogre-tools/injectable": "^15.1.1",
"@ogre-tools/injectable-extension-for-auto-registration": "^15.1.1",
"@ogre-tools/injectable-utils": "^15.1.1",
"lodash": "^4.17.15"
"electron": "^19.1.9"
},
"devDependencies": {
"@async-fn/jest": "^1.6.4"
"@async-fn/jest": "^1.6.4",
"@ogre-tools/test-utils": "^15.1.1"
}
}

View File

@ -0,0 +1,14 @@
/**
* 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 { app } from "electron";
const electronAppInjectable = getInjectable({
id: "electron-app",
instantiate: () => app,
causesSideEffects: true,
});
export default electronAppInjectable;

View File

@ -0,0 +1,17 @@
import electronAppInjectable from "./electron-app.injectable";
import { app } from 'electron';
import { createContainer } from "@ogre-tools/injectable";
import { feature } from "../feature";
import { registerFeature } from "@k8slens/feature-core";
describe('electron-app', () => {
it('is electron app', () => {
const di = createContainer('irrelevant');
registerFeature(di, feature)
const actual = di.inject(electronAppInjectable);
expect(actual).toBe(app);
});
});

View File

@ -1,8 +1,9 @@
import { getFeature } from "@k8slens/feature-core";
import { autoRegister } from "@ogre-tools/injectable-extension-for-auto-registration";
import { feature as applicationFeature } from '@k8slens/application';
export const feature = getFeature({
id: "application",
id: "application-for-electron-main",
register: (di) => {
autoRegister({
@ -14,4 +15,8 @@ export const feature = getFeature({
],
});
},
dependencies: [
applicationFeature
]
});

View File

@ -0,0 +1,6 @@
import type { DiContainer } from "@ogre-tools/injectable";
import whenAppIsReadyInjectable from "./start-application/when-app-is-ready.injectable";
export const overrideSideEffectsWithFakes = (di: DiContainer) => {
di.override(whenAppIsReadyInjectable, () => () => Promise.resolve())
};

View File

@ -0,0 +1,56 @@
import {
DiContainer,
getInjectable,
instantiationDecoratorToken,
lifecycleEnum,
} from "@ogre-tools/injectable";
import { startApplicationInjectionToken } from "@k8slens/application";
import whenAppIsReadyInjectable from "./when-app-is-ready.injectable";
import { beforeAnythingInjectionToken } from "./timeslots/before-anything-injection-token";
import { beforeElectronIsReadyInjectionToken } from "./timeslots/before-electron-is-ready-injection-token";
import { runManySyncFor } from "@ogre-tools/injectable-utils";
const startElectronApplicationInjectable = getInjectable({
id: "start-electron-application",
instantiate: () => ({
decorate:
(toBeDecorated: unknown) =>
(di: DiContainer, ...args: unknown[]) => {
const whenAppIsReady = di.inject(whenAppIsReadyInjectable);
const runManySync = runManySyncFor(di);
const beforeAnything = runManySync(beforeAnythingInjectionToken);
const beforeElectronIsReady = runManySync(
beforeElectronIsReadyInjectionToken
);
const typedToBeDecorated = toBeDecorated as (
di: DiContainer,
...args: unknown[]
) => unknown;
const startApplication = typedToBeDecorated(di, ...args) as (
...args: unknown[]
) => unknown;
return async (...startApplicationArgs: unknown[]) => {
beforeAnything();
beforeElectronIsReady();
await whenAppIsReady();
return startApplication(...startApplicationArgs);
};
},
target: startApplicationInjectionToken,
}),
decorable: false,
injectionToken: instantiationDecoratorToken,
lifecycle: lifecycleEnum.singleton,
});
export default startElectronApplicationInjectable;

View File

@ -0,0 +1,6 @@
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "@ogre-tools/injectable-utils";
export const beforeAnythingInjectionToken = getInjectionToken<Runnable>({
id: "before-anything",
});

View File

@ -0,0 +1,6 @@
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "@ogre-tools/injectable-utils";
export const beforeElectronIsReadyInjectionToken = getInjectionToken<Runnable>({
id: "before-electron-is-ready-injection-token",
});

View File

@ -0,0 +1,16 @@
import { getInjectable } from "@ogre-tools/injectable";
import electronAppInjectable from "../electron/electron-app.injectable";
const whenAppIsReadyInjectable = getInjectable({
id: "when-app-is-ready",
instantiate: (di) => {
const electronApp = di.inject(electronAppInjectable);
return () => electronApp.whenReady();
},
decorable: false,
});
export default whenAppIsReadyInjectable;

View File

@ -0,0 +1,50 @@
import { createContainer, DiContainer } from "@ogre-tools/injectable";
import { feature } from "../../feature";
import { registerFeature } from "@k8slens/feature-core";
import whenAppIsReadyInjectable from "../when-app-is-ready.injectable";
import { getPromiseStatus } from "@ogre-tools/test-utils";
import electronAppInjectable from "../../electron/electron-app.injectable";
import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
describe("when-app-is-ready", () => {
let di: DiContainer;
let whenReadyMock: AsyncFnMock<() => Promise<void>>;
beforeEach(() => {
di = createContainer("irrelevant");
registerFeature(di, feature);
whenReadyMock = asyncFn();
di.override(
electronAppInjectable,
() => ({ whenReady: whenReadyMock } as unknown)
);
});
describe("when called", () => {
let actualPromise: Promise<void>;
beforeEach(() => {
const whenAppIsReady = di.inject(whenAppIsReadyInjectable);
actualPromise = whenAppIsReady();
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(actualPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("when app is ready, resolves", async () => {
await whenReadyMock.resolve();
const promiseStatus = await getPromiseStatus(actualPromise);
expect(promiseStatus.fulfilled).toBe(true);
});
});
});

View File

@ -0,0 +1,94 @@
import {
createContainer,
DiContainer,
getInjectable,
} from "@ogre-tools/injectable";
import { registerFeature } from "@k8slens/feature-core";
import { feature } from "./feature";
import {
beforeApplicationIsLoadingInjectionToken,
startApplicationInjectionToken,
} from "@k8slens/application";
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
import whenAppIsReadyInjectable from "./start-application/when-app-is-ready.injectable";
import { beforeAnythingInjectionToken } from "./start-application/timeslots/before-anything-injection-token";
import { beforeElectronIsReadyInjectionToken } from "./start-application/timeslots/before-electron-is-ready-injection-token";
describe("starting-of-electron-main-application", () => {
let di: DiContainer;
let beforeAnythingMock: jest.Mock;
let beforeElectronIsReadyMock: jest.Mock;
let beforeApplicationIsLoadingMock: AsyncFnMock<() => Promise<void>>;
let whenAppIsReadyMock: AsyncFnMock<() => Promise<void>>;
beforeEach(() => {
di = createContainer("irrelevant");
beforeAnythingMock = jest.fn();
beforeElectronIsReadyMock = jest.fn();
beforeApplicationIsLoadingMock = asyncFn();
whenAppIsReadyMock = asyncFn();
registerFeature(di, feature);
const beforeAnythingIsLoadingInjectable = getInjectable({
id: "before-anything",
instantiate: () => ({ run: beforeAnythingMock }),
injectionToken: beforeAnythingInjectionToken,
});
const beforeElectronIsReadyIsLoadingInjectable = getInjectable({
id: "before-electron-is-ready",
instantiate: () => ({ run: beforeElectronIsReadyMock }),
injectionToken: beforeElectronIsReadyInjectionToken,
});
const beforeApplicationIsLoadingInjectable = getInjectable({
id: "before-application-is-loading",
instantiate: () => ({ run: beforeApplicationIsLoadingMock }),
injectionToken: beforeApplicationIsLoadingInjectionToken,
});
di.register(
beforeAnythingIsLoadingInjectable,
beforeElectronIsReadyIsLoadingInjectable,
beforeApplicationIsLoadingInjectable
);
di.override(whenAppIsReadyInjectable, () => whenAppIsReadyMock);
});
describe("when application is started", () => {
beforeEach(() => {
const startApplication = di.inject(startApplicationInjectionToken);
startApplication();
});
it("calls for synchronous runnables for before anything", () => {
expect(beforeAnythingMock).toHaveBeenCalled();
});
it("calls for synchronous runnables for before electron is ready", () => {
expect(beforeElectronIsReadyMock).toHaveBeenCalled();
});
it("calls to wait when electron is ready", () => {
expect(whenAppIsReadyMock).toHaveBeenCalled();
});
it("does not call runnables for before application is loading yet", () => {
expect(beforeApplicationIsLoadingMock).not.toHaveBeenCalled();
});
describe("when electron is ready", () => {
beforeEach(async () => {
await whenAppIsReadyMock.resolve();
});
it("calls runnables for before application is loading", () => {
expect(beforeApplicationIsLoadingMock).toHaveBeenCalled();
});
});
});
});