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

Fix runnables to be sync if init is sync

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-01 10:21:36 -04:00
parent 6ec38835d3
commit a06bd63701
3 changed files with 39 additions and 14 deletions

View File

@ -2,9 +2,7 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { initAppPathsOnMainInjectable } from "../../main/app-paths/impl.injectable"; import directoryForUserDataInjectable, { initDirectoryForUserDataOnMainInjectable, initDirectoryForUserDataOnRendererInjectable } from "./directory-for-user-data.injectable";
import { initAppPathsOnRendererInjectable } from "../../renderer/app-paths/impl.injectable";
import directoryForUserDataInjectable from "./directory-for-user-data.injectable";
import joinPathsInjectable from "../path/join-paths.injectable"; import joinPathsInjectable from "../path/join-paths.injectable";
import { createDependentInitializableState } from "../initializable-state/create-dependent"; import { createDependentInitializableState } from "../initializable-state/create-dependent";
@ -22,7 +20,10 @@ const {
return joinPaths(directoryForUserData.get(), "binaries"); return joinPaths(directoryForUserData.get(), "binaries");
}, },
initAfter: [initAppPathsOnMainInjectable, initAppPathsOnRendererInjectable], initAfter: [
initDirectoryForUserDataOnMainInjectable,
initDirectoryForUserDataOnRendererInjectable,
],
}); });
export { export {

View File

@ -47,19 +47,31 @@ export function createDependentInitializableState<T>(args: CreateDependentInitia
}); });
const initializers = initAfter.map(runnableInjectable => getInjectable({ const initializers = initAfter.map(runnableInjectable => getInjectable({
id: `initialize-${id}`, id: `initialize-${id}-during-${runnableInjectable.injectionToken?.id}`,
instantiate: (di) => ({ instantiate: (di) => ({
id: `initialize-${id}`, id: `initialize-${id}`,
run: async () => { run: (): void | Promise<void> => {
if (initCalled) { if (initCalled) {
throw new Error(`Cannot initialize InitializableState(${id}) more than once`); throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
} }
initCalled = true; initCalled = true;
const potentialValue = init(di);
if (potentialValue instanceof Promise) {
// This is done because we have to run syncronously if `init` is syncronous to prevent ordering issues
return (async () => {
box = { box = {
set: true, set: true,
value: await init(di), value: await potentialValue,
}; };
})();
} else {
box = {
set: true,
value: potentialValue,
};
}
}, },
runAfter: di.inject(runnableInjectable), runAfter: di.inject(runnableInjectable),
}), }),

View File

@ -63,10 +63,22 @@ export function createInitializableState<T>(args: CreateInitializableStateArgs<T
} }
initCalled = true; initCalled = true;
const potentialValue = init(di);
if (potentialValue instanceof Promise) {
// This is done because we have to run syncronously if `init` is syncronous to prevent ordering issues
return (async () => {
box = { box = {
set: true, set: true,
value: await init(di), value: await potentialValue,
}; };
})();
} else {
box = {
set: true,
value: potentialValue,
};
}
}, },
}), }),
injectionToken: when, injectionToken: when,