|
435 | 435 | * Handle client request to leave request. |
436 | 436 | * |
437 | 437 | * @param data - Leave event details. |
438 | | - * @param [client] - Specific client to handle leave request. |
| 438 | + * @param [invalidatedClient] - Specific client to handle leave request. |
| 439 | + * @param [invalidatedClientServiceRequestId] - Identifier of the service request ID for which the invalidated |
| 440 | + * client waited for a subscribe response. |
439 | 441 | */ |
440 | | - const handleSendLeaveRequestEvent = (data, client) => { |
| 442 | + const handleSendLeaveRequestEvent = (data, invalidatedClient, invalidatedClientServiceRequestId) => { |
441 | 443 | var _a, _b; |
442 | 444 | var _c; |
443 | | - client = client !== null && client !== void 0 ? client : pubNubClients[data.clientIdentifier]; |
444 | | - const request = leaveTransportRequestFromEvent(data); |
| 445 | + const client = invalidatedClient !== null && invalidatedClient !== void 0 ? invalidatedClient : pubNubClients[data.clientIdentifier]; |
| 446 | + const request = leaveTransportRequestFromEvent(data, invalidatedClient); |
445 | 447 | if (!client) |
446 | 448 | return; |
447 | 449 | // Clean up client subscription information if there is no more channels / groups to use. |
448 | 450 | const { subscription, heartbeat } = client; |
449 | | - const serviceRequestId = subscription === null || subscription === void 0 ? void 0 : subscription.serviceRequestId; |
| 451 | + const serviceRequestId = invalidatedClientServiceRequestId !== null && invalidatedClientServiceRequestId !== void 0 ? invalidatedClientServiceRequestId : subscription === null || subscription === void 0 ? void 0 : subscription.serviceRequestId; |
450 | 452 | if (subscription && subscription.channels.length === 0 && subscription.channelGroups.length === 0) { |
451 | 453 | subscription.channelGroupQuery = ''; |
452 | 454 | subscription.path = ''; |
|
571 | 573 | const sendRequest = (request, getClients, success, failure, responsePreProcess) => { |
572 | 574 | (() => __awaiter(void 0, void 0, void 0, function* () { |
573 | 575 | var _a; |
574 | | - // Request progress support. |
575 | | - new Date().getTime(); |
576 | 576 | Promise.race([ |
577 | 577 | fetch(requestFromTransportRequest(request), { |
578 | 578 | signal: (_a = abortControllers.get(request.identifier)) === null || _a === void 0 ? void 0 : _a.signal, |
|
583 | 583 | .then((response) => response.arrayBuffer().then((buffer) => [response, buffer])) |
584 | 584 | .then((response) => (responsePreProcess ? responsePreProcess(response) : response)) |
585 | 585 | .then((response) => { |
586 | | - response[1].byteLength > 0 ? response[1] : undefined; |
587 | 586 | const clients = getClients(); |
588 | 587 | if (clients.length === 0) |
589 | 588 | return; |
|
872 | 871 | if (aggregated && hbRequestsBySubscriptionKey[heartbeatRequestKey].clientIdentifier) { |
873 | 872 | const expectedTimestamp = hbRequestsBySubscriptionKey[heartbeatRequestKey].timestamp + minimumHeartbeatInterval * 1000; |
874 | 873 | const currentTimestamp = Date.now(); |
875 | | - // Check whether it is too soon to send request or not (5 is leeway which let send request a bit earlier). |
876 | | - // Request should be sent if previous attempt failed. |
877 | | - if (!failedPreviousRequest && currentTimestamp < expectedTimestamp && expectedTimestamp - currentTimestamp > 5000) |
| 874 | + // Check whether it is too soon to send request or not. |
| 875 | + // Request should be sent if a previous attempt failed. |
| 876 | + const leeway = minimumHeartbeatInterval * 0.05 * 1000; |
| 877 | + if (!failedPreviousRequest && currentTimestamp < expectedTimestamp && expectedTimestamp - currentTimestamp > leeway) |
878 | 878 | return undefined; |
879 | 879 | } |
880 | 880 | delete hbRequestsBySubscriptionKey[heartbeatRequestKey].response; |
|
928 | 928 | * |
929 | 929 | * Filter out channels and groups, which is still in use by other PubNub client instances from leave request. |
930 | 930 | * |
931 | | - * @param event - Client's send leave event request. |
| 931 | + * @param event - Client's sending leave event request. |
| 932 | + * @param [invalidatedClient] - Invalidated PubNub client state. |
932 | 933 | * |
933 | | - * @returns Final transport request or `undefined` in case if there is no channels and groups for which request can be |
| 934 | + * @returns Final transport request or `undefined` in case if there are no channels and groups for which request can be |
934 | 935 | * done. |
935 | 936 | */ |
936 | | - const leaveTransportRequestFromEvent = (event) => { |
| 937 | + const leaveTransportRequestFromEvent = (event, invalidatedClient) => { |
937 | 938 | var _a; |
938 | | - const client = pubNubClients[event.clientIdentifier]; |
939 | | - const clients = clientsForSendLeaveRequestEvent(event); |
| 939 | + const client = invalidatedClient !== null && invalidatedClient !== void 0 ? invalidatedClient : pubNubClients[event.clientIdentifier]; |
| 940 | + const clients = clientsForSendLeaveRequestEvent(event, invalidatedClient); |
940 | 941 | let channelGroups = channelGroupsFromRequest(event.request); |
941 | 942 | let channels = channelsFromRequest(event.request); |
942 | 943 | const request = Object.assign({}, event.request); |
|
1385 | 1386 | const invalidatedClient = pubNubClients[clientId]; |
1386 | 1387 | delete pubNubClients[clientId]; |
1387 | 1388 | let clients = pubNubClientsBySubscriptionKey[subscriptionKey]; |
| 1389 | + let serviceRequestId; |
1388 | 1390 | // Unsubscribe invalidated PubNub client. |
1389 | 1391 | if (invalidatedClient) { |
1390 | 1392 | // Cancel long-poll request if possible. |
1391 | 1393 | if (invalidatedClient.subscription) { |
1392 | | - const { serviceRequestId } = invalidatedClient.subscription; |
| 1394 | + serviceRequestId = invalidatedClient.subscription.serviceRequestId; |
1393 | 1395 | delete invalidatedClient.subscription.serviceRequestId; |
1394 | 1396 | if (serviceRequestId) |
1395 | 1397 | cancelRequest(serviceRequestId); |
|
1403 | 1405 | } |
1404 | 1406 | // Leave subscribed channels / groups properly. |
1405 | 1407 | if (invalidatedClient.unsubscribeOfflineClients) |
1406 | | - unsubscribeClient(invalidatedClient); |
| 1408 | + unsubscribeClient(invalidatedClient, serviceRequestId); |
1407 | 1409 | } |
1408 | 1410 | if (clients) { |
1409 | 1411 | // Clean up linkage between client and subscription key. |
|
1436 | 1438 | for (const _client of clients) |
1437 | 1439 | consoleLog(message, _client); |
1438 | 1440 | }; |
1439 | | - const unsubscribeClient = (client) => { |
| 1441 | + /** |
| 1442 | + * Unsubscribe offline / invalidated PubNub client. |
| 1443 | + * |
| 1444 | + * @param client - Invalidated PubNub client state object. |
| 1445 | + * @param [invalidatedClientServiceRequestId] - Identifier of the service request ID for which the invalidated |
| 1446 | + * client waited for a subscribe response. |
| 1447 | + */ |
| 1448 | + const unsubscribeClient = (client, invalidatedClientServiceRequestId) => { |
1440 | 1449 | if (!client.subscription) |
1441 | 1450 | return; |
1442 | | - const { channels, channelGroups, serviceRequestId } = client.subscription; |
| 1451 | + const { channels, channelGroups } = client.subscription; |
1443 | 1452 | const encodedChannelGroups = (channelGroups !== null && channelGroups !== void 0 ? channelGroups : []) |
1444 | 1453 | .filter((name) => !name.endsWith('-pnpres')) |
1445 | 1454 | .map((name) => encodeString(name)) |
|
1469 | 1478 | identifier: query.requestid, |
1470 | 1479 | }, |
1471 | 1480 | }; |
1472 | | - handleSendLeaveRequestEvent(request, client); |
| 1481 | + handleSendLeaveRequestEvent(request, client, invalidatedClientServiceRequestId); |
1473 | 1482 | }; |
1474 | 1483 | /** |
1475 | 1484 | * Validate received event payload. |
@@ -1583,12 +1592,13 @@ which has started by '${client.clientIdentifier}' client. Waiting for existing ' |
1583 | 1592 | * - `auth` key |
1584 | 1593 | * |
1585 | 1594 | * @param event - Send leave request event information. |
| 1595 | + * @param [invalidatedClient] - Invalidated PubNub client state. |
1586 | 1596 | * |
1587 | 1597 | * @returns List of PubNub client states which works from other pages for the same user. |
1588 | 1598 | */ |
1589 | | - const clientsForSendLeaveRequestEvent = (event) => { |
| 1599 | + const clientsForSendLeaveRequestEvent = (event, invalidatedClient) => { |
1590 | 1600 | var _a; |
1591 | | - const reqClient = pubNubClients[event.clientIdentifier]; |
| 1601 | + const reqClient = invalidatedClient !== null && invalidatedClient !== void 0 ? invalidatedClient : pubNubClients[event.clientIdentifier]; |
1592 | 1602 | if (!reqClient) |
1593 | 1603 | return []; |
1594 | 1604 | const query = event.request.queryParameters; |
|
0 commit comments