Skip to content

Commit bac9859

Browse files
author
David Bengsch
authored
Merge pull request #119 from kuzzleio/onDoneFoolproof
make subscribe.onDone foolproof
2 parents cfcc217 + 192b05d commit bac9859

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/kuzzleSubscribeResult.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
*/
55
function KuzzleSubscribeResult() {
66
this.cbs = [];
7+
this.error = null;
8+
this.room = null;
79
}
810

911
/**
1012
* Registers a callback to be called with a subscription result
1113
* @param {Function} cb
1214
*/
1315
KuzzleSubscribeResult.prototype.onDone = function (cb) {
14-
this.cbs.push(cb);
16+
if (this.error || this.room) {
17+
cb(this.error, this.room);
18+
}
19+
else {
20+
this.cbs.push(cb);
21+
}
22+
1523
return this;
1624
};
1725

@@ -22,6 +30,9 @@ KuzzleSubscribeResult.prototype.onDone = function (cb) {
2230
* @param {KuzzleRoom} room
2331
*/
2432
KuzzleSubscribeResult.prototype.done = function (error, room) {
33+
this.error = error;
34+
this.room = room;
35+
2536
this.cbs.forEach(function (cb) {
2637
cb(error, room);
2738
});

test/kuzzleSubscribeResult/kuzzleSubscribeResult.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ describe('KuzzleSubscribeResult object', function () {
3434
count++;
3535
should(err).be.eql(foo);
3636
should(res).be.eql(bar);
37+
should(ksr.error).be.eql(err);
38+
should(ksr.room).be.eql(res);
3739
if (count === 3) {
3840
done();
3941
}
@@ -49,5 +51,22 @@ describe('KuzzleSubscribeResult object', function () {
4951

5052
ksr.done(foo, bar);
5153
});
54+
55+
it('should invoke the provided callback directly if Kuzzle response is already stored', function (done) {
56+
var
57+
cb = function (err, res) {
58+
should(err).be.eql('foo');
59+
should(res).be.eql('bar');
60+
should(ksr.cbs).be.empty();
61+
done();
62+
};
63+
64+
this.timeout(50);
65+
66+
ksr.error = 'foo';
67+
ksr.room = 'bar';
68+
69+
ksr.onDone(cb);
70+
});
5271
});
5372

0 commit comments

Comments
 (0)