@loaders.gl/worker-utils
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -8,3 +8,3 @@ import type { WorkerContext, Process, ProcessInBatches } from '../../types'; | ||
*/ | ||
export declare function createWorker(process: Process, processInBatches?: ProcessInBatches): void; | ||
export declare function createWorker(process: Process, processInBatches?: ProcessInBatches): Promise<void>; | ||
//# sourceMappingURL=create-worker.d.ts.map |
@@ -6,4 +6,4 @@ import AsyncQueue from "../async-queue/async-queue.js"; | ||
let options; | ||
export function createWorker(process, processInBatches) { | ||
if (!WorkerBody.inWorkerThread()) { | ||
export async function createWorker(process, processInBatches) { | ||
if (!(await WorkerBody.inWorkerThread())) { | ||
return; | ||
@@ -10,0 +10,0 @@ } |
@@ -17,3 +17,7 @@ import { assert } from "../env-utils/assert.js"; | ||
if (options._workerType === 'test') { | ||
url = `modules/${worker.module}/dist/${workerFile}`; | ||
if (isBrowser) { | ||
url = `modules/${worker.module}/dist/${workerFile}`; | ||
} else { | ||
url = `modules/${worker.module}/src/workers/${worker.id}-worker-node.ts`; | ||
} | ||
} | ||
@@ -20,0 +24,0 @@ if (!url) { |
@@ -7,6 +7,6 @@ import type { WorkerMessageType, WorkerMessagePayload } from '../../types'; | ||
/** Check that we are actually in a worker thread */ | ||
static inWorkerThread(): boolean; | ||
static inWorkerThread(): Promise<boolean>; | ||
static set onmessage(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any); | ||
static addEventListener(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any): void; | ||
static removeEventListener(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any): void; | ||
static addEventListener(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any): Promise<void>; | ||
static removeEventListener(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any): Promise<void>; | ||
/** | ||
@@ -17,4 +17,4 @@ * Send a message from a worker to creating thread (main thread) | ||
*/ | ||
static postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): void; | ||
static postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): Promise<void>; | ||
} | ||
//# sourceMappingURL=worker-body.d.ts.map |
import { getTransferList } from "../worker-utils/get-transfer-list.js"; | ||
function getParentPort() { | ||
async function getParentPort() { | ||
let parentPort; | ||
@@ -7,3 +7,11 @@ try { | ||
parentPort = globalThis.parentPort; | ||
} catch {} | ||
} catch { | ||
try { | ||
eval('globalThis.workerThreadsPromise = import(\'worker_threads\')'); | ||
const workerThreads = await globalThis.workerThreadsPromise; | ||
parentPort = workerThreads.parentPort; | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
return parentPort; | ||
@@ -13,8 +21,8 @@ } | ||
export default class WorkerBody { | ||
static inWorkerThread() { | ||
return typeof self !== 'undefined' || Boolean(getParentPort()); | ||
static async inWorkerThread() { | ||
return typeof self !== 'undefined' || Boolean(await getParentPort()); | ||
} | ||
static set onmessage(onMessage) { | ||
function handleMessage(message) { | ||
const parentPort = getParentPort(); | ||
async function handleMessage(message) { | ||
const parentPort = await getParentPort(); | ||
const { | ||
@@ -26,18 +34,19 @@ type, | ||
} | ||
const parentPort = getParentPort(); | ||
if (parentPort) { | ||
parentPort.on('message', handleMessage); | ||
parentPort.on('exit', () => console.debug('Node worker closing')); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
getParentPort().then(parentPort => { | ||
if (parentPort) { | ||
parentPort.on('message', handleMessage); | ||
parentPort.on('exit', () => console.debug('Node worker closing')); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
}); | ||
} | ||
static addEventListener(onMessage) { | ||
static async addEventListener(onMessage) { | ||
let onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
if (!onMessageWrapper) { | ||
onMessageWrapper = message => { | ||
onMessageWrapper = async message => { | ||
if (!isKnownMessage(message)) { | ||
return; | ||
} | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
const { | ||
@@ -50,3 +59,3 @@ type, | ||
} | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
@@ -58,6 +67,6 @@ console.error('not implemented'); | ||
} | ||
static removeEventListener(onMessage) { | ||
static async removeEventListener(onMessage) { | ||
const onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
onMessageWrapperMap.delete(onMessage); | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
@@ -69,3 +78,3 @@ console.error('not implemented'); | ||
} | ||
static postMessage(type, payload) { | ||
static async postMessage(type, payload) { | ||
const data = { | ||
@@ -77,3 +86,3 @@ source: 'loaders.gl', | ||
const transferList = getTransferList(payload); | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
@@ -80,0 +89,0 @@ parentPort.postMessage(data, transferList); |
@@ -106,3 +106,3 @@ "use strict"; | ||
// src/lib/worker-farm/worker-body.ts | ||
function getParentPort() { | ||
async function getParentPort() { | ||
let parentPort; | ||
@@ -113,2 +113,9 @@ try { | ||
} catch { | ||
try { | ||
eval("globalThis.workerThreadsPromise = import('worker_threads')"); | ||
const workerThreads = await globalThis.workerThreadsPromise; | ||
parentPort = workerThreads.parentPort; | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
@@ -120,4 +127,4 @@ return parentPort; | ||
/** Check that we are actually in a worker thread */ | ||
static inWorkerThread() { | ||
return typeof self !== "undefined" || Boolean(getParentPort()); | ||
static async inWorkerThread() { | ||
return typeof self !== "undefined" || Boolean(await getParentPort()); | ||
} | ||
@@ -128,23 +135,24 @@ /* | ||
static set onmessage(onMessage) { | ||
function handleMessage(message) { | ||
const parentPort3 = getParentPort(); | ||
const { type, payload } = parentPort3 ? message : message.data; | ||
async function handleMessage(message) { | ||
const parentPort2 = await getParentPort(); | ||
const { type, payload } = parentPort2 ? message : message.data; | ||
onMessage(type, payload); | ||
} | ||
const parentPort2 = getParentPort(); | ||
if (parentPort2) { | ||
parentPort2.on("message", handleMessage); | ||
parentPort2.on("exit", () => console.debug("Node worker closing")); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
getParentPort().then((parentPort2) => { | ||
if (parentPort2) { | ||
parentPort2.on("message", handleMessage); | ||
parentPort2.on("exit", () => console.debug("Node worker closing")); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
}); | ||
} | ||
static addEventListener(onMessage) { | ||
static async addEventListener(onMessage) { | ||
let onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
if (!onMessageWrapper) { | ||
onMessageWrapper = (message) => { | ||
onMessageWrapper = async (message) => { | ||
if (!isKnownMessage(message)) { | ||
return; | ||
} | ||
const parentPort3 = getParentPort(); | ||
const parentPort3 = await getParentPort(); | ||
const { type, payload } = parentPort3 ? message : message.data; | ||
@@ -154,3 +162,3 @@ onMessage(type, payload); | ||
} | ||
const parentPort2 = getParentPort(); | ||
const parentPort2 = await getParentPort(); | ||
if (parentPort2) { | ||
@@ -162,6 +170,6 @@ console.error("not implemented"); | ||
} | ||
static removeEventListener(onMessage) { | ||
static async removeEventListener(onMessage) { | ||
const onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
onMessageWrapperMap.delete(onMessage); | ||
const parentPort2 = getParentPort(); | ||
const parentPort2 = await getParentPort(); | ||
if (parentPort2) { | ||
@@ -178,6 +186,6 @@ console.error("not implemented"); | ||
*/ | ||
static postMessage(type, payload) { | ||
static async postMessage(type, payload) { | ||
const data = { source: "loaders.gl", type, payload }; | ||
const transferList = getTransferList(payload); | ||
const parentPort2 = getParentPort(); | ||
const parentPort2 = await getParentPort(); | ||
if (parentPort2) { | ||
@@ -199,4 +207,4 @@ parentPort2.postMessage(data, transferList); | ||
var options; | ||
function createWorker(process, processInBatches) { | ||
if (!WorkerBody.inWorkerThread()) { | ||
async function createWorker(process, processInBatches) { | ||
if (!await WorkerBody.inWorkerThread()) { | ||
return; | ||
@@ -203,0 +211,0 @@ } |
@@ -109,3 +109,3 @@ "use strict"; | ||
// src/lib/worker-farm/worker-body.ts | ||
function getParentPort() { | ||
async function getParentPort() { | ||
let parentPort; | ||
@@ -116,2 +116,9 @@ try { | ||
} catch { | ||
try { | ||
eval("globalThis.workerThreadsPromise = import('worker_threads')"); | ||
const workerThreads = await globalThis.workerThreadsPromise; | ||
parentPort = workerThreads.parentPort; | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
@@ -123,4 +130,4 @@ return parentPort; | ||
/** Check that we are actually in a worker thread */ | ||
static inWorkerThread() { | ||
return typeof self !== "undefined" || Boolean(getParentPort()); | ||
static async inWorkerThread() { | ||
return typeof self !== "undefined" || Boolean(await getParentPort()); | ||
} | ||
@@ -131,23 +138,24 @@ /* | ||
static set onmessage(onMessage) { | ||
function handleMessage(message) { | ||
const parentPort3 = getParentPort(); | ||
const { type, payload } = parentPort3 ? message : message.data; | ||
async function handleMessage(message) { | ||
const parentPort2 = await getParentPort(); | ||
const { type, payload } = parentPort2 ? message : message.data; | ||
onMessage(type, payload); | ||
} | ||
const parentPort2 = getParentPort(); | ||
if (parentPort2) { | ||
parentPort2.on("message", handleMessage); | ||
parentPort2.on("exit", () => console.debug("Node worker closing")); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
getParentPort().then((parentPort2) => { | ||
if (parentPort2) { | ||
parentPort2.on("message", handleMessage); | ||
parentPort2.on("exit", () => console.debug("Node worker closing")); | ||
} else { | ||
globalThis.onmessage = handleMessage; | ||
} | ||
}); | ||
} | ||
static addEventListener(onMessage) { | ||
static async addEventListener(onMessage) { | ||
let onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
if (!onMessageWrapper) { | ||
onMessageWrapper = (message) => { | ||
onMessageWrapper = async (message) => { | ||
if (!isKnownMessage(message)) { | ||
return; | ||
} | ||
const parentPort3 = getParentPort(); | ||
const parentPort3 = await getParentPort(); | ||
const { type, payload } = parentPort3 ? message : message.data; | ||
@@ -157,3 +165,3 @@ onMessage(type, payload); | ||
} | ||
const parentPort2 = getParentPort(); | ||
const parentPort2 = await getParentPort(); | ||
if (parentPort2) { | ||
@@ -165,6 +173,6 @@ console.error("not implemented"); | ||
} | ||
static removeEventListener(onMessage) { | ||
static async removeEventListener(onMessage) { | ||
const onMessageWrapper = onMessageWrapperMap.get(onMessage); | ||
onMessageWrapperMap.delete(onMessage); | ||
const parentPort2 = getParentPort(); | ||
const parentPort2 = await getParentPort(); | ||
if (parentPort2) { | ||
@@ -181,6 +189,6 @@ console.error("not implemented"); | ||
*/ | ||
static postMessage(type, payload) { | ||
static async postMessage(type, payload) { | ||
const data = { source: "loaders.gl", type, payload }; | ||
const transferList = getTransferList(payload); | ||
const parentPort2 = getParentPort(); | ||
const parentPort2 = await getParentPort(); | ||
if (parentPort2) { | ||
@@ -202,4 +210,4 @@ parentPort2.postMessage(data, transferList); | ||
var options; | ||
function createWorker(process, processInBatches) { | ||
if (!WorkerBody.inWorkerThread()) { | ||
async function createWorker(process, processInBatches) { | ||
if (!await WorkerBody.inWorkerThread()) { | ||
return; | ||
@@ -206,0 +214,0 @@ } |
{ | ||
"name": "@loaders.gl/worker-utils", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "Utilities for running tasks on worker threads", | ||
@@ -55,3 +55,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "765e5a26a6bf3f2cc02cabffc4a1e3665ec92a53" | ||
"gitHead": "471058d109d5652f28c32c1f296fd632f9a5c806" | ||
} |
@@ -26,4 +26,7 @@ import type { | ||
*/ | ||
export function createWorker(process: Process, processInBatches?: ProcessInBatches): void { | ||
if (!WorkerBody.inWorkerThread()) { | ||
export async function createWorker( | ||
process: Process, | ||
processInBatches?: ProcessInBatches | ||
): Promise<void> { | ||
if (!(await WorkerBody.inWorkerThread())) { | ||
return; | ||
@@ -30,0 +33,0 @@ } |
@@ -44,3 +44,8 @@ // loaders.gl, MIT license | ||
if (options._workerType === 'test') { | ||
url = `modules/${worker.module}/dist/${workerFile}`; | ||
if (isBrowser) { | ||
url = `modules/${worker.module}/dist/${workerFile}`; | ||
} else { | ||
// In the test environment the ts-node loader requires TypeScript code | ||
url = `modules/${worker.module}/src/workers/${worker.id}-worker-node.ts`; | ||
} | ||
} | ||
@@ -47,0 +52,0 @@ |
@@ -5,3 +5,3 @@ import type {WorkerMessageData, WorkerMessageType, WorkerMessagePayload} from '../../types'; | ||
/** Vile hack to defeat over-zealous bundlers from stripping out the require */ | ||
function getParentPort() { | ||
async function getParentPort() { | ||
// const isNode = globalThis.process; | ||
@@ -13,4 +13,12 @@ let parentPort; | ||
parentPort = globalThis.parentPort; | ||
// eslint-disable-next-line no-empty | ||
} catch {} | ||
} catch { | ||
try { | ||
// prettier-ignore | ||
eval('globalThis.workerThreadsPromise = import(\'worker_threads\')'); // eslint-disable-line no-eval | ||
const workerThreads = await globalThis.workerThreadsPromise; | ||
parentPort = workerThreads.parentPort; | ||
} catch (error) { | ||
console.error((error as Error).message); // eslint-disable-line no-console | ||
} | ||
} | ||
return parentPort; | ||
@@ -26,4 +34,4 @@ } | ||
/** Check that we are actually in a worker thread */ | ||
static inWorkerThread(): boolean { | ||
return typeof self !== 'undefined' || Boolean(getParentPort()); | ||
static async inWorkerThread(): Promise<boolean> { | ||
return typeof self !== 'undefined' || Boolean(await getParentPort()); | ||
} | ||
@@ -35,5 +43,5 @@ | ||
static set onmessage(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any) { | ||
function handleMessage(message) { | ||
async function handleMessage(message) { | ||
const parentPort = await getParentPort(); | ||
// Confusingly the message itself also has a 'type' field which is always set to 'message' | ||
const parentPort = getParentPort(); | ||
const {type, payload} = parentPort ? message : message.data; | ||
@@ -46,15 +54,16 @@ // if (!isKnownMessage(message)) { | ||
const parentPort = getParentPort(); | ||
if (parentPort) { | ||
parentPort.on('message', handleMessage); | ||
// if (message == 'exit') { parentPort.unref(); } | ||
// eslint-disable-next-line | ||
parentPort.on('exit', () => console.debug('Node worker closing')); | ||
} else { | ||
// eslint-disable-next-line no-restricted-globals | ||
globalThis.onmessage = handleMessage; | ||
} | ||
getParentPort().then((parentPort) => { | ||
if (parentPort) { | ||
parentPort.on('message', handleMessage); | ||
// if (message == 'exit') { parentPort.unref(); } | ||
// eslint-disable-next-line | ||
parentPort.on('exit', () => console.debug('Node worker closing')); | ||
} else { | ||
// eslint-disable-next-line no-restricted-globals | ||
globalThis.onmessage = handleMessage; | ||
} | ||
}); | ||
} | ||
static addEventListener( | ||
static async addEventListener( | ||
onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any | ||
@@ -65,3 +74,3 @@ ) { | ||
if (!onMessageWrapper) { | ||
onMessageWrapper = (message: MessageEvent<any>) => { | ||
onMessageWrapper = async (message: MessageEvent<any>) => { | ||
if (!isKnownMessage(message)) { | ||
@@ -71,4 +80,4 @@ return; | ||
const parentPort = await getParentPort(); | ||
// Confusingly in the browser, the message itself also has a 'type' field which is always set to 'message' | ||
const parentPort = getParentPort(); | ||
const {type, payload} = parentPort ? message : message.data; | ||
@@ -79,3 +88,3 @@ onMessage(type, payload); | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
@@ -88,3 +97,3 @@ console.error('not implemented'); // eslint-disable-line | ||
static removeEventListener( | ||
static async removeEventListener( | ||
onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any | ||
@@ -94,3 +103,3 @@ ) { | ||
onMessageWrapperMap.delete(onMessage); | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
@@ -108,3 +117,3 @@ console.error('not implemented'); // eslint-disable-line | ||
*/ | ||
static postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): void { | ||
static async postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): Promise<void> { | ||
const data: WorkerMessageData = {source: 'loaders.gl', type, payload}; | ||
@@ -114,3 +123,3 @@ // console.log('posting message', data); | ||
const parentPort = getParentPort(); | ||
const parentPort = await getParentPort(); | ||
if (parentPort) { | ||
@@ -117,0 +126,0 @@ parentPort.postMessage(data, transferList); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
331884
4977
12