Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
sdgluck committed Jun 14, 2016
1 parent ef61d59 commit 5e1a1a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ Made with ❤ at [@outlandish](http://www.twitter.com/outlandish)

Makes communication between a client and service worker super easy...

- Send messages from `client -> worker` and `worker -> client` with one call to `channel.send()`
- Simple API: send anonymous data-only messages _or_ a typed message with data
- Register handlers for typed messages and anonymous messages
- Easily respond to any message by calling `respond()` in the handler
- Register response handlers for anonymous and typed messages using the familiar `then()` method

## Table of Contents

- [Import](#import)
- [Initialise](#initialise)
- [Initialise & Example](#initialise-&-example)
- [msgr API](#msgr-api)
- [Channel API](#channel-api)
- [Message API](#message-api)
Expand All @@ -35,9 +41,7 @@ define(['msgr'], function (msgr) {/*...*/})
<script src="/node_modules/msgr/index.js"></script>
```

## Initialise

### Example
## Initialise & Example

__Client: `msgr.client()`__

Expand Down
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ try {
var _MessageChannel = MessageChannel
} catch (err) {}

// ---
// API
// ---

/**
* Initialise a client or worker to send and receive messages.
* @param {Object} messageHandlers
Expand Down Expand Up @@ -50,7 +54,7 @@ msgr.types = {
}

// ---
// App
// Channel
// ---

function Channel (handlers, worker) {
Expand All @@ -71,6 +75,8 @@ function Channel (handlers, worker) {
if (this.isClient) {
this.open.resolve()
this.recipient = worker
this.messageChannel = new _MessageChannel()
this.messageChannel.port1.onmessage = this._handleMessage.bind(this)
this.send(msgr.types.CONNECT, 'connect')
} else {
_self.onmessage = this._handleMessage.bind(this)
Expand All @@ -94,11 +100,11 @@ Channel.prototype._handleMessage = function (event) {
this.send(msgr.types.RESPONSE, data, id)
}.bind(this)

if (this.isWorker) {
if (this.isWorker && request.type === msgr.types.CONNECT) {
// Special init message type that gives us the port
// that we will be sending messages to the client over
this.recipient = event.ports[0]
if (request.type === msgr.types.CONNECT) {
this.open.resolve()
}
this.open.resolve()
}

if (request.type in this.handlers) {
Expand Down Expand Up @@ -136,8 +142,7 @@ Channel.prototype.receive = function (handler) {
* Send a message.
* @param {String|*} type The message type or message data
* @param {*} [data] The message data
* @param {String} [_id] Message ID if this is a response
* @returns {{then: then}}
* @returns {Object}
*/
Channel.prototype.send = function (type, data, _id) {
_id = _id || shortid.generate()
Expand All @@ -152,6 +157,7 @@ Channel.prototype.send = function (type, data, _id) {
}

var payload = JSON.stringify({
__msgr: true,
type: type,
data: data,
id: _id
Expand All @@ -160,9 +166,7 @@ Channel.prototype.send = function (type, data, _id) {
var args = [payload]

if (this.isClient) {
var messageChannel = new _MessageChannel()
messageChannel.port1.onmessage = this._handleMessage.bind(this)
args.push([messageChannel.port2])
args.push([this.messageChannel.port2])
}

this.open.promise.then(function () {
Expand Down

0 comments on commit 5e1a1a7

Please sign in to comment.