# workhorsejs

> web workers wrapped in event emitters

Latest version **1.0.1** (published 2015-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install workhorsejs
pnpm add workhorsejs
yarn add workhorsejs
bun add workhorsejs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2015-04-21 |
| First published | 2015-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dave Gramlich |
| Maintainers | callmehiphop |
| Keywords | web, worker, event, emitter, promise |

## Links

- npm: https://www.npmjs.com/package/workhorsejs
- Repository: https://github.com/callmehiphop/workhorse
- Issues: https://github.com/callmehiphop/workhorse/issues
- npm.io page: https://npm.io/package/workhorsejs

## Dependencies (2)

- [es6-promise](https://npm.io/package/es6-promise.md) ^1.0.0
- [lodash.bind](https://npm.io/package/lodash.bind.md) ^3.1.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-04-21

## README

workhorse.js
============

> [web workers](http://www.html5rocks.com/en/tutorials/workers/basics/) on crack! (should have called it whitney)

The goal behind workhorse is to simplify messaging between a worker and client interface. It also adopts some concepts from the [ServiceWorker](http://jakearchibald.com/2014/service-worker-first-draft/) spec.

### Events

#### `on(event, handler)`

```javascript
var worker = new Workhorse('thing.js');

worker.on('wat', function (e) {
  console.log(e.data);
});
```

#### `off(event, [handler])`

```javascript
// remove all the "wats"
worker.off('wat');
```

#### `emit(event, [data])`

```javascript
worker.on('wat', function (e) {
  console.log(e.data.message);
});
worker.emit('wat', { message : 'hi!' }); // => "hi!"
```

#### `once(event, handler)`

```javascript
worker.once('wat', function () {
  console.log('hi!');
});
worker.emit('wat'); // => "hi!"
worker.emit('wat'); // nothin'
```

#### `post(event, data)`

```javascript
worker.post('wat', { message: 'hi' });
```

### What are the differences between `emit` and `post`?

* `post` sends a message across the wire, this would be the equivilant of `postMessage`.
* `emit` triggers any and all event handlers associated with an event. This does not send a message across the wire.

### Responding to events

By default web workers offer a very simple messaging system which can get [messy](http://www.html5rocks.com/en/tutorials/workers/basics/#toc-gettingstarted-workercomm) quick when a worker potentially has multiple responsibilities.

Rather than wiring up multiple events for a game of ping pong, we should be able to easily respond to an event via the `event` object.

```javascript
worker.on('wat', function (event) {
  event.respondWith({
    message: 'Hello!'
  });
});
```

### Recieving responses

So just like the issue of responding to events, how does a client recieve that response without wiring up an additional event? This seems like a fitting place to use promises.

```javascript
worker.post('wat', { wat: 'wat' })
  .then(function (response) {
    console.log(response.message); // => "Hello!"
  });
```

### Considerations

Things to consider while developing

* There are multiple types of workers (Worker, SharedWorker, etc.)
* It's possible to spin up a worker via `port` property (`worker.port`)
* Nothing could probably ever take Whitney in a fist fight

---
_Source: https://npm.io/package/workhorsejs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
