Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/utils/imageBlurrer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,30 @@

import { generateFilePath } from '@nextcloud/router'

const worker = new Worker(generateFilePath('spreed', '', 'js/image-blurrer-worker.js'))
let worker

const pendingResults = {}
let pendingResultsNextId = 0

worker.onmessage = function(message) {
const pendingResult = pendingResults[message.data.id]
if (!pendingResult) {
console.debug('No pending result for blurring image with id ' + message.data.id)
function loadWorker() {
try {
worker = new Worker(generateFilePath('spreed', '', 'js/image-blurrer-worker.js'))
worker.onmessage = function(message) {
const pendingResult = pendingResults[message.data.id]
if (!pendingResult) {
console.debug('No pending result for blurring image with id ' + message.data.id)

return
}
return
}

pendingResult(message.data.blurredImageAsDataUrl)
pendingResult(message.data.blurredImageAsDataUrl)

delete pendingResults[message.data.id]
delete pendingResults[message.data.id]
}
} catch (exception) {
worker = null
console.error('Image blurrer worker could not be loaded', exception)
}
}

function blurSync(image, width, height, blurRadius) {
Expand All @@ -58,6 +66,14 @@ export default function blur(image, width, height, blurRadius) {
return blurSync(image, width, height, blurRadius)
}

if (worker === undefined) {
loadWorker()
}

if (!worker) {
return blurSync(image, width, height, blurRadius)
}

const id = pendingResultsNextId

pendingResultsNextId++
Expand Down