Skip to content

Commit b02dd44

Browse files
committed
add stop_timers() method
1 parent ab8ecc2 commit b02dd44

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎core/pubnub-common.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ function PN_API(setup) {
291291
, jsonp_cb = setup['jsonp_cb'] || function() { return 0 }
292292
, db = setup['db'] || {'get': function(){}, 'set': function(){}}
293293
, CIPHER_KEY = setup['cipher_key']
294-
, UUID = setup['uuid'] || ( db && db['get'](SUBSCRIBE_KEY+'uuid') || '');
294+
, UUID = setup['uuid'] || ( db && db['get'](SUBSCRIBE_KEY+'uuid') || '')
295+
, _poll_timer
296+
, _poll_timer2;
295297

296298
var crypto_obj = setup['crypto_obj'] ||
297299
{
@@ -1351,6 +1353,10 @@ function PN_API(setup) {
13511353
fail : function(response) { _invoke_error(response, err); }
13521354
});
13531355
},
1356+
'stop_timers': function () {
1357+
clearTimeout(_poll_timer);
1358+
clearTimeout(_poll_timer2);
1359+
},
13541360

13551361
// Expose PUBNUB Functions
13561362
'xdr' : xdr,
@@ -1372,7 +1378,8 @@ function PN_API(setup) {
13721378
_is_online() || _reset_offline( 1, {
13731379
"error" : "Offline. Please check your network settings. "
13741380
});
1375-
timeout( _poll_online, SECOND );
1381+
_poll_timer && clearTimeout(_poll_timer);
1382+
_poll_timer = timeout( _poll_online, SECOND );
13761383
}
13771384

13781385
function _poll_online2() {
@@ -1382,20 +1389,24 @@ function PN_API(setup) {
13821389
"error" : "Heartbeat failed to connect to Pubnub Servers." +
13831390
"Please check your network settings."
13841391
});
1385-
timeout( _poll_online2, KEEPALIVE );
1392+
_poll_timer2 && clearTimeout(_poll_timer2);
1393+
_poll_timer2 = timeout( _poll_online2, KEEPALIVE );
13861394
});
13871395
}
13881396

13891397
function _reset_offline(err, msg) {
13901398
SUB_RECEIVER && SUB_RECEIVER(err, msg);
13911399
SUB_RECEIVER = null;
1400+
1401+
clearTimeout(_poll_timer);
1402+
clearTimeout(_poll_timer2);
13921403
}
13931404

13941405
if (!UUID) UUID = SELF['uuid']();
13951406
db['set']( SUBSCRIBE_KEY + 'uuid', UUID );
13961407

1397-
timeout( _poll_online, SECOND );
1398-
timeout( _poll_online2, KEEPALIVE );
1408+
_poll_timer = timeout( _poll_online, SECOND );
1409+
_poll_timer2 = timeout( _poll_online2, KEEPALIVE );
13991410
PRESENCE_HB_TIMEOUT = timeout( start_presence_heartbeat, ( PRESENCE_HB_INTERVAL - 3 ) * SECOND ) ;
14001411

14011412
// Detect Age of Message

‎node.js/tests/shutdown-test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var Pubnub = require('../pubnub'),
2+
pubnub = Pubnub.init({
3+
publish_key : 'demo',
4+
subscribe_key : 'demo'
5+
}),
6+
pubnub2 = Pubnub.init({
7+
publish_key : 'demo',
8+
subscribe_key : 'demo'
9+
});
10+
11+
pubnub.subscribe({
12+
channel: 'demo',
13+
connect: function () {
14+
pubnub.publish({
15+
channel: 'demo',
16+
message: 'hello'
17+
});
18+
},
19+
callback: function (message) {
20+
console.log(message);
21+
pubnub.unsubscribe({
22+
channel: 'demo'
23+
});
24+
}
25+
});
26+
27+
pubnub2.stop_timers();

0 commit comments

Comments
 (0)