1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/initializable-state/create.ts
Sebastian Malton 54f4c51791 Rename AsyncSyncBox to InitializableState
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-09-15 09:43:31 -04:00

54 lines
1.3 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainerForInjection } from "@ogre-tools/injectable";
import { getInjectable } from "@ogre-tools/injectable";
export interface CreateInitializableStateArgs<T> {
id: string;
init: (di: DiContainerForInjection) => Promise<T>;
}
type InitializableStateValue<T> = { set: false } | { set: true; value: T };
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>) {
const { id, init } = args;
return getInjectable({
id,
instantiate: (di) => {
let box: InitializableStateValue<T> = {
set: false,
};
let initCalled = false;
return {
init: async () => {
if (initCalled) {
throw new Error(`Cannot initialized AsyncSyncBox ${id}) more than once`);
}
initCalled = true;
box = {
set: true,
value: await init(di),
};
},
get: () => {
if (!initCalled) {
throw new Error(`AsyncSyncBox(${id}) has not been initialized yet`);
}
if (!box.set) {
throw new Error(`AsyncSyncBox(${id}) has not finished initializing`);
}
return box.value;
},
};
},
});
}