All files / src/computed-channel duplicate-channel-observer-guard.injectable.ts

100% Statements 42/42
100% Branches 8/8
100% Functions 3/3
100% Lines 42/42

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 431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 90x 90x 90x 90x 90x 90x 90x 47x 47x 47x 47x 47x 47x 47x 47x 47x 2x 2x 2x 2x 2x 2x 47x 90x 90x 90x 90x 1x 1x 1x  
import { onLoadOfApplicationInjectionToken } from "@k8slens/application";
import { pipeline } from "@ogre-tools/fp";
import { getInjectable } from "@ogre-tools/injectable";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import { filter, groupBy, nth, map, toPairs } from "lodash/fp";
import { reaction } from "mobx";
import { computedChannelObserverInjectionToken } from "./computed-channel.injectable";
 
export const duplicateChannelObserverGuardInjectable = getInjectable({
  id: "duplicate-channel-observer-guard",
 
  instantiate: (di) => {
    const computedInjectMany = di.inject(computedInjectManyInjectable);
 
    return {
      run: () => {
        reaction(
          () => computedInjectMany(computedChannelObserverInjectionToken).get(),
          (observers) => {
            const duplicateObserverChannelIds = pipeline(
              observers,
              groupBy((observer) => observer.channel.id),
              toPairs,
              filter(([, channelObservers]) => channelObservers.length > 1),
              map(nth(0)),
            );
 
            if (duplicateObserverChannelIds.length) {
              throw new Error(
                `Tried to register duplicate channel observer for channels "${duplicateObserverChannelIds.join(
                  '", "',
                )}"`,
              );
            }
          },
        );
      },
    };
  },
 
  injectionToken: onLoadOfApplicationInjectionToken,
});