Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: App context event handling for channel and membership #362

Merged
merged 8 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6659,8 +6659,9 @@
issuingClientId: envelope.i,
subscribeKey: envelope.k,
originationTimetoken: envelope.o,
userMetadata: envelope.u,
publishMetaData: {
timetoken: envelope.p.t,
publishTimetoken: envelope.p.t,
region: envelope.p.r,
},
};
Expand Down Expand Up @@ -7640,10 +7641,10 @@
}
};
EventEngine.prototype.getSubscribedChannels = function () {
return this.channels.slice(0);
return Array.from(new Set(this.channels.slice(0)));
};
EventEngine.prototype.getSubscribedChannelGroups = function () {
return this.groups.slice(0);
return Array.from(new Set(this.groups.slice(0)));
};
EventEngine.prototype.dispose = function () {
this.disconnect();
Expand Down Expand Up @@ -8166,13 +8167,13 @@
this.listenerManager.announceUser(userEvent);
this._announce('user', userEvent, announce.channel, announce.subscription);
}
else if (message.payload.type === 'channel') {
else if (e.payload.type === 'channel') {
var eventData = this._renameChannelField(announce);
var spaceEvent = __assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), type: 'space' }) });
this.listenerManager.announceSpace(spaceEvent);
this._announce('space', spaceEvent, announce.channel, announce.subscription);
}
else if (message.payload.type === 'membership') {
else if (e.payload.type === 'membership') {
var eventData = this._renameChannelField(announce);
var _a = eventData.message.data, user = _a.uuid, space = _a.channel, membershipData = __rest(_a, ["uuid", "channel"]);
membershipData.user = user;
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/core/components/eventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ var EventEmitter = /** @class */ (function () {
this.listenerManager.announceUser(userEvent);
this._announce('user', userEvent, announce.channel, announce.subscription);
}
else if (message.payload.type === 'channel') {
else if (e.payload.type === 'channel') {
var eventData = this._renameChannelField(announce);
var spaceEvent = __assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), type: 'space' }) });
this.listenerManager.announceSpace(spaceEvent);
this._announce('space', spaceEvent, announce.channel, announce.subscription);
}
else if (message.payload.type === 'membership') {
else if (e.payload.type === 'membership') {
var eventData = this._renameChannelField(announce);
var _a = eventData.message.data, user = _a.uuid, space = _a.channel, membershipData = __rest(_a, ["uuid", "channel"]);
membershipData.user = user;
Expand Down
3 changes: 2 additions & 1 deletion lib/core/endpoints/subscriptionUtils/receiveMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ var endpoint = {
issuingClientId: envelope.i,
subscribeKey: envelope.k,
originationTimetoken: envelope.o,
userMetadata: envelope.u,
publishMetaData: {
timetoken: envelope.p.t,
publishTimetoken: envelope.p.t,
region: envelope.p.r,
},
};
Expand Down
4 changes: 2 additions & 2 deletions lib/event-engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ var EventEngine = /** @class */ (function () {
}
};
EventEngine.prototype.getSubscribedChannels = function () {
return this.channels.slice(0);
return Array.from(new Set(this.channels.slice(0)));
};
EventEngine.prototype.getSubscribedChannelGroups = function () {
return this.groups.slice(0);
return Array.from(new Set(this.groups.slice(0)));
};
EventEngine.prototype.dispose = function () {
this.disconnect();
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions src/core/components/eventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default class EventEmitter {
};
this.listenerManager.announceUser(userEvent);
this._announce('user', userEvent, announce.channel, announce.subscription);
} else if (message.payload.type === 'channel') {
} else if (e.payload.type === 'channel') {
const eventData = this._renameChannelField(announce);
const spaceEvent = {
...eventData,
Expand All @@ -118,7 +118,7 @@ export default class EventEmitter {
};
this.listenerManager.announceSpace(spaceEvent);
this._announce('space', spaceEvent, announce.channel, announce.subscription);
} else if (message.payload.type === 'membership') {
} else if (e.payload.type === 'membership') {
const eventData = this._renameChannelField(announce);
const { uuid: user, channel: space, ...membershipData } = eventData.message.data;
membershipData.user = user;
Expand Down
3 changes: 2 additions & 1 deletion src/core/endpoints/subscriptionUtils/receiveMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ const endpoint = {
issuingClientId: envelope.i,
subscribeKey: envelope.k,
originationTimetoken: envelope.o,
userMetadata: envelope.u,
publishMetaData: {
timetoken: envelope.p.t,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to rename field? It is alsready inside of publishMetaData and it doesn't look like that we need to clarify once again that it is publishTimetoken. Is there other reason for it?

Copy link
Contributor Author

@mohitpubnub mohitpubnub Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was typo before.
In eventEmitter (the same code was there in subscriptionManager before) while creating event information object, this was the way we used to assign.

so instead of changing property name in all places (which may be error prone like we have now with channel/membership) I rename it here in subscription utility method.. same as older endpoint implementation here

in short, It's renaming to make is compatible with old implementation logic in event handling

Copy link
Contributor Author

@mohitpubnub mohitpubnub Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be refactored other way around than as it's there!
For consistent naming, Renamed property name in old subscribe endpoint definition as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parfeon refactored!

publishTimetoken: envelope.p.t,
region: envelope.p.r,
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/event-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ export class EventEngine {
}

getSubscribedChannels(): string[] {
return this.channels.slice(0);
return Array.from(new Set(this.channels));
}

getSubscribedChannelGroups(): string[] {
return this.groups.slice(0);
return Array.from(new Set(this.groups));
}

dispose(): void {
Expand Down
138 changes: 130 additions & 8 deletions test/integration/components/listeners.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,24 +449,26 @@ describe('#listeners', () => {
);
subscription.removeListener(listener);
subscription.subscribe();
expect(pubnub.getSubscribedChannels()).to.deep.equal(['ch1']);
const actual = await messagePromise;
expect(JSON.stringify(actual.message)).to.equal('{"message":"My message!"}');
expect(messages.length).to.equal(0);

const subscriptionCh2 = pubnub.channel('ch2').subscription();
subscriptionCh2.subscribe();
expect(pubnub.getSubscribedChannels()).to.deep.equal(['ch1', 'ch2']);

const subscriptionCh3 = pubnub.channel('ch3').subscription();
const subscriptionSetCh23 = subscriptionCh3.addSubscription(pubnub.channel('ch2').subscription());
const messagePromiseChannel2 = new Promise((resolveMessage) =>
subscriptionSetCh23.addListener({
message: (m) => resolveMessage(m),
}),
);
subscriptionSetCh23.subscribe();
expect(pubnub.getSubscribedChannels()).to.deep.equal(['ch1', 'ch2', 'ch3']);

subscription.unsubscribe();
expect(pubnub.getSubscribedChannels()).to.deep.equal(['ch2', 'ch3']);

subscriptionCh2.unsubscribe();
const actualChannel2MessageAfterOneUnsubCh2 = await messagePromiseChannel2;
expect(pubnub.getSubscribedChannels()).to.deep.equal(['ch2', 'ch3']);
pubnub.destroy();
expect(JSON.stringify(actualChannel2MessageAfterOneUnsubCh2.message)).to.equal('{"ch2":"My message!"}');
});

it('should work with event type specific listener registraction', async () => {
Expand Down Expand Up @@ -538,7 +540,127 @@ describe('#listeners', () => {
subscription.subscribe();
const actual = await presencePromise;
expect(JSON.stringify(actual)).to.equal(
'{"channel":"ch1","subscription":null,"action":"join","occupancy":1,"uuid":"testid","timestamp":1461451222,"actualChannel":null,"subscribedChannel":"ch1-pnpres"}',
'{"channel":"ch1","subscription":null,"action":"join","timetoken":"8","occupancy":1,"uuid":"testid","timestamp":1461451222,"actualChannel":null,"subscribedChannel":"ch1-pnpres"}',
);
});

it('should work with objects data - membership', async () => {
utils
.createNock()
.get('/v2/subscribe/mySubKey/ch1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
state: '{}',
tt: 0,
})
.reply(200, '{"t":{"t":"3","r":1},"m":[]}');
utils
.createNock()
.get('/v2/subscribe/mySubKey/ch1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
tt: '3',
tr: 1,
})
.reply(
200,
`{"t":{"t":"17087917617921775","r":31},"m":[{"a":"0","f":0,"e":2,"p":{"t":"17087917617921775","r":31},"k":"mySubKey","c":"ch1","d":{"source":"objects","version":"2.0","event":"set","type":"membership","data":{"channel":{"id":"c1"},"eTag":"AZO/t53al7m8fw","updated":"2024-02-24T16:22:41.786844939Z","uuid":{"id":"userIdTest"}}},"b":"ch1"}]}`,
);
var channel = pubnub.channel('ch1');
var subscription = channel.subscription();
var membershipPromise = new Promise((resolveObjects) =>
subscription.addListener({
objects: (objectsEvent) => resolveObjects(objectsEvent),
}),
);
subscription.subscribe();
const actual = await membershipPromise;
expect(JSON.stringify(actual)).to.equal(
`{"channel":"ch1","subscription":null,"timetoken":"17087917617921775","message":{"event":"set","type":"membership","data":{"channel":{"id":"c1"},"eTag":"AZO/t53al7m8fw","updated":"2024-02-24T16:22:41.786844939Z","uuid":{"id":"userIdTest"}}}}`,
);
});

it('should work with objects data - channel', async () => {
utils
.createNock()
.get('/v2/subscribe/mySubKey/ch1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
state: '{}',
tt: 0,
})
.reply(200, '{"t":{"t":"3","r":1},"m":[]}');
utils
.createNock()
.get('/v2/subscribe/mySubKey/ch1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
tt: '3',
tr: 1,
})
.reply(
200,
`{"t":{"t":"17087625097482155","r":33},"m":[{"a":"0","f":0,"e":2,"p":{"t":"17087625097482155","r":33},"k":"mySubKey","c":"ch1","d":{"source":"objects","version":"2.0","event":"set","type":"channel","data":{"custom":{"occupancy":1},"eTag":"125aasww","id":"user-1","updated":"2024-02-24T08:15:09.744661Z"}},"b":"ch1"}]}`,
);
var channel = pubnub.channel('ch1');
var subscription = channel.subscription();
var objectsEventPromise = new Promise((resolveObjects) =>
subscription.addListener({
objects: (objectsEvent) => resolveObjects(objectsEvent),
}),
);
subscription.subscribe();
const actual = await objectsEventPromise;
expect(JSON.stringify(actual)).to.equal(
'{"channel":"ch1","subscription":null,"timetoken":"17087625097482155","message":{"event":"set","type":"channel","data":{"custom":{"occupancy":1},"eTag":"125aasww","id":"user-1","updated":"2024-02-24T08:15:09.744661Z"}}}',
);
});

it('should work with message actions', async () => {
utils
.createNock()
.get('/v2/subscribe/mySubKey/ch1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
state: '{}',
tt: 0,
})
.reply(200, '{"t":{"t":"3","r":1},"m":[]}');
utils
.createNock()
.get('/v2/subscribe/mySubKey/ch1/0')
.query({
pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`,
uuid: 'myUUID',
ee: '',
tt: '3',
tr: 1,
})
.reply(
200,
`{"t":{"t":"17087942541729876","r":31},"m":[{"a":"0","f":0,"e":3,"i":"userIdTest","p":{"t":"17087942541729876","r":31},"k":"mySubKey","c":"ch1","d":{"source":"actions","version":"1.0","data":{"messageTimetoken":"17087942466262824","type":"reaction","value":"smiley","actionTimetoken":"17087942541639760"},"event":"added"},"b":"ch1"}]}`,
);
var channel = pubnub.channel('ch1');
var subscription = channel.subscription();
var messageActionPromise = new Promise((resolveMessageActionEvent) =>
subscription.addListener({
messageAction: (messageActionEvent) => resolveMessageActionEvent(messageActionEvent),
}),
);
subscription.subscribe();
const actual = await messageActionPromise;
expect(JSON.stringify(actual)).to.equal(
'{"channel":"ch1","subscription":null,"timetoken":"17087942541729876","publisher":"userIdTest","data":{"messageTimetoken":"17087942466262824","actionTimetoken":"17087942541639760","type":"reaction","uuid":"userIdTest","value":"smiley"},"event":"added"}',
);
});
});
Loading