1 import { isElectronMail } from '../helpers/desktop';
3 IPCInboxClientUpdateMessage,
4 IPCInboxDesktopFeature,
5 IPCInboxGetInfoMessage,
6 IPCInboxGetUserInfoMessage,
7 IPCInboxHostUpdateListener,
8 IPCInboxHostUpdateListenerRemover,
9 IPCInboxHostUpdateMessageType,
10 IPCInboxMessageBroker,
11 PayloadOfHostUpdateType,
12 } from './desktopTypes';
16 ipcInboxMessageBroker?: IPCInboxMessageBroker;
20 export function invokeInboxDesktopIPC({ type, payload }: IPCInboxClientUpdateMessage) {
21 if (isElectronMail && window.ipcInboxMessageBroker?.send) {
22 window.ipcInboxMessageBroker!.send!(type, payload);
26 export const canGetInboxDesktopInfo =
27 isElectronMail && !!window.ipcInboxMessageBroker && !!window.ipcInboxMessageBroker!.getInfo;
29 export const canListenInboxDesktopHostMessages =
30 isElectronMail && !!window.ipcInboxMessageBroker && !!window.ipcInboxMessageBroker!.on;
32 export function getInboxDesktopInfo<T extends IPCInboxGetInfoMessage['type']>(key: T) {
33 return window.ipcInboxMessageBroker!.getInfo!<T>(key);
36 export const getInboxDesktopUserInfo = <T extends IPCInboxGetUserInfoMessage['type']>(key: T, userID: string) => {
37 return window.ipcInboxMessageBroker!.getUserInfo!<T>(key, userID);
40 export const hasInboxDesktopFeature = (feature: IPCInboxDesktopFeature) =>
42 !!window.ipcInboxMessageBroker &&
43 !!window.ipcInboxMessageBroker!.hasFeature &&
44 window.ipcInboxMessageBroker!.hasFeature(feature);
46 export function addIPCHostUpdateListener<T extends IPCInboxHostUpdateMessageType>(
48 callback: (payload: PayloadOfHostUpdateType<T>) => void
49 ): IPCInboxHostUpdateListenerRemover {
50 // THIS NEEDS REFACTOR inda-refactor-001
51 // This shouldn't be needed, better to avoid it with custom type-safe event emmiter
53 // With generic T we make sure first correct callback type is added to
54 // correct event type. But the `on` function must accept union of callbacks.
55 const unsafeCallback = callback as IPCInboxHostUpdateListener;
56 return window.ipcInboxMessageBroker!.on!(eventType, unsafeCallback);