Extract agnostic and electron-main ways of starting application to separate packages (#7259)

* Introduce an agnostic way to start application is the Feature

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce directory for application-packages

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove non agnostic timeslots from agnostic application

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce empty package for electron main specific stuff for Application Feature

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce electron-main specific timeslots

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Update injectable version

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Update README

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Tweak export names to make them easier to import

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make starting of application synchronous for Electron main

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Tweak versions in package jsons

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Consolidate name of feature file

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

---------

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen authored and GitHub committed 2023-03-06 10:17:24 +02:00
1 parent a917494e9c
commit a9bd5ae7e2
35 files changed
+1841 -1036

No files matched your search

@@ -0,0 +1,58 @@
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 (...startApplicationArgs: unknown[]) => {
beforeAnything();
beforeElectronIsReady();
return (async () => {
await whenAppIsReady();
return startApplication(...startApplicationArgs);
})()
};
},
target: startApplicationInjectionToken,
}),
decorable: false,
injectionToken: instantiationDecoratorToken,
lifecycle: lifecycleEnum.singleton,
});
export default startElectronApplicationInjectable;
@@ -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",
});
@@ -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",
});
@@ -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;
@@ -0,0 +1,50 @@
import { createContainer, DiContainer } from "@ogre-tools/injectable";
import { applicationFeatureForElectronMain } 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, applicationFeatureForElectronMain);
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);
});
});
});