zone-patch-electron.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2022 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. Zone.__load_patch('electron', (global, Zone, api) => {
  8. function patchArguments(target, name, source) {
  9. return api.patchMethod(target, name, (delegate) => (self, args) => {
  10. return delegate && delegate.apply(self, api.bindArguments(args, source));
  11. });
  12. }
  13. let { desktopCapturer, shell, CallbacksRegistry, ipcRenderer } = require('electron');
  14. if (!CallbacksRegistry) {
  15. try {
  16. // Try to load CallbacksRegistry class from @electron/remote src
  17. // since from electron 14+, the CallbacksRegistry is moved to @electron/remote
  18. // package and not exported to outside, so this is a hack to patch CallbacksRegistry.
  19. CallbacksRegistry =
  20. require('@electron/remote/dist/src/renderer/callbacks-registry').CallbacksRegistry;
  21. }
  22. catch (err) {
  23. }
  24. }
  25. // patch api in renderer process directly
  26. // desktopCapturer
  27. if (desktopCapturer) {
  28. patchArguments(desktopCapturer, 'getSources', 'electron.desktopCapturer.getSources');
  29. }
  30. // shell
  31. if (shell) {
  32. patchArguments(shell, 'openExternal', 'electron.shell.openExternal');
  33. }
  34. // patch api in main process through CallbackRegistry
  35. if (!CallbacksRegistry) {
  36. if (ipcRenderer) {
  37. patchArguments(ipcRenderer, 'on', 'ipcRenderer.on');
  38. }
  39. return;
  40. }
  41. patchArguments(CallbacksRegistry.prototype, 'add', 'CallbackRegistry.add');
  42. });