Receives the alerts, newest first
The subscription
const { stop } = watchPriceAlerts((alerts) => setAlerts(alerts));
export function watchPriceAlerts(onLoad: (alerts: PriceAlert[]) => void): PriceAlertsWatch {
let isActive = true;
const load = async () => {
try {
const loaded = await loadPriceAlerts();
if (isActive) onLoad(loaded);
} catch (error) {
logger.warn(`Failed to load price alerts: ${getErrorMessage(error)}`, { error });
}
};
const handleChange: ChangedListener = (changes, areaName) => {
if (areaName === 'local' && Object.keys(changes).some(isPriceAlertKey)) void load();
};
const ready = load();
cstorage.onChanged.addListener(handleChange);
return {
ready,
stop: () => {
isActive = false;
cstorage.onChanged.removeListener(handleChange);
},
};
}
Loads the user's price alerts, now and whenever any page or the background worker changes them.