Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/icholy/messagebus
PubSub for WebWorkers
https://github.com/icholy/messagebus
Last synced: about 1 month ago
JSON representation
PubSub for WebWorkers
- Host: GitHub
- URL: https://github.com/icholy/messagebus
- Owner: icholy
- Created: 2015-03-10T19:46:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-06T18:44:20.000Z (over 7 years ago)
- Last Synced: 2024-10-03T15:20:00.904Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MessageBus [![Build Status](https://travis-ci.org/icholy/MessageBus.svg)](https://travis-ci.org/icholy/MessageBus)
> PubSub library for WebWorkers
### Install
``` sh
$ npm install
$ gulp dist
```### Usage Example
**main.js**
``` js
let worker = new Worker("worker.js"),
bus = MessageBus.create(worker);bus.on("pong", payload => {
console.log(payload.foo);
});bus.emit("ping");
```**worker.js**
``` js
importScripts("MessageBus.js");let bus = MessageBus.create(self);
bus.on("ping", () => {
bus.emit("pong", { foo: "Hello World" });
});
```### SharedWorker Example
**events.ts**
``` ts
enum Events { PONG, PING }
```**main.ts**
``` ts
let worker = new SharedWorker("worker.js"),
bus = MessageBus.create(worker.port);worker.port.start();
bus.on(Events.PONG, payload => {
console.log(payload.foo);
});bus.emit(Events.PING);
```**worker.ts**
``` ts
importScripts("MessageBus.js", "events.js");onconnect = event => {
let port = events.port[0],
bus = MessageBus.create(port);port.start();
bus.on(Events.PING, () => {
bus.emit(Events.PONG, { foo: "Hello World" });
});
};
```