Skip to content

Commit 6b5d4fd

Browse files
author
Devendra
committed
adding global error callback, presence heartbeat value validation, removing duplicate code
1 parent bf483c9 commit 6b5d4fd

24 files changed

Lines changed: 706 additions & 706 deletions

File tree

core/pubnub-common.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var NOW = 1
99
, SECOND = 1000 // A THOUSAND MILLISECONDS.
1010
, URLBIT = '/'
1111
, PARAMSBIT = '&'
12+
, PRESENCE_HB_THRESHOLD = 5
1213
, REPL = /{([\w\-]+)}/g;
1314

1415
/**
@@ -213,7 +214,7 @@ function PN_API(setup) {
213214
, TIMETOKEN = 0
214215
, CHANNELS = {}
215216
, PRESENCE_HB_TIMEOUT = null
216-
, PRESENCE_HB_INTERVAL = setup['pnexpires'] || 0
217+
, PRESENCE_HB_INTERVAL = validate_presence_heartbeat(setup['pnexpires'] || 0, setup['error'])
217218
, PRESENCE_HB_RUNNING = false
218219
, NO_WAIT_FOR_PENDING = setup['no_wait_for_pending']
219220
, xdr = setup['xdr']
@@ -230,6 +231,24 @@ function PN_API(setup) {
230231
'decrypt' : function(b,key){return b}
231232
};
232233

234+
function validate_presence_heartbeat(pnexpires, cur_pnexpires, error) {
235+
var err = false;
236+
237+
if (typeof pnexpires === 'number') {
238+
if (pnexpires > PRESENCE_HB_THRESHOLD || pnexpires == 0)
239+
err = false;
240+
else
241+
err = true;
242+
} else {
243+
err = true;
244+
}
245+
246+
if (err) {
247+
error && error("Presence Heartbeat value invalid. Valid range ( x > " + PRESENCE_HB_THRESHOLD + " or x = 0). Current Value : " + (cur_pnexpires || PRESENCE_HB_THRESHOLD));
248+
return cur_pnexpires || PRESENCE_HB_THRESHOLD;
249+
} else return pnexpires;
250+
}
251+
233252
function encrypt(input, key) {
234253
return crypto_obj['encrypt'](input, key || CIPHER_KEY) || input;
235254
}
@@ -345,11 +364,7 @@ function PN_API(setup) {
345364
return PRESENCE_HB_INTERVAL;
346365
},
347366
'set_pnexpires' : function(pnexpires) {
348-
if (pnexpires > 0 && pnexpires <= 5) {
349-
error_common("Pubnub Presence Expiry Timeout cannot be less than 5 seconds");
350-
return;
351-
}
352-
PRESENCE_HB_INTERVAL = pnexpires || 0;
367+
PRESENCE_HB_INTERVAL = validate_presence_heartbeat(pnexpires, PRESENCE_HB_INTERVAL, error);
353368
CONNECT();
354369
_presence_heartbeat();
355370
},
@@ -612,7 +627,7 @@ function PN_API(setup) {
612627
, sub_timeout = args['timeout'] || SUB_TIMEOUT
613628
, windowing = args['windowing'] || SUB_WINDOWING
614629
, metadata = args['metadata']
615-
, pnexpires = args['pnexpires'] || 0
630+
, pnexpires = args['pnexpires']
616631
, restore = args['restore'];
617632

618633
// Restore Enabled?
@@ -626,7 +641,7 @@ function PN_API(setup) {
626641
if (!callback) return error('Missing Callback');
627642
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
628643

629-
if (pnexpires) PRESENCE_HB_INTERVAL = pnexpires;
644+
if (pnexpires || pnexpires === 0) PRESENCE_HB_INTERVAL = validate_presence_heartbeat(pnexpires, PRESENCE_HB_INTERVAL, error)
630645

631646
// Setup Channel(s)
632647
each( (channel.join ? channel.join(',') : ''+channel).split(','),
@@ -1184,21 +1199,6 @@ function PN_API(setup) {
11841199
});
11851200
}
11861201

1187-
function _presence_heartbeat() {
1188-
1189-
if (!PRESENCE_HB_INTERVAL || PRESENCE_HB_INTERVAL >= 320){
1190-
PRESENCE_HB_RUNNING = false;
1191-
return;
1192-
}
1193-
1194-
clearTimeout(PRESENCE_HB_TIMEOUT);
1195-
1196-
PRESENCE_HB_RUNNING = true;
1197-
SELF['presence_heartbeat'](function(success){
1198-
PRESENCE_HB_TIMEOUT = timeout( _presence_heartbeat, (PRESENCE_HB_INTERVAL - 3) * SECOND );
1199-
});
1200-
}
1201-
12021202
function start_presence_heartbeat() {
12031203
!PRESENCE_HB_RUNNING && _presence_heartbeat();
12041204
}

modern/pubnub.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var NOW = 1
1010
, SECOND = 1000 // A THOUSAND MILLISECONDS.
1111
, URLBIT = '/'
1212
, PARAMSBIT = '&'
13+
, PRESENCE_HB_THRESHOLD = 5
1314
, REPL = /{([\w\-]+)}/g;
1415

1516
/**
@@ -214,7 +215,7 @@ function PN_API(setup) {
214215
, TIMETOKEN = 0
215216
, CHANNELS = {}
216217
, PRESENCE_HB_TIMEOUT = null
217-
, PRESENCE_HB_INTERVAL = setup['pnexpires'] || 0
218+
, PRESENCE_HB_INTERVAL = validate_presence_heartbeat(setup['pnexpires'] || 0, setup['error'])
218219
, PRESENCE_HB_RUNNING = false
219220
, NO_WAIT_FOR_PENDING = setup['no_wait_for_pending']
220221
, xdr = setup['xdr']
@@ -231,6 +232,24 @@ function PN_API(setup) {
231232
'decrypt' : function(b,key){return b}
232233
};
233234

235+
function validate_presence_heartbeat(pnexpires, cur_pnexpires, error) {
236+
var err = false;
237+
238+
if (typeof pnexpires === 'number') {
239+
if (pnexpires > PRESENCE_HB_THRESHOLD || pnexpires == 0)
240+
err = false;
241+
else
242+
err = true;
243+
} else {
244+
err = true;
245+
}
246+
247+
if (err) {
248+
error && error("Presence Heartbeat value invalid. Valid range ( x > " + PRESENCE_HB_THRESHOLD + " or x = 0). Current Value : " + (cur_pnexpires || PRESENCE_HB_THRESHOLD));
249+
return cur_pnexpires || PRESENCE_HB_THRESHOLD;
250+
} else return pnexpires;
251+
}
252+
234253
function encrypt(input, key) {
235254
return crypto_obj['encrypt'](input, key || CIPHER_KEY) || input;
236255
}
@@ -346,11 +365,7 @@ function PN_API(setup) {
346365
return PRESENCE_HB_INTERVAL;
347366
},
348367
'set_pnexpires' : function(pnexpires) {
349-
if (pnexpires > 0 && pnexpires <= 5) {
350-
error_common("Pubnub Presence Expiry Timeout cannot be less than 5 seconds");
351-
return;
352-
}
353-
PRESENCE_HB_INTERVAL = pnexpires || 0;
368+
PRESENCE_HB_INTERVAL = validate_presence_heartbeat(pnexpires, PRESENCE_HB_INTERVAL, error);
354369
CONNECT();
355370
_presence_heartbeat();
356371
},
@@ -613,7 +628,7 @@ function PN_API(setup) {
613628
, sub_timeout = args['timeout'] || SUB_TIMEOUT
614629
, windowing = args['windowing'] || SUB_WINDOWING
615630
, metadata = args['metadata']
616-
, pnexpires = args['pnexpires'] || 0
631+
, pnexpires = args['pnexpires']
617632
, restore = args['restore'];
618633

619634
// Restore Enabled?
@@ -627,7 +642,7 @@ function PN_API(setup) {
627642
if (!callback) return error('Missing Callback');
628643
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
629644

630-
if (pnexpires) PRESENCE_HB_INTERVAL = pnexpires;
645+
if (pnexpires || pnexpires === 0) PRESENCE_HB_INTERVAL = validate_presence_heartbeat(pnexpires, PRESENCE_HB_INTERVAL, error)
631646

632647
// Setup Channel(s)
633648
each( (channel.join ? channel.join(',') : ''+channel).split(','),
@@ -1185,21 +1200,6 @@ function PN_API(setup) {
11851200
});
11861201
}
11871202

1188-
function _presence_heartbeat() {
1189-
1190-
if (!PRESENCE_HB_INTERVAL || PRESENCE_HB_INTERVAL >= 320){
1191-
PRESENCE_HB_RUNNING = false;
1192-
return;
1193-
}
1194-
1195-
clearTimeout(PRESENCE_HB_TIMEOUT);
1196-
1197-
PRESENCE_HB_RUNNING = true;
1198-
SELF['presence_heartbeat'](function(success){
1199-
PRESENCE_HB_TIMEOUT = timeout( _presence_heartbeat, (PRESENCE_HB_INTERVAL - 3) * SECOND );
1200-
});
1201-
}
1202-
12031203
function start_presence_heartbeat() {
12041204
!PRESENCE_HB_RUNNING && _presence_heartbeat();
12051205
}
@@ -2679,7 +2679,7 @@ function CREATE_PUBNUB(setup) {
26792679

26802680
setup['db'] = db;
26812681
setup['xdr'] = xdr;
2682-
setup['error'] = error;
2682+
setup['error'] = setup['error'] || error;
26832683
setup['PNSDK'] = PNSDK;
26842684
setup['hmac_SHA256']= get_hmac_SHA256;
26852685
setup['crypto_obj'] = crypto_obj();

0 commit comments

Comments
 (0)