Move worker creation into worker-util.
This allows to more easily swap out how the worker is created.
Change-Id: I513918cb8f9e46e914e78c55405381fe9d0dd2d1
diff --git a/polygerrit-ui/app/services/highlight/highlight-service.ts b/polygerrit-ui/app/services/highlight/highlight-service.ts
index 414d3f8..2711ba5 100644
--- a/polygerrit-ui/app/services/highlight/highlight-service.ts
+++ b/polygerrit-ui/app/services/highlight/highlight-service.ts
@@ -10,7 +10,7 @@
SyntaxWorkerMessageType,
SyntaxLayerLine,
} from '../../types/syntax-worker-api';
-import {wrapUrl} from '../../utils/worker-util';
+import {createWorker} from '../../utils/worker-util';
import {Finalizable} from '../registry';
const hljsLibUrl = `${window.STATIC_RESOURCE_PATH}/bower_components/highlightjs/highlight.min.js`;
@@ -50,7 +50,7 @@
/** Allows tests to produce fake workers. */
protected createWorker() {
- return new Worker(wrapUrl(syntaxWorkerUrl));
+ return createWorker(syntaxWorkerUrl);
}
/** Creates, initializes and then moves a worker to the idle pool. */
diff --git a/polygerrit-ui/app/utils/worker-util.ts b/polygerrit-ui/app/utils/worker-util.ts
index d4df34d..e4f5d2e 100644
--- a/polygerrit-ui/app/utils/worker-util.ts
+++ b/polygerrit-ui/app/utils/worker-util.ts
@@ -10,7 +10,11 @@
* the fly and pull the actual worker via `importScripts()`. Apparently that
* is a well established pattern.
*/
-export function wrapUrl(url: string) {
+function wrapUrl(url: string) {
const content = `importScripts("${url}");`;
return URL.createObjectURL(new Blob([content], {type: 'text/javascript'}));
}
+
+export function createWorker(workerUrl: string): Worker {
+ return new Worker(wrapUrl(workerUrl));
+}