Skip to content

Commit

Permalink
Add error hander
Browse files Browse the repository at this point in the history
  • Loading branch information
monochromer committed Dec 13, 2023
1 parent f14b26f commit 500a04f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 7 additions & 3 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { formatFileSize, parseFileName } from './utils.js';
const worker = new Worker(new URL('worker.js', import.meta.url))

function subsetFont(file, options) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const opts = {
'desubroutinize': false,
'no-hinting': false,
Expand All @@ -29,9 +29,13 @@ function subsetFont(file, options) {
worker.addEventListener('message', function onWorkerMessage(event) {
const { type, payload } = event.data

if (type === 'file' && fileId === payload.fileId) {
if (fileId === payload.fileId) {
worker.removeEventListener('message', onWorkerMessage)
resolve(payload)
if (type === 'file' ) {
resolve(payload)
} else if (type === 'error') {
reject(payload.error)
}
}
})
})
Expand Down
11 changes: 9 additions & 2 deletions scripts/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ pythonSourcePromise = loadPythonSource()
.catch(console.error)

self.addEventListener('message', async (event) => {
try {
const { type, payload } = event.data
const { type, payload } = event.data

try {
if (type === 'file') {
const pythonSource = await pythonSourcePromise
const fileBuffer = await payload.file.arrayBuffer()
Expand All @@ -87,5 +87,12 @@ self.addEventListener('message', async (event) => {
}
} catch (error) {
console.error(error)
self.postMessage({
type: 'error',
payload: {
fileId: payload.fileId,
error
}
})
}
})

0 comments on commit 500a04f

Please sign in to comment.