Skip to content

Commit

Permalink
fix(configuration): fix automatic heartbeatInterval set
Browse files Browse the repository at this point in the history
Fix issue because of which `heartbeatInterval` wasn't computed if `presenceTimeout` provided
during PubNub client configuration
  • Loading branch information
parfeon committed Jun 4, 2024
1 parent 26c4d73 commit 6e101b8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
7 changes: 5 additions & 2 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3525,7 +3525,7 @@
* @internal
*/
const setDefaults$1 = (configuration) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
// Copy configuration.
const configurationCopy = Object.assign({}, configuration);
(_a = configurationCopy.logVerbosity) !== null && _a !== void 0 ? _a : (configurationCopy.logVerbosity = USE_VERBOSE_LOGGING);
Expand Down Expand Up @@ -3560,7 +3560,10 @@
// eslint-disable-next-line no-console
console.log('WARNING: Presence timeout is less than the minimum. Using minimum value: ', PRESENCE_TIMEOUT_MINIMUM);
}
(_q = configurationCopy.presenceTimeout) !== null && _q !== void 0 ? _q : (configurationCopy.presenceTimeout = PRESENCE_TIMEOUT);
if (configurationCopy.presenceTimeout !== undefined)
configurationCopy.heartbeatInterval = configurationCopy.presenceTimeout / 2 - 1;
else
configurationCopy.presenceTimeout = PRESENCE_TIMEOUT;
// Apply extended configuration defaults.
let announceSuccessfulHeartbeats = ANNOUNCE_HEARTBEAT_SUCCESS;
let announceFailedHeartbeats = ANNOUNCE_HEARTBEAT_FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions lib/core/interfaces/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const PRESENCE_TIMEOUT_MINIMUM = 20;
* @internal
*/
const setDefaults = (configuration) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
// Copy configuration.
const configurationCopy = Object.assign({}, configuration);
(_a = configurationCopy.logVerbosity) !== null && _a !== void 0 ? _a : (configurationCopy.logVerbosity = USE_VERBOSE_LOGGING);
Expand Down Expand Up @@ -129,7 +129,10 @@ const setDefaults = (configuration) => {
// eslint-disable-next-line no-console
console.log('WARNING: Presence timeout is less than the minimum. Using minimum value: ', PRESENCE_TIMEOUT_MINIMUM);
}
(_q = configurationCopy.presenceTimeout) !== null && _q !== void 0 ? _q : (configurationCopy.presenceTimeout = PRESENCE_TIMEOUT);
if (configurationCopy.presenceTimeout !== undefined)
configurationCopy.heartbeatInterval = configurationCopy.presenceTimeout / 2 - 1;
else
configurationCopy.presenceTimeout = PRESENCE_TIMEOUT;
// Apply extended configuration defaults.
let announceSuccessfulHeartbeats = ANNOUNCE_HEARTBEAT_SUCCESS;
let announceFailedHeartbeats = ANNOUNCE_HEARTBEAT_FAILURE;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/core/interfaces/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,9 @@ export const setDefaults = (configuration: UserConfiguration): ExtendedConfigura
console.log('WARNING: Presence timeout is less than the minimum. Using minimum value: ', PRESENCE_TIMEOUT_MINIMUM);
}

configurationCopy.presenceTimeout ??= PRESENCE_TIMEOUT;
if (configurationCopy.presenceTimeout !== undefined)
configurationCopy.heartbeatInterval = configurationCopy.presenceTimeout / 2 - 1;
else configurationCopy.presenceTimeout = PRESENCE_TIMEOUT;

// Apply extended configuration defaults.
let announceSuccessfulHeartbeats = ANNOUNCE_HEARTBEAT_SUCCESS;
Expand Down
22 changes: 22 additions & 0 deletions test/integration/components/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import assert from 'assert';
import PubNub from '../../../src/node/index';
import { PrivateClientConfiguration } from '../../../src/core/interfaces/configuration';

describe('components/config', () => {
describe('AuthKey parameter', () => {
Expand Down Expand Up @@ -82,5 +83,26 @@ describe('components/config', () => {
pubnub.setUUID(' ');
});
});

it('heartbeatInterval not set if presenceTimeout not set', () => {
const pubnub = new PubNub({
subscribeKey: 'mySubKey',
publishKey: 'myPublishKey',
uuid: 'myUUID',
});
assert.equal((pubnub.configuration as PrivateClientConfiguration).getPresenceTimeout(), 300);
assert.equal((pubnub.configuration as PrivateClientConfiguration).getHeartbeatInterval(), undefined);
});

it('heartbeatInterval is set by formula when presenceTimeout is set', () => {
const pubnub = new PubNub({
subscribeKey: 'mySubKey',
publishKey: 'myPublishKey',
presenceTimeout: 30,
uuid: 'myUUID',
});
assert.equal((pubnub.configuration as PrivateClientConfiguration).getPresenceTimeout(), 30);
assert.equal((pubnub.configuration as PrivateClientConfiguration).getHeartbeatInterval(), 30 / 2 - 1);
});
});
});

0 comments on commit 6e101b8

Please sign in to comment.