All files / src/channel-listeners enlist-request-channel-listener.injectable.ts

100% Statements 31/31
100% Branches 5/5
100% Functions 3/3
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 13x 13x 1x 1x 1x 1x 1x  
import { getInjectable } from "@ogre-tools/injectable";
import type { IpcMainInvokeEvent } from "electron";
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
import type { RequestChannel, RequestChannelListener } from "@k8slens/messaging";
import { enlistRequestChannelListenerInjectionToken } from "@k8slens/messaging";
 
export type EnlistRequestChannelListener = <TChannel extends RequestChannel<unknown, unknown>>(
  listener: RequestChannelListener<TChannel>,
) => () => void;
 
const enlistRequestChannelListenerInjectable = getInjectable({
  id: "enlist-request-channel-listener-for-main",
 
  instantiate: (di): EnlistRequestChannelListener => {
    const ipcMain = di.inject(ipcMainInjectable);
 
    return ({ channel, handler }) => {
      const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => handler(request);
 
      ipcMain.handle(channel.id, nativeHandleCallback);
 
      return () => {
        ipcMain.off(channel.id, nativeHandleCallback);
      };
    };
  },
 
  injectionToken: enlistRequestChannelListenerInjectionToken,
});
 
export default enlistRequestChannelListenerInjectable;