FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Client suspension and reactivation by parfeon · Pull Request #496 · pubnub/javascript · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (5) .json  (1) .ts  (13) All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
62 changes: 43 additions & 19 deletions dist/web/pubnub.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,10 @@
* Whether subscription worker has been initialized and ready to handle events.
*/
this.subscriptionWorkerReady = false;
/**
* Promise chain serializing token change operations to preserve ordering.
*/
this.tokenChangeChain = Promise.resolve();
/**
* Map of base64-encoded access tokens to their parsed representations.
*/
Expand Down Expand Up @@ -3634,21 +3638,29 @@
* @param [token] - Authorization token which should be used.
*/
onTokenChange(token) {
const updateEvent = {
type: 'client-update',
heartbeatInterval: this.configuration.heartbeatInterval,
clientIdentifier: this.configuration.clientIdentifier,
subscriptionKey: this.configuration.subscriptionKey,
userId: this.configuration.userId,
workerLogLevel: this.configuration.workerLogLevel,
};
// Trigger request processing by Service Worker.
this.parsedAccessToken(token)
.then((accessToken) => {
updateEvent.preProcessedToken = accessToken;
updateEvent.accessToken = token;
this.tokenChangeChain = this.tokenChangeChain
.then(() => {
const updateEvent = {
type: 'client-update',
heartbeatInterval: this.configuration.heartbeatInterval,
clientIdentifier: this.configuration.clientIdentifier,
subscriptionKey: this.configuration.subscriptionKey,
userId: this.configuration.userId,
workerLogLevel: this.configuration.workerLogLevel,
};
return this.parsedAccessToken(token)
.then((accessToken) => {
updateEvent.preProcessedToken = accessToken;
updateEvent.accessToken = token;
})
.then(() => this.scheduleEventPost(updateEvent));
})
.then(() => this.scheduleEventPost(updateEvent));
.catch((error) => {
this.configuration.logger.warn('SubscriptionWorkerMiddleware', () => ({
messageType: 'text',
message: `Token change processing failed: ${error}`,
}));
});
}
/**
* Disconnect client and terminate ongoing long-poll requests (if needed).
Expand Down Expand Up @@ -3818,6 +3830,7 @@
data.type !== 'shared-worker-connected' &&
data.type !== 'shared-worker-console-log' &&
data.type !== 'shared-worker-console-dir' &&
data.type !== 'shared-worker-client-suspended' &&
data.clientIdentifier !== this.configuration.clientIdentifier)
return;
if (data.type === 'shared-worker-connected') {
Expand Down Expand Up @@ -3854,6 +3867,14 @@
workerLogLevel: this.configuration.workerLogLevel,
});
}
else if (data.type === 'shared-worker-client-suspended') {
this.configuration.logger.warn('SubscriptionWorkerMiddleware', 'Client suspended by SharedWorker (missed ping-pong). Triggering retryable error for recovery.');
// Reject all pending callbacks with a retryable (timeout) error to trigger retry/reconnect.
this.callbacks.forEach(({ reject }, identifier) => {
this.callbacks.delete(identifier);
reject(new PubNubAPIError('SharedWorker client suspended', StatusCategory$1.PNTimeoutCategory, 0, new Error('SharedWorker client suspended')));
});
}
else if (data.type === 'request-process-success' || data.type === 'request-process-error') {
if (this.callbacks.has(data.identifier)) {
const { resolve, reject } = this.callbacks.get(data.identifier);
Expand Down Expand Up @@ -3913,9 +3934,8 @@
return this.stringifyAccessToken(accessToken).then(([token, stringifiedToken]) => {
if (!token || !stringifiedToken)
return undefined;
return (this.accessTokensMap = {
[accessToken]: { token: stringifiedToken, expiration: token.timestamp + token.ttl * 60 },
})[accessToken];
this.accessTokensMap[accessToken] = { token: stringifiedToken, expiration: token.timestamp + token.ttl * 60 };
return this.accessTokensMap[accessToken];
});
});
}
Expand Down Expand Up @@ -4008,7 +4028,11 @@
isNewerSharedWorkerVersion(version) {
const [currentMajor, currentMinor, currentPatch] = this.configuration.sdkVersion.split('.').map(Number);
const [storedMajor, storedMinor, storedPatch] = version.split('.').map(Number);
return storedMajor > currentMajor || storedMinor > currentMinor || storedPatch > currentPatch;
if (storedMajor !== currentMajor)
return storedMajor > currentMajor;
if (storedMinor !== currentMinor)
return storedMinor > currentMinor;
return storedPatch > currentPatch;
}
}

Expand Down Expand Up @@ -18419,7 +18443,7 @@
window.addEventListener('pagehide', (event) => {
if (!event.persisted)
middleware.terminate();
}, { once: true });
});
}
}
catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

Loading
Loading

Back | FazBrowse Home | New Git URL