1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/sync-box/provide-initial-values-for-sync-boxes.injectable.ts
Janne Savolainen f3e41954c5
Synchronize initial values of sync boxes when window starts
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-05-20 14:11:15 +03:00

41 lines
1.5 KiB
TypeScript

/**
* 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 { syncBoxInjectionToken } from "../../common/sync-box/sync-box-injection-token";
import getValueFromChannelInjectable from "../channel/get-value-from-channel.injectable";
import syncBoxInitialValueChannelInjectable from "../../common/sync-box/sync-box-initial-value-channel.injectable";
import assert from "assert";
const provideInitialValuesForSyncBoxesInjectable = getInjectable({
id: "provide-initial-values-for-sync-boxes",
instantiate: (di) => {
const syncBoxes = di.injectMany(syncBoxInjectionToken);
const getValueFromChannel = di.inject(getValueFromChannelInjectable);
const syncBoxInitialValueChannel = di.inject(syncBoxInitialValueChannelInjectable);
return {
run: async () => {
const initialValues = await getValueFromChannel(syncBoxInitialValueChannel);
assert(initialValues);
initialValues.forEach(({ id, value }) => {
const targetBox = syncBoxes.find(box => box.id === id);
if (targetBox) {
targetBox.set(value);
}
});
},
};
},
injectionToken: beforeFrameStartsInjectionToken,
});
export default provideInitialValuesForSyncBoxesInjectable;