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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 45x 45x 45x 45x 45x 45x 45x 45x 45x 24x 24x 24x 1x 1x 1x 1x 23x 23x 23x 23x 23x 45x 45x 8x 8x 8x 8x 8x 8x 45x 26x 26x 26x 26x 26x 8x 8x 8x 26x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 4x 4x 4x 13x 13x 1x 1x 1x 1x 1x | import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import { enlistMessageChannelListenerInjectionToken } from "../message/enlist-message-channel-listener-injection-token";
import { getStartableStoppable, StartableStoppable } from "@k8slens/startable-stoppable";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import { IComputedValue, reaction } from "mobx";
import {
MessageChannel,
messageChannelListenerInjectionToken,
} from "../message/message-channel-listener-injection-token";
import {
RequestChannel,
requestChannelListenerInjectionToken,
} from "../request/request-channel-listener-injection-token";
import { enlistRequestChannelListenerInjectionToken } from "../request/enlist-request-channel-listener-injection-token";
export type ListeningOfChannels = StartableStoppable;
export const listeningOfChannelsInjectionToken = getInjectionToken<ListeningOfChannels>({
id: "listening-of-channels-injection-token",
});
const listening = <
T extends { id: string; channel: MessageChannel<any> | RequestChannel<any, any> },
>(
channelListeners: IComputedValue<T[]>,
enlistChannelListener: (listener: T) => () => void,
getId: (listener: T) => string,
) => {
const listenerDisposers = new Map<string, () => void>();
const reactionDisposer = reaction(
() => channelListeners.get(),
(newValues, oldValues = []) => {
const addedListeners = newValues.filter(
(newValue) => !oldValues.some((oldValue) => oldValue.id === newValue.id),
);
const removedListeners = oldValues.filter(
(oldValue) => !newValues.some((newValue) => newValue.id === oldValue.id),
);
addedListeners.forEach((listener) => {
const id = getId(listener);
if (listenerDisposers.has(id)) {
throw new Error(
`Tried to add listener for channel "${listener.channel.id}" but listener already exists.`,
);
}
const disposer = enlistChannelListener(listener);
listenerDisposers.set(id, disposer);
});
removedListeners.forEach((listener) => {
const dispose = listenerDisposers.get(getId(listener));
dispose?.();
listenerDisposers.delete(getId(listener));
});
},
{ fireImmediately: true },
);
return () => {
reactionDisposer();
listenerDisposers.forEach((dispose) => dispose());
};
};
const listeningOfChannelsInjectable = getInjectable({
id: "listening-of-channels",
instantiate: (di) => {
const enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
const enlistRequestChannelListener = di.inject(enlistRequestChannelListenerInjectionToken);
const computedInjectMany = di.inject(computedInjectManyInjectable);
const messageChannelListeners = computedInjectMany(messageChannelListenerInjectionToken);
const requestChannelListeners = computedInjectMany(requestChannelListenerInjectionToken);
return getStartableStoppable("listening-of-channels", () => {
const stopListeningOfMessageChannels = listening(
messageChannelListeners,
enlistMessageChannelListener,
(x) => x.id,
);
const stopListeningOfRequestChannels = listening(
requestChannelListeners,
enlistRequestChannelListener,
(x) => x.channel.id,
);
return () => {
stopListeningOfMessageChannels();
stopListeningOfRequestChannels();
};
});
},
injectionToken: listeningOfChannelsInjectionToken,
});
export default listeningOfChannelsInjectable;
|