Fix race condition with multiple opens

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2021-07-28 14:55:02 -04:00
1 parent 1f0e79cee8
commit 2ef2cbb2df
5 files changed
+97 -35

No files matched your search

@@ -23,13 +23,17 @@ import { catalogCategoryRegistry } from "../catalog/catalog-category-registry";
import { CatalogEntity, CatalogEntityAddMenuContext, CatalogEntityContextMenuContext, CatalogEntityMetadata, CatalogEntityStatus } from "../catalog";
import { clusterActivateHandler, clusterDeleteHandler, clusterDisconnectHandler, navigateToClusterHandler } from "../cluster-ipc";
import { ClusterStore } from "../cluster-store";
import { onNewWindowForClusterHandler, requestMain } from "../ipc";
import { requestMain } from "../ipc";
import { CatalogCategory, CatalogCategorySpec } from "../catalog";
import { addClusterURL } from "../routes";
import { app } from "electron";
import type { CatalogEntitySpec } from "../catalog/catalog-entity";
import type { CatalogEntityActionContext, CatalogEntitySpec } from "../catalog/catalog-entity";
import { HotbarStore } from "../hotbar-store";
export interface KubernetesClusterActionContextArgs extends Record<string, any> {
newWindow?: boolean;
}
export interface KubernetesClusterPrometheusMetrics {
address?: {
namespace: string;
@@ -90,8 +94,12 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
}
}
async onRun() {
await requestMain(navigateToClusterHandler, this.getId());
async onRun(context: CatalogEntityActionContext<KubernetesClusterActionContextArgs>) {
await requestMain(
navigateToClusterHandler,
this.getId(),
context.args?.newWindow ?? false,
);
}
onDetailsOpen(): void {
@@ -103,20 +111,28 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
}
async onContextMenuOpen(context: CatalogEntityContextMenuContext) {
if (this.status.phase === "connected" || this.status.phase === "disconnected") {
context.menuItems.push(
{
title: "Open",
icon: "open_in_full",
onClick: () => this.onRun(),
},
{
title: "Open in new window",
icon: "launch",
onClick: () => requestMain(onNewWindowForClusterHandler, this.getId()),
},
);
}
const runContext = () => ({
navigate: context.navigate,
setCommandPaletteContext: context.setCommandPaletteContext,
});
context.menuItems.push(
{
title: "Open",
icon: "open_in_full",
onClick: () => this.onRun(runContext()),
},
{
title: "Open in new window",
icon: "launch",
onClick: () => this.onRun({
...runContext(),
args: {
newWindow: true,
}
}),
},
);
if (!this.metadata.source || this.metadata.source === "local") {
context.menuItems.push(
+2 -1
View File
@@ -95,9 +95,10 @@ export interface CatalogEntityStatus {
active?: boolean;
}
export interface CatalogEntityActionContext {
export interface CatalogEntityActionContext<T extends Record<string, any> = Record<string, any>> {
navigate: (url: string) => void;
setCommandPaletteContext: (context?: CatalogEntity) => void;
args?: T;
}
export interface CatalogEntityContextMenu {
+1
View File
@@ -51,6 +51,7 @@ export * from "./tar";
export * from "./toggle-set";
export * from "./toJS";
export * from "./type-narrowing";
export * from "./map-helpers";
import * as iter from "./iter";
+35
View File
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Get the current value ascociated with `key` or
* @param map The map to interact with
* @param key The key to optionally initialize
* @param value The value to use if the key needs initializing
* @returns The current value in the map
*/
export function getOrInsert<K, V>(map: Map<K, V>, key: K, value: V): V {
if (!map.has(key)) {
map.set(key, value);
}
return map.get(key);
}