pinia-plugin-persistedstate icon indicating copy to clipboard operation
pinia-plugin-persistedstate copied to clipboard

Having interval inside a persistent store breaks persisting

Open KarelVendla opened this issue 1 year ago • 0 comments

Describe the bug

I have a store which persists data as cookies. I added stateful interval that I am sharing across components, this broke the store persistence.

anything related to the interval is not persisted.

the store persistence options

  {
    persist: {
      paths: ["stripeSubscriptionParams", "checkoutSuccess"],
      storage: persistedState.cookiesWithOptions({
        maxAge: HoursToSeconds(23),
      }),
    },
  },

row in the store

const { durationRemaining, stopTicking, startTicking } = useCountdown(MinutesToSeconds(10));

useCountdown composable

export function useCountdown(duration: number) {
  const durationRemaining = ref<number>(duration);
  const interval = ref<ReturnType<typeof setInterval>>();
  function startTicking() {
    durationRemaining.value = duration;
    interval.value = setInterval(() => {
      if (durationRemaining.value > 0) {
        durationRemaining.value--;
      } else {
        clearInterval(interval.value);
      }
    }, 1000);
  }

  function stopTicking() {
    clearInterval(interval.value);
  }

  return {
    durationRemaining,
    stopTicking,
    startTicking,
  };
}

Reproduction

provided some code in bug description.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
    Memory: 5.50 GB / 15.89 GB
  Binaries:
    Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (122.0.2365.52)
    Internet Explorer: 11.0.19041.3636



### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/prazdevs/pinia-plugin-persistedstate/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guide](https://github.com/prazdevs/pinia-plugin-persistedstate/blob/main/CONTRIBUTING.md).
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a [minimal reproducible](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

KarelVendla avatar Feb 26 '24 11:02 KarelVendla