Skip to content

Commit

Permalink
feat: new listener interface (#359)
Browse files Browse the repository at this point in the history
* added `Channel`, `ChannelGroup`, `ChannelMetadata`, `UserMetadata`, `Subscription`, `SubscriptionSet` entities definitions

* added factory function at PubNub and implementation at EventEmitter to support new listener interface

* lib/dist

* * test: adding channel, channelGroup, subscription, SubscriptionSet listenr tests
* fix: unique channel and groups for subscription, no telemetry for event engine, avoid duplicate listener registration

* dist/lib

* refactored listeners tests with callback style

* take-1: commented new tests

* Update run-tests.yml

* fix: telemetry  condition to add query param

* lib/dist

* fix: lint

* reverted workflow file changes

* restore all previous changes of telemetry

* dist/lib

* *remove unncessary subscriptionOptions prop. * added support for subscriptionSet operations

* lib/dist

* refactor: naming convention in subscriptionSet class

* no presence subscription for channel metadata

* fix channles and groups initialisation in constructor

* refactor: subscriptionSet will get constructed in form of set of subscriptions when channels/groups set is provided

* handling overlapping or unique channels/groups subscribe/unsubscribe with new listener/entity interface

* tests and utils

* lib/dist

* test/ cleanup after test

* refactor/fix: new listeners backward compatible to old subscription loop

* dist/lib

* fix typo in argument

* dist/lib

* attemp to address Codacy reported warnings

* lint!

* Codacy warnings fix

* Codacy warning fix

* added support for event specific listener registration.
* refactor subscription/subscriptionSet class definition through common abstract class.

* dist/lib

* fix: test flakiness

* PubNub SDK v7.6.0 release.

---------

Co-authored-by: PubNub Release Bot <120067856+pubnub-release-bot@users.noreply.github.com>
  • Loading branch information
mohitpubnub and pubnub-release-bot authored Feb 21, 2024
1 parent f0bd4e5 commit a73bc58
Show file tree
Hide file tree
Showing 44 changed files with 2,128 additions and 807 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ jobs:
needs: [tests]
steps:
- name: Tests summary
run: echo -e "\033[38;2;95;215;0m\033[1mAll tests successfully passed"
run: echo -e "\033[38;2;95;215;0m\033[1mAll tests successfully passed"
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-02-21
version: v7.6.0
changes:
- type: feature
text: "Adding channel, channelGroup, channelMetadata and userMetadata entities to be first-class citizens to access APIs related to them. Currently, access is provided only for subscription API."
- date: 2024-01-16
version: v7.5.0
changes:
Expand Down Expand Up @@ -938,7 +943,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '7.5.0'
version: '7.6.0'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -954,7 +959,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v7.5.0.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v7.6.0.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -1625,7 +1630,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v7.5.0/pubnub.7.5.0.js
location: https://github.com/pubnub/javascript/releases/download/v7.6.0/pubnub.7.6.0.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 @@
## v7.6.0
February 21 2024

#### Added
- Adding channel, channelGroup, channelMetadata and userMetadata entities to be first-class citizens to access APIs related to them. Currently, access is provided only for subscription API.

## v7.5.0
January 16 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.7.5.0.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.5.0.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.6.0.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.6.0.min.js
2. Configure your keys:
Expand Down
778 changes: 477 additions & 301 deletions dist/web/pubnub.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ var default_1 = /** @class */ (function () {
return this;
};
default_1.prototype.getVersion = function () {
return '7.5.0';
return '7.6.0';
};
default_1.prototype._setRetryConfiguration = function (configuration) {
if (configuration.minimumdelay < 2) {
Expand Down
78 changes: 75 additions & 3 deletions lib/core/components/eventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var EventEmitter = /** @class */ (function () {
this.modules = modules;
this.listenerManager = listenerManager;
this.getFileUrl = getFileUrl;
this._channelListenerMap = new Map();
this._groupListenerMap = new Map();
if (modules.cryptoModule)
this._decoder = new TextDecoder();
}
Expand Down Expand Up @@ -62,7 +64,12 @@ var EventEmitter = /** @class */ (function () {
if (e.payload.timeout) {
announce.timeout = e.payload.timeout;
}
// deprecated -->
announce.actualChannel = subscriptionMatch != null ? channel : null;
announce.subscribedChannel = subscriptionMatch != null ? subscriptionMatch : channel;
// <-- deprecated
this.listenerManager.announcePresence(announce);
this._announce('presence', announce, announce.channel, announce.subscription);
}
else if (e.messageType === 1) {
var announce = {};
Expand All @@ -77,6 +84,7 @@ var EventEmitter = /** @class */ (function () {
}
announce.message = e.payload;
this.listenerManager.announceSignal(announce);
this._announce('signal', announce, announce.channel, announce.subscription);
}
else if (e.messageType === 2) {
var announce = {};
Expand All @@ -95,20 +103,27 @@ var EventEmitter = /** @class */ (function () {
data: e.payload.data,
};
this.listenerManager.announceObjects(announce);
this._announce('objects', announce, announce.channel, announce.subscription);
if (e.payload.type === 'uuid') {
var eventData = this._renameChannelField(announce);
this.listenerManager.announceUser(__assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), type: 'user' }) }));
var userEvent = __assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), type: 'user' }) });
this.listenerManager.announceUser(userEvent);
this._announce('user', userEvent, announce.channel, announce.subscription);
}
else if (message.payload.type === 'channel') {
var eventData = this._renameChannelField(announce);
this.listenerManager.announceSpace(__assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), type: 'space' }) }));
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') {
var eventData = this._renameChannelField(announce);
var _a = eventData.message.data, user = _a.uuid, space = _a.channel, membershipData = __rest(_a, ["uuid", "channel"]);
membershipData.user = user;
membershipData.space = space;
this.listenerManager.announceMembership(__assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), data: membershipData }) }));
var membershipEvent = __assign(__assign({}, eventData), { message: __assign(__assign({}, eventData.message), { event: this._renameEvent(eventData.message.event), data: membershipData }) });
this.listenerManager.announceMembership(membershipEvent);
this._announce('membership', membershipEvent, announce.channel, announce.subscription);
}
}
else if (e.messageType === 3) {
Expand All @@ -126,6 +141,7 @@ var EventEmitter = /** @class */ (function () {
};
announce.event = e.payload.event;
this.listenerManager.announceMessageAction(announce);
this._announce('messageAction', announce, announce.channel, announce.subscription);
}
else if (e.messageType === 4) {
var announce = {};
Expand Down Expand Up @@ -163,6 +179,7 @@ var EventEmitter = /** @class */ (function () {
}),
};
this.listenerManager.announceFile(announce);
this._announce('file', announce, announce.channel, announce.subscription);
}
else {
var announce = {};
Expand Down Expand Up @@ -196,9 +213,59 @@ var EventEmitter = /** @class */ (function () {
else {
announce.message = e.payload;
}
// deprecated -->
announce.actualChannel = subscriptionMatch != null ? channel : null;
announce.subscribedChannel = subscriptionMatch != null ? subscriptionMatch : channel;
// <-- deprecated
this.listenerManager.announceMessage(announce);
this._announce('message', announce, announce.channel, announce.subscription);
}
};
EventEmitter.prototype.addListener = function (l, channels, groups) {
var _this = this;
if (!(channels && groups)) {
this.listenerManager.addListener(l);
}
else {
channels === null || channels === void 0 ? void 0 : channels.forEach(function (c) {
if (_this._channelListenerMap[c]) {
if (!_this._channelListenerMap[c].includes(l))
_this._channelListenerMap[c].push(l);
}
else {
_this._channelListenerMap[c] = [l];
}
});
groups === null || groups === void 0 ? void 0 : groups.forEach(function (g) {
if (_this._groupListenerMap[g]) {
if (!_this._groupListenerMap[g].includes(l))
_this._groupListenerMap[g].push(l);
}
else {
_this._groupListenerMap[g] = [l];
}
});
}
};
EventEmitter.prototype.removeListener = function (listener, channels, groups) {
var _this = this;
if (!(channels && groups)) {
this.listenerManager.removeListener(listener);
}
else {
channels === null || channels === void 0 ? void 0 : channels.forEach(function (c) {
var _a;
_this._channelListenerMap[c] = (_a = _this._channelListenerMap[c]) === null || _a === void 0 ? void 0 : _a.filter(function (l) { return l !== listener; });
});
groups === null || groups === void 0 ? void 0 : groups.forEach(function (g) {
var _a;
_this._groupListenerMap[g] = (_a = _this._groupListenerMap[g]) === null || _a === void 0 ? void 0 : _a.filter(function (l) { return l !== listener; });
});
}
};
EventEmitter.prototype.removeAllListeners = function () {
this.listenerManager.removeAllListeners();
};
EventEmitter.prototype._renameEvent = function (e) {
return e === 'set' ? 'updated' : 'removed';
};
Expand All @@ -207,6 +274,11 @@ var EventEmitter = /** @class */ (function () {
eventData.spaceId = channel;
return eventData;
};
EventEmitter.prototype._announce = function (type, event, channel, group) {
var _a, _b;
(_a = this._channelListenerMap[channel]) === null || _a === void 0 ? void 0 : _a.forEach(function (l) { return l[type] && l[type](event); });
(_b = this._groupListenerMap[group]) === null || _b === void 0 ? void 0 : _b.forEach(function (l) { return l[type] && l[type](event); });
};
return EventEmitter;
}());
exports.default = EventEmitter;
Loading

0 comments on commit a73bc58

Please sign in to comment.