All files / src/send-message-to-channel send-message-to-channel.injectable.ts

100% Statements 48/48
100% Branches 12/12
100% Functions 6/6
100% Lines 48/48

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 6x 6x 8x 8x 6x 5x 5x 14x 14x 5x 5x 5x 1x 1x 1x 1x 1x  
import { getInjectable } from "@ogre-tools/injectable";
import { pipeline } from "@ogre-tools/fp";
import { SendMessageToChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import getWebContentsInjectable from "./get-web-contents.injectable";
import { flatMap, reject } from "lodash/fp";
import type { WebContents } from "electron";
import frameIdsInjectable from "./frameIds.injectable";
 
const isDestroyed = (webContent: WebContents) => webContent.isDestroyed();
const isCrashed = (webContent: WebContents) => webContent.isCrashed();
 
const forEach =
  <T>(predicate: (item: T) => void) =>
  (items: T[]) =>
    items.forEach(predicate);
 
const sendMessageToChannelInjectable = getInjectable({
  id: "send-message-to-channel",
 
  instantiate: (di) => {
    const getWebContents = di.inject(getWebContentsInjectable);
    const frameIds = di.inject(frameIdsInjectable);
 
    return ((channel, message) => {
      pipeline(
        getWebContents(),
        reject(isDestroyed),
        reject(isCrashed),
 
        flatMap((webContent) => [
          (channelId: string, ...args: any[]) => webContent.send(channelId, ...args),
 
          ...[...frameIds].map(({ frameId, processId }) => (channelId: string, ...args: any[]) => {
            webContent.sendToFrame([frameId, processId], channelId, ...args);
          }),
        ]),
 
        forEach((send) => {
          send(channel.id, message);
        }),
      );
    }) as SendMessageToChannel;
  },
 
  injectionToken: sendMessageToChannelInjectionToken,
});
 
export default sendMessageToChannelInjectable;