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

svelte-optimistic-store

v0.0.3

Published

Handle optimistic state in svelte with one store

Downloads

2

Readme

Svelte v4 npm License downloads

svelte-optimistic-store

Store to handle optimistic values in svelte

Inspired by reacts useOptimistic Hook

Installation

# install the dependence
npm i svelte-optimistic-store

Api

optimistic

Creates a writable store which can handle optimistic updates.

  • initialValue: value of the store on initialization

Returns a store with these properties:

  • subscribe: subscribe method to get the current value of the store
  • update: update method, either optimistic or just like a basic writable store
  • set: basic set method like a writable store
  • isOptimistic: readonly store to know if the current value of the store is optimistic or not
  • cancel: cancel an optimistic update

Usage

// create a optimistic store with a default value
import { optimistic } from 'svelte-optimistic-store';

const likeStore = optimistic(1);

//update and set the store like always
likeStore.set(2);
likeStore.update((cur) => cur + 1);

//update asynchronously (after one second store will change)
likeStore.update(
	(cur) =>
		new Promise((res) =>
			setTimeout(() => {
				res(cur + 1);
			}, 1000)
		)
);

//update optimistically (after one second store will change to the actual value, but during this second the second argument will be value of the store)
likeStore.update(
	(cur) =>
		new Promise((res) =>
			setTimeout(() => {
				res(cur + 1);
			}, 1000)
		),
	(cur) => cur + 1 // updater function or just a value
);

A Rejected promise in the updater function will reset the value to its previous state

//Value will be set to 2 and after one second back to 1 due to the rejected promise
likeStore.update(
	(cur) =>
		new Promise((res, rej) =>
			setTimeout(() => {
				rej();
			}, 1000)
		),
	(cur) => cur + 1
);

If you want your optimistic item to have a different type than the normal store type you can provide two generics to the store (e.g if you want a way to distinguish a optimistic array item and add special classes to it). By default the second generic is the same as the first.

import { optimistic } from 'svelte-optimistic-store';

type Todo = {
	id: number;
	content: string;
	done: boolean;
};

type OptimisticTodo = Todo & {
	optimistic: true;
};

const todoStore = optimistic<Todo[], OptimisticTodo[]>([]);

todoStore.update(
	(cur) =>
		new Promise((res) =>
			setTimeout(() => {
				res([...cur, { id: 2, content: 'Write a post', done: false }]);
			}, 1000)
		),
	(cur) => [...cur, { id: 2, content: 'Write a post', done: false, optimistic: true }]
);

In your UI you can do something like this now

{#each $todoStore as todo }
	<div class:ghost={todo.optimistic}>
		{todo.content}
	</div>
{/each}

You can cancel an optimistic update by calling cancel. This will reset the value of the store back to its value before making an optimistic update. Note it will also prevent the current asynchronous update to the store.

const likeStore = optimistic(1);

//update optimistically (after one second store will change to the actual value, as long as the update is not canceled)
likeStore.update(
	(cur) =>
		new Promise((res) =>
			setTimeout(() => {
				res(cur + 1);
			}, 1000)
		),
	(cur) => cur + 1 // updater function or just a value
);

//value will be the optimistic value as long as the update didn't resolve or cancel hasn't been called
console.log(get(likeStore)); // 2

//store will be reset to last value (here 1) and the asynchronous update won't be applied
likeStore.cancel();

// after 1 second store will still be 1 and won't update to 2

console.log(get(likeStore)); // 1