HTML and CSS Reference
In-Depth Information
}.bind(this));
return promise;
};
The listener we just added is empty, as we don't yet have a test that tells us what
it needs to do. That seems like a suitable topic for the next test, which will assert
that adding a message causes waitForMessagesSince to resolve with the new
message. For symmetry with getMessagesSince , we expect the single message
to arrive as an array. Listing 14.63 shows the test.
Listing 14.63 Adding a message should resolve waiting requests
"new message should resolve waiting": function (test) {
var user = "cjno";
var msg = "Are you waiting for this?";
this.room.waitForMessagesSince(0).then(function (msgs) {
test.isArray(msgs);
test.equals(msgs.length, 1);
test.equals(msgs[0].user, user);
test.equals(msgs[0].message, msg);
test.done();
});
process.nextTick(function () {
this.room.addMessage(user, msg);
}.bind(this));
}
Unsurprisingly, the test does not pass, prompting us to fill in the “message”
listener we just added. Listing 14.64 shows the working listener.
Listing 14.64 Implementing the message listener
/* ... */
this.addListener("message", function (message) {
promise.resolve([message]);
});
/* ... */
And that's all it takes, the tests all pass, and our very rudimentary data layer is
complete enough to serve its purpose in the application. Still, there is one very im-
portant task to complete, and one that I will leave as an exercise. Once the promise
 
Search WWH ::




Custom Search