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

    Function updatePriceSeries

    • Read, change and write a price-history series in one transaction, so the page and the background worker can't overwrite each other's points.

      Parameters

      • id: string

        The series id.

      • update: (existing: undefined | PriceHistoryEntry) => undefined | PriceHistoryEntry

        Returns the series to store, given the stored one (if any). Return undefined to leave it unchanged. Must be synchronous, or the transaction would close.

      Returns Promise<void>

      Resolves once the write completes; errors are logged, not thrown.

      await updatePriceSeries("3f9c2a…", (series) =>
      series && { ...series, points: [...series.points, { t: Date.now(), usd: 19.99 }] },
      );
      export async function updatePriceSeries(
      id: string,
      update: (existing: PriceHistoryEntry | undefined) => PriceHistoryEntry | undefined,
      ): Promise<void> {
      try {
      const db = await getDB();
      const tx = db.transaction(IDB_STORE.PRICE_HISTORY, 'readwrite');
      const updated = update(await tx.store.get(id));
      if (updated) {
      await tx.store.put(updated);
      }
      await tx.done;
      } catch (error) {
      logger.error(`Failed to update price series in IndexedDB: ${getErrorMessage(error)}`, {
      error,
      });
      }
      }