1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/shell-session/shell-environment-cache.injectable.ts
Sebastian Malton d293554c42 Get tests passing
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-10 15:18:10 -05:00

35 lines
1.2 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 type { IAsyncComputed } from "@ogre-tools/injectable-react";
import { asyncComputed } from "@ogre-tools/injectable-react";
import { when } from "mobx";
import { now } from "mobx-utils";
import type { ClusterId } from "../../common/cluster-types";
import { getOrInsert } from "../../common/utils";
export type ShellEnvironmentCache = (clusterId: string, builder: () => Promise<Partial<Record<string, string>>>) => Promise<Partial<Record<string, string>>>;
const shellEnvironmentCacheInjectable = getInjectable({
id: "shell-environment-cache",
instantiate: (): ShellEnvironmentCache => {
const cache = new Map<ClusterId, IAsyncComputed<Partial<Record<string, string>>>>();
return async (clusterId, builder) => {
const cacheLine = getOrInsert(cache, clusterId, asyncComputed(() => {
now(1000 * 60 * 10); // update every 10 minutes
return builder();
}));
await when(() => !cacheLine.pending.get());
return cacheLine.value.get();
};
},
});
export default shellEnvironmentCacheInjectable;