Skip to content

Commit

Permalink
Merge pull request #609 from pusher/subcount
Browse files Browse the repository at this point in the history
Add subscription_count event
  • Loading branch information
amad authored Jul 15, 2022
2 parents f6e3b53 + 7924214 commit b18b6d3
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.2.0

* [ADDED] Add support for subscription_count event

## 7.1.1-beta

[FIXED] Exported Typescript types in index.d.ts
Expand Down
16 changes: 14 additions & 2 deletions dist/node/pusher.js

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

2 changes: 1 addition & 1 deletion dist/node/pusher.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/react-native/pusher.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react-native/pusher.js.map

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions dist/web/pusher-with-encryption.js

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

2 changes: 1 addition & 1 deletion dist/web/pusher-with-encryption.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pusher-with-encryption.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/web/pusher-with-encryption.min.js.map

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions dist/web/pusher.js

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

2 changes: 1 addition & 1 deletion dist/web/pusher.js.map

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/web/pusher.min.js.map

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions dist/worker/pusher-with-encryption.worker.js

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

2 changes: 1 addition & 1 deletion dist/worker/pusher-with-encryption.worker.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/worker/pusher-with-encryption.worker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/worker/pusher-with-encryption.worker.min.js.map

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions dist/worker/pusher.worker.js

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

2 changes: 1 addition & 1 deletion dist/worker/pusher.worker.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/worker/pusher.worker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/worker/pusher.worker.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pusher-js",
"version": "7.1.1-beta",
"version": "7.2.0",
"description": "Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers",
"main": "dist/node/pusher.js",
"browser": "dist/web/pusher.js",
Expand Down
25 changes: 25 additions & 0 deletions spec/javascripts/unit/core/channels/channel_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ describe("Channel", function() {
});
});

describe('on pusher_internal:subscription_count', function() {
it('should emit pusher:subscription_count', function() {
const testEvent = {
event: 'pusher_internal:subscription_count',
data: { subscription_count: 101 }
};

var callback = jasmine.createSpy('callback');
channel.bind('pusher:subscription_count', callback);

channel.handleEvent(testEvent);

expect(callback).toHaveBeenCalledWith(testEvent.data);
});

it('should set #subscriptionCount', function() {
channel.handleEvent({
event: 'pusher_internal:subscription_count',
data: { subscription_count: 101 }
});

expect(channel.subscriptionCount).toEqual(101);
});
});

describe("on other events", function() {
it("should emit the event", function() {
var callback = jasmine.createSpy("callback");
Expand Down
11 changes: 11 additions & 0 deletions src/core/channels/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class Channel extends EventsDispatcher {
subscribed: boolean;
subscriptionPending: boolean;
subscriptionCancelled: boolean;
subscriptionCount: null;

constructor(name: string, pusher: Pusher) {
super(function(event, data) {
Expand Down Expand Up @@ -78,6 +79,8 @@ export default class Channel extends EventsDispatcher {
var data = event.data;
if (eventName === 'pusher_internal:subscription_succeeded') {
this.handleSubscriptionSucceededEvent(event);
} else if (eventName === 'pusher_internal:subscription_count') {
this.handleSubscriptionCountEvent(event);
} else if (eventName.indexOf('pusher_internal:') !== 0) {
var metadata: Metadata = {};
this.emit(eventName, data, metadata);
Expand All @@ -94,6 +97,14 @@ export default class Channel extends EventsDispatcher {
}
}

handleSubscriptionCountEvent(event: PusherEvent) {
if (event.data.subscription_count) {
this.subscriptionCount = event.data.subscription_count;
}

this.emit('pusher:subscription_count', event.data);
}

/** Sends a subscription request. For internal use only. */
subscribe() {
if (this.subscribed) {
Expand Down
3 changes: 3 additions & 0 deletions src/core/channels/presence_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export default class PresenceChannel extends PrivateChannel {
case 'pusher_internal:subscription_succeeded':
this.handleSubscriptionSucceededEvent(event);
break;
case 'pusher_internal:subscription_count':
this.handleSubscriptionCountEvent(event);
break;
case 'pusher_internal:member_added':
var addedMember = this.members.addMember(data);
this.emit('pusher:member_added', addedMember);
Expand Down
2 changes: 2 additions & 0 deletions types/src/core/channels/channel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export default class Channel extends EventsDispatcher {
subscribed: boolean;
subscriptionPending: boolean;
subscriptionCancelled: boolean;
subscriptionCount: null;
constructor(name: string, pusher: Pusher);
authorize(socketId: string, callback: ChannelAuthorizationCallback): void;
trigger(event: string, data: any): boolean;
disconnect(): void;
handleEvent(event: PusherEvent): void;
handleSubscriptionSucceededEvent(event: PusherEvent): void;
handleSubscriptionCountEvent(event: PusherEvent): void;
subscribe(): void;
unsubscribe(): void;
cancelSubscription(): void;
Expand Down

0 comments on commit b18b6d3

Please sign in to comment.