electron.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import type { Electron } from "./types/ipc";
  2. // type ElectronAPIsType =
  3. // TODO (MR):
  4. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5. const ElectronAPIs = (globalThis as unknown as any)[
  6. // eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unsafe-member-access
  7. "ElectronAPIs"
  8. ] as Electron;
  9. // /**
  10. // * Extend the global object's (`globalThis`) interface to state that it can
  11. // * potentially hold a property called `electron`. It will be injected by our
  12. // * preload.js script when we're running in the context of our desktop app.
  13. // */
  14. // declare global {
  15. // const electron: Electron | undefined;
  16. // }
  17. // export const globalElectron = globalThis.electron;
  18. export default ElectronAPIs;
  19. /**
  20. * A wrapper over a non-null assertion of `globalThis.electron`.
  21. *
  22. * This is useful where we have previously verified that the code path in which
  23. * we're running only executes when we're in electron (usually by directly
  24. * checking that `globalThis.electron` is defined somewhere up the chain).
  25. *
  26. * Generally, this should not be required - the check and the use should be
  27. * colocated, or the unwrapped non-null value saved somewhere. But sometimes
  28. * doing so requires code refactoring, so as an escape hatch we provide this
  29. * convenience function.
  30. *
  31. * It will throw if `globalThis.electron` is undefined.
  32. *
  33. * @see `global-electron.d.ts`.
  34. */
  35. export const ensureElectron = (): Electron => {
  36. const et = globalThis.electron;
  37. if (et) return et;
  38. throw new Error(
  39. "Attempting to assert globalThis.electron in a non-electron context",
  40. );
  41. };