|
| 1 | +var __assign = (this && this.__assign) || function () { |
| 2 | + __assign = Object.assign || function(t) { |
| 3 | + for (var s, i = 1, n = arguments.length; i < n; i++) { |
| 4 | + s = arguments[i]; |
| 5 | + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) |
| 6 | + t[p] = s[p]; |
| 7 | + } |
| 8 | + return t; |
| 9 | + }; |
| 10 | + return __assign.apply(this, arguments); |
| 11 | +}; |
| 12 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 13 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 14 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 15 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 16 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 17 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 18 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 19 | + }); |
| 20 | +}; |
| 21 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 22 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 23 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 24 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 25 | + function step(op) { |
| 26 | + if (f) throw new TypeError("Generator is already executing."); |
| 27 | + while (_) try { |
| 28 | + 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; |
| 29 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 30 | + switch (op[0]) { |
| 31 | + case 0: case 1: t = op; break; |
| 32 | + case 4: _.label++; return { value: op[1], done: false }; |
| 33 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 34 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 35 | + default: |
| 36 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 37 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 38 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 39 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 40 | + if (t[2]) _.ops.pop(); |
| 41 | + _.trys.pop(); continue; |
| 42 | + } |
| 43 | + op = body.call(thisArg, _); |
| 44 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 45 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 46 | + } |
| 47 | +}; |
| 48 | +var __spreadArray = (this && this.__spreadArray) || function (to, from) { |
| 49 | + for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) |
| 50 | + to[j] = from[i]; |
| 51 | + return to; |
| 52 | +}; |
| 53 | +import { gzip } from '../data-processing/compression'; |
| 54 | +import { retrieveData, saveData, clearData } from '../browser/storage'; |
| 55 | +(function (self) { |
| 56 | + console.log("WorkerGlobalScope: ", WorkerGlobalScope); |
| 57 | + console.log("self: ", self); |
| 58 | +})(self); |
| 59 | +var STORAGE_SETTINGS_DEFAULTS = { |
| 60 | + resourceLimit: 0, |
| 61 | + ttl: 5000, |
| 62 | + apiUrl: "", |
| 63 | + dataKey: "events", |
| 64 | +}; |
| 65 | +var storageSettings = STORAGE_SETTINGS_DEFAULTS; |
| 66 | +var isAppLoaded = true; |
| 67 | +export function start(options) { |
| 68 | + return __awaiter(this, void 0, void 0, function () { |
| 69 | + return __generator(this, function (_a) { |
| 70 | + init(options); |
| 71 | + reAttemptSync(storageSettings.apiUrl, storageSettings.ttl, storageSettings.dataKey); |
| 72 | + return [2 /*return*/]; |
| 73 | + }); |
| 74 | + }); |
| 75 | +} |
| 76 | +export function init(options) { |
| 77 | + if (storageSettings.resourceLimit === 0) { |
| 78 | + storageSettings = __assign(__assign({}, options), { dataKey: options.dataKey ? options.dataKey : storageSettings.dataKey }); |
| 79 | + } |
| 80 | +} |
| 81 | +export function getConfig() { |
| 82 | + return storageSettings; |
| 83 | +} |
| 84 | +export function handle(data) { |
| 85 | + return __awaiter(this, void 0, void 0, function () { |
| 86 | + var eventsData, compressedData; |
| 87 | + return __generator(this, function (_a) { |
| 88 | + switch (_a.label) { |
| 89 | + case 0: return [4 /*yield*/, retrieveData(storageSettings.dataKey)]; |
| 90 | + case 1: |
| 91 | + eventsData = _a.sent(); |
| 92 | + if (eventsData) { |
| 93 | + if (eventsData.length < storageSettings.resourceLimit) { |
| 94 | + saveData(storageSettings.dataKey, __spreadArray([data], eventsData)); |
| 95 | + } |
| 96 | + else { |
| 97 | + if (self.navigator.onLine) { |
| 98 | + compressedData = gzip(eventsData); |
| 99 | + syncData(storageSettings.apiUrl, compressedData) |
| 100 | + .then(function (res) { |
| 101 | + clearData(); |
| 102 | + saveData(storageSettings.dataKey, [data]); |
| 103 | + }) |
| 104 | + .catch(function (err) { |
| 105 | + saveData(storageSettings.dataKey, __spreadArray([data], eventsData)); |
| 106 | + }); |
| 107 | + } |
| 108 | + else { |
| 109 | + saveData(storageSettings.dataKey, __spreadArray([data], eventsData)); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + else { |
| 114 | + saveData(storageSettings.dataKey, [data]); |
| 115 | + } |
| 116 | + return [2 /*return*/]; |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | +} |
| 121 | +export function onAppClose() { |
| 122 | + isAppLoaded = false; |
| 123 | + reAttemptSync(storageSettings.apiUrl, storageSettings.ttl, storageSettings.dataKey); |
| 124 | +} |
| 125 | +function reAttemptSync(url, ttl, key) { |
| 126 | + function compressAndSend() { |
| 127 | + return __awaiter(this, void 0, void 0, function () { |
| 128 | + var eventsData, compressedData; |
| 129 | + return __generator(this, function (_a) { |
| 130 | + switch (_a.label) { |
| 131 | + case 0: |
| 132 | + if (!self.navigator.onLine) return [3 /*break*/, 2]; |
| 133 | + return [4 /*yield*/, retrieveData(key)]; |
| 134 | + case 1: |
| 135 | + eventsData = _a.sent(); |
| 136 | + if (eventsData) { |
| 137 | + compressedData = gzip(eventsData); |
| 138 | + syncData(url, compressedData) |
| 139 | + .then(function (res) { |
| 140 | + clearData(); |
| 141 | + }) |
| 142 | + .catch(function (err) { |
| 143 | + console.log("error is", err); |
| 144 | + }); |
| 145 | + } |
| 146 | + else { |
| 147 | + if (!isAppLoaded) { |
| 148 | + // @ts-ignore |
| 149 | + self.close(); // kill web worker |
| 150 | + } |
| 151 | + } |
| 152 | + _a.label = 2; |
| 153 | + case 2: return [2 /*return*/]; |
| 154 | + } |
| 155 | + }); |
| 156 | + }); |
| 157 | + } |
| 158 | + var interval = setInterval(compressAndSend, ttl); |
| 159 | +} |
| 160 | +export function syncData(url, data) { |
| 161 | + return __awaiter(this, void 0, void 0, function () { |
| 162 | + var response; |
| 163 | + return __generator(this, function (_a) { |
| 164 | + switch (_a.label) { |
| 165 | + case 0: return [4 /*yield*/, fetch(url, { |
| 166 | + method: 'POST', |
| 167 | + //mode: 'cors', |
| 168 | + cache: 'no-cache', |
| 169 | + headers: { |
| 170 | + 'Content-Type': 'application/json', |
| 171 | + 'Content-Encoding': "gzip", |
| 172 | + }, |
| 173 | + redirect: 'follow', |
| 174 | + referrerPolicy: 'no-referrer', |
| 175 | + body: data |
| 176 | + })]; |
| 177 | + case 1: |
| 178 | + response = _a.sent(); |
| 179 | + return [2 /*return*/, response.json()]; |
| 180 | + } |
| 181 | + }); |
| 182 | + }); |
| 183 | +} |
0 commit comments