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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tanstack/optimistic

v0.0.3

Published

Core optimistic updates library

Readme

@TanStack/optimistic

A core library for creating fast optimistic updates with flexible backend support.

Installation

pnpm add @TanStack/optimistic

Overview

@TanStack/optimistic provides a robust solution for managing data synchronization between your frontend application and backend services. It offers:

  • Optimistic Updates: Apply changes instantly in the UI while syncing in the background
  • Flexible Backend Support: Works with any backend or sync engine
  • Immutable Snapshots: Create immutable snapshots of updates that can be persisted and rolled back

Core Concepts

Collections

Collections are the central concept in @TanStack/optimistic. A collection represents a set of data that can be synchronized, queried, and modified. Each collection:

  • Has a unique identifier
  • Contains data items
  • Provides CRUD operations (insert, update, delete)
  • Manages its own sync and persistence logic

Transactions

All mutations in @TanStack/optimistic are handled through transactions. Transactions:

  • Group related changes together
  • Track the state of mutations (pending, persisting, completed, failed)
  • Support rollback in case of errors
  • Provide optimistic updates to the UI

Proxies

The library uses proxies to create immutable snapshots and track changes:

  • Deep change tracking at any level of object nesting
  • Special handling for various types (Date, RegExp, Map, Set)
  • Circular reference handling with WeakMap cache

Framework Adapters

This is a core package that provides the fundamental optimistic update functionality. For most applications, you'll want to use this package with a framework-specific adapter:

  • @TanStack/react-optimistic - React adapter with hooks for easy integration
  • Other framework adapters (coming soon)

The framework adapters provide idiomatic ways to use the core optimistic update functionality within your chosen framework.

API Reference

Data Operations

Insert

// Insert a single item
insert({ text: "Buy groceries", completed: false })

// Insert multiple items
insert([
  { text: "Buy groceries", completed: false },
  { text: "Walk dog", completed: false },
])

// Insert with custom key
insert({ text: "Buy groceries" }, { key: "grocery-task" })

Update

We use a proxy to capture updates as immutable draft optimistic updates.

// Update a single item
update(todo, (draft) => {
  draft.completed = true
})

// Update multiple items
update([todo1, todo2], (drafts) => {
  drafts.forEach((draft) => {
    draft.completed = true
  })
})

// Update with metadata
update(todo, { metadata: { reason: "user update" } }, (draft) => {
  draft.text = "Updated text"
})

Delete

// Delete a single item
delete todo

// Delete multiple items
delete [todo1, todo2]

// Delete with metadata
delete (todo, { metadata: { reason: "completed" } })

Schema Validation

Collections can optionally include a standard schema for data validation:

const todoCollection = createCollection({
  id: "todos",
  sync: {
    /* sync config */
  },
  mutationFn: {
    /* mutation functions */
  },
  schema: todoSchema, // Standard schema interface
})

Transaction Management

The library includes a simple yet powerful transaction management system. Transactions are created using the createTransaction function:

const tx = createTransaction({
  mutationFn: async ({ transaction }) => {
    // Implement your mutation logic here
    // This function is called when the transaction is committed
  },
})

// Apply mutations within the transaction
tx.mutate(() => {
  // All collection operations (insert/update/delete) within this callback
  // will be part of this transaction
})

Transactions progress through several states:

  1. pending: Initial state when a transaction is created
  2. persisting: Transaction is being persisted to the backend
  3. completed: Transaction has been successfully persisted
  4. failed: An error was thrown while persisting or syncing back the Transaction