posthog-js
Version:
Posthog-js allows you to automatically capture usage and send events to PostHog.
163 lines • 7.1 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestQueue = exports.DEFAULT_FLUSH_INTERVAL_MS = void 0;
var general_utils_1 = require("@posthog/browser-common/utils/general-utils");
var core_1 = require("@posthog/core");
var logger_1 = require("@posthog/browser-common/utils/logger");
exports.DEFAULT_FLUSH_INTERVAL_MS = 3000;
var RequestQueue = /** @class */ (function () {
function RequestQueue(sendRequest, config) {
// We start in a paused state and only start flushing when enabled by the parent
this._isPaused = true;
this._queue = [];
this._flushTimeoutMs = (0, core_1.clampToRange)((config === null || config === void 0 ? void 0 : config.flush_interval_ms) || exports.DEFAULT_FLUSH_INTERVAL_MS, 250, 5000, logger_1.logger.createLogger('flush interval'), exports.DEFAULT_FLUSH_INTERVAL_MS);
this._sendRequest = sendRequest;
}
RequestQueue.prototype.enqueue = function (req) {
this._queue.push(req);
if (!this._flushTimeout) {
this._setFlushTimeout();
}
};
RequestQueue.prototype.unload = function () {
var _this = this;
this._clearFlushTimeout();
var requests = this._queue.length > 0 ? this._formatQueue() : {};
var requestValues = Object.values(requests);
// Always force events to be sent before recordings, as events are more important, and recordings are bigger and thus less likely to arrive
var sortedRequests = __spreadArray(__spreadArray([], __read(requestValues.filter(function (r) { return r.url.indexOf('/e') === 0; })), false), __read(requestValues.filter(function (r) { return r.url.indexOf('/e') !== 0; })), false);
sortedRequests.map(function (req) {
// A split beacon's fallback can acknowledge only part of the batch. Keep identifiers pending on unload.
_this._sendRequestSafely(__assign(__assign({}, req), { callback: undefined }), 'sendBeacon');
});
};
RequestQueue.prototype.enable = function () {
this._isPaused = false;
this._setFlushTimeout();
};
RequestQueue.prototype._setFlushTimeout = function () {
var _this = this;
if (this._isPaused) {
return;
}
this._flushTimeout = setTimeout(function () {
_this._clearFlushTimeout();
_this._flush();
}, this._flushTimeoutMs);
};
RequestQueue.prototype._flush = function () {
if (this._queue.length > 0) {
var requests = this._formatQueue();
for (var key in requests) {
this._sendRequestSafely(requests[key]);
}
}
};
RequestQueue.prototype._sendRequestSafely = function (req, transportOverride) {
try {
this._sendRequest(req, transportOverride);
}
catch (error) {
logger_1.logger.error(error);
}
};
RequestQueue.prototype._clearFlushTimeout = function () {
clearTimeout(this._flushTimeout);
this._flushTimeout = undefined;
};
RequestQueue.prototype._formatQueue = function () {
var requests = {};
var callbacks = {};
(0, general_utils_1.each)(this._queue, function (request) {
var _a;
var req = request;
var key = ((req ? req.batchKey : null) || req.url) + (req.batchGroup ? ":".concat(req.batchGroup) : '');
if ((0, core_1.isUndefined)(requests[key])) {
// TODO: What about this -it seems to batch data into an array - do we always want that?
// Drop notifications are opt-in per request, not inherited from the first event in a batch.
requests[key] = __assign(__assign({}, req), { data: [], callback: undefined, fireCallbackOnDrop: undefined });
callbacks[key] = [];
}
(_a = requests[key].data) === null || _a === void 0 ? void 0 : _a.push(req.data);
if (req.callback) {
callbacks[key].push(req.callback);
}
});
var _loop_1 = function (key) {
var batchCallbacks = callbacks[key];
if (batchCallbacks.length) {
requests[key].callback = function (response) {
var e_1, _a;
try {
for (var batchCallbacks_1 = (e_1 = void 0, __values(batchCallbacks)), batchCallbacks_1_1 = batchCallbacks_1.next(); !batchCallbacks_1_1.done; batchCallbacks_1_1 = batchCallbacks_1.next()) {
var callback = batchCallbacks_1_1.value;
callback(response);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (batchCallbacks_1_1 && !batchCallbacks_1_1.done && (_a = batchCallbacks_1.return)) _a.call(batchCallbacks_1);
}
finally { if (e_1) throw e_1.error; }
}
};
}
};
for (var key in callbacks) {
_loop_1(key);
}
this._queue = [];
return requests;
};
return RequestQueue;
}());
exports.RequestQueue = RequestQueue;
//# sourceMappingURL=request-queue.js.map