1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const EventEmitter = require('events');
|
17 | const {helper, debugError} = require('./helper');
|
18 | const {ExecutionContext, JSHandle} = require('./ExecutionContext');
|
19 |
|
20 | class Worker extends EventEmitter {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | constructor(client, url, consoleAPICalled, exceptionThrown) {
|
28 | super();
|
29 | this._client = client;
|
30 | this._url = url;
|
31 | this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
|
32 |
|
33 | let jsHandleFactory;
|
34 | this._client.once('Runtime.executionContextCreated', event => {return (fn => {
|
35 | const gen = fn.call(this);
|
36 | return new Promise((resolve, reject) => {
|
37 | function step(key, arg) {
|
38 | let info, value;
|
39 | try {
|
40 | info = gen[key](arg);
|
41 | value = info.value;
|
42 | } catch (error) {
|
43 | reject(error);
|
44 | return;
|
45 | }
|
46 | if (info.done) {
|
47 | resolve(value);
|
48 | } else {
|
49 | return Promise.resolve(value).then(
|
50 | value => {
|
51 | step('next', value);
|
52 | },
|
53 | err => {
|
54 | step('throw', err);
|
55 | });
|
56 | }
|
57 | }
|
58 | return step('next');
|
59 | });
|
60 | })(function*(){
|
61 | jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
|
62 | const executionContext = new ExecutionContext(client, event.context, jsHandleFactory, null);
|
63 | this._executionContextCallback(executionContext);
|
64 | });});
|
65 |
|
66 | this._client.send('Runtime.enable', {}).catch(debugError);
|
67 |
|
68 | this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory)));
|
69 | this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
|
70 | }
|
71 |
|
72 | |
73 |
|
74 |
|
75 | url() {
|
76 | return this._url;
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 | executionContext() {return (fn => {
|
83 | const gen = fn.call(this);
|
84 | return new Promise((resolve, reject) => {
|
85 | function step(key, arg) {
|
86 | let info, value;
|
87 | try {
|
88 | info = gen[key](arg);
|
89 | value = info.value;
|
90 | } catch (error) {
|
91 | reject(error);
|
92 | return;
|
93 | }
|
94 | if (info.done) {
|
95 | resolve(value);
|
96 | } else {
|
97 | return Promise.resolve(value).then(
|
98 | value => {
|
99 | step('next', value);
|
100 | },
|
101 | err => {
|
102 | step('throw', err);
|
103 | });
|
104 | }
|
105 | }
|
106 | return step('next');
|
107 | });
|
108 | })(function*(){
|
109 | return this._executionContextPromise;
|
110 | });}
|
111 |
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 | evaluate(pageFunction, ...args) {return (fn => {
|
118 | const gen = fn.call(this);
|
119 | return new Promise((resolve, reject) => {
|
120 | function step(key, arg) {
|
121 | let info, value;
|
122 | try {
|
123 | info = gen[key](arg);
|
124 | value = info.value;
|
125 | } catch (error) {
|
126 | reject(error);
|
127 | return;
|
128 | }
|
129 | if (info.done) {
|
130 | resolve(value);
|
131 | } else {
|
132 | return Promise.resolve(value).then(
|
133 | value => {
|
134 | step('next', value);
|
135 | },
|
136 | err => {
|
137 | step('throw', err);
|
138 | });
|
139 | }
|
140 | }
|
141 | return step('next');
|
142 | });
|
143 | })(function*(){
|
144 | return ((yield this._executionContextPromise)).evaluate(pageFunction, ...args);
|
145 | });}
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 | evaluateHandle(pageFunction, ...args) {return (fn => {
|
153 | const gen = fn.call(this);
|
154 | return new Promise((resolve, reject) => {
|
155 | function step(key, arg) {
|
156 | let info, value;
|
157 | try {
|
158 | info = gen[key](arg);
|
159 | value = info.value;
|
160 | } catch (error) {
|
161 | reject(error);
|
162 | return;
|
163 | }
|
164 | if (info.done) {
|
165 | resolve(value);
|
166 | } else {
|
167 | return Promise.resolve(value).then(
|
168 | value => {
|
169 | step('next', value);
|
170 | },
|
171 | err => {
|
172 | step('throw', err);
|
173 | });
|
174 | }
|
175 | }
|
176 | return step('next');
|
177 | });
|
178 | })(function*(){
|
179 | return ((yield this._executionContextPromise)).evaluateHandle(pageFunction, ...args);
|
180 | });}
|
181 | }
|
182 |
|
183 | module.exports = {Worker};
|
184 | helper.tracePublicAPI(Worker);
|