service-worker-main.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { IpcMainImpl } from '@electron/internal/browser/ipc-main-impl';
  2. const { ServiceWorkerMain } = process._linkedBinding('electron_browser_service_worker_main');
  3. Object.defineProperty(ServiceWorkerMain.prototype, 'ipc', {
  4. get () {
  5. const ipc = new IpcMainImpl();
  6. Object.defineProperty(this, 'ipc', { value: ipc });
  7. return ipc;
  8. }
  9. });
  10. ServiceWorkerMain.prototype.send = function (channel, ...args) {
  11. if (typeof channel !== 'string') {
  12. throw new TypeError('Missing required channel argument');
  13. }
  14. try {
  15. return this._send(false /* internal */, channel, args);
  16. } catch (e) {
  17. console.error('Error sending from ServiceWorkerMain: ', e);
  18. }
  19. };
  20. ServiceWorkerMain.prototype.startTask = function () {
  21. // TODO(samuelmaddock): maybe make timeout configurable in the future
  22. const hasTimeout = false;
  23. const { id, ok } = this._startExternalRequest(hasTimeout);
  24. if (!ok) {
  25. throw new Error('Unable to start service worker task.');
  26. }
  27. return {
  28. end: () => this._finishExternalRequest(id)
  29. };
  30. };
  31. module.exports = ServiceWorkerMain;