Skip to content

Commit

Permalink
added pb.realtime.onDisconnect hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Dec 15, 2024
1 parent 848b77d commit 53bf1f4
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.23.0

- Added optional `pb.realtime.onDisconnect` hook function.
_Note that the realtime client autoreconnect on its own and this hook is useful only for the cases where you want to apply a special behavior on server error or after closing the realtime connection._


## 0.22.1

- Fixed old `pb.authStore.isAdmin`/`pb.authStore.isAuthRecord` and marked them as deprecated in favour of `pb.authStore.isSuperuser` ([#323](https://github.com/pocketbase/js-sdk/issues/323)).
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,18 @@ const result = await batch.send()

// Unsubscribe from all subscriptions matching the specified topic and listener function.
🔓 pb.realtime.unsubscribeByTopicAndListener(topic, callback);

// Getter that checks whether the realtime connection has been established.
pb.realtime.isConnected

// An optional hook that is invoked when the realtime client disconnects
// either when unsubscribing from all subscriptions or when the connection
// was interrupted or closed by the server.
//
// Note that the realtime client autoreconnect on its own and this hook is
// useful only for the cases where you want to apply a special behavior on
// server error or after closing the realtime connection.
pb.realtime.onDisconnect = function(activeSubscriptions)
```

---
Expand Down
13 changes: 13 additions & 0 deletions dist/pocketbase.cjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ declare class RealtimeService extends BaseService {
* Returns whether the realtime connection has been established.
*/
get isConnected(): boolean;
/**
* An optional hook that is invoked when the realtime client disconnects
* either when unsubscribing from all subscriptions or when the
* connection was interrupted or closed by the server.
*
* The received argument could be used to determine whether the disconnect
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
* or because of network/server error (`activeSubscriptions.length > 0`).
*
* If you want to listen for the opposite, aka. when the client connection is established,
* subscribe to the `PB_CONNECT` event.
*/
onDisconnect?: (activeSubscriptions: Array<string>) => void;
/**
* Register the subscription listener.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions dist/pocketbase.es.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,19 @@ declare class RealtimeService extends BaseService {
* Returns whether the realtime connection has been established.
*/
get isConnected(): boolean;
/**
* An optional hook that is invoked when the realtime client disconnects
* either when unsubscribing from all subscriptions or when the
* connection was interrupted or closed by the server.
*
* The received argument could be used to determine whether the disconnect
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
* or because of network/server error (`activeSubscriptions.length > 0`).
*
* If you want to listen for the opposite, aka. when the client connection is established,
* subscribe to the `PB_CONNECT` event.
*/
onDisconnect?: (activeSubscriptions: Array<string>) => void;
/**
* Register the subscription listener.
*
Expand Down
13 changes: 13 additions & 0 deletions dist/pocketbase.es.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,19 @@ declare class RealtimeService extends BaseService {
* Returns whether the realtime connection has been established.
*/
get isConnected(): boolean;
/**
* An optional hook that is invoked when the realtime client disconnects
* either when unsubscribing from all subscriptions or when the
* connection was interrupted or closed by the server.
*
* The received argument could be used to determine whether the disconnect
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
* or because of network/server error (`activeSubscriptions.length > 0`).
*
* If you want to listen for the opposite, aka. when the client connection is established,
* subscribe to the `PB_CONNECT` event.
*/
onDisconnect?: (activeSubscriptions: Array<string>) => void;
/**
* Register the subscription listener.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions dist/pocketbase.iife.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ declare class RealtimeService extends BaseService {
* Returns whether the realtime connection has been established.
*/
get isConnected(): boolean;
/**
* An optional hook that is invoked when the realtime client disconnects
* either when unsubscribing from all subscriptions or when the
* connection was interrupted or closed by the server.
*
* The received argument could be used to determine whether the disconnect
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
* or because of network/server error (`activeSubscriptions.length > 0`).
*
* If you want to listen for the opposite, aka. when the client connection is established,
* subscribe to the `PB_CONNECT` event.
*/
onDisconnect?: (activeSubscriptions: Array<string>) => void;
/**
* Register the subscription listener.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions dist/pocketbase.umd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ declare class RealtimeService extends BaseService {
* Returns whether the realtime connection has been established.
*/
get isConnected(): boolean;
/**
* An optional hook that is invoked when the realtime client disconnects
* either when unsubscribing from all subscriptions or when the
* connection was interrupted or closed by the server.
*
* The received argument could be used to determine whether the disconnect
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
* or because of network/server error (`activeSubscriptions.length > 0`).
*
* If you want to listen for the opposite, aka. when the client connection is established,
* subscribe to the `PB_CONNECT` event.
*/
onDisconnect?: (activeSubscriptions: Array<string>) => void;
/**
* Register the subscription listener.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.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,5 +1,5 @@
{
"version": "0.22.1",
"version": "0.23.0",
"name": "pocketbase",
"description": "PocketBase JavaScript SDK",
"author": "Gani Georgiev",
Expand Down
18 changes: 18 additions & 0 deletions src/services/RealtimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ export class RealtimeService extends BaseService {
return !!this.eventSource && !!this.clientId && !this.pendingConnects.length;
}

/**
* An optional hook that is invoked when the realtime client disconnects
* either when unsubscribing from all subscriptions or when the
* connection was interrupted or closed by the server.
*
* The received argument could be used to determine whether the disconnect
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
* or because of network/server error (`activeSubscriptions.length > 0`).
*
* If you want to listen for the opposite, aka. when the client connection is established,
* subscribe to the `PB_CONNECT` event.
*/
onDisconnect?: (activeSubscriptions: Array<string>) => void;

/**
* Register the subscription listener.
*
Expand Down Expand Up @@ -465,6 +479,10 @@ export class RealtimeService extends BaseService {
}

private disconnect(fromReconnect = false): void {
if (this.clientId && this.onDisconnect) {
this.onDisconnect(Object.keys(this.subscriptions));
}

clearTimeout(this.connectTimeoutId);
clearTimeout(this.reconnectTimeoutId);
this.removeAllSubscriptionListeners();
Expand Down

0 comments on commit 53bf1f4

Please sign in to comment.