Skip to content

Commit

Permalink
fix(event-engine): fix missing presence events on subscription
Browse files Browse the repository at this point in the history
Fix issue because of which presence events not delivered to the `Subscription` and
`SubscriptionSet` objects (only global listeners).
  • Loading branch information
parfeon committed Nov 17, 2024
1 parent 2b523b2 commit 02e3db1
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 31 deletions.
19 changes: 13 additions & 6 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6403,6 +6403,7 @@
* @param event - Received real-time event.
*/
emitEvent(event) {
var _a;
if (event.type === PubNubEventType.Message) {
this.listenerManager.announceMessage(event.data);
this.announce('message', event.data, event.data.channel, event.data.subscription);
Expand All @@ -6413,7 +6414,7 @@
}
else if (event.type === PubNubEventType.Presence) {
this.listenerManager.announcePresence(event.data);
this.announce('presence', event.data, event.data.channel, event.data.subscription);
this.announce('presence', event.data, (_a = event.data.subscription) !== null && _a !== void 0 ? _a : event.data.channel, event.data.subscription);
}
else if (event.type === PubNubEventType.AppContext) {
const { data: objectEvent } = event;
Expand Down Expand Up @@ -9963,7 +9964,7 @@
* types of events.
*/
addListener(listener) {
this.eventEmitter.addListener(listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
this.eventEmitter.addListener(listener, this.channelNames, this.groupNames);
}
/**
* Remove events handler.
Expand Down Expand Up @@ -10065,7 +10066,7 @@
this.subscriptionList.push(subscription);
});
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Add additional entity's subscription to the subscription set.
Expand Down Expand Up @@ -10187,7 +10188,7 @@
this.pubnub = pubnub;
this.eventEmitter = eventEmitter;
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Merge entities' subscription objects into subscription set.
Expand Down Expand Up @@ -10283,9 +10284,12 @@
*/
subscription(subscriptionOptions) {
{
const channelGroups = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channelGroups.push(`${this.name}-pnpres`);
return new Subscription({
channels: [],
channelGroups: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups,
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
Expand Down Expand Up @@ -10370,8 +10374,11 @@
*/
subscription(subscriptionOptions) {
{
const channels = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channels.push(`${this.name}-pnpres`);
return new Subscription({
channels: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channels,
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/core/components/eventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EventEmitter {
* @param event - Received real-time event.
*/
emitEvent(event) {
var _a;
if (event.type === subscribe_1.PubNubEventType.Message) {
this.listenerManager.announceMessage(event.data);
this.announce('message', event.data, event.data.channel, event.data.subscription);
Expand All @@ -55,7 +56,7 @@ class EventEmitter {
}
else if (event.type === subscribe_1.PubNubEventType.Presence) {
this.listenerManager.announcePresence(event.data);
this.announce('presence', event.data, event.data.channel, event.data.subscription);
this.announce('presence', event.data, (_a = event.data.subscription) !== null && _a !== void 0 ? _a : event.data.channel, event.data.subscription);
}
else if (event.type === subscribe_1.PubNubEventType.AppContext) {
const { data: objectEvent } = event;
Expand Down
5 changes: 4 additions & 1 deletion lib/entities/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ class Channel {
*/
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channels = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channels.push(`${this.name}-pnpres`);
return new Subscription_1.Subscription({
channels: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channels,
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
Expand Down
5 changes: 4 additions & 1 deletion lib/entities/ChannelGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ class ChannelGroup {
*/
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channelGroups = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channelGroups.push(`${this.name}-pnpres`);
return new Subscription_1.Subscription({
channels: [],
channelGroups: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups,
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/SubscribeCapable.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SubscribeCapable {
* types of events.
*/
addListener(listener) {
this.eventEmitter.addListener(listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
this.eventEmitter.addListener(listener, this.channelNames, this.groupNames);
}
/**
* Remove events handler.
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Subscription extends SubscribeCapable_1.SubscribeCapable {
this.pubnub = pubnub;
this.eventEmitter = eventEmitter;
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Merge entities' subscription objects into subscription set.
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/SubscriptionSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SubscriptionSet extends SubscribeCapable_1.SubscribeCapable {
this.subscriptionList.push(subscription);
});
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Add additional entity's subscription to the subscription set.
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class EventEmitter {
this.announce('signal', event.data, event.data.channel, event.data.subscription);
} else if (event.type === PubNubEventType.Presence) {
this.listenerManager.announcePresence(event.data);
this.announce('presence', event.data, event.data.channel, event.data.subscription);
this.announce('presence', event.data, event.data.subscription ?? event.data.channel, event.data.subscription);
} else if (event.type === PubNubEventType.AppContext) {
const { data: objectEvent } = event;
const { message: object } = objectEvent;
Expand Down
6 changes: 5 additions & 1 deletion src/entities/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ export class Channel {
*/
subscription(subscriptionOptions?: SubscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channels = [this.name];
if (subscriptionOptions?.receivePresenceEvents && !this.name.endsWith('-pnpres'))
channels.push(`${this.name}-pnpres`);

return new Subscription({
channels: subscriptionOptions?.receivePresenceEvents ? [this.name, `${this.name}-pnpres`] : [this.name],
channels,
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
Expand Down
6 changes: 5 additions & 1 deletion src/entities/ChannelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ export class ChannelGroup {
*/
subscription(subscriptionOptions?: SubscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channelGroups = [this.name];
if (subscriptionOptions?.receivePresenceEvents && !this.name.endsWith('-pnpres'))
channelGroups.push(`${this.name}-pnpres`);

return new Subscription({
channels: [],
channelGroups: subscriptionOptions?.receivePresenceEvents ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups,
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
Expand Down
6 changes: 1 addition & 5 deletions src/entities/SubscribeCapable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ export abstract class SubscribeCapable {
* types of events.
*/
addListener(listener: Listener) {
this.eventEmitter.addListener(
listener,
this.channelNames.filter((c) => !c.endsWith('-pnpres')),
this.groupNames.filter((cg) => !cg.endsWith('-pnpres')),
);
this.eventEmitter.addListener(listener, this.channelNames, this.groupNames);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/entities/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ export class Subscription extends SubscribeCapable {
this.pubnub = pubnub;
this.eventEmitter = eventEmitter;
this.listener = {};
eventEmitter.addListener(
this.listener,
this.channelNames.filter((c) => !c.endsWith('-pnpres')),
this.groupNames.filter((cg) => !cg.endsWith('-pnpres')),
);
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/entities/SubscriptionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ export class SubscriptionSet extends SubscribeCapable {
this.subscriptionList.push(subscription);
});
this.listener = {};
eventEmitter.addListener(
this.listener,
this.channelNames.filter((c) => !c.endsWith('-pnpres')),
this.groupNames.filter((cg) => !cg.endsWith('-pnpres')),
);
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}

/**
Expand Down

0 comments on commit 02e3db1

Please sign in to comment.