react-native-kochava-measurement
Version:
A lightweight and easy to integrate SDK, providing first-class integration with Kochava’s installation attribution and analytics platform.
434 lines (433 loc) • 21.4 kB
JavaScript
/*
Authored by Brett Barinaga on 11/17/21.
Copyright (c) Kochava, Inc. All rights reserved.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import * as Event from "./payloads/event";
import * as IdLink from "./payloads/identityLink";
import { Log } from "./utils/log";
import { uuidv4 } from "./utils/utils";
import { PersistKey, updatePersistedValue, getItem } from "./browser/persist";
function jobIsEventJob(obj) {
return 'eventName' in obj;
}
function jobIsIdLinkJob(obj) {
return 'idLink' in obj;
}
var JobQueue = /** @class */ (function () {
function JobQueue() {
this.eventQueue = [];
this.idLinkQueue = [];
this.processing = false;
this.stopped = false;
this.paused = false;
}
// Persist the entire in-memory event queue to storage (eliminates race conditions)
JobQueue.prototype.persistEventQueue = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, updatePersistedValue(PersistKey.EventQueue, JSON.stringify(this.eventQueue), false)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
// Persist the entire in-memory idLink queue to storage (eliminates race conditions)
JobQueue.prototype.persistIdLinkQueue = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, updatePersistedValue(PersistKey.IdLinkQueue, JSON.stringify(this.idLinkQueue), false)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
// Public method to persist idLink queue (used by install success handler)
JobQueue.prototype.persistIdLinkQueuePublic = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.persistIdLinkQueue()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
JobQueue.prototype.start = function (instance) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, _d, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
_a = this;
_c = (_b = JSON).parse;
return [4 /*yield*/, getItem(PersistKey.EventQueue)];
case 1:
_a.eventQueue = _c.apply(_b, [_g.sent()]) || [];
_d = this;
_f = (_e = JSON).parse;
return [4 /*yield*/, getItem(PersistKey.IdLinkQueue)];
case 2:
_d.idLinkQueue = _f.apply(_e, [_g.sent()]) || [];
return [4 /*yield*/, this.updateEventJobs(instance)];
case 3:
_g.sent();
return [4 /*yield*/, this.updateIdLinkJobs(instance)];
case 4:
_g.sent();
Log.trace("Starting Event Queue", JSON.parse(JSON.stringify(this.eventQueue)));
Log.trace("Starting IdLink Queue", JSON.parse(JSON.stringify(this.idLinkQueue)));
this.stopped = false;
this.paused = false;
return [4 /*yield*/, this.dequeueJob(instance)];
case 5:
_g.sent();
return [2 /*return*/];
}
});
});
};
JobQueue.prototype.stop = function () {
this.stopped = true;
if (this.timeOut)
clearTimeout(this.timeOut);
this.processing = false;
};
JobQueue.prototype.pause = function () {
this.paused = true;
};
JobQueue.prototype.enqueueEvent = function (instance, args) {
return __awaiter(this, void 0, void 0, function () {
var eventName, eventData, eventPreStartBody, postStartBody, newJob, newEventJob;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
eventName = args[0];
eventData = args[1];
eventPreStartBody = Event.constructPreStart(instance, eventName, eventData);
if (!(instance.installDone && instance.kvinitDone)) return [3 /*break*/, 3];
postStartBody = Event.constructPostStart(instance, eventPreStartBody);
newJob = {
id: uuidv4(),
queuedBeforeStart: false,
preStartBody: eventPreStartBody,
postStartBody: postStartBody,
retries: 0,
eventName: eventName,
};
this.eventQueue.push(newJob);
return [4 /*yield*/, this.persistEventQueue()];
case 1:
_a.sent();
return [4 /*yield*/, this.dequeueJob(instance)];
case 2:
_a.sent();
return [2 /*return*/];
case 3:
newEventJob = {
id: uuidv4(),
queuedBeforeStart: true,
preStartBody: eventPreStartBody,
postStartBody: undefined,
retries: 0,
eventName: eventName,
};
this.eventQueue.push(newEventJob);
return [4 /*yield*/, this.persistEventQueue()];
case 4:
_a.sent();
return [2 /*return*/];
}
});
});
};
JobQueue.prototype.enqueueIdLink = function (instance, idLink) {
return __awaiter(this, void 0, void 0, function () {
var idLinkPreStartBody, postStartBody, newJob_1, newJob;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
idLinkPreStartBody = IdLink.constructPreStart(instance, idLink);
if (!(instance.installDone && instance.kvinitDone)) return [3 /*break*/, 3];
postStartBody = IdLink.constructPostStart(instance, idLinkPreStartBody);
newJob_1 = {
id: uuidv4(),
queuedBeforeStart: false,
preStartBody: idLinkPreStartBody,
postStartBody: postStartBody,
retries: 0,
idLink: idLink,
};
this.idLinkQueue.push(newJob_1);
return [4 /*yield*/, this.persistIdLinkQueue()];
case 1:
_a.sent();
return [4 /*yield*/, this.dequeueJob(instance)];
case 2:
_a.sent();
return [2 /*return*/];
case 3:
newJob = {
id: uuidv4(),
queuedBeforeStart: true,
preStartBody: idLinkPreStartBody,
postStartBody: undefined,
retries: 0,
idLink: idLink,
};
this.idLinkQueue.push(newJob);
return [4 /*yield*/, this.persistIdLinkQueue()];
case 4:
_a.sent();
return [2 /*return*/];
}
});
});
};
JobQueue.prototype.dequeueJob = function (instance) {
return __awaiter(this, void 0, void 0, function () {
var idLinkJob, result, eventJob, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// If queue is busy, prev job not finished
if (this.processing)
return [2 /*return*/, false];
// If the queue is paused, do not dequeue a new job
if (this.paused)
return [2 /*return*/, false];
// If the queue is stopped do not dequeue a new job
if (this.stopped) {
return [2 /*return*/, false];
}
idLinkJob = this.idLinkQueue.shift();
if (!idLinkJob) return [3 /*break*/, 4];
// handle idlinkjob
Log.trace("Dequeued Job: ", idLinkJob);
this.processing = true;
return [4 /*yield*/, this.processJob(instance, idLinkJob)];
case 1:
result = _a.sent();
if (this.stopped) {
return [2 /*return*/, true];
}
this.processing = false;
if (!result) return [3 /*break*/, 4];
return [4 /*yield*/, this.persistIdLinkQueue()];
case 2:
_a.sent();
return [4 /*yield*/, this.dequeueJob(instance)];
case 3: return [2 /*return*/, _a.sent()];
case 4:
eventJob = this.eventQueue.shift();
if (!eventJob) return [3 /*break*/, 8];
//handle eventJob
Log.trace("Dequeued Job: ", eventJob);
// Process the job
this.processing = true;
return [4 /*yield*/, this.processJob(instance, eventJob)];
case 5:
result = _a.sent();
if (this.stopped) {
return [2 /*return*/, true];
}
this.processing = false;
if (!result) return [3 /*break*/, 8];
return [4 /*yield*/, this.persistEventQueue()];
case 6:
_a.sent();
return [4 /*yield*/, this.dequeueJob(instance)];
case 7: return [2 /*return*/, _a.sent()];
case 8:
// If neither queue had a job, break out of recursion
if (!idLinkJob && !eventJob)
return [2 /*return*/, false];
return [2 /*return*/, true];
}
});
});
};
JobQueue.prototype.processJob = function (instance, job) {
return __awaiter(this, void 0, void 0, function () {
var _i, _a, denyName, _b, _c, denyIdLinkKey, success, _loop_1, this_1, state_1;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (jobIsEventJob(job)) {
for (_i = 0, _a = instance.kochavaConfig.privacy.deny_event_names; _i < _a.length; _i++) {
denyName = _a[_i];
if (denyName === job.eventName) {
Log.debug("Denied event_name ".concat(denyName, ", dropping request."));
return [2 /*return*/, true];
}
}
}
else if (jobIsIdLinkJob(job)) {
for (_b = 0, _c = instance.kochavaConfig.privacy.deny_identity_links; _b < _c.length; _b++) {
denyIdLinkKey = _c[_b];
if (denyIdLinkKey === Object.keys(job.idLink)[0]) {
Log.debug("Denied identity_link ".concat(denyIdLinkKey, ", dropping request."));
return [2 /*return*/, true];
}
}
}
success = false;
_loop_1 = function () {
var retryWaterfall, retryIndex, retrySec_1;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
if (!!instance.sdkDisabled) return [3 /*break*/, 2];
return [4 /*yield*/, this_1.attemptJob(instance, job)];
case 1:
success = _e.sent();
return [3 /*break*/, 3];
case 2:
success = true;
_e.label = 3;
case 3:
// If our job succeeded
if (success) {
// Job Done
Log.trace("Job processed successfully:", job);
return [2 /*return*/, { value: true }];
}
if (!!this_1.stopped) return [3 /*break*/, 5];
retryWaterfall = instance.kochavaConfig.networking.retry_waterfall;
retryIndex = (job.retries > retryWaterfall.length - 1) ?
retryWaterfall.length - 1 : job.retries;
retrySec_1 = retryWaterfall[retryIndex];
Log.error("Job failed, attempting again in ".concat(retrySec_1, " seconds"));
return [4 /*yield*/, new Promise(function (resolve) { return _this.timeOut = setTimeout(resolve, retrySec_1 * 1000); })];
case 4:
_e.sent();
job.retries++;
_e.label = 5;
case 5: return [2 /*return*/];
}
});
};
this_1 = this;
_d.label = 1;
case 1: return [5 /*yield**/, _loop_1()];
case 2:
state_1 = _d.sent();
if (typeof state_1 === "object")
return [2 /*return*/, state_1.value];
_d.label = 3;
case 3:
if (!success && !this.stopped) return [3 /*break*/, 1];
_d.label = 4;
case 4:
// Job Canceled
return [2 /*return*/, true];
}
});
});
};
JobQueue.prototype.attemptJob = function (instance, job) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(job.preStartBody.action === "event")) return [3 /*break*/, 2];
return [4 /*yield*/, Event.send(instance, job.preStartBody, job.postStartBody)];
case 1: return [2 /*return*/, _a.sent()];
case 2:
if (!(job.preStartBody.action === "identityLink")) return [3 /*break*/, 4];
return [4 /*yield*/, IdLink.send(instance, job)];
case 3: return [2 /*return*/, _a.sent()];
case 4:
Log.warn("Invalid action in job from jobqueue, cancelling.");
return [2 /*return*/, true];
}
});
});
};
JobQueue.prototype.updateEventJobs = function (instance) {
return __awaiter(this, void 0, void 0, function () {
var _i, _a, job;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
for (_i = 0, _a = this.eventQueue; _i < _a.length; _i++) {
job = _a[_i];
if (job.queuedBeforeStart) {
job.postStartBody = Event.constructPostStart(instance, job.preStartBody);
}
}
return [4 /*yield*/, this.persistEventQueue()];
case 1:
_b.sent();
return [2 /*return*/];
}
});
});
};
JobQueue.prototype.updateIdLinkJobs = function (instance) {
return __awaiter(this, void 0, void 0, function () {
var _i, _a, job;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
for (_i = 0, _a = this.idLinkQueue; _i < _a.length; _i++) {
job = _a[_i];
if (job.queuedBeforeStart) {
job.postStartBody = IdLink.constructPostStart(instance, job.preStartBody);
}
}
return [4 /*yield*/, this.persistIdLinkQueue()];
case 1:
_b.sent();
return [2 /*return*/];
}
});
});
};
return JobQueue;
}());
export default JobQueue;