UNPKG

fastcomments-sdk

Version:

FastComments API Client - A SDK for interacting with the FastComments API

110 lines 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = subscribeToChanges; const utils_js_1 = require("./utils.js"); function subscribeToChanges(config, tenantIdWS, urlIdWS, userIdWS, handleLiveEvent, onConnectionStatusChange, lastLiveEventTime) { try { let isIntentionallyClosed = false; function extractCommentIdFromEvent(liveEvent) { if (liveEvent.type === 'new-comment' && liveEvent.comment) { return liveEvent.comment.id; } } async function fetchEventLog(startTime, endTime) { return new Promise((resolve) => { (0, utils_js_1.debounce)('fetchEventLog:' + config.tenantId, async () => { try { const response = await (0, utils_js_1.makeRequest)(config, 'GET', '/event-log/' + config.tenantId + '/' + (0, utils_js_1.createURLQueryString)({ urlId: config.urlId, startTime, endTime, userIdWS })); if (!(0, utils_js_1.isAPIError)(response) && response.events) { const eventsParsed = response.events.map(event => JSON.parse(event.data)); for (const eventDataParsed of eventsParsed) { if (eventDataParsed.timestamp) { lastLiveEventTime = Math.max(lastLiveEventTime, eventDataParsed.timestamp); } handleLiveEvent(eventDataParsed); } } resolve(); } catch (error) { console.error('FastComments: fetchEventLog FAILURE', error); if (!isIntentionallyClosed) { setTimeout(() => { if (!isIntentionallyClosed) { fetchEventLog(startTime, Date.now()); } }, 30000 * Math.random()); } resolve(); } }, 2000); }); } const wsHost = config.wsHost || 'wss://fastcomments.com'; const socket = new globalThis.WebSocket(wsHost + '/sub' + (0, utils_js_1.createURLQueryString)({ urlId: urlIdWS, userIdWS, tenantIdWS })); socket.onopen = function () { if (isIntentionallyClosed) { return; } setInterval(() => { if (socket.readyState === 1) { // WebSocket.OPEN socket.send('ping'); } }, 60000); if (lastLiveEventTime) { fetchEventLog(lastLiveEventTime, Date.now()); } onConnectionStatusChange && onConnectionStatusChange(true, lastLiveEventTime); }; socket.onerror = function () { if (lastLiveEventTime) { setTimeout(() => { fetchEventLog(lastLiveEventTime, Date.now()); }, 30000 * Math.random()); } }; socket.onclose = function () { if (!lastLiveEventTime) { lastLiveEventTime = Date.now(); } if (!isIntentionallyClosed) { onConnectionStatusChange && onConnectionStatusChange(false, lastLiveEventTime); setTimeout(() => { subscribeToChanges(config, tenantIdWS, urlIdWS, userIdWS, handleLiveEvent, onConnectionStatusChange, lastLiveEventTime); }, 4000 * Math.random()); } }; socket.onmessage = function (event) { if (isIntentionallyClosed) { return; } const eventDataParsed = JSON.parse(event.data); if (eventDataParsed.timestamp) { lastLiveEventTime = Math.max(lastLiveEventTime, eventDataParsed.timestamp); } handleLiveEvent(eventDataParsed); }; return { close: function () { isIntentionallyClosed = true; socket && socket.close(); } }; } catch (e) { console.error(e); return { close: () => { } }; } } //# sourceMappingURL=subscribe-to-changes.js.map