npm.io
1.0.2 • Published 5 years ago

duck-worker

Licence
MIT
Version
1.0.2
Deps
7
Size
33 kB
Vulns
0
Weekly
0

duck-worker

Version

duckfficer service worker that serves an ipc bridge for communication

Installation

$ npm i duck-worker --save
# or
$ yarn add duck-worker

Features

duck worker client success

const client = await new DuckWorkerClient()
t.is(await client.workerA('some payload'), 'received: some payload')

duck worker client fail

const client = await new DuckWorkerClient()
await t.throwsAsync(() => client.workerB('some error'), {
  instanceOf: Error,
  message: 'some error'
})

duck worker provides ipc interface

ipc.connectTo('worker')
return new Promise((resolve) => {
  ipc.of['worker'].on('pong', () => {
    t.pass()
    resolve()
  })
  ipc.of['worker'].emit('ping')
})

loads workers from folder

await t.throwsAsync(() => client.taskA('something'), {
  message: 'got: something'
})

t.like(await client.taskB({ name: 'Olivia' }), {
  output: 'name is: Olivia'
})


const r = await client.nameSpace.taskC('what is love?')

t.like(r.errorsThrown[0], {
  payload: 'what is love?'
})


DuckWorkerClient


new DuckWorkerClient(appSpace, [clientId], [workerId])
Param Type Default Description
appSpace String default to local package.json->name
[clientId] String client
[workerId] String worker

Returns: Promise.<{Object}> - the proxy to communicate with the worker


duckWorkerIpc(workers, [appName], [id]) ⇒ Promise.<ipc.server>
Param Type Default Description
workers Object workers object mapping to functions
[appName] String <package.json->name> the appName (defaults to project's package.json name)
[id] String worker worker id


duckWorker(workerDir, [appName], [id]) ⇒ exports.<void>
Param Type Description
workerDir String the worker dir
[appName] String ipc appname
[id] String ipc id

License

MIT

2020-present Martin Rafael Gonzalez tin@devtin.io

Keywords