From b73fb6489e57c5c2915d476cd4fbe008a1ccaf28 Mon Sep 17 00:00:00 2001 From: Niklas Gruhn Date: Wed, 18 Nov 2020 22:32:24 +0100 Subject: [PATCH] fix(worker-prop): cant instantiate arrow functions When the function passed via the `worker` prop is an arrow function than we can't construct instances of it with the `new` keyword. The problem probably went unnoticed because in many implementations arrow functions are transpiled to regular functions. Issue: #197 --- src/components/QrcodeStream.vue | 4 ++-- src/misc/scanner.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/QrcodeStream.vue b/src/components/QrcodeStream.vue index c8138354..7fbfb4a0 100644 --- a/src/components/QrcodeStream.vue +++ b/src/components/QrcodeStream.vue @@ -29,7 +29,7 @@ import { keepScanning } from "../misc/scanner.js"; import { thinSquare } from "../misc/track-func.js"; import Camera from "../misc/camera.js"; import CommonAPI from "../mixins/CommonAPI.vue"; -import Worker from "../worker/jsqr.js"; +import spawnWorker from "../worker/jsqr.js"; export default { name: "qrcode-stream", @@ -58,7 +58,7 @@ export default { worker: { type: Function, - default: Worker + default: spawnWorker } }, diff --git a/src/misc/scanner.js b/src/misc/scanner.js index d613c29e..c1716598 100644 --- a/src/misc/scanner.js +++ b/src/misc/scanner.js @@ -1,7 +1,7 @@ import { eventOn } from "callforth"; -export async function scan(Worker, imageData) { - const worker = new Worker(); +export const scan = async (spawnWorker, imageData) => { + const worker = spawnWorker(); worker.postMessage(imageData, [imageData.data.buffer]); @@ -16,14 +16,14 @@ export async function scan(Worker, imageData) { * Continuously extracts frames from camera stream and tries to read * potentially pictured QR codes. */ -export function keepScanning(Worker, camera, options) { +export const keepScanning = (spawnWorker, camera, options) => { const { detectHandler, locateHandler, minDelay } = options; let contentBefore = null; let locationBefore = null; let lastScanned = performance.now(); - const worker = new Worker(); + const worker = spawnWorker(); // If worker can't process frames fast enough, memory will quickly full up. // Make sure to process only one frame at a time.