Skip to content

Commit

Permalink
Add missing #ready fn
Browse files Browse the repository at this point in the history
  • Loading branch information
sdgluck committed Jun 13, 2016
1 parent 9fb9559 commit c4c0755
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function Channel (handlers, worker) {
this.isWorker = !this.isClient

// Is the comms channel open?
this.ready = defer()
this.open = defer()

// Handlers for unknown message types
this.receiveHandlers = []
Expand All @@ -74,7 +74,7 @@ function Channel (handlers, worker) {
this.responseHandlers = {}

if (this.isClient) {
this.ready.resolve()
this.open.resolve()
this.recipient = worker
this.messageChannel = new MessageChannel()
this.messageChannel.port1.onmessage = this._handleMessage.bind(this)
Expand Down Expand Up @@ -105,7 +105,7 @@ Channel.prototype._handleMessage = function (event) {
// Special init message type that gives us the port
// that we will be sending messages to the client over
this.recipient = event.ports[0]
this.ready.resolve()
this.open.resolve()
}

if (request.type in this.handlers) {
Expand All @@ -124,6 +124,10 @@ Channel.prototype._handleMessage = function (event) {
}
}

Channel.prototype.ready = function (fn) {
this.open.promise.then(fn)
}

/**
* Receive an "unknown" message that does not have a predefined handler.
* @param {Function} handler
Expand Down Expand Up @@ -166,7 +170,7 @@ Channel.prototype.send = function (type, data, _id) {
args.push([this.messageChannel.port2])
}

this.ready.promise.then(function () {
this.open.promise.then(function () {
this.recipient.postMessage.apply(this.recipient, args)
}.bind(this))

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.1.1",
"version": "0.2.0",
"description": "Nifty worker/client postMessage utility",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 9 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ test('Message handler throws on malformed message', function (t) {

test('Client sends CONNECT, worker receives', function (t) {
t.equal(worker_connectSpy.callCount, 1)
client.ready.promise.then(function () {
client.open.promise.then(function () {
t.end()
})
})

test('Client sends known message, worker receives', function (t) {
client.send('WORKER_KNOWN_MESSAGE_TYPE', 'data')
client.ready.promise.then(function () {
client.open.promise.then(function () {
t.equal(worker_knownMessageTypeStub.callCount, 1)
t.end()
})
})

test('Worker sends known message, client receives', function (t) {
worker.send('CLIENT_KNOWN_MESSAGE_TYPE', 'data')
worker.ready.promise.then(function () {
worker.open.promise.then(function () {
t.equal(client_knownMessageTypeStub.callCount, 1)
t.end()
})
Expand All @@ -96,7 +96,7 @@ test('Worker sends known message, client receives', function (t) {
test('Client sends known message, worker responds', function (t) {
resetStubs()
var message = client.send('WORKER_KNOWN_MESSAGE_TYPE', 'data')
client.ready.promise.then(function () {
client.open.promise.then(function () {
worker_knownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
Expand All @@ -118,7 +118,7 @@ test('Registering multiple response handlers throws', function (t) {
test('Worker sends known message, client responds', function (t) {
resetStubs()
var message = worker.send('CLIENT_KNOWN_MESSAGE_TYPE', 'data')
worker.ready.promise.then(function () {
worker.open.promise.then(function () {
client_knownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
Expand All @@ -129,7 +129,7 @@ test('Worker sends known message, client responds', function (t) {
test('Client sends unknown message, worker receives', function (t) {
resetStubs()
client.send('data')
client.ready.promise.then(function () {
client.open.promise.then(function () {
t.equal(worker_unknownMessageTypeStub.callCount, 1)
t.end()
})
Expand All @@ -138,7 +138,7 @@ test('Client sends unknown message, worker receives', function (t) {
test('Worker sends unknown message, client receives', function (t) {
resetStubs()
worker.send('data')
worker.ready.promise.then(function () {
worker.open.promise.then(function () {
t.equal(client_unknownMessageTypeStub.callCount, 1)
t.end()
})
Expand All @@ -147,7 +147,7 @@ test('Worker sends unknown message, client receives', function (t) {
test('Client sends unknown message, worker responds', function (t) {
resetStubs()
var message = client.send('data')
client.ready.promise.then(function () {
client.open.promise.then(function () {
worker_unknownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
Expand All @@ -158,7 +158,7 @@ test('Client sends unknown message, worker responds', function (t) {
test('Worker sends unknown message, client responds', function (t) {
resetStubs()
var message = worker.send('data')
worker.ready.promise.then(function () {
worker.open.promise.then(function () {
client_unknownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
Expand Down

0 comments on commit c4c0755

Please sign in to comment.