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

chore: Remove InitializableState for loosing competition

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-04-17 16:45:10 -04:00
parent 353da78d4b
commit eafd1efbc0
2 changed files with 3 additions and 125 deletions

View File

@ -7,77 +7,10 @@ import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import type { Runnable } from "@k8slens/run-many";
import { runManyFor } from "@k8slens/run-many";
import type { DiContainer, Injectable, InjectionToken } from "@ogre-tools/injectable";
import type { DiContainer, InjectionToken } from "@ogre-tools/injectable";
import { createContainer, getInjectionToken } from "@ogre-tools/injectable";
import { runInAction } from "mobx";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import type { ImplInitializableInjectionTokensArgs, Initializable, InitializableState } from "./create";
import { getInjectablesForInitializable, getInitializable, createInitializableState } from "./create";
describe("InitializableState tests", () => {
let di: DiContainer;
beforeEach(() => {
di = getDiForUnitTesting();
});
describe("when created", () => {
let stateInjectable: Injectable<InitializableState<number>, unknown, void>;
let initMock: AsyncFnMock<() => number>;
beforeEach(() => {
initMock = asyncFn();
stateInjectable = createInitializableState({
id: "my-state",
init: initMock,
});
runInAction(() => {
di.register(stateInjectable);
});
});
describe("when injected", () => {
let state: InitializableState<number>;
beforeEach(() => {
state = di.inject(stateInjectable);
});
it("when get is called, throw", () => {
expect(() => state.get()).toThrowError("InitializableState(my-state) has not been initialized yet");
});
describe("when init is called", () => {
beforeEach(() => {
state.init();
});
it("should call provided initialization function", () => {
expect(initMock).toBeCalled();
});
it("when get is called, throw", () => {
expect(() => state.get()).toThrowError("InitializableState(my-state) has not finished initializing");
});
describe("when initialization resolves", () => {
beforeEach(async () => {
await initMock.resolve(42);
});
it("when get is called, returns value", () => {
expect(state.get()).toBe(42);
});
it("when init is called again, throws", async () => {
await expect(() => state.init()).rejects.toThrow("Cannot initialize InitializableState(my-state) more than once");
});
});
});
});
});
});
import type { ImplInitializableInjectionTokensArgs, Initializable } from "./create";
import { getInjectablesForInitializable, getInitializable } from "./create";
describe("InitializableTokens technical tests", () => {
let di: DiContainer;

View File

@ -8,21 +8,6 @@ import type { DiContainerForInjection, Injectable, InjectionToken } from "@ogre-
import { getInjectionToken, getInjectable } from "@ogre-tools/injectable";
import assert from "assert";
export interface CreateInitializableStateArgs<T> {
id: string;
init: (di: DiContainerForInjection) => Promise<T> | T;
injectionToken?: InjectionToken<InitializableState<T>, void>;
}
export interface InitializableState<T> {
get: () => T;
init: () => Promise<void>;
}
type InitializableStateValue<T> =
| { set: false }
| { set: true; value: T } ;
export interface Initializable<T> {
readonly rootId: string;
readonly stateToken: InjectionToken<T, void>;
@ -96,43 +81,3 @@ export const getInjectablesForInitializable = <T>({
initializationInjectable,
};
};
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): Injectable<InitializableState<T>, unknown, void> {
const { id, init, injectionToken } = args;
return getInjectable({
id,
instantiate: (di) => {
let box: InitializableStateValue<T> = {
set: false,
};
let initCalled = false;
return {
init: async () => {
if (initCalled) {
throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
}
initCalled = true;
box = {
set: true,
value: await init(di),
};
},
get: () => {
if (!initCalled) {
throw new Error(`InitializableState(${id}) has not been initialized yet`);
}
if (box.set === false) {
throw new Error(`InitializableState(${id}) has not finished initializing`);
}
return box.value;
},
};
},
injectionToken,
});
}