Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(474)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/common/FormatterWorkerPool.js

Issue 2570263002: DevTools: introduce API for the Common.FormatterWorkerPool. (Closed)
Patch Set: rebaseline Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 /** 4 /**
5 * @unrestricted 5 * @unrestricted
6 */ 6 */
7 Common.FormatterWorkerPool = class { 7 Common.FormatterWorkerPool = class {
8 constructor() { 8 constructor() {
9 this._taskQueue = []; 9 this._taskQueue = [];
10 /** @type {!Map<!Common.Worker, ?Common.FormatterWorkerPool.Task>} */ 10 /** @type {!Map<!Common.Worker, ?Common.FormatterWorkerPool.Task>} */
(...skipping 25 matching lines...) Expand all
36 freeWorker.postMessage({method: task.method, params: task.params}); 36 freeWorker.postMessage({method: task.method, params: task.params});
37 } 37 }
38 38
39 /** 39 /**
40 * @param {!Common.Worker} worker 40 * @param {!Common.Worker} worker
41 * @param {!MessageEvent} event 41 * @param {!MessageEvent} event
42 */ 42 */
43 _onWorkerMessage(worker, event) { 43 _onWorkerMessage(worker, event) {
44 var task = this._workerTasks.get(worker); 44 var task = this._workerTasks.get(worker);
45 if (task.isChunked && event.data && !event.data['isLastChunk']) { 45 if (task.isChunked && event.data && !event.data['isLastChunk']) {
46 task.callback(event); 46 task.callback(event.data);
47 return; 47 return;
48 } 48 }
49 49
50 this._workerTasks.set(worker, null); 50 this._workerTasks.set(worker, null);
51 this._processNextTask(); 51 this._processNextTask();
52 task.callback(event.data ? event : null); 52 task.callback(event.data ? event.data : null);
53 } 53 }
54 54
55 /** 55 /**
56 * @param {!Common.Worker} worker 56 * @param {!Common.Worker} worker
57 * @param {!Event} event 57 * @param {!Event} event
58 */ 58 */
59 _onWorkerError(worker, event) { 59 _onWorkerError(worker, event) {
60 console.error(event); 60 console.error(event);
61 var task = this._workerTasks.get(worker); 61 var task = this._workerTasks.get(worker);
62 worker.terminate(); 62 worker.terminate();
63 this._workerTasks.delete(worker); 63 this._workerTasks.delete(worker);
64 64
65 var newWorker = this._createWorker(); 65 var newWorker = this._createWorker();
66 this._workerTasks.set(newWorker, null); 66 this._workerTasks.set(newWorker, null);
67 this._processNextTask(); 67 this._processNextTask();
68 task.callback(null); 68 task.callback(null);
69 } 69 }
70 70
71 /** 71 /**
72 * @param {string} methodName 72 * @param {string} methodName
73 * @param {!Object<string, string>} params 73 * @param {!Object<string, string>} params
74 * @param {function(?MessageEvent)} callback 74 * @param {function(boolean, ?Object)} callback
75 */ 75 */
76 runChunkedTask(methodName, params, callback) { 76 _runChunkedTask(methodName, params, callback) {
77 var task = new Common.FormatterWorkerPool.Task(methodName, params, callback, true); 77 var task = new Common.FormatterWorkerPool.Task(methodName, params, onData, t rue);
78 this._taskQueue.push(task); 78 this._taskQueue.push(task);
79 this._processNextTask(); 79 this._processNextTask();
80
81 /**
82 * @param {?Object} data
83 */
84 function onData(data) {
85 if (!data) {
86 callback(true, null);
87 return;
88 }
89 var isLastChunk = !!data['isLastChunk'];
90 var chunk = /** @type {?Object} */ (data['chunk']);
91 callback(isLastChunk, chunk);
92 }
80 } 93 }
81 94
82 /** 95 /**
83 * @param {string} methodName 96 * @param {string} methodName
84 * @param {!Object<string, string>} params 97 * @param {!Object<string, string>} params
85 * @return {!Promise<?MessageEvent>} 98 * @return {!Promise<?Object>}
dgozman 2016/12/14 19:32:59 *
lushnikov 2016/12/14 23:36:56 Done.
86 */ 99 */
87 runTask(methodName, params) { 100 _runTask(methodName, params) {
88 var callback; 101 var callback;
89 var promise = new Promise(fulfill => callback = fulfill); 102 var promise = new Promise(fulfill => callback = fulfill);
90 var task = new Common.FormatterWorkerPool.Task(methodName, params, callback, false); 103 var task = new Common.FormatterWorkerPool.Task(methodName, params, callback, false);
91 this._taskQueue.push(task); 104 this._taskQueue.push(task);
92 this._processNextTask(); 105 this._processNextTask();
93 return promise; 106 return promise;
94 } 107 }
108
109 /**
110 * @param {string} content
111 * @return {!Promise<?Object>}
dgozman 2016/12/14 19:32:59 *
lushnikov 2016/12/14 23:36:56 Done.
112 */
113 relaxedJSONParser(content) {
dgozman 2016/12/14 19:32:59 parseJSONRelaxed
lushnikov 2016/12/14 23:36:56 Done.
114 return this._runTask('relaxedJSONParser', {content: content});
115 }
116
117 /**
118 * @param {string} content
119 * @return {!Promise<!Array<!Object>>}
dgozman 2016/12/14 19:32:59 Let's add types.
lushnikov 2016/12/14 23:36:56 Done.
120 */
121 parseSCSS(content) {
122 return this._runTask('parseSCSS', {content: content}).then(rules => rules || []);
123 }
124
125 /**
126 * @param {string} mimeType
127 * @param {string} content
128 * @param {string} indentString
129 * @return {!Promise<?Common.FormatterWorkerPool.FormatResult>}
130 */
131 format(mimeType, content, indentString) {
132 var parameters = {mimeType: mimeType, content: content, indentString: indent String};
133 return /** @type {!Promise<?Common.FormatterWorkerPool.FormatResult>} */ (th is._runTask('format', parameters));
134 }
135
136 /**
137 * @param {string} content
138 * @return {!Promise<!Array<!{name: string, offset: number}>>}
139 */
140 javaScriptIdentifiers(content) {
141 return this._runTask('javaScriptIdentifiers', {content: content}).then(ids = > ids || []);
142 }
143
144 /**
145 * @param {string} content
146 * @return {!Promise<string>}
147 */
148 evaluatableJavaScriptSubstring(content) {
149 return this._runTask('evaluatableJavaScriptSubstring', {content: content}).t hen(text => text || '');
150 }
151
152 /**
153 * @param {string} content
154 * @param {function(boolean, !Array<!Common.FormatterWorkerPool.CSSRule>)} cal lback
155 */
156 parseCSS(content, callback) {
157 this._runChunkedTask('parseCSS', {content: content}, onDataChunk);
158
159 /**
160 * @param {boolean} isLastChunk
161 * @param {?Object} data
162 */
163 function onDataChunk(isLastChunk, data) {
164 var rules = /** @type {!Array<!Common.FormatterWorkerPool.CSSRule>} */ (da ta || []);
165 callback(isLastChunk, rules);
166 }
167 }
168
169 /**
170 * @param {string} content
171 * @param {function(boolean, !Array<!Common.FormatterWorkerPool.JSOutlineItem> )} callback
172 */
173 javaScriptOutline(content, callback) {
174 this._runChunkedTask('javaScriptOutline', {content: content}, onDataChunk);
175
176 /**
177 * @param {boolean} isLastChunk
178 * @param {?Object} data
179 */
180 function onDataChunk(isLastChunk, data) {
181 var items = /** @type {!Array.<!Common.FormatterWorkerPool.JSOutlineItem>} */ (data || []);
182 callback(isLastChunk, items);
183 }
184 }
95 }; 185 };
96 186
97 Common.FormatterWorkerPool.MaxWorkers = 2; 187 Common.FormatterWorkerPool.MaxWorkers = 2;
98 188
99 /** 189 /**
100 * @unrestricted 190 * @unrestricted
101 */ 191 */
102 Common.FormatterWorkerPool.Task = class { 192 Common.FormatterWorkerPool.Task = class {
103 /** 193 /**
104 * @param {string} method 194 * @param {string} method
105 * @param {!Object<string, string>} params 195 * @param {!Object<string, string>} params
106 * @param {function(?MessageEvent)} callback 196 * @param {function(?MessageEvent)} callback
107 * @param {boolean=} isChunked 197 * @param {boolean=} isChunked
108 */ 198 */
109 constructor(method, params, callback, isChunked) { 199 constructor(method, params, callback, isChunked) {
110 this.method = method; 200 this.method = method;
111 this.params = params; 201 this.params = params;
112 this.callback = callback; 202 this.callback = callback;
113 this.isChunked = isChunked; 203 this.isChunked = isChunked;
114 } 204 }
115 }; 205 };
116 206
207 Common.FormatterWorkerPool.FormatResult = class {
208 constructor() {
209 /** @type {string} */
210 this.content;
211 /** @type {!Common.FormatterWorkerPool.FormatMapping} */
212 this.mapping;
213 }
214 };
215
216 /** @typedef {{original: !Array<number>, formatted: !Array<number>}} */
217 Common.FormatterWorkerPool.FormatMapping;
218
219 Common.FormatterWorkerPool.JSOutlineItem = class {
220 constructor() {
221 /** @type {string} */
222 this.name;
223 /** @type {(string|undefined)} */
224 this.arguments;
225 /** @type {number} */
226 this.line;
227 /** @type {number} */
228 this.column;
229 }
230 };
231
232 Common.FormatterWorkerPool.CSSStyleRule = class {
233 constructor() {
234 /** @type {string} */
235 this.selectorText;
236 /** @type {!Common.FormatterWorkerPool.CSSRange} */
237 this.styleRange;
238 /** @type {number} */
239 this.lineNumber;
240 /** @type {number} */
241 this.columnNumber;
242 /** @type {!Array.<!Common.FormatterWorkerPool.CSSProperty>} */
243 this.properties;
244 }
245 };
246
247 /**
248 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}}
249 */
250 Common.FormatterWorkerPool.CSSAtRule;
251
252 /**
253 * @typedef {(Common.FormatterWorkerPool.CSSStyleRule|Common.FormatterWorkerPool .CSSAtRule)}
254 */
255 Common.FormatterWorkerPool.CSSRule;
256
257 /**
258 * @typedef {{startLine: number, startColumn: number, endLine: number, endColumn : number}}
259 */
260 Common.FormatterWorkerPool.CSSRange;
261
262 Common.FormatterWorkerPool.CSSProperty = class {
263 constructor() {
264 /** @type {string} */
265 this.name;
266 /** @type {!Common.FormatterWorkerPool.CSSRange} */
267 this.nameRange;
268 /** @type {string} */
269 this.value;
270 /** @type {!Common.FormatterWorkerPool.CSSRange} */
271 this.valueRange;
272 /** @type {!Common.FormatterWorkerPool.CSSRange} */
273 this.range;
274 /** @type {(boolean|undefined)} */
275 this.disabled;
276 }
277 };
278
117 /** @type {!Common.FormatterWorkerPool} */ 279 /** @type {!Common.FormatterWorkerPool} */
118 Common.formatterWorkerPool; 280 Common.formatterWorkerPool;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698