Skip to content

Commit 2f14c4e

Browse files
chore: add methods to check session connection status for standard and managed channels
1 parent 5a67c8a commit 2f14c4e

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

lib/client.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,32 @@ module.exports = function (dependencies) {
161161
return isStatusCodeRetryable || isProviderTokenExpired || isTransientWriteFailure;
162162
};
163163

164+
/**
165+
* returns true if the standard session is connected and ready for use, false otherwise
166+
* Note that even if this returns true, a request can still fail due to transient network issues, in which case the request will be retried according to the retry logic in the write method.
167+
* @returns {boolean}
168+
*/
169+
Client.prototype.isStandardSessionConnected = function isStandardSessionConnected() {
170+
return (
171+
this.session && !this.isSessionClosing && !this.session.closed && !this.session.destroyed
172+
);
173+
};
174+
175+
/**
176+
* returns true if the manageChannels session is connected and ready for use, false otherwise
177+
* Note that even if this returns true, a request can still fail due to transient network issues, in which case the request will be retried according to the retry logic in the write method.
178+
* @returns {boolean}
179+
*/
180+
Client.prototype.isManagedChannelsSessionConnected =
181+
function isManagedChannelsSessionConnected() {
182+
return (
183+
this.manageChannelsSession &&
184+
!this.isSessionClosing &&
185+
!this.manageChannelsSession.closed &&
186+
!this.manageChannelsSession.destroyed
187+
);
188+
};
189+
164190
Client.prototype.write = async function write(notification, subDirectory, type, method, count) {
165191
const retryCount = count || 0;
166192
const subDirectoryLabel = this.subDirectoryLabel(type) ?? type;
@@ -193,12 +219,7 @@ module.exports = function (dependencies) {
193219

194220
if (path.includes('/1/apps/')) {
195221
// Connect manageChannelsSession.
196-
if (
197-
!this.manageChannelsSession ||
198-
this.isSessionClosing ||
199-
this.manageChannelsSession.closed ||
200-
this.manageChannelsSession.destroyed
201-
) {
222+
if (!this.isManagedChannelsSessionConnected()) {
202223
try {
203224
await this.manageChannelsConnect();
204225
} catch (error) {
@@ -247,7 +268,7 @@ module.exports = function (dependencies) {
247268
}
248269
} else {
249270
// Connect to standard session.
250-
if (!this.session || this.isSessionClosing || this.session.closed || this.session.destroyed) {
271+
if (!this.isStandardSessionConnected()) {
251272
try {
252273
await this.connect();
253274
} catch (error) {

0 commit comments

Comments
 (0)