Skip to content

Commit

Permalink
store notification metadata in individual keys to avoid races (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
eritbh authored Oct 10, 2024
1 parent 3d4046f commit cdbde48
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions extension/data/background/handlers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,40 @@ import browser from 'webextension-polyfill';
import {messageHandlers} from '../messageHandling';
import {makeRequest} from './webrequest';

const NOTIFICATION_STORAGE_KEY = 'tb-notifications-storage';
/**
* Gets the storage key where metadata for the given notification is stored.
* @param {string} notificationID
* @returns {string}
*/
const notificationMetaDataKey = notificationID => `notifmeta-${notificationID}`;

/**
* Sets the notification ID and meta data object for a given notification.
* @param notificationID string notificationID
* @param notificationObject object containing the meta data
*/
async function setNotificationMetaData (notificationID, notificationObject) {
const result = await browser.storage.local.get({[NOTIFICATION_STORAGE_KEY]: {}});
result[NOTIFICATION_STORAGE_KEY][notificationID] = notificationObject;
await browser.storage.local.set({
[NOTIFICATION_STORAGE_KEY]: result[NOTIFICATION_STORAGE_KEY],
function setNotificationMetaData (notificationID, notificationObject) {
return browser.storage.session.set({
[notificationMetaDataKey(notificationID)]: notificationObject,
});
return;
}

/**
* Returns the notification meta data object for a given notification id.
* @param notificationID notificationID
* @returns {promise<object>}
* @returns {Promise<object>}
*/
async function getNotificationMetaData (notificationID) {
const result = await browser.storage.local.get({[NOTIFICATION_STORAGE_KEY]: {}});
if (Object.prototype.hasOwnProperty.call(result[NOTIFICATION_STORAGE_KEY], notificationID)) {
return result[NOTIFICATION_STORAGE_KEY][notificationID];
}
return null;
function getNotificationMetaData (notificationID) {
return browser.storage.session.get({[notificationMetaDataKey(notificationID)]: null});
}

/**
* Deletes the notification meta data object for a given notification id.
* @param notificationID subreddit
* @returns {promise<object>}
*/
async function deleteNotificationMetaData (notificationID) {
const result = await browser.storage.local.get({[NOTIFICATION_STORAGE_KEY]: {}});
if (Object.prototype.hasOwnProperty.call(result[NOTIFICATION_STORAGE_KEY], notificationID)) {
delete result[NOTIFICATION_STORAGE_KEY][notificationID];
await browser.storage.local.set({
[NOTIFICATION_STORAGE_KEY]: result[NOTIFICATION_STORAGE_KEY],
});
}
return;
function deleteNotificationMetaData (notificationID) {
return browser.storage.session.remove(notificationMetaDataKey(notificationID));
}

// TODO: I know we've had this conversation before but I'm 99% sure this isn't
Expand Down

0 comments on commit cdbde48

Please sign in to comment.