UNPKG

@airgrid/edgekit

Version:

A privacy focused library for cookie-less audience creation.

47 lines 1.7 kB
import { __assign, __spreadArrays } from "tslib"; import { storage, timeStampInSecs } from '../utils'; import { StorageKeys } from '../../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 = storage.get(StorageKeys.PAGE_VIEWS) || []; }; ViewStore.prototype._save = function () { storage.set(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 = timeStampInSecs(); var pageView = __assign({ ts: ts, features: features }, metadata); this.pageViews.push(pageView); this._trim(); this._save(); }; ViewStore.prototype.getCopyOfPageViews = function () { return __spreadArrays(this.pageViews); }; return ViewStore; }()); export var viewStore = new ViewStore(); //# sourceMappingURL=pageview.js.map