Skip to content

Commit

Permalink
Segment locally stored data by access token to allow multiple embeds …
Browse files Browse the repository at this point in the history
…from same source url with different tokens.
  • Loading branch information
Asheem Mamoowala committed Jul 26, 2018
1 parent 2c25dd8 commit ab94794
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,18 @@ class TurnstileEvent {
if (this.pendingRequest || this.queue.length === 0) {
return;
}
let dueForEvent = false;
const storageKey = `${turnstileEventStorageKey}:${config.ACCESS_TOKEN || ''}`;
let dueForEvent = (this.eventData.accessToken !== config.ACCESS_TOKEN);

//Reset event data cache if the access token changed.
if (dueForEvent) {
this.eventData.anonId = this.eventData.lastSuccess = null;
}
if (!this.eventData.anonId || !this.eventData.lastSuccess &&
isLocalStorageAvailable) {
//Retrieve cached data
try {
const data = window.localStorage.getItem(turnstileEventStorageKey);
const data = window.localStorage.getItem(storageKey);
if (data) {
this.eventData = JSON.parse(data);
}
Expand All @@ -179,8 +185,6 @@ class TurnstileEvent {
dueForEvent = dueForEvent || daysElapsed >= 1 || daysElapsed < 0 || lastUpdate.getDate() !== nextDate.getDate();
}

dueForEvent = dueForEvent || (this.eventData.accessToken !== config.ACCESS_TOKEN);

if (!dueForEvent) {
return this.processRequests();
}
Expand Down Expand Up @@ -210,7 +214,7 @@ class TurnstileEvent {
this.eventData.accessToken = config.ACCESS_TOKEN;
if (isLocalStorageAvailable) {
try {
window.localStorage.setItem(turnstileEventStorageKey, this.eventData);
window.localStorage.setItem(storageKey, JSON.stringify(this.eventData));
} catch (e) {
warnOnce('Unable to write to LocalStorage');
}
Expand Down

0 comments on commit ab94794

Please sign in to comment.