svelte-cached Svelte Themes

Svelte Cached

Store that automatically syncs it's value with the source

🌟svelte-cached

A simplified store for storing cached data. Useful for data that needs to keep in sync with it's source.


Install

npm install svelte-cached

Usage

import { cached } from 'svelte-cached';

Set the initial value for the store. To keep the store in sync with it's source, provide a retrieve async function as the second argument.

TTL is set as third argument, if not provided you will need to manually call store.retrieve() function whenever you want to sync the state.

const getLatestBitcoinPrice = async () => {
  const response = await fetch(...);
  // ...
  return price;
};

// get a fresh price every 5 seconds
const btcPrice = cached(0, getLatestBitcoinPrice, 5000);

Use the cached store instance just like any other Svelte store.

<p>Current BTC price:{$btcPrice}</p>

Licence

MIT

Top categories

Loading Svelte Themes