Skip to content

Commit

Permalink
fix: Subscription entity's subscribe() signature to accept timeto…
Browse files Browse the repository at this point in the history
…ken (#385)

* fix: subscribe() signature to accept timetoken in subscribe() method

* fix lint errors

* added test for custom timetoken with subscription

* format

* sync package-lock

* PubNub SDK v8.2.4 release.

---------

Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
mohitpubnub and pubnub-release-bot committed Jun 17, 2024
1 parent 4e18e9d commit 5dc7ec7
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 44 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
changelog:
- date: 2024-06-17
version: v8.2.4
changes:
- type: bug
text: "Subscription/SubscriptionSet's `subscribe()` method accepts `timetoken` parameter. Instead as in subscriptionOption."
- date: 2024-06-06
version: v8.2.3
changes:
Expand Down Expand Up @@ -1003,7 +1008,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '8.2.3'
version: '8.2.4'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -1019,7 +1024,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v8.2.3.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v8.2.4.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -1690,7 +1695,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v8.2.3/pubnub.8.2.3.js
location: https://github.com/pubnub/javascript/releases/download/v8.2.4/pubnub.8.2.4.js
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v8.2.4
June 17 2024

#### Fixed
- Subscription/SubscriptionSet's `subscribe()` method accepts `timetoken` parameter. Instead as in subscriptionOption.

## v8.2.3
June 06 2024

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.2.3.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.2.3.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.2.4.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.2.4.min.js

2. Configure your keys:

Expand Down
8 changes: 4 additions & 4 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3813,7 +3813,7 @@
return base.PubNubFile;
},
get version() {
return '8.2.3';
return '8.2.4';
},
getVersion() {
return this.version;
Expand Down Expand Up @@ -8963,9 +8963,9 @@
}

