diff --git a/packages/core/src/common/initializable-state/create.test.ts b/packages/core/src/common/initializable-state/create.test.ts index 5bd1d6d461..d93848cdcc 100644 --- a/packages/core/src/common/initializable-state/create.test.ts +++ b/packages/core/src/common/initializable-state/create.test.ts @@ -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, 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; - - 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; diff --git a/packages/core/src/common/initializable-state/create.ts b/packages/core/src/common/initializable-state/create.ts index fc13e8d949..760a97e907 100644 --- a/packages/core/src/common/initializable-state/create.ts +++ b/packages/core/src/common/initializable-state/create.ts @@ -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 { - id: string; - init: (di: DiContainerForInjection) => Promise | T; - injectionToken?: InjectionToken, void>; -} - -export interface InitializableState { - get: () => T; - init: () => Promise; -} - -type InitializableStateValue = - | { set: false } - | { set: true; value: T } ; - export interface Initializable { readonly rootId: string; readonly stateToken: InjectionToken; @@ -96,43 +81,3 @@ export const getInjectablesForInitializable = ({ initializationInjectable, }; }; - -export function createInitializableState(args: CreateInitializableStateArgs): Injectable, unknown, void> { - const { id, init, injectionToken } = args; - - return getInjectable({ - id, - instantiate: (di) => { - let box: InitializableStateValue = { - 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, - }); -}