@neosjs/web-worker
v1.0.3
Published
一个简化web工作者使用的实用程序
Downloads
2
Readme
@neosjs/web-worker
创建和使用Web Worker有时会很麻烦。@neosjs/web-worker
的目的是方便使用Web Worker。
安装
pnpm add @neosjs/web-worker
npm install @neosjs/web-worker
yarn add @neosjs/web-worker
使用
import webWorker from '@neosjs/web-worker'
webWorker.run(func, [args]?).then((res) => {
console.log(res)
})
API
webWorker.run(func, [args]?)
eg:
import webWorker from '@neosjs/web-worker'
webWorker.run(() => 'webWorker run 1: Function in other thread')
.then(console.log)
.catch(console.error)
webWorker.run((arg1, arg2) => `webWorker run 2: ${arg1} ${arg2}`, ['Another', 'function in other thread'])
.then(console.log)
.catch(console.error)
webWorker.create([actions]?)
eg:
import webWorker from '@neosjs/web-worker'
const actions = [
{ message: 'func1', action: () => `Worker 1: Working on func1` },
{ message: 'func2', action: arg => `Worker 2: ${arg}` },
{ message: 'func3', action: arg => `Worker 3: ${arg}` },
{ message: 'func4', action: (arg = 'Working on func4') => `Worker 4: ${arg}` }
]
const worker = webWorker.create(actions)
console.log(worker)
.postMessage(message, [args]?)
eg:
import webWorker from '@neosjs/web-worker'
const actions = [
{ message: 'func1', action: () => `Worker 1: Working on func1` },
{ message: 'func2', action: arg => `Worker 2: ${arg}` },
{ message: 'func3', action: arg => `Worker 3: ${arg}` },
{ message: 'func4', action: (arg = 'Working on func4') => `Worker 4: ${arg}` }
]
const worker = webWorker.create(actions)
worker.postMessage('func1')
.then(console.log) // logs 'Worker 1: Working on func1'
.catch(console.error) // logs any possible error
worker.postMessage('func2')
.then(console.log) // logs 'Worker 2: undefined'
.catch(console.error) // logs any possible error
worker.postMessage('func4', ['Overwrited argument'])
.then(console.log) // logs 'Worker 4: Overwrited argument'
.catch(console.error) // logs any possible error
.postAllMessage([message1,... || {message: message1, args: [args1]},... || [args1],...]?)
eg:
import webWorker from '@neosjs/web-worker'
const actions = [
{ message: 'func1', action: () => `Worker 1: Working on func1` },
{ message: 'func2', action: arg => `Worker 2: ${arg}` },
{ message: 'func3', action: arg => `Worker 3: ${arg}` },
{ message: 'func4', action: (arg = 'Working on func4') => `Worker 4: ${arg}` }
]
const worker = webWorker.create(actions)
worker.postAllMessage()
.then(console.log) // logs ['Worker 1: Working on func1', 'Worker 2: undefined', 'Worker 3: undefined', 'Worker 4: Working on func4']
.catch(console.error) // logs any possible error
worker.postAllMessage(['func1', 'func3'])
.then(console.log) // logs ['Worker 1: Working on func1', 'Worker 3: undefined']
.catch(console.error) // logs any possible error
.register(action || [actions])
eg:
import webWorker from '@neosjs/web-worker'
const initialActions = [
{ message: 'func1', action: () => 'Working on func1' }
]
const worker = webWorker.create(initialActions)
worker.register({ message: 'func2', action: () => 'Working on func2' })
// 注册多个操作
worker.register([
{ message: 'func3', action: () => 'Working on func3' },
{ message: 'func4', action: () => 'Working on func4' }
])
.unregister(message || [messages])
eg:
import webWorker from '@neosjs/web-worker'
const initialActions = [
{ message: 'func1', action: () => 'Working on func1' },
{ message: 'func2', action: () => 'Working on func2' },
{ message: 'func3', action: () => 'Working on func3' },
{ message: 'func4', action: () => 'Working on func4' }
]
const worker = webWorker.create(initialActions)
// 取消注册单个动作
worker.unregister('func2')
// 取消注册多个动作
worker.unregister(['func3', 'func1'])
关闭 workers
import webWorker from '@neosjs/web-worker'
let worker = webWorker.create(actions)
// ...
worker = null // close worker