class SubscribeCapable {
subscribe() {
var _a, _b;
this.pubnub.subscribe(Object.assign({ channels: this.channelNames, channelGroups: this.groupNames }, (((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.cursor) === null || _b === void 0 ? void 0 : _b.timetoken) && { timetoken: this.options.cursor.timetoken })));
subscribe(subscribeParameters) {
const timetoken = subscribeParameters === null || subscribeParameters === void 0 ? void 0 : subscribeParameters.timetoken;
this.pubnub.subscribe(Object.assign({ channels: this.channelNames, channelGroups: this.groupNames }, (timetoken !== null && timetoken !== '' && { timetoken: timetoken })));
}
unsubscribe() {
this.pubnub.unsubscribe({
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const makeConfiguration = (base, setupCryptoModule) => {
return base.PubNubFile;
},
get version() {
return '8.2.3';
return '8.2.4';
},
getVersion() {
return this.version;
Expand Down
6 changes: 3 additions & 3 deletions lib/entities/SubscribeCapable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubscribeCapable = void 0;
class SubscribeCapable {
subscribe() {
var _a, _b;
this.pubnub.subscribe(Object.assign({ channels: this.channelNames, channelGroups: this.groupNames }, (((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.cursor) === null || _b === void 0 ? void 0 : _b.timetoken) && { timetoken: this.options.cursor.timetoken })));
subscribe(subscribeParameters) {
const timetoken = subscribeParameters === null || subscribeParameters === void 0 ? void 0 : subscribeParameters.timetoken;
this.pubnub.subscribe(Object.assign({ channels: this.channelNames, channelGroups: this.groupNames }, (timetoken !== null && timetoken !== '' && { timetoken: timetoken })));
}
unsubscribe() {
this.pubnub.unsubscribe({
Expand Down
4 changes: 3 additions & 1 deletion lib/types/entities/SubscribeCapable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export declare abstract class SubscribeCapable {
protected abstract eventEmitter: EventEmitter;
protected abstract pubnub: PubNub<unknown, unknown>;
protected abstract options?: SubscriptionOptions;
subscribe(): void;
subscribe(subscribeParameters?: {
timetoken?: string;
}): void;
unsubscribe(): void;
set onMessage(onMessageListener: (messageEvent: Subscription.Message) => void);
set onPresence(onPresenceListener: (presenceEvent: Subscription.Presence) => void);
Expand Down
4 changes: 0 additions & 4 deletions lib/types/entities/commonTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export type SubscriptionOptions = {
cursor?: {
timetoken?: string;
region?: number;
};
receivePresenceEvents?: boolean;
};
18 changes: 9 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pubnub",
"version": "8.2.3",
"version": "8.2.4",
"author": "PubNub <[email protected]>",
"description": "Publish & Subscribe Real-time Messaging with PubNub",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const makeConfiguration = (
return base.PubNubFile;
},
get version(): string {
return '8.2.3';
return '8.2.4';
},
getVersion(): string {
return this.version;
Expand Down
5 changes: 3 additions & 2 deletions src/entities/SubscribeCapable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ export abstract class SubscribeCapable {
protected abstract pubnub: PubNub<unknown, unknown>;
protected abstract options?: SubscriptionOptions;

subscribe() {
subscribe(subscribeParameters?: { timetoken?: string }) {
const timetoken = subscribeParameters?.timetoken;
this.pubnub.subscribe({
channels: this.channelNames,
channelGroups: this.groupNames,
...(this.options?.cursor?.timetoken && { timetoken: this.options.cursor.timetoken }),
...(timetoken !== null && timetoken !== '' && { timetoken: timetoken }),
});
}
unsubscribe() {
Expand Down
1 change: 0 additions & 1 deletion src/entities/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export type SubscriptionOptions = {
cursor?: { timetoken?: string; region?: number };
receivePresenceEvents?: boolean;
};
78 changes: 67 additions & 11 deletions test/integration/endpoints/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import utils from '../../utils';
describe('subscribe endpoints', () => {
let pubnubWithFiltering: PubNub;
let pubnub: PubNub;
let pubnubWithEE: PubNub;

before(() => {
nock.disableNetConnect();
Expand Down Expand Up @@ -38,6 +39,14 @@ describe('subscribe endpoints', () => {
filterExpression: 'hello!',
autoNetworkDetection: false,
});
pubnubWithEE = new PubNub({
subscribeKey: 'mySubKey',
publishKey: 'myPublishKey',
uuid: 'myUUID',
// @ts-expect-error Force override default value.
useRequestId: false,
enableEventEngine: true,
});
});

afterEach(() => {
Expand Down Expand Up @@ -65,9 +74,9 @@ describe('subscribe endpoints', () => {
if (status.category === 'PNConnectedCategory') {
try {
assert.equal(scope.isDone(), true);
assert.deepEqual(pubnub.getSubscribedChannels(), ["coolChannel", "coolChannel2"]);
assert.deepEqual(pubnub.getSubscribedChannels(), ['coolChannel', 'coolChannel2']);
assert.deepEqual(pubnub.getSubscribedChannelGroups(), []);
assert.deepEqual(status.affectedChannels, ["coolChannel", "coolChannel2"]);
assert.deepEqual(status.affectedChannels, ['coolChannel', 'coolChannel2']);
assert.deepEqual(status.affectedChannelGroups, []);
done();
} catch (error) {
Expand Down Expand Up @@ -99,12 +108,12 @@ describe('subscribe endpoints', () => {
pubnub.addListener({
status(status) {
if (status.category === 'PNConnectedCategory') {
try {
try {
assert.equal(scope.isDone(), true);
assert.deepEqual(pubnub.getSubscribedChannels(), ["coolChannel", "coolChannel2"]);
assert.deepEqual(pubnub.getSubscribedChannelGroups(), ["cg1", "cg2"]);
assert.deepEqual(status.affectedChannels, ["coolChannel", "coolChannel2"]);
assert.deepEqual(status.affectedChannelGroups, ["cg1", "cg2"]);
assert.deepEqual(pubnub.getSubscribedChannels(), ['coolChannel', 'coolChannel2']);
assert.deepEqual(pubnub.getSubscribedChannelGroups(), ['cg1', 'cg2']);
assert.deepEqual(status.affectedChannels, ['coolChannel', 'coolChannel2']);
assert.deepEqual(status.affectedChannelGroups, ['cg1', 'cg2']);
done();
} catch (error) {
done(error);
Expand Down Expand Up @@ -141,9 +150,9 @@ describe('subscribe endpoints', () => {
try {
assert.equal(scope.isDone(), true);
assert.deepEqual(pubnub.getSubscribedChannels(), []);
assert.deepEqual(pubnub.getSubscribedChannelGroups(), ["cg1", "cg2"]);
assert.deepEqual(pubnub.getSubscribedChannelGroups(), ['cg1', 'cg2']);
assert.deepEqual(status.affectedChannels, []);
assert.deepEqual(status.affectedChannelGroups, ["cg1", "cg2"]);
assert.deepEqual(status.affectedChannelGroups, ['cg1', 'cg2']);
done();
} catch (error) {
done(error);
Expand Down Expand Up @@ -176,9 +185,9 @@ describe('subscribe endpoints', () => {
if (status.category === 'PNConnectedCategory') {
try {
assert.equal(scope.isDone(), true);
assert.deepEqual(pubnubWithFiltering.getSubscribedChannels(), ["coolChannel", "coolChannel2"]);
assert.deepEqual(pubnubWithFiltering.getSubscribedChannels(), ['coolChannel', 'coolChannel2']);
assert.deepEqual(pubnubWithFiltering.getSubscribedChannelGroups(), []);
assert.deepEqual(status.affectedChannels, ["coolChannel", "coolChannel2"]);
assert.deepEqual(status.affectedChannels, ['coolChannel', 'coolChannel2']);
assert.deepEqual(status.affectedChannelGroups, []);
done();
} catch (error) {
Expand All @@ -192,4 +201,51 @@ describe('subscribe endpoints', () => {
channels: ['coolChannel', 'coolChannel2'],
});
});

it('supports timetoken', (done) => {
const scope0 = utils
.createNock()
.get('/v2/subscribe/mySubKey/c1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
tt: 0,
})
.reply(
200,
'{"t":{"t":"14523669555221452","r":1},"m":[{"a":"4","f":0,"i":"Client-g5d4g","p":{"t":"14607577960925503","r":1},"k":"sub-c-4cec9f8e-01fa-11e6-8180-0619f8945a4f","c":"coolChannel","d":{"text":"Enter Message Here"},"b":"coolChan-bnel"}]}',
{ 'content-type': 'text/javascript' },
);
const scope = utils
.createNock()
.get('/v2/subscribe/mySubKey/c1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
tt: '1234567890',
tr: 1,
})
.reply(
200,
'{"t":{"t":"146075779609322","r":1},"m":[{"a":"4","f":0,"i":"test","p":{"t":"14607577960925503","r":1},"k":"mySubKey","c":"c1","d":{"text":"customttresponse"},"b":"c1"}]}',
{ 'content-type': 'text/javascript' },
);

pubnubWithEE.addListener({
message(message) {
try {
assert.deepEqual(message.message, { text: 'customttresponse' });
assert.equal(scope.isDone(), true);
done();
} catch (error) {
done(error);
}
},
});
const channel = pubnubWithEE.channel('c1');
const subscription = channel.subscription();
subscription.subscribe({ timetoken: '1234567890' });
});
});

0 comments on commit 5dc7ec7

Please sign in to comment.