Skip to content

Commit 53bf1f4

Browse files
committed
added pb.realtime.onDisconnect hook
1 parent 848b77d commit 53bf1f4

19 files changed

+112
-11
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.23.0
2+
3+
- Added optional `pb.realtime.onDisconnect` hook function.
4+
_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._
5+
6+
17
## 0.22.1
28

39
- 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)).

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,18 @@ const result = await batch.send()
10151015

10161016
// Unsubscribe from all subscriptions matching the specified topic and listener function.
10171017
🔓 pb.realtime.unsubscribeByTopicAndListener(topic, callback);
1018+
1019+
// Getter that checks whether the realtime connection has been established.
1020+
pb.realtime.isConnected
1021+
1022+
// An optional hook that is invoked when the realtime client disconnects
1023+
// either when unsubscribing from all subscriptions or when the connection
1024+
// was interrupted or closed by the server.
1025+
//
1026+
// Note that the realtime client autoreconnect on its own and this hook is
1027+
// useful only for the cases where you want to apply a special behavior on
1028+
// server error or after closing the realtime connection.
1029+
pb.realtime.onDisconnect = function(activeSubscriptions)
10181030
```
10191031

10201032
---

dist/pocketbase.cjs.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ declare class RealtimeService extends BaseService {
382382
* Returns whether the realtime connection has been established.
383383
*/
384384
get isConnected(): boolean;
385+
/**
386+
* An optional hook that is invoked when the realtime client disconnects
387+
* either when unsubscribing from all subscriptions or when the
388+
* connection was interrupted or closed by the server.
389+
*
390+
* The received argument could be used to determine whether the disconnect
391+
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
392+
* or because of network/server error (`activeSubscriptions.length > 0`).
393+
*
394+
* If you want to listen for the opposite, aka. when the client connection is established,
395+
* subscribe to the `PB_CONNECT` event.
396+
*/
397+
onDisconnect?: (activeSubscriptions: Array<string>) => void;
385398
/**
386399
* Register the subscription listener.
387400
*

dist/pocketbase.cjs.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.cjs.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.es.d.mts

+13
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,19 @@ declare class RealtimeService extends BaseService {
425425
* Returns whether the realtime connection has been established.
426426
*/
427427
get isConnected(): boolean;
428+
/**
429+
* An optional hook that is invoked when the realtime client disconnects
430+
* either when unsubscribing from all subscriptions or when the
431+
* connection was interrupted or closed by the server.
432+
*
433+
* The received argument could be used to determine whether the disconnect
434+
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
435+
* or because of network/server error (`activeSubscriptions.length > 0`).
436+
*
437+
* If you want to listen for the opposite, aka. when the client connection is established,
438+
* subscribe to the `PB_CONNECT` event.
439+
*/
440+
onDisconnect?: (activeSubscriptions: Array<string>) => void;
428441
/**
429442
* Register the subscription listener.
430443
*

dist/pocketbase.es.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,19 @@ declare class RealtimeService extends BaseService {
425425
* Returns whether the realtime connection has been established.
426426
*/
427427
get isConnected(): boolean;
428+
/**
429+
* An optional hook that is invoked when the realtime client disconnects
430+
* either when unsubscribing from all subscriptions or when the
431+
* connection was interrupted or closed by the server.
432+
*
433+
* The received argument could be used to determine whether the disconnect
434+
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
435+
* or because of network/server error (`activeSubscriptions.length > 0`).
436+
*
437+
* If you want to listen for the opposite, aka. when the client connection is established,
438+
* subscribe to the `PB_CONNECT` event.
439+
*/
440+
onDisconnect?: (activeSubscriptions: Array<string>) => void;
428441
/**
429442
* Register the subscription listener.
430443
*

dist/pocketbase.es.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.es.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.es.mjs

+1-1
Large diffs are not rendered by default.

dist/pocketbase.es.mjs.map

+1-1
Large diffs are not rendered by default.

dist/pocketbase.iife.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ declare class RealtimeService extends BaseService {
382382
* Returns whether the realtime connection has been established.
383383
*/
384384
get isConnected(): boolean;
385+
/**
386+
* An optional hook that is invoked when the realtime client disconnects
387+
* either when unsubscribing from all subscriptions or when the
388+
* connection was interrupted or closed by the server.
389+
*
390+
* The received argument could be used to determine whether the disconnect
391+
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
392+
* or because of network/server error (`activeSubscriptions.length > 0`).
393+
*
394+
* If you want to listen for the opposite, aka. when the client connection is established,
395+
* subscribe to the `PB_CONNECT` event.
396+
*/
397+
onDisconnect?: (activeSubscriptions: Array<string>) => void;
385398
/**
386399
* Register the subscription listener.
387400
*

dist/pocketbase.iife.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.iife.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.umd.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ declare class RealtimeService extends BaseService {
382382
* Returns whether the realtime connection has been established.
383383
*/
384384
get isConnected(): boolean;
385+
/**
386+
* An optional hook that is invoked when the realtime client disconnects
387+
* either when unsubscribing from all subscriptions or when the
388+
* connection was interrupted or closed by the server.
389+
*
390+
* The received argument could be used to determine whether the disconnect
391+
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
392+
* or because of network/server error (`activeSubscriptions.length > 0`).
393+
*
394+
* If you want to listen for the opposite, aka. when the client connection is established,
395+
* subscribe to the `PB_CONNECT` event.
396+
*/
397+
onDisconnect?: (activeSubscriptions: Array<string>) => void;
385398
/**
386399
* Register the subscription listener.
387400
*

dist/pocketbase.umd.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pocketbase.umd.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.22.1",
2+
"version": "0.23.0",
33
"name": "pocketbase",
44
"description": "PocketBase JavaScript SDK",
55
"author": "Gani Georgiev",

src/services/RealtimeService.ts

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ export class RealtimeService extends BaseService {
3434
return !!this.eventSource && !!this.clientId && !this.pendingConnects.length;
3535
}
3636

37+
/**
38+
* An optional hook that is invoked when the realtime client disconnects
39+
* either when unsubscribing from all subscriptions or when the
40+
* connection was interrupted or closed by the server.
41+
*
42+
* The received argument could be used to determine whether the disconnect
43+
* is a result from unsubscribing (`activeSubscriptions.length == 0`)
44+
* or because of network/server error (`activeSubscriptions.length > 0`).
45+
*
46+
* If you want to listen for the opposite, aka. when the client connection is established,
47+
* subscribe to the `PB_CONNECT` event.
48+
*/
49+
onDisconnect?: (activeSubscriptions: Array<string>) => void;
50+
3751
/**
3852
* Register the subscription listener.
3953
*
@@ -465,6 +479,10 @@ export class RealtimeService extends BaseService {
465479
}
466480

467481
private disconnect(fromReconnect = false): void {
482+
if (this.clientId && this.onDisconnect) {
483+
this.onDisconnect(Object.keys(this.subscriptions));
484+
}
485+
468486
clearTimeout(this.connectTimeoutId);
469487
clearTimeout(this.reconnectTimeoutId);
470488
this.removeAllSubscriptionListeners();

0 commit comments

Comments
 (0)