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:
parent
8a291cde9e
commit
dbd8e3bebc
53
src/common/async-sync/create.ts
Normal file
53
src/common/async-sync/create.ts
Normal 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;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
19
src/renderer/vars/build-version/box.injectable.ts
Normal file
19
src/renderer/vars/build-version/box.injectable.ts
Normal 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;
|
||||||
19
src/renderer/vars/build-version/build-version.injectable.ts
Normal file
19
src/renderer/vars/build-version/build-version.injectable.ts
Normal 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;
|
||||||
21
src/renderer/vars/build-version/init.injectable.ts
Normal file
21
src/renderer/vars/build-version/init.injectable.ts
Normal 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;
|
||||||
@ -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;
|
|
||||||
Loading…
Reference in New Issue
Block a user