swr-cache-event-target
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

swr-cache-event-target

A stale-while-revalidate cache with synchronous reads, background refreshes and event emitting.

Introduction

swr-cache-event-target is a minimal stale-while-revalidate cache that returns values synchronously when available while periodically revalidating them in the background. It uses the browser's EventTarget API to emit events when the cache is updated.

Usage

import { SwrCache } from "swr-cache-event-target";

const cache = new SwrCache({
  getValue: async (id: string) => {
    const response = await fetch(`https://api.example.com/data?id=${id}`);
    return response.json();
  },
  ttlMs: 10_000,
});

// Initial read is asynchronous
console.log(await cache.read("123"));
// Subsequent reads are synchronous
console.log(cache.read("123"));

cache.addEventListener("state:update", (event) => {
  console.log("Cache event:", event.detail);
});

Synchronous Usage

// Initially the cache is empty
console.log(cache.peek("123"));

// Prime the cache
cache.prime("123");

// Later, access the cache synchronously
console.log(cache.peek("123"));

Installation

With pnpm

pnpm i swr-cache-event-target

With npm

npm i swr-cache-event-target

Contribute

We welcome contributions! If you'd like to improve swr-cache-event-target or have any feedback, feel free to open an issue or submit a pull request.

License

MIT

/swr-cache-event-target/

    Package Sidebar

    Install

    npm i swr-cache-event-target

    Weekly Downloads

    3

    Version

    0.0.7

    License

    MIT

    Unpacked Size

    46.9 kB

    Total Files

    11

    Last publish

    Collaborators

    • sebinsua