npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rtk-optimistic

v1.0.0

Published

Automatic optimistic updates with snapshot-based rollback for Redux Toolkit createAsyncThunk

Readme

rtk-optimistic

Automatic optimistic updates with snapshot-based rollback for Redux Toolkit's createAsyncThunk.

Stop hand-writing rollback logic for every optimistic update. rtk-optimistic snapshots your slice state right before the optimistic write, and automatically restores it if the underlying API call fails — no manual try/catch + revert code in every thunk.

Why

A common pattern with Redux Toolkit is:

// The "manual" way — every thunk repeats this dance
builder.addCase(updateTodo.pending, (state, action) => {
  state.entities[action.meta.arg.id] = { ...state.entities[action.meta.arg.id], ...action.meta.arg.changes };
});
builder.addCase(updateTodo.rejected, (state, action) => {
  // ...now you have to remember what it was before, and revert it by hand
});

That revert logic gets duplicated (and often buggy or forgotten) across every optimistic thunk in a codebase. rtk-optimistic handles the snapshot + restore for you.

Install

npm install rtk-optimistic

Requires @reduxjs/toolkit (^1.9.0 or ^2.0.0) as a peer dependency.

Usage

import { createOptimisticThunk } from "rtk-optimistic";
import { createSlice } from "@reduxjs/toolkit";

interface Todo {
  id: string;
  text: string;
  done: boolean;
}

interface TodosState {
  list: Todo[];
}

const toggleTodo = createOptimisticThunk<TodosState, { id: string; done: boolean }, Todo>({
  typePrefix: "todos/toggle",
  apiCall: (arg) => api.updateTodo(arg.id, { done: arg.done }),
  optimisticUpdate: (state, arg) => {
    const todo = state.list.find((t) => t.id === arg.id);
    if (todo) todo.done = arg.done;
  },
  // optional: reconcile with the real server response on success
  onSuccess: (state, serverTodo) => {
    const todo = state.list.find((t) => t.id === serverTodo.id);
    if (todo) Object.assign(todo, serverTodo);
  },
});

const todosSlice = createSlice({
  name: "todos",
  initialState: { list: [] } as TodosState,
  reducers: {},
  extraReducers: (builder) => {
    toggleTodo.attach(builder);
  },
});

// Dispatch it like any other thunk:
dispatch(toggleTodo.thunk({ id: "1", done: true }));

If api.updateTodo rejects, the slice state is automatically restored to exactly what it was before the optimistic update — no extra code needed.

API

createOptimisticThunk(config)

| Option | Required | Description | |---|---|---| | typePrefix | ✅ | Same as the first argument to createAsyncThunk. | | apiCall | ✅ | Your async function (the real network request). | | optimisticUpdate | ✅ | Mutates the draft state immediately, before the API call resolves. | | onSuccess | – | Reconcile state with the real server response once resolved. | | getSnapshot | – | Customize what gets snapshotted (default: full slice state via structuredClone). Useful for large slices where snapshotting everything is wasteful. | | restoreSnapshot | – | Customize how a snapshot is restored on failure (default: Object.assign). |

Returns { thunk, attach }:

  • thunk — the underlying createAsyncThunk action creator. Dispatch it as usual.
  • attach(builder) — call inside your slice's extraReducers to register the pending/fulfilled/rejected cases.

How it works

  1. On dispatch (pending) — a snapshot of the current slice state is stored (keyed by the thunk's requestId, so concurrent calls don't clash), then your optimisticUpdate is applied immediately.
  2. On success (fulfilled) — the snapshot is discarded; your optional onSuccess runs to reconcile with the real server response.
  3. On failure (rejected) — the stored snapshot is restored into the draft automatically, undoing the optimistic write.

Works with plain object/array state shapes, including state managed by createEntityAdapter.

License

MIT