@airgrid/edgekit
Version:
A privacy focused library for cookie-less audience creation.
50 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.viewStore = void 0;
var tslib_1 = require("tslib");
var utils_1 = require("../utils");
var types_1 = require("../../types");
var DEFAULT_MAX_FEATURES_SIZE = 300;
var ViewStore = /** @class */ (function () {
function ViewStore() {
this.pageViews = [];
this.storageSize = DEFAULT_MAX_FEATURES_SIZE;
this._load();
}
ViewStore.prototype._load = function () {
this.pageViews = utils_1.storage.get(types_1.StorageKeys.PAGE_VIEWS) || [];
};
ViewStore.prototype._save = function () {
utils_1.storage.set(types_1.StorageKeys.PAGE_VIEWS, this.pageViews);
};
ViewStore.prototype._trim = function () {
if (this.pageViews.length <= this.storageSize)
return;
this.pageViews.sort(function (a, b) { return b.ts - a.ts; });
this.pageViews = this.pageViews.slice(0, this.storageSize);
};
/**
* @param storageSize Max pageView items to be kept
*/
ViewStore.prototype.setStorageSize = function (storageSize) {
if (!storageSize || storageSize < 0)
return;
this.storageSize = storageSize;
};
ViewStore.prototype.savePageView = function (features, metadata) {
if (!features || Object.keys(features).length < 1)
return;
var ts = utils_1.timeStampInSecs();
var pageView = tslib_1.__assign({ ts: ts,
features: features }, metadata);
this.pageViews.push(pageView);
this._trim();
this._save();
};
ViewStore.prototype.getCopyOfPageViews = function () {
return tslib_1.__spreadArrays(this.pageViews);
};
return ViewStore;
}());
exports.viewStore = new ViewStore();
//# sourceMappingURL=pageview.js.map