-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add workerize for cleaner interop with workers (#2321)
* feat: add workerize as a worker loader * fix: transfer large array instead of copy when sending/getting data from workers * feat: consume workerize-transferable to add transferable capability to workerize-loader * chore: remove copywrite headers for adopted code, remove dead code * fix: remove self-assignment * fix: add webworker lib for typedoc generation Co-authored-by: cognite-bulldozer[bot] <51074376+cognite-bulldozer[bot]@users.noreply.github.com>
- Loading branch information
1 parent
af815bc
commit 1ce4078
Showing
16 changed files
with
588 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
viewer/packages/utilities/src/workers/workerize-transferable/.eslintrc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/*! | ||
* Copyright 2022 Cognite AS | ||
*/ | ||
|
||
module.exports = { | ||
'rules': { | ||
'header/header': 'off' | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
viewer/packages/utilities/src/workers/workerize-transferable/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This is a consumed library from https://github.com/naoak/workerize-transferable | ||
|
||
The source code has been integrated such that ESM modules are properly translated in jest. |
7 changes: 7 additions & 0 deletions
7
viewer/packages/utilities/src/workers/workerize-transferable/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/*! | ||
* Adopted from https://github.com/naoak/workerize-transferable | ||
*/ | ||
export * from './message-types'; | ||
export * from './on-main'; | ||
export * from './on-worker'; | ||
export * from './util'; |
5 changes: 5 additions & 0 deletions
5
viewer/packages/utilities/src/workers/workerize-transferable/message-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/*! | ||
* Adopted from https://github.com/naoak/workerize-transferable | ||
*/ | ||
|
||
export const MESSAGE_TYPE_RPC_TRANSFERABLE = 'RPC_transferable'; |
57 changes: 57 additions & 0 deletions
57
viewer/packages/utilities/src/workers/workerize-transferable/on-main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*! | ||
* Adopted from https://github.com/naoak/workerize-transferable | ||
*/ | ||
import { MESSAGE_TYPE_RPC_TRANSFERABLE } from './message-types'; | ||
|
||
type Resolve = (value?: unknown) => void; | ||
type Reject = (reason?: any) => void; | ||
|
||
/** Options for worker method params */ | ||
export type WorkerMethodParamsOptions = { | ||
/** pick transferables from method params */ | ||
pickTransferablesFromParams?: (params: any) => any[]; | ||
}; | ||
|
||
/** | ||
* Setup worker methods which receive transferables from worker method params. This function should be executed on the main thread. | ||
* @param worker worker instance | ||
* @param methods an object whose key is method name and whose value is options how to pick transferables from method params | ||
*/ | ||
export function setupTransferableMethodsOnMain<WORKER extends Worker>( | ||
worker: WORKER, | ||
methods: { | ||
[x: string]: WorkerMethodParamsOptions; | ||
} | ||
): void { | ||
let c = 0; | ||
const callbacks: { | ||
[x: number]: [Resolve, Reject]; | ||
} = {}; | ||
worker.addEventListener('message', e => { | ||
const d = e.data; | ||
if (d.type !== MESSAGE_TYPE_RPC_TRANSFERABLE) { | ||
return; | ||
} | ||
const f = callbacks[d.id]; | ||
if (f) { | ||
delete callbacks[d.id]; | ||
if (d.error) { | ||
f[1](Object.assign(Error(d.error.message), d.error)); | ||
} else { | ||
f[0](d.result); | ||
} | ||
} | ||
}); | ||
Object.keys(methods).forEach(method => { | ||
(worker as any)[method] = (...params: any[]) => | ||
new Promise((a: Resolve, b: Reject) => { | ||
const id = ++c; | ||
callbacks[id] = [a, b]; | ||
const opts = methods[method]; | ||
worker.postMessage( | ||
{ type: MESSAGE_TYPE_RPC_TRANSFERABLE, id, method, params }, | ||
opts.pickTransferablesFromParams ? opts.pickTransferablesFromParams(params) : [] | ||
); | ||
}); | ||
}); | ||
} |
61 changes: 61 additions & 0 deletions
61
viewer/packages/utilities/src/workers/workerize-transferable/on-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*! | ||
* Adopted from https://github.com/naoak/workerize-transferable | ||
*/ | ||
import { MESSAGE_TYPE_RPC_TRANSFERABLE } from './message-types'; | ||
import { getGlobalThis } from './util'; | ||
|
||
/** Options for worker method result */ | ||
export type WorkerMethodResultOptions = { | ||
/** worker method */ | ||
fn: (...params: any[]) => any; | ||
|
||
/** pick transferables from method result */ | ||
pickTransferablesFromResult?: (result: any) => any[]; | ||
}; | ||
|
||
/** | ||
* Setup worker methods which return transferables. This function should be executed on the worker thread. | ||
* @param methods an object whose key is method name and whose value is options how to pick transferables from method result | ||
*/ | ||
export function setupTransferableMethodsOnWorker(methods: { [x: string]: WorkerMethodResultOptions }): void { | ||
const globals = getGlobalThis(); | ||
globals.addEventListener('message', e => { | ||
const { type, method, id, params } = e.data; | ||
let opts: WorkerMethodResultOptions; | ||
let p: Promise<any>; | ||
if (type === MESSAGE_TYPE_RPC_TRANSFERABLE && method) { | ||
if ((opts = methods[method])) { | ||
p = Promise.resolve().then(() => opts.fn(...params)); | ||
} else { | ||
p = Promise.reject('No such method'); | ||
} | ||
p.then(result => { | ||
globals.postMessage( | ||
{ type: MESSAGE_TYPE_RPC_TRANSFERABLE, id, result }, | ||
opts.pickTransferablesFromResult ? opts.pickTransferablesFromResult(result) : [] | ||
); | ||
}).catch(e => { | ||
let message; | ||
try { | ||
message = e.message.toString(); | ||
} catch (ex) { | ||
message = null; | ||
} | ||
const error: any = { message }; | ||
if (e.stack) { | ||
error.stack = e.stack; | ||
error.name = e.name; | ||
} | ||
if (e.status) { | ||
error.status = e.status; | ||
error.responseJson = e.responseJson; | ||
} | ||
globals.postMessage({ | ||
type: MESSAGE_TYPE_RPC_TRANSFERABLE, | ||
id, | ||
error | ||
}); | ||
}); | ||
} | ||
}); | ||
} |
Oops, something went wrong.