UNPKG

@airgrid/edgekit

Version:

A privacy focused library for cookie-less audience creation.

88 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchedAudienceStore = void 0; var utils_1 = require("../utils"); var types_1 = require("../../types"); var MatchedAudienceStore = /** @class */ (function () { function MatchedAudienceStore() { this.matchedAudiences = {}; this.matchedAudienceIds = []; this.unsetDueToVersionIncAudienceIds = []; this.storeLoadedAt = utils_1.timeStampInSecs(); this._load(); } MatchedAudienceStore.prototype._save = function () { utils_1.storage.set(types_1.StorageKeys.MATCHED_AUDIENCES, this.matchedAudiences); utils_1.storage.set(types_1.StorageKeys.MATCHED_AUDIENCE_IDS, this.matchedAudienceIds); }; MatchedAudienceStore.prototype._hasAudienceExpired = function (expiresAt) { return expiresAt < this.storeLoadedAt; }; MatchedAudienceStore.prototype._unsetAudience = function (id) { delete this.matchedAudiences[id]; }; MatchedAudienceStore.prototype._updatePageViewFlag = function (id, state) { this.matchedAudiences[id].matchedOnCurrentPageView = state; }; MatchedAudienceStore.prototype._load = function () { var _this = this; // TODO: @ydennisy remove this backward compat code. // https://github.com/AirGrid/edgekit/issues/152 var loadedAudiences = utils_1.storage.get(types_1.StorageKeys.MATCHED_AUDIENCES) || {}; if (Array.isArray(loadedAudiences)) { utils_1.storage.remove(types_1.StorageKeys.MATCHED_AUDIENCES); loadedAudiences = loadedAudiences.reduce(function (acc, cur) { acc[cur.id] = cur; }, {}); } this.matchedAudiences = loadedAudiences; Object.entries(loadedAudiences).forEach(function (_a) { var id = _a[0], audience = _a[1]; if (_this._hasAudienceExpired(audience.expiresAt)) { _this._unsetAudience(id); return; } _this._updatePageViewFlag(id, false); }); this.matchedAudienceIds = Object.keys(this.matchedAudiences); this._save(); }; MatchedAudienceStore.prototype.unsetAudiencesIfVersionIncremented = function (audienceDefinitions) { var _this = this; audienceDefinitions.forEach(function (audience) { var incomingVersion = audience.version; var currentVersion = _this.matchedAudiences[audience.id] ? _this.matchedAudiences[audience.id].version : null; if (currentVersion && currentVersion < incomingVersion) { _this._unsetAudience(audience.id); _this.unsetDueToVersionIncAudienceIds.push(audience.id); } }); }; MatchedAudienceStore.prototype.isMatched = function (id, version) { return !!(this.matchedAudiences[id] && this.matchedAudiences[id].version >= version); }; MatchedAudienceStore.prototype.setAudiences = function (matchedAudiences) { var _this = this; matchedAudiences.forEach(function (audience) { _this.matchedAudiences[audience.id] = audience; if (_this.unsetDueToVersionIncAudienceIds.includes(audience.id)) { _this._updatePageViewFlag(audience.id, false); } }); this.matchedAudienceIds = Object.keys(this.matchedAudiences); this._save(); }; MatchedAudienceStore.prototype.getMatchedAudiences = function () { // TODO: this is for backward compat. // https://github.com/AirGrid/edgekit/issues/152 return Object.entries(this.matchedAudiences).map(function (_a) { var _ = _a[0], audience = _a[1]; return audience; }); }; return MatchedAudienceStore; }()); exports.matchedAudienceStore = new MatchedAudienceStore(); //# sourceMappingURL=matchedAudiences.js.map