worker.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Worker = void 0;
  6. var _events = require("./events");
  7. var _channelOwner = require("./channelOwner");
  8. var _jsHandle = require("./jsHandle");
  9. var _utils = require("../utils");
  10. var _errors = require("./errors");
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class Worker extends _channelOwner.ChannelOwner {
  27. static from(worker) {
  28. return worker._object;
  29. }
  30. constructor(parent, type, guid, initializer) {
  31. super(parent, type, guid, initializer);
  32. this._page = void 0;
  33. // Set for web workers.
  34. this._context = void 0;
  35. // Set for service workers.
  36. this._closedScope = new _utils.LongStandingScope();
  37. this._channel.on('close', () => {
  38. if (this._page) this._page._workers.delete(this);
  39. if (this._context) this._context._serviceWorkers.delete(this);
  40. this.emit(_events.Events.Worker.Close, this);
  41. });
  42. this.once(_events.Events.Worker.Close, () => {
  43. var _this$_page;
  44. return this._closedScope.close(((_this$_page = this._page) === null || _this$_page === void 0 ? void 0 : _this$_page._closeErrorWithReason()) || new _errors.TargetClosedError());
  45. });
  46. }
  47. url() {
  48. return this._initializer.url;
  49. }
  50. async evaluate(pageFunction, arg) {
  51. (0, _jsHandle.assertMaxArguments)(arguments.length, 2);
  52. const result = await this._channel.evaluateExpression({
  53. expression: String(pageFunction),
  54. isFunction: typeof pageFunction === 'function',
  55. arg: (0, _jsHandle.serializeArgument)(arg)
  56. });
  57. return (0, _jsHandle.parseResult)(result.value);
  58. }
  59. async evaluateHandle(pageFunction, arg) {
  60. (0, _jsHandle.assertMaxArguments)(arguments.length, 2);
  61. const result = await this._channel.evaluateExpressionHandle({
  62. expression: String(pageFunction),
  63. isFunction: typeof pageFunction === 'function',
  64. arg: (0, _jsHandle.serializeArgument)(arg)
  65. });
  66. return _jsHandle.JSHandle.from(result.handle);
  67. }
  68. }
  69. exports.Worker = Worker;