| OLD | NEW |
| 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 WebInspector.FormatterWorkerPool = class { | 7 Common.FormatterWorkerPool = class { |
| 8 constructor() { | 8 constructor() { |
| 9 this._taskQueue = []; | 9 this._taskQueue = []; |
| 10 /** @type {!Map<!WebInspector.Worker, ?WebInspector.FormatterWorkerPool.Task
>} */ | 10 /** @type {!Map<!Common.Worker, ?Common.FormatterWorkerPool.Task>} */ |
| 11 this._workerTasks = new Map(); | 11 this._workerTasks = new Map(); |
| 12 } | 12 } |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * @return {!WebInspector.Worker} | 15 * @return {!Common.Worker} |
| 16 */ | 16 */ |
| 17 _createWorker() { | 17 _createWorker() { |
| 18 var worker = new WebInspector.Worker('formatter_worker'); | 18 var worker = new Common.Worker('formatter_worker'); |
| 19 worker.onmessage = this._onWorkerMessage.bind(this, worker); | 19 worker.onmessage = this._onWorkerMessage.bind(this, worker); |
| 20 worker.onerror = this._onWorkerError.bind(this, worker); | 20 worker.onerror = this._onWorkerError.bind(this, worker); |
| 21 return worker; | 21 return worker; |
| 22 } | 22 } |
| 23 | 23 |
| 24 _processNextTask() { | 24 _processNextTask() { |
| 25 if (!this._taskQueue.length) | 25 if (!this._taskQueue.length) |
| 26 return; | 26 return; |
| 27 | 27 |
| 28 var freeWorker = this._workerTasks.keysArray().find(worker => !this._workerT
asks.get(worker)); | 28 var freeWorker = this._workerTasks.keysArray().find(worker => !this._workerT
asks.get(worker)); |
| 29 if (!freeWorker && this._workerTasks.size < WebInspector.FormatterWorkerPool
.MaxWorkers) | 29 if (!freeWorker && this._workerTasks.size < Common.FormatterWorkerPool.MaxWo
rkers) |
| 30 freeWorker = this._createWorker(); | 30 freeWorker = this._createWorker(); |
| 31 if (!freeWorker) | 31 if (!freeWorker) |
| 32 return; | 32 return; |
| 33 | 33 |
| 34 var task = this._taskQueue.shift(); | 34 var task = this._taskQueue.shift(); |
| 35 this._workerTasks.set(freeWorker, task); | 35 this._workerTasks.set(freeWorker, task); |
| 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 {!WebInspector.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); |
| 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 : null); |
| 53 } | 53 } |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * @param {!WebInspector.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(?MessageEvent)} callback |
| 75 */ | 75 */ |
| 76 runChunkedTask(methodName, params, callback) { | 76 runChunkedTask(methodName, params, callback) { |
| 77 var task = new WebInspector.FormatterWorkerPool.Task(methodName, params, cal
lback, true); | 77 var task = new Common.FormatterWorkerPool.Task(methodName, params, callback,
true); |
| 78 this._taskQueue.push(task); | 78 this._taskQueue.push(task); |
| 79 this._processNextTask(); | 79 this._processNextTask(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 /** | 82 /** |
| 83 * @param {string} methodName | 83 * @param {string} methodName |
| 84 * @param {!Object<string, string>} params | 84 * @param {!Object<string, string>} params |
| 85 * @return {!Promise<?MessageEvent>} | 85 * @return {!Promise<?MessageEvent>} |
| 86 */ | 86 */ |
| 87 runTask(methodName, params) { | 87 runTask(methodName, params) { |
| 88 var callback; | 88 var callback; |
| 89 var promise = new Promise(fulfill => callback = fulfill); | 89 var promise = new Promise(fulfill => callback = fulfill); |
| 90 var task = new WebInspector.FormatterWorkerPool.Task(methodName, params, cal
lback, false); | 90 var task = new Common.FormatterWorkerPool.Task(methodName, params, callback,
false); |
| 91 this._taskQueue.push(task); | 91 this._taskQueue.push(task); |
| 92 this._processNextTask(); | 92 this._processNextTask(); |
| 93 return promise; | 93 return promise; |
| 94 } | 94 } |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 WebInspector.FormatterWorkerPool.MaxWorkers = 2; | 97 Common.FormatterWorkerPool.MaxWorkers = 2; |
| 98 | 98 |
| 99 /** | 99 /** |
| 100 * @unrestricted | 100 * @unrestricted |
| 101 */ | 101 */ |
| 102 WebInspector.FormatterWorkerPool.Task = class { | 102 Common.FormatterWorkerPool.Task = class { |
| 103 /** | 103 /** |
| 104 * @param {string} method | 104 * @param {string} method |
| 105 * @param {!Object<string, string>} params | 105 * @param {!Object<string, string>} params |
| 106 * @param {function(?MessageEvent)} callback | 106 * @param {function(?MessageEvent)} callback |
| 107 * @param {boolean=} isChunked | 107 * @param {boolean=} isChunked |
| 108 */ | 108 */ |
| 109 constructor(method, params, callback, isChunked) { | 109 constructor(method, params, callback, isChunked) { |
| 110 this.method = method; | 110 this.method = method; |
| 111 this.params = params; | 111 this.params = params; |
| 112 this.callback = callback; | 112 this.callback = callback; |
| 113 this.isChunked = isChunked; | 113 this.isChunked = isChunked; |
| 114 } | 114 } |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 /** @type {!WebInspector.FormatterWorkerPool} */ | 117 /** @type {!Common.FormatterWorkerPool} */ |
| 118 WebInspector.formatterWorkerPool; | 118 Common.formatterWorkerPool; |
| OLD | NEW |