Skip to content

Commit 103b3e4

Browse files
committed
Merge branch 'pr/54'
2 parents e1a51f3 + 01a31b4 commit 103b3e4

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

lib/ddp-client.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ var DDPClient = function(opts) {
4949
self._nextId = 0;
5050
self._callbacks = {};
5151
self._updatedCallbacks = {};
52+
self._pendingMethods = {};
5253
self._observers = {};
5354
};
5455

56+
57+
DDPClient.ERRORS = {
58+
DISCONNECTED: new Error("DDPClient: Disconnected from DDP server")
59+
};
60+
61+
5562
/**
5663
* Inherits from EventEmitter
5764
*/
@@ -81,6 +88,7 @@ DDPClient.prototype._prepareHandlers = function() {
8188

8289
self.socket.on("close", function(event) {
8390
self.emit("socket-close", event.code, event.reason);
91+
self._endPendingMethodCalls();
8492
self._recoverNetworkError();
8593
});
8694

@@ -321,6 +329,24 @@ DDPClient.prototype.connect = function(connected) {
321329
}
322330
};
323331

332+
DDPClient.prototype._endPendingMethodCalls = function() {
333+
var self = this;
334+
var ids = _.keys(self._pendingMethods);
335+
self._pendingMethods = {};
336+
337+
ids.forEach(function (id) {
338+
if(self._callbacks[id]) {
339+
self._callbacks[id](DDPClient.ERRORS.DISCONNECTED);
340+
delete self._callbacks[id];
341+
}
342+
343+
if(self._updatedCallbacks[id]) {
344+
self._updatedCallbacks[id]();
345+
delete self._updatedCallbacks[id];
346+
}
347+
});
348+
};
349+
324350
DDPClient.prototype._makeSockJSConnection = function() {
325351
var self = this;
326352

@@ -395,13 +421,23 @@ DDPClient.prototype.call = function(name, params, callback, updatedCallback) {
395421
var self = this;
396422
var id = self._getNextId();
397423

398-
if (callback) {
399-
self._callbacks[id] = callback;
400-
}
424+
self._callbacks[id] = function () {
425+
delete self._pendingMethods[id];
401426

402-
if (updatedCallback) {
403-
self._updatedCallbacks[id] = updatedCallback;
404-
}
427+
if (callback) {
428+
callback.apply(this, arguments);
429+
}
430+
};
431+
432+
self._updatedCallbacks[id] = function () {
433+
delete self._pendingMethods[id];
434+
435+
if (updatedCallback) {
436+
updatedCallback.apply(this, arguments);
437+
}
438+
};
439+
440+
self._pendingMethods[id] = true;
405441

406442
self._send({
407443
msg : "method",

test/ddp-client.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,68 @@ describe('Automatic reconnection', function() {
154154
done();
155155
}, 15);
156156
});
157+
158+
it('should save currently running method calls', function() {
159+
var ddpclient = new DDPClient();
160+
ddpclient._getNextId = sinon.stub().returns('_test');
161+
ddpclient._send = Function.prototype;
162+
163+
ddpclient.connect();
164+
ddpclient.call();
165+
166+
assert("_test" in ddpclient._pendingMethods)
167+
});
168+
169+
it('should remove id when callback is called', function() {
170+
var ddpclient = new DDPClient();
171+
ddpclient._getNextId = sinon.stub().returns('_test');
172+
ddpclient._send = Function.prototype;
173+
174+
ddpclient.connect();
175+
ddpclient.call();
176+
177+
assert("_test" in ddpclient._pendingMethods)
178+
179+
ddpclient._callbacks._test();
180+
assert(!("_test" in ddpclient._pendingMethods))
181+
});
182+
183+
it('should remove id when updated-callback is called', function() {
184+
var ddpclient = new DDPClient();
185+
ddpclient._getNextId = sinon.stub().returns('_test');
186+
ddpclient._send = Function.prototype;
187+
188+
ddpclient.connect();
189+
ddpclient.call();
190+
191+
assert("_test" in ddpclient._pendingMethods)
192+
193+
ddpclient._updatedCallbacks._test();
194+
assert(!("_test" in ddpclient._pendingMethods))
195+
});
196+
197+
it('should end method calls which could not be completed', function() {
198+
var ddpclient = new DDPClient();
199+
var callback = sinon.spy();
200+
var updatedCallback = sinon.spy();
201+
202+
ddpclient._pendingMethods = { _test: true };
203+
ddpclient._callbacks = { _test: callback };
204+
ddpclient._updatedCallbacks = { _test: updatedCallback };
205+
206+
ddpclient.connect();
207+
ddpclient.socket.emit('close', {});
208+
209+
assert(callback.calledOnce);
210+
assert(callback.calledWithExactly(DDPClient.ERRORS.DISCONNECTED));
211+
212+
assert(updatedCallback.calledOnce);
213+
214+
// callbacks should be removed after calling them
215+
assert(!("_test" in ddpclient._callbacks));
216+
assert(!("_test" in ddpclient._updatedCallbacks));
217+
assert(!("_test" in ddpclient._pendingMethods));
218+
});
157219
});
158220

159221

0 commit comments

Comments
 (0)