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

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: address comments 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, *)} 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 = 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<*>}
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<*>}
112 */
113 parseJSONRelaxed(content) {
114 return this._runTask('parseJSONRelaxed', {content: content});
115 }
116
117 /**
118 * @param {string} content
119 * @return {!Promise<!Array<!Common.FormatterWorkerPool.SCSSRule>>}
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 {*} 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 {*} 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 /**
233 * @typedef {{startLine: number, startColumn: number, endLine: number, endColumn : number}}
234 */
235 Common.FormatterWorkerPool.TextRange;
236
237 Common.FormatterWorkerPool.CSSProperty = class {
238 constructor() {
239 /** @type {string} */
240 this.name;
241 /** @type {!Common.FormatterWorkerPool.TextRange} */
242 this.nameRange;
243 /** @type {string} */
244 this.value;
245 /** @type {!Common.FormatterWorkerPool.TextRange} */
246 this.valueRange;
247 /** @type {!Common.FormatterWorkerPool.TextRange} */
248 this.range;
249 /** @type {(boolean|undefined)} */
250 this.disabled;
251 }
252 };
253
254 Common.FormatterWorkerPool.CSSStyleRule = class {
255 constructor() {
256 /** @type {string} */
257 this.selectorText;
258 /** @type {!Common.FormatterWorkerPool.TextRange} */
259 this.styleRange;
260 /** @type {number} */
261 this.lineNumber;
262 /** @type {number} */
263 this.columnNumber;
264 /** @type {!Array.<!Common.FormatterWorkerPool.CSSProperty>} */
265 this.properties;
266 }
267 };
268
269 /**
270 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}}
271 */
272 Common.FormatterWorkerPool.CSSAtRule;
273
274 /**
275 * @typedef {(Common.FormatterWorkerPool.CSSStyleRule|Common.FormatterWorkerPool .CSSAtRule)}
276 */
277 Common.FormatterWorkerPool.CSSRule;
278
279 Common.FormatterWorkerPool.SCSSProperty = class {
280 constructor() {
281 /** @type {!Common.FormatterWorkerPool.TextRange} */
282 this.range;
283 /** @type {!Common.FormatterWorkerPool.TextRange} */
284 this.name;
285 /** @type {!Common.FormatterWorkerPool.TextRange} */
286 this.value;
287 /** @type {boolean} */
288 this.disabled;
289 }
290 };
291
292 Common.FormatterWorkerPool.SCSSRule = class {
293 constructor() {
294 /** @type {!Array<!Common.FormatterWorkerPool.TextRange>} */
295 this.selectors;
296 /** @type {!Array<!Common.FormatterWorkerPool.SCSSProperty>} */
297 this.properties;
298 /** @type {!Common.FormatterWorkerPool.TextRange} */
299 this.styleRange;
300 }
301 };
302
117 /** @type {!Common.FormatterWorkerPool} */ 303 /** @type {!Common.FormatterWorkerPool} */
118 Common.formatterWorkerPool; 304 Common.formatterWorkerPool;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698