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

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

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 291x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 5x 5x 8x 8x 8x 8x 1x 1x 8x 8x 1x 1x 1x 1x 1x  
import { getInjectable } from "@ogre-tools/injectable";
import type { IpcMainEvent } from "electron";
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
import { enlistMessageChannelListenerInjectionToken } from "@k8slens/messaging";
 
const enlistMessageChannelListenerInjectable = getInjectable({
  id: "enlist-message-channel-listener",
 
  instantiate: (di) => {
    const ipcMain = di.inject(ipcMainInjectable);
 
    return ({ channel, handler }) => {
      const nativeOnCallback = (nativeEvent: IpcMainEvent, message: any) => {
        handler(message, { frameId: nativeEvent.frameId, processId: nativeEvent.processId });
      };
 
      ipcMain.on(channel.id, nativeOnCallback);
 
      return () => {
        ipcMain.off(channel.id, nativeOnCallback);
      };
    };
  },
 
  injectionToken: enlistMessageChannelListenerInjectionToken,
});
 
export default enlistMessageChannelListenerInjectable;