HTML and CSS Reference
In-Depth Information
add.then(function () {
test.notEquals(messages[0].id, messages[1].id);
test.done();
});
},
/* ... */
});
For consistency, the
getMessagesSince
method should be updated to use
promises as well. I will leave doing so as yet another exercise. Try to make sure you
never fail more than one test at a time while refactoring. When you're done you
should end up with something like Listing 14.53.
Listing 14.53
getMessagesSince
using promises
getMessagesSince: function (id) {
var promise = new Promise();
process.nextTick(function () {
promise.resolve((this.messages
||
[]).slice(id));
}.bind(this));
return promise;
}
When the client polls the server for new messages, one of two things can happen.
Either new messages are available, in which case the request is responded to and
ended immediately, or the server should hold the request until messages are ready.
So far we've covered the first case, but the second case, the one that enables long
polling, is not yet covered.
chatRoom
will provide a
waitForMessagesSince
method, which works
just like the
getMessagesSince
method; except if no messages are available, it
will idly wait for some to become available. In order to implement this, we need
chatRoom
to emit an event when new messages are added.
14.5.1 Making
chatRoom
an Event Emitter
The first test to verify that
chatRoom
is an event emitter is to test that it has the
addListener
and
emit
methods, as Listing 14.54 shows.