Skip to content

⬅️ Back to Table of Contents

📄 createWorkerBlobUrl

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 2

📚 Table of Contents

🛠️ File Location:

📂 packages/core/useWebWorkerFn/lib/createWorkerBlobUrl.ts

📦 Imports

Name Source
depsParser ./depsParser
jobRunner ./jobRunner

Functions

createWorkerBlobUrl(fn: Function, deps: string[], localDeps: Function[]): string

Converts the "fn" function into the syntax needed to be executed within a web worker

Parameters:

  • fn any: the function to run with web worker
  • deps any: array of strings, imported into the worker through "importScripts"
  • localDeps any: array of function, local dependencies

Returns: undefined a blob url, containing the code of "fn" as a string

Examples:

createWorkerBlobUrl((a,b) => a+b, [])
// return "onmessage=return Promise.resolve((a,b) => a + b)
.then(postMessage(['SUCCESS', result]))
.catch(postMessage(['ERROR', error])"
Raw JSDoc
/**
 * Converts the "fn" function into the syntax needed to be executed within a web worker
 *
 * @param fn the function to run with web worker
 * @param deps array of strings, imported into the worker through "importScripts"
 * @param localDeps array of function, local dependencies
 *
 * @returns a blob url, containing the code of "fn" as a string
 *
 * @example
 * createWorkerBlobUrl((a,b) => a+b, [])
 * // return "onmessage=return Promise.resolve((a,b) => a + b)
 * .then(postMessage(['SUCCESS', result]))
 * .catch(postMessage(['ERROR', error])"
 */

Calls:

  • depsParser (from ./depsParser)
  • URL.createObjectURL
Code
function createWorkerBlobUrl(fn: Function, deps: string[], localDeps: Function[]) {
  const blobCode = `${depsParser(deps, localDeps)}; onmessage=(${jobRunner})(${fn})`
  const blob = new Blob([blobCode], { type: 'text/javascript' })
  const url = URL.createObjectURL(blob)
  return url
}

Generated by Syntax Scribe