Comparing version 2.3.1 to 2.4.0
@@ -17,3 +17,3 @@ import Worker from './worker'; | ||
_autoterminate(): void; | ||
_getMethods(methods: Methods<MethodName, MethodFunction> | string): string; | ||
_getMethods(methods: Methods<MethodName, MethodFunction> | URL | string): string; | ||
_getTaskReady(): Task<MethodName, MethodFunction> | undefined; | ||
@@ -20,0 +20,0 @@ _getWorkerName(): string; |
@@ -36,5 +36,15 @@ /* IMPORT */ | ||
_getMethods(methods) { | ||
if (typeof methods === 'string') { // Already serialized methods, useful for complex and/or bundled workers | ||
return methods; | ||
if (typeof methods === 'string') { // Already serialized methods, or URL to import, useful for complex and/or bundled workers | ||
if (/^(file|https?):\/\//.test(methods)) { // URL to import | ||
const methodsImport = `${'import'} * as Methods ${'from'} '${methods}';`; | ||
const methodsRegister = `Object.entries ( Methods ).forEach ( ([ name, fn ]) => typeof fn === 'function' && WorkTankWorkerBackend.register ( name, fn ) );`; | ||
return `${methodsImport}\n\n${methodsRegister}`; | ||
} | ||
else { // Serialized methods | ||
return methods; | ||
} | ||
} | ||
else if (methods instanceof URL) { // URL to import | ||
return this._getMethods(methods.href); | ||
} | ||
else { // Serializable methods | ||
@@ -130,4 +140,3 @@ const names = Object.keys(methods); | ||
} | ||
; | ||
/* EXPORT */ | ||
export default WorkTank; |
@@ -1,3 +0,3 @@ | ||
declare type FN = (...args: any[]) => any; | ||
declare type MessageExec = { | ||
type FN = (...args: any[]) => any; | ||
type MessageExec = { | ||
type: 'exec'; | ||
@@ -7,13 +7,13 @@ method: string; | ||
}; | ||
declare type MessageInit = { | ||
type MessageInit = { | ||
type: 'init'; | ||
}; | ||
declare type MessageReady = { | ||
type MessageReady = { | ||
type: 'ready'; | ||
}; | ||
declare type MessageResultSuccess = { | ||
type MessageResultSuccess = { | ||
type: 'result'; | ||
value: any; | ||
}; | ||
declare type MessageResultError = { | ||
type MessageResultError = { | ||
type: 'result'; | ||
@@ -26,6 +26,6 @@ error: { | ||
}; | ||
declare type MessageResult = MessageResultSuccess | MessageResultError; | ||
declare type Message = MessageExec | MessageInit | MessageReady | MessageResult; | ||
declare type Methods<MethodName extends string = string, MethodFunction extends FN = FN> = Record<MethodName, MethodFunction>; | ||
declare type Options<MethodName extends string = string, MethodFunction extends FN = FN> = { | ||
type MessageResult = MessageResultSuccess | MessageResultError; | ||
type Message = MessageExec | MessageInit | MessageReady | MessageResult; | ||
type Methods<MethodName extends string = string, MethodFunction extends FN = FN> = Record<MethodName, MethodFunction>; | ||
type Options<MethodName extends string = string, MethodFunction extends FN = FN> = { | ||
name?: string; | ||
@@ -37,3 +37,3 @@ size?: number; | ||
}; | ||
declare type Task<MethodName extends string = string, MethodFunction extends FN = FN> = { | ||
type Task<MethodName extends string = string, MethodFunction extends FN = FN> = { | ||
method: MethodName; | ||
@@ -40,0 +40,0 @@ args: Parameters<Methods<MethodName, MethodFunction>[MethodName]>; |
@@ -5,3 +5,3 @@ { | ||
"description": "A simple isomorphic library for executing functions inside WebWorkers or Node Threads pools.", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"type": "module", | ||
@@ -33,10 +33,10 @@ "main": "dist/index.js", | ||
"promise-make-naked": "^2.0.0", | ||
"webworker-shim": "^1.0.0" | ||
"webworker-shim": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"esbuild": "^0.14.39", | ||
"fava": "^0.0.6", | ||
"tsex": "^1.1.2", | ||
"typescript": "^4.6.4" | ||
"esbuild": "^0.19.0", | ||
"fava": "^0.2.1", | ||
"tsex": "^3.0.1", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # WorkTank | ||
- **Dynamic pools**: You can create pools dynamically, just by passing serializable functions to the library at run time, without needing any bundler plugins at all. | ||
- **Static pools**: You can create pools at build-time too, if the functions you need to send to workers require bundling, just by using the official [WebPack loader](https://github.com/fabiospampinato/worktank-loader). | ||
- **Static pools**: You can create pools at build-time too, if the functions you need to send to workers require bundling, just by using the official Vite [plugin](https://github.com/fabiospampinato/worktank-vite-plugin). | ||
- **Electron-ready**: Electron's special renderer process environment is supported out of the box too. | ||
@@ -77,3 +77,2 @@ - **TypeScript-ready**: Types come with the library and aren't an afterthought. | ||
- **Vite**: [worktank-vite-plugin](https://github.com/fabiospampinato/worktank-vite-plugin), the official plugin for Vite. | ||
- **WebPack**: [worktank-loader](https://github.com/fabiospampinato/worktank-loader), the official plugin for WebPack, it only works with v1.x.x. | ||
@@ -80,0 +79,0 @@ Read their documentation to learn how to use them, but TL;DR: it's mostly just a matter of adding a couple of lines of configuration for your bundlers. |
@@ -5,4 +5,4 @@ | ||
import makeNakedPromise from 'promise-make-naked'; | ||
import Worker from './worker'; | ||
import type {FN, Methods, Options, Task} from './types'; | ||
import Worker from '~/worker'; | ||
import type {FN, Methods, Options, Task} from '~/types'; | ||
@@ -72,8 +72,23 @@ /* MAIN */ | ||
_getMethods ( methods: Methods<MethodName, MethodFunction> | string ): string { | ||
_getMethods ( methods: Methods<MethodName, MethodFunction> | URL | string ): string { | ||
if ( typeof methods === 'string' ) { // Already serialized methods, useful for complex and/or bundled workers | ||
if ( typeof methods === 'string' ) { // Already serialized methods, or URL to import, useful for complex and/or bundled workers | ||
return methods; | ||
if ( /^(file|https?):\/\//.test ( methods ) ) { // URL to import | ||
const methodsImport = `${'import'} * as Methods ${'from'} '${methods}';`; | ||
const methodsRegister = `Object.entries ( Methods ).forEach ( ([ name, fn ]) => typeof fn === 'function' && WorkTankWorkerBackend.register ( name, fn ) );`; | ||
return `${methodsImport}\n\n${methodsRegister}`; | ||
} else { // Serialized methods | ||
return methods; | ||
} | ||
} else if ( methods instanceof URL ) { // URL to import | ||
return this._getMethods ( methods.href ); | ||
} else { // Serializable methods | ||
@@ -225,3 +240,3 @@ | ||
}; | ||
} | ||
@@ -228,0 +243,0 @@ /* EXPORT */ |
/* IMPORT */ | ||
import type {Message} from '../types'; | ||
import type {Message} from '~/types'; | ||
@@ -6,0 +6,0 @@ /* MAIN */ |
@@ -5,4 +5,4 @@ | ||
import WorkerShim from 'webworker-shim'; | ||
import WorkerBackend from './backend_compiled'; | ||
import type {Message} from '../types'; | ||
import WorkerBackend from '~/worker/backend_compiled'; | ||
import type {Message} from '~/types'; | ||
@@ -9,0 +9,0 @@ /* MAIN */ |
/* IMPORT */ | ||
import WorkerFrontend from './frontend'; | ||
import type {FN, Message, MessageReady, MessageResult, Task} from '../types'; | ||
import WorkerFrontend from '~/worker/frontend'; | ||
import type {FN, Message, MessageReady, MessageResult, Task} from '~/types'; | ||
@@ -7,0 +7,0 @@ /* MAIN */ |
@@ -6,2 +6,3 @@ | ||
import path from 'node:path'; | ||
import {pathToFileURL} from 'node:url'; | ||
import WorkTank from '../dist/index.js'; | ||
@@ -15,3 +16,3 @@ | ||
it ( 'can execute a passed method', async t => { | ||
it ( 'can execute passed methods', async t => { | ||
@@ -43,2 +44,21 @@ const pool = new WorkTank ({ | ||
it ( 'can execute imported methods', async t => { | ||
const pool = new WorkTank ({ | ||
name: 'example', | ||
methods: pathToFileURL ( path.resolve ( './test/worker.js' ) ) | ||
}); | ||
const sumResult = await pool.exec ( 'sum', [10, 20] ); | ||
t.is ( sumResult, 30 ); | ||
const sepResult = await pool.exec ( 'sep' ); | ||
t.is ( sepResult, path.sep ); | ||
pool.terminate (); | ||
}); | ||
}); |
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
34493
26
767
82
Updatedwebworker-shim@^1.1.0