npm package: @neosjs/web-worker
Description: 一个简化web工作者使用的实用程序
Author: 听着情歌流泪
Latest version: 1.0.3

# @neosjs/web-worker

![NPM Version][npm-version-image] ![Npm Week Downloads][npm-downloads-image-week] ![Npm Month Downloads][npm-downloads-image-month] ![License][license-image] ![Web Worker][web-worker]

创建和使用Web Worker有时会很麻烦。`@neosjs/web-worker`的目的是方便使用Web Worker。

## 安装

```bash
pnpm add @neosjs/web-worker

npm install @neosjs/web-worker

yarn add @neosjs/web-worker
```

## 使用

```ts
import webWorker from '@neosjs/web-worker'

webWorker.run(func, [args]?).then((res) => {
  console.log(res)
})
```

## API

#### webWorker.run(func, [args]?)

##### eg:

```ts
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:

```ts
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)
```

#### <worker>.postMessage(message, [args]?)

##### eg:

```ts
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
```

#### <worker>.postAllMessage([message1,... || {message: message1, args: [args1]},... || [args1],...]?)

##### eg:

```ts
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
```

#### <worker>.register(action || [actions])

##### eg:

```ts
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' }
])
```

#### <worker>.unregister(message || [messages])

##### eg:

```ts
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

```ts
import webWorker from '@neosjs/web-worker'
let worker = webWorker.create(actions)
// ...
worker = null // close worker
```

## License

[MIT](./LICENSE) License © 2021-PRESENT [NeosJS](https://docs.neosjs.com)

[npm-version-image]: https://img.shields.io/npm/v/@neosjs/web-worker?style=flat&colorA=2d333b&colorB=1fa669
[npm-downloads-image-week]: https://img.shields.io/npm/dw/@neosjs/web-worker?style=flatt&colorA=2d333b&colorB=1fa669
[npm-downloads-image-month]: https://img.shields.io/npm/dm/@neosjs/web-worker?style=flat&colorA=2d333b&colorB=1fa669
[license-image]: https://img.shields.io/npm/l/@neosjs/web-worker?style=flat&colorA=2d333b&colorB=1fa669
[web-worker]: https://img.shields.io/badge/NeosJS-web--worker-1fa669?style=flat&colorA=2d333b&colorB=1fa669