Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/kuzzleSubscribeResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
*/
function KuzzleSubscribeResult() {
this.cbs = [];
this.error = null;
this.room = null;
}

/**
* Registers a callback to be called with a subscription result
* @param {Function} cb
*/
KuzzleSubscribeResult.prototype.onDone = function (cb) {
this.cbs.push(cb);
if (this.error || this.room) {
cb(this.error, this.room);
}
else {
this.cbs.push(cb);
}

return this;
};

Expand All @@ -22,6 +30,9 @@ KuzzleSubscribeResult.prototype.onDone = function (cb) {
* @param {KuzzleRoom} room
*/
KuzzleSubscribeResult.prototype.done = function (error, room) {
this.error = error;
this.room = room;

this.cbs.forEach(function (cb) {
cb(error, room);
});
Expand Down
19 changes: 19 additions & 0 deletions test/kuzzleSubscribeResult/kuzzleSubscribeResult.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe('KuzzleSubscribeResult object', function () {
count++;
should(err).be.eql(foo);
should(res).be.eql(bar);
should(ksr.error).be.eql(err);
should(ksr.room).be.eql(res);
if (count === 3) {
done();
}
Expand All @@ -49,5 +51,22 @@ describe('KuzzleSubscribeResult object', function () {

ksr.done(foo, bar);
});

it('should invoke the provided callback directly if Kuzzle response is already stored', function (done) {
var
cb = function (err, res) {
should(err).be.eql('foo');
should(res).be.eql('bar');
should(ksr.cbs).be.empty();
done();
};

this.timeout(50);

ksr.error = 'foo';
ksr.room = 'bar';

ksr.onDone(cb);
});
});