Skip to content

Commit

Permalink
feat: use webpack top var to specify a location to load web workers from
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Dec 19, 2023
1 parent 49d79ab commit 79222e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"build": "npx rimraf dist && npm run html && npm run docs && npm run build:types && npm run build:webpack && npm run build:site",
"build:webpack": "NODE_ENV=production webpack",
"build:types": "tsc",
"build:site": "cp dist/*function-plot.js site/js",
"build:site": "cp dist/*function-plot.js site/js && cp dist/*function-plot.js.map site/js",
"start": "webpack-dev-server",
"docs": "typedoc --excludeExternals --externalPattern \"**/node_modules/**\" --out site/docs src/index.ts",
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ registerGraphType('scatter', scatter)
registerGraphType('text', text)

// Web workers initializer.
function withWebWorkers(nWorkers = 8) {
function withWebWorkers(nWorkers = 8, publicPath = window.location.href) {
// @ts-ignore
global.__webpack_public_path__ = publicPath
globals.workerPool = new IntervalWorkerPool(nWorkers)
}

Expand Down
15 changes: 5 additions & 10 deletions src/samplers/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@ async function asyncInterval1d({
)
}

const samples: IntervalSamplerResultGroup = []
let allWorkersDone: Array<ArrayBuffer>
try {
allWorkersDone = await Promise.all(promises)
} catch (err) {
// This run was invalidated by a new run.
;(samples as any).scaledDx = 0
return [samples]
}
// Transfer the typed array back to the main thread.
const allWorkersDone = await Promise.all(promises)

// Transfer the typed arrays back to the main thread.
for (let i = 0; i < allWorkersDone.length; i += 1) {
interval2dTypedArrayGroups[i] = new Float32Array(allWorkersDone[i])
}

const samples: IntervalSamplerResultGroup = []
for (let i = 0; i < interval2dTypedArrayGroups.length; i += 1) {
const group = interval2dTypedArrayGroups[i]
for (let j = 0; j < group.length; j += 4) {
Expand Down
4 changes: 3 additions & 1 deletion src/samplers/interval_worker_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class IntervalWorkerPool {
while (this.hasWork()) {
const task = this.tasks.shift()
if (!task.valid) {
this.rejects[task.nTask](new Error('This task is no longer valid'))
// This task is no longer valid (because there's a newer task)
// resolve with the input value.
this.resolves[task.nTask](task.interval2d.buffer)
continue
}
const idleWorker = this.idleWorkers.shift()
Expand Down

0 comments on commit 79222e6

Please sign in to comment.