ChemPal Documentation - v1.18.0
    Preparing search index...

    Function watchPriceAlerts

    • Loads the user's price alerts, now and whenever any page or the background worker changes them.

      Parameters

      • onLoad: (alerts: PriceAlert[]) => void

        Receives the alerts, newest first

      Returns PriceAlertsWatch

      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);
      },
      };
      }