Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sdgluck committed Jun 14, 2016
1 parent 4613e94 commit 2c6bdb6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Makes communication between a client and service worker super easy...
- 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
- Receive a response for a message using the familiar Promise `then()` method

## Table of Contents

Expand Down Expand Up @@ -200,8 +200,6 @@ Register a handler to receive the response to a message.

- __handler__ {Function} Response handler

Note: a message can only have one `then` handler. Registering more than one will throw an error.

Example:

```js
Expand Down
39 changes: 15 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ function Channel (handlers, worker) {
// Handlers for unknown message types
this.receiveHandlers = []

// ID/response handlers use for handling `responds()`s
// key = id string, value = function
this.responseHandlers = {}
// Deferreds for sent messages so we can resolve
// the promise if they receive a response
this.promises = {}

if (this.isClient) {
this.open.resolve()
Expand Down Expand Up @@ -110,11 +110,11 @@ Channel.prototype._handleMessage = function (event) {
if (request.type in this.handlers) {
// Known message type, invoke registered handler
this.handlers[request.type](request.data, responder)
} else if (id && id in this.responseHandlers) {
} else if (id && id in this.promises) {
// Response to a message, invoke registered response handler
var handler = this.responseHandlers[id]
handler(request.data)
this.responseHandlers[id] = null
var promise = this.promises[id]
promise.resolve()
this.promises[id] = null
} else {
// Unknown message type, invoke receive handlers
this.receiveHandlers.forEach(function (handler) {
Expand Down Expand Up @@ -145,9 +145,7 @@ Channel.prototype.receive = function (handler) {
* @returns {Object}
*/
Channel.prototype.send = function (type, data, _id) {
_id = _id || shortid.generate()

var _this = this
var id = _id || shortid.generate()

if (!data) {
data = type
Expand All @@ -156,11 +154,13 @@ Channel.prototype.send = function (type, data, _id) {
}
}

var deferred = defer()

var payload = JSON.stringify({
__msgr: true,
id: id,
type: type,
data: data,
id: _id
data: data
})

var args = [payload]
Expand All @@ -173,18 +173,9 @@ Channel.prototype.send = function (type, data, _id) {
this.recipient.postMessage.apply(this.recipient, args)
}.bind(this))

return {
/**
* Register a one-off response handler for a message.
* @param {Function} handler
*/
then: function (handler) {
if (_this.responseHandlers[_id]) {
throw new Error('msgr: you can register only one response handler')
}
_this.responseHandlers[_id] = handler
}
}
this.promises[id] = deferred

return deferred.promise
}

// ---
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msgr",
"version": "0.5.1",
"version": "1.0.0",
"description": "Nifty worker/client postMessage utility",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 0 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,6 @@ test('Client sends known message, worker responds', function (t) {
})
})

test('Registering multiple response handlers throws', function (t) {
resetStubs()
var message = client.send('WORKER_KNOWN_MESSAGE_TYPE', 'data')
message.then(function () {})
try {
message.then(function () {})
} catch (err) {
t.end()
}
})

test('Worker sends known message, client responds', function (t) {
resetStubs()
var message = worker.send('CLIENT_KNOWN_MESSAGE_TYPE', 'data')
Expand Down

0 comments on commit 2c6bdb6

Please sign in to comment.