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

Introduce and use AsyncSyncBox

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-09-12 11:21:22 -04:00
parent 8a291cde9e
commit dbd8e3bebc
5 changed files with 112 additions and 33 deletions

View File

@ -0,0 +1,53 @@
/**
* 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 CreateAsyncSyncBoxArgs<T> {
id: string;
init: (di: DiContainerForInjection) => Promise<T>;
}
type AsyncSyncBoxValue<T> = { set: false } | { set: true; value: T };
export function createAsyncSyncBox<T>(args: CreateAsyncSyncBoxArgs<T>) {
const { id, init } = args;
return getInjectable({
id,
instantiate: (di) => {
let box: AsyncSyncBoxValue<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;
},
};
},
});
}

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { createAsyncSyncBox } from "../../../common/async-sync/create";
import { requestFromChannelInjectionToken } from "../../../common/utils/channel/request-from-channel-injection-token";
import { buildVersionChannel } from "../../../common/vars/build-semantic-version.injectable";
const buildVersionAsyncSyncBoxInjectable = createAsyncSyncBox({
id: "build-version",
init: (di) => {
const requestFromChannel = di.inject(requestFromChannelInjectionToken);
return requestFromChannel(buildVersionChannel);
},
});
export default buildVersionAsyncSyncBoxInjectable;

View File

@ -0,0 +1,19 @@
/**
* 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 { buildVersionInjectionToken } from "../../../common/vars/build-semantic-version.injectable";
import buildVersionAsyncSyncBoxInjectable from "./box.injectable";
const buildVersionInjectable = getInjectable({
id: "build-version",
instantiate: (di) => {
const buildVersionAsyncSyncBox = di.inject(buildVersionAsyncSyncBoxInjectable);
return buildVersionAsyncSyncBox.get();
},
injectionToken: buildVersionInjectionToken,
});
export default buildVersionInjectable;

View File

@ -0,0 +1,21 @@
/**
* 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 { beforeFrameStartsInjectionToken } from "../../before-frame-starts/before-frame-starts-injection-token";
import buildVersionAsyncSyncBoxInjectable from "./box.injectable";
const initializeBuildVersionAsyncSyncBoxInjectable = getInjectable({
id: "initialize-build-version-async-sync-box",
instantiate: (di) => {
const buildVersionAsyncSyncBox = di.inject(buildVersionAsyncSyncBoxInjectable);
return {
run: () => buildVersionAsyncSyncBox.init(),
};
},
injectionToken: beforeFrameStartsInjectionToken,
});
export default initializeBuildVersionAsyncSyncBoxInjectable;

View File

@ -1,33 +0,0 @@
/**
* 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 { runInAction } from "mobx";
import { requestFromChannelInjectionToken } from "../../common/utils/channel/request-from-channel-injection-token";
import { buildVersionChannel, buildVersionInjectionToken } from "../../common/vars/build-semantic-version.injectable";
import { beforeFrameStartsInjectionToken } from "../before-frame-starts/before-frame-starts-injection-token";
const setupBuildVersionInjectable = getInjectable({
id: "setup-build-version",
instantiate: (di) => {
const requestFromChannel = di.inject(requestFromChannelInjectionToken);
return {
run: async () => {
const buildVersion = await requestFromChannel(buildVersionChannel);
runInAction(() => {
di.register(getInjectable({
id: "build-version",
instantiate: () => buildVersion,
injectionToken: buildVersionInjectionToken,
}));
});
},
};
},
injectionToken: beforeFrameStartsInjectionToken,
});
export default setupBuildVersionInjectable;