type TaskData = {
[key: string]: Data | Record<string, Data>
- url: string
- id: string
+ taskUrl: string
+ taskId: string
}
type Task = {
buffers.push(d)
}
}
- const id = crypto.randomUUID()
+ const taskId = crypto.randomUUID()
const data: TaskData = {
...payload,
- url: this.#url,
- id
+ taskUrl: this.#url,
+ taskId
}
return new Promise((resolve, reject): void => {
const task: Task = {
resolve,
reject
}
- this.#tasks.set(id, task)
+ this.#tasks.set(taskId, task)
try {
console.log('host postMessage(data)', data)
BROWSER: this.#worker.postMessage(data, buffers)
#report (results: unknown): void {
if (results == null) return
- const { url, id, error, isLocked } = results as Record<string, unknown>
- if (url === this.#url) {
- if (typeof id !== 'string') {
+ const { taskUrl, taskId, error, isLocked } = results as Record<string, unknown>
+ if (taskUrl === this.#url) {
+ if (typeof taskId !== 'string') {
throw new Error('Vault worker job invalid ID')
}
if (typeof isLocked === 'boolean' && this.#isLocked !== isLocked) {
this.#isLocked = isLocked
this.dispatchEvent(new Event(isLocked ? LOCKED : UNLOCKED))
}
- if (id === 'autolock') return
- const task = this.#tasks.get(id)
+ if (taskId === 'autolock') return
+ const task = this.#tasks.get(taskId)
if (task == null) {
- throw new Error(`Vault worker returned results without an associated job for ID ${id}`)
+ throw new Error(`Vault worker returned results without an associated task for ID ${taskId}`)
}
const { resolve, reject } = task
if (error != null) {
throw new Error('Invalid data')
}
const data = event.data as Record<string, unknown>
- const { url, id } = data
- if (typeof id !== 'string') return
- BROWSER: if (url !== location.href) return
- NODE: if (url !== dec.toString(threadId)) return
+ const { taskUrl, taskId } = data
+ if (typeof taskId !== 'string') return
+ BROWSER: if (taskUrl !== location.href) return
+ NODE: if (taskUrl !== dec.toString(threadId)) return
NODE: if (parentPort == null) setTimeout(() => listener(event), 0)
const action = parseAction(data)
const keySalt = parseKeySalt(action, data)
}
}
}
- result.url = url
- result.id = id
+ result.taskUrl = taskUrl
+ result.taskId = taskId
console.log('worker postMessage(result)', result)
//@ts-expect-error
BROWSER: self.postMessage(result, transfer)
data = undefined
}
console.log('worker postMessage(error)', err)
- BROWSER: self.postMessage({ url, id, error: 'Failed to process Vault request', cause: err })
- NODE: parentPort?.postMessage({ data: { url, id, error: 'Failed to process Vault request', cause: err } })
+ BROWSER: self.postMessage({ taskUrl, taskId, error: 'Failed to process Vault request', cause: err })
+ NODE: parentPort?.postMessage({ data: { taskUrl, taskId, error: 'Failed to process Vault request', cause: err } })
})
}
BROWSER: self.addEventListener('message', listener)