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 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-storable

v1.0.4

Published

extending 'writable' from 'svelte/store' with localStorage

Downloads

6

Readme

Svelte Storable

Svelte Storable is an extention to the writable store that comes with svelte/store. The purpose of the package is to preserve the state of the store, even after refresh.

Svelte Store adds localStorage management to the store while, with 1 exception preserves the api.

Installation

Include with npm:

npm install svelte-storable

Include locally:

Download storable.js from this repository and include it in your project.

Documentation

Use the svelte writalbe doumentation as a companion to this documentation if you aren't comfortable with svelte stores.

Creating a Store

The api for svelte-storable matches almost identically to the writable from svelte/store. The key difference is that you need to provide a key to the storable. This key will be used when storing and retrieving the persisted data.

// store.js
import { storable } from 'svelte-storable';

                                ||
                                \/
export const count = storable('count', 0);

Using the Store

Now you can use the store with all your usual writable methods. Additionally, you have the detatch() method. This can be used to remove the data from localStorage so that the value will be fresh on refresh.

:warning: NB: If you update the value after detatch() it will be stored again.

<!-- App.svelte -->
<script>
    import { count } from './store';

    const increase  = () => count.update((value) => value + 1);
    const decrease  = () => count.update((value) => value - 1);
    const reset     = () => count.set(0);
    const detatch   = () => count.detatch();

</script>

<h1>{$count}</h1>
<button on:click={count.increase}>Increase Count</button>
<button on:click={count.decrease}>Decrese Count</button>
<button on:click={count.reset}>Reset</button>
<button on:click={count.detatch}>Detatch from Storage</button>

Subscribe

You can use the $ syntax like any other svelte store or do it manually with unsubscribe/subscribe.

const unsubscribe = count.subscribe(value => doSomething(value));

Custom Stores

With writable you can make custom stores. This functionallity also copies over to svelte-storable. Again, the key difference is that you need to provide a key. You can optionally take that key as a parameter to your createStore() function with a default value.

// store.js
import { storable } from 'svelte-storable';

const createCountStore = (key = 'count') => {
    const { subscribe, set, update, detatch } = storable(key, 0);

    return {
        subscribe,
        increase:   () => update((value) => value + 1),
	decrease:   () => update((value) => value - 1),
	reset:      () => set(0),
        detatch:    () => detatch()
    }
}

export const count = createCountStore('count');

:warning: NB: Remember to provide different keys to all the different instances of your custom store. If two or more stores have the same key, they will update each other.