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

@@ -3,35 +3,29 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import type { RequestChannel } from "../../common/utils/channel/request-channel-injection-token";
import type { RequestChannelListener } from "../../common/utils/channel/request-channel-listener-injection-token";
import type { RequestChannel } from "../../common/utils/channel/request-channel-listener-injection-token";
import type { RequestFromChannel } from "../../common/utils/channel/request-from-channel-injection-token";
import enlistRequestChannelListenerInjectableInMain from "../../main/utils/channel/channel-listeners/enlist-request-channel-listener.injectable";
import type { RequestChannelListener } from "../../main/utils/channel/channel-listeners/listener-tokens";
import requestFromChannelInjectable from "../../renderer/utils/channel/request-from-channel.injectable";
export const overrideRequestingFromWindowToMain = (mainDi: DiContainer) => {
const requestChannelListenerFakesForMain = new Map<
string,
RequestChannelListener<RequestChannel<any, any>>
>();
string,
RequestChannelListener<RequestChannel<unknown, unknown>>
>();
mainDi.override(
enlistRequestChannelListenerInjectableInMain,
() => (listener) => {
if (requestChannelListenerFakesForMain.get(listener.channel.id)) {
if (requestChannelListenerFakesForMain.has(listener.channel.id)) {
throw new Error(
`Tried to enlist listener for channel "${listener.channel.id}", but it was already enlisted`,
);
}
requestChannelListenerFakesForMain.set(
listener.channel.id,
// TODO: Figure out typing
listener as unknown as RequestChannelListener<
RequestChannel<any, any>
>,
);
requestChannelListenerFakesForMain.set(listener.channel.id, listener);
return () => {
requestChannelListenerFakesForMain.delete(listener.channel.id);
@@ -43,7 +37,7 @@ export const overrideRequestingFromWindowToMain = (mainDi: DiContainer) => {
windowDi.override(
requestFromChannelInjectable,
() => async (channel, ...[request]) => {
() => (async (channel, request) => {
const requestListener = requestChannelListenerFakesForMain.get(channel.id);
if (!requestListener) {
@@ -53,7 +47,7 @@ export const overrideRequestingFromWindowToMain = (mainDi: DiContainer) => {
}
return requestListener.handler(request);
},
}) as RequestFromChannel,
);
};
};