mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
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,
|
|
});
|