Add better type safety around Channels (#6133)

* Add better type safety around Channels

- Reallow non-JSON serializable complex types

- Remove unnecessary JSON serialization step to IPC

- Remove unnecesary injectable-ization of MessageChannel and
  RequestChannel declarations

- Add channel listener injectable getter function to remove general need
  for type casting and improve type safety

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add check to verify data in IPC related tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix implementation in light of new safety check in tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix listener ID clash potential

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Switch to not forcing split of request channel handlers being seperate from the listener decl

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move message channels to changed signature too

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix not registering listener

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix test imports

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Refactor listening of different channel kinds

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add test covering new checking code for request channel handlers

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-10-13 07:50:19 -04:00
1 parent d96918c966
commit 5a5eda935b
115 files changed
+949 -1568

No files matched your search

@@ -1,20 +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 type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-injection-token";
import { requestChannelInjectionToken } from "../../../../common/utils/channel/request-channel-injection-token";
export type ClearClusterAsDeletingChannel = RequestChannel<ClusterId, void>;
const clearClusterAsDeletingChannelInjectable = getInjectable({
id: "clear-cluster-as-deleting-channel",
instantiate: (): ClearClusterAsDeletingChannel => ({
id: "clear-cluster-as-deleting",
}),
injectionToken: requestChannelInjectionToken,
});
export default clearClusterAsDeletingChannelInjectable;
@@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
export type ClearClusterAsDeletingChannel = RequestChannel<ClusterId, void>;
export const clearClusterAsDeletingChannel: ClearClusterAsDeletingChannel = {
id: "clear-cluster-as-deleting",
};
@@ -1,20 +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 type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-injection-token";
import { requestChannelInjectionToken } from "../../../../common/utils/channel/request-channel-injection-token";
export type DeleteClusterChannel = RequestChannel<ClusterId, void>;
const deleteClusterChannelInjectable = getInjectable({
id: "delete-cluster-channel",
instantiate: (): DeleteClusterChannel => ({
id: "delete-cluster",
}),
injectionToken: requestChannelInjectionToken,
});
export default deleteClusterChannelInjectable;
@@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
export type DeleteClusterChannel = RequestChannel<ClusterId, void>;
export const deleteClusterChannel: DeleteClusterChannel = {
id: "delete-cluster",
};
@@ -1,20 +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 type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-injection-token";
import { requestChannelInjectionToken } from "../../../../common/utils/channel/request-channel-injection-token";
export type SetClusterAsDeletingChannel = RequestChannel<ClusterId, void>;
const setClusterAsDeletingChannelInjectable = getInjectable({
id: "set-cluster-as-deleting-channel",
instantiate: (): SetClusterAsDeletingChannel => ({
id: "set-cluster-as-deleting",
}),
injectionToken: requestChannelInjectionToken,
});
export default setClusterAsDeletingChannelInjectable;
@@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
export type SetClusterAsDeletingChannel = RequestChannel<ClusterId, void>;
export const setClusterAsDeletingChannel: SetClusterAsDeletingChannel = {
id: "set-cluster-as-deleting",
};
@@ -1,23 +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 { requestChannelListenerInjectionToken } from "../../../../common/utils/channel/request-channel-listener-injection-token";
import clustersThatAreBeingDeletedInjectable from "../../../../main/cluster/are-being-deleted.injectable";
import clearClusterAsDeletingChannelInjectable from "../common/clear-as-deleting-channel.injectable";
const clearClusterAsDeletingChannelHandlerInjectable = getInjectable({
id: "clear-cluster-as-deleting-channel-handler",
instantiate: (di) => {
const clustersThatAreBeingDeleted = di.inject(clustersThatAreBeingDeletedInjectable);
return {
channel: di.inject(clearClusterAsDeletingChannelInjectable),
handler: (clusterId) => clustersThatAreBeingDeleted.delete(clusterId),
};
},
injectionToken: requestChannelListenerInjectionToken,
});
export default clearClusterAsDeletingChannelHandlerInjectable;
@@ -0,0 +1,20 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import clustersThatAreBeingDeletedInjectable from "../../../../main/cluster/are-being-deleted.injectable";
import { getRequestChannelListenerInjectable } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
import { clearClusterAsDeletingChannel } from "../common/clear-as-deleting-channel";
const clearClusterAsDeletingChannelListenerInjectable = getRequestChannelListenerInjectable({
channel: clearClusterAsDeletingChannel,
handler: (di) => {
const clustersThatAreBeingDeleted = di.inject(clustersThatAreBeingDeletedInjectable);
return (clusterId) => {
clustersThatAreBeingDeleted.delete(clusterId);
};
},
});
export default clearClusterAsDeletingChannelListenerInjectable;
@@ -1,56 +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 appEventBusInjectable from "../../../../common/app-event-bus/app-event-bus.injectable";
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import directoryForLensLocalStorageInjectable from "../../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
import deleteFileInjectable from "../../../../common/fs/delete-file.injectable";
import joinPathsInjectable from "../../../../common/path/join-paths.injectable";
import { requestChannelListenerInjectionToken } from "../../../../common/utils/channel/request-channel-listener-injection-token";
import deleteClusterChannelInjectable from "../common/delete-channel.injectable";
const deleteClusterChannelHandlerInjectable = getInjectable({
id: "delete-cluster-channel-handler",
instantiate: (di) => {
const appEventBus = di.inject(appEventBusInjectable);
const clusterStore = di.inject(clusterStoreInjectable);
const clusterFrames = di.inject(clusterFramesInjectable);
const joinPaths = di.inject(joinPathsInjectable);
const directoryForLensLocalStorage = di.inject(directoryForLensLocalStorageInjectable);
const deleteFile = di.inject(deleteFileInjectable);
return {
channel: di.inject(deleteClusterChannelInjectable),
handler: async (clusterId) =>{
appEventBus.emit({ name: "cluster", action: "remove" });
const cluster = clusterStore.getById(clusterId);
if (!cluster) {
return;
}
cluster.disconnect();
clusterFrames.delete(cluster.id);
// Remove from the cluster store as well, this should clear any old settings
clusterStore.clusters.delete(cluster.id);
try {
// remove the local storage file
const localStorageFilePath = joinPaths(directoryForLensLocalStorage, `${cluster.id}.json`);
await deleteFile(localStorageFilePath);
} catch {
// ignore error
}
},
};
},
injectionToken: requestChannelListenerInjectionToken,
});
export default deleteClusterChannelHandlerInjectable;
@@ -0,0 +1,51 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import appEventBusInjectable from "../../../../common/app-event-bus/app-event-bus.injectable";
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import directoryForLensLocalStorageInjectable from "../../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
import deleteFileInjectable from "../../../../common/fs/delete-file.injectable";
import joinPathsInjectable from "../../../../common/path/join-paths.injectable";
import { getRequestChannelListenerInjectable } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
import { deleteClusterChannel } from "../common/delete-channel";
const deleteClusterChannelListenerInjectable = getRequestChannelListenerInjectable({
channel: deleteClusterChannel,
handler: (di) => {
const appEventBus = di.inject(appEventBusInjectable);
const clusterStore = di.inject(clusterStoreInjectable);
const clusterFrames = di.inject(clusterFramesInjectable);
const joinPaths = di.inject(joinPathsInjectable);
const directoryForLensLocalStorage = di.inject(directoryForLensLocalStorageInjectable);
const deleteFile = di.inject(deleteFileInjectable);
return async (clusterId) => {
appEventBus.emit({ name: "cluster", action: "remove" });
const cluster = clusterStore.getById(clusterId);
if (!cluster) {
return;
}
cluster.disconnect();
clusterFrames.delete(cluster.id);
// Remove from the cluster store as well, this should clear any old settings
clusterStore.clusters.delete(cluster.id);
try {
// remove the local storage file
const localStorageFilePath = joinPaths(directoryForLensLocalStorage, `${cluster.id}.json`);
await deleteFile(localStorageFilePath);
} catch {
// ignore error
}
};
},
});
export default deleteClusterChannelListenerInjectable;
@@ -1,23 +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 { requestChannelListenerInjectionToken } from "../../../../common/utils/channel/request-channel-listener-injection-token";
import clustersThatAreBeingDeletedInjectable from "../../../../main/cluster/are-being-deleted.injectable";
import setClusterAsDeletingChannelInjectable from "../common/set-as-deleting-channel.injectable";
const setClusterAsDeletingChannelHandlerInjectable = getInjectable({
id: "set-cluster-as-deleting-channel-handler",
instantiate: (di) => {
const clustersThatAreBeingDeleted = di.inject(clustersThatAreBeingDeletedInjectable);
return {
channel: di.inject(setClusterAsDeletingChannelInjectable),
handler: (clusterId) => clustersThatAreBeingDeleted.add(clusterId),
};
},
injectionToken: requestChannelListenerInjectionToken,
});
export default setClusterAsDeletingChannelHandlerInjectable;
@@ -0,0 +1,20 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import clustersThatAreBeingDeletedInjectable from "../../../../main/cluster/are-being-deleted.injectable";
import { getRequestChannelListenerInjectable } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
import { setClusterAsDeletingChannel } from "../common/set-as-deleting-channel";
const setClusterAsDeletingChannelHandlerInjectable = getRequestChannelListenerInjectable({
channel: setClusterAsDeletingChannel,
handler: (di) => {
const clustersThatAreBeingDeleted = di.inject(clustersThatAreBeingDeletedInjectable);
return (clusterId) => {
clustersThatAreBeingDeleted.add(clusterId);
};
},
});
export default setClusterAsDeletingChannelHandlerInjectable;
@@ -5,7 +5,7 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { ClusterId } from "../../../../common/cluster-types";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import clearClusterAsDeletingChannelInjectable from "../common/clear-as-deleting-channel.injectable";
import { clearClusterAsDeletingChannel } from "../common/clear-as-deleting-channel";
export type RequestClearClusterAsDeleting = (clusterId: ClusterId) => Promise<void>;
@@ -13,7 +13,6 @@ const requestClearClusterAsDeletingInjectable = getInjectable({
id: "request-clear-cluster-as-deleting",
instantiate: (di): RequestClearClusterAsDeleting => {
const requestChannel = di.inject(requestFromChannelInjectable);
const clearClusterAsDeletingChannel = di.inject(clearClusterAsDeletingChannelInjectable);
return (clusterId) => requestChannel(clearClusterAsDeletingChannel, clusterId);
},
@@ -5,7 +5,7 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { ClusterId } from "../../../../common/cluster-types";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import deleteClusterChannelInjectable from "../common/delete-channel.injectable";
import { deleteClusterChannel } from "../common/delete-channel";
export type RequestDeleteCluster = (clusterId: ClusterId) => Promise<void>;
@@ -13,7 +13,6 @@ const requestDeleteClusterInjectable = getInjectable({
id: "request-delete-cluster",
instantiate: (di): RequestDeleteCluster => {
const requestChannel = di.inject(requestFromChannelInjectable);
const deleteClusterChannel = di.inject(deleteClusterChannelInjectable);
return (clusterId) => requestChannel(deleteClusterChannel, clusterId);
},
@@ -5,7 +5,7 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { ClusterId } from "../../../../common/cluster-types";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import setClusterAsDeletingChannelInjectable from "../common/set-as-deleting-channel.injectable";
import { setClusterAsDeletingChannel } from "../common/set-as-deleting-channel";
export type RequestSetClusterAsDeleting = (clusterId: ClusterId) => Promise<void>;
@@ -13,7 +13,6 @@ const requestSetClusterAsDeletingInjectable = getInjectable({
id: "request-set-cluster-as-deleting",
instantiate: (di): RequestSetClusterAsDeleting => {
const requestChannel = di.inject(requestFromChannelInjectable);
const setClusterAsDeletingChannel = di.inject(setClusterAsDeletingChannelInjectable);
return (clusterId) => requestChannel(setClusterAsDeletingChannel, clusterId);
},