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-loadable-store

v2.0.1

Published

[![npm](https://img.shields.io/npm/v/svelte-loadable-store)](https://www.npmjs.com/package/svelte-loadable-store) [![license](https://img.shields.io/github/license/ngalaiko/svelte-loadable-store)](https://raw.githubusercontent.com/ngalaiko/svelte-loadable

Downloads

267

Readme

Svelte Loadable Store

npm license test

This library provides wrappers for vanilla svelte stores to simplify consumption of async APIs.

Types

Store value can be in three states: loading, loaded or error. Core types reflect that:

type Loadable<T> = Loading | Loaded<T>;
type Loading = { isLoading: true };
type Loaded<T> = { isLoading: false; value: T } | { isLoading: false; error: any };

writable / readable

writable and readable allow to define stores that are initialized asyncronously, for example:

import { readable } from 'svelte-loadable-store';

const start = performance.now();

const delay = (timeout: number) =>
	new Promise<number>((resolve) => setTimeout(() => resolve(performance.now() - start), timeout));

const store = readable(delay(100), (set) => {
	delay(200).then((value) => set(value));
});

store.subscribe(console.log);

/*
 * prints:
 * { isLoading: true }
 * { isLoading: false, value: 101 }
 * { isLoading: false, value: 201 }
 */

writable is exactly the same, just allows to .set and .update according to the Svelte's contract.

derived

The real power comes with derived stores. Derived function will be executed only after all of the input stores are loaded successfully. You can also derive asyncronously.

import { writable, derived } from 'svelte-loadable-store';

const start = performance.now();

const delay = (timeout: number) =>
	new Promise<number>((resolve) => setTimeout(() => resolve(performance.now() - start), timeout));

const first = writable(delay(100));
const second = writable(delay(200));
const third = derived([first, second], async ([first, second]) => first + second);

first.subscribe((v) => console.log('first', v));
second.subscribe((v) => console.log('second', v));
third.subscribe((v) => console.log('third', v));

/*
 * prints:
 * first { isLoading: true }
 * second { isLoading: true }
 * third { isLoading: true }
 * first { isLoading: false, value: 101 }
 * second { isLoading: false, value: 201 }
 * third { isLoading: false, value: 302 }
 */

error handling

Library exposes a few handy functions to use when working with store values, for example:

<script lang="ts">
    import { writable, derived, Loaded } from 'svelte-loadable-store'

    const fetchData = (): Promise<Data> => fetch('https://api.example.com/data').then(response => response.json())

    const data = writable(fetchData)
</script>

{#if $data.isLoading}
    loading..
{:else Loaded.isError($data)}
    error: {$data.error}
{:else}
    data: {$data.value}
{/if}

promisify

A util funciton that converts a store into promise that will resolve with the first loaded value.

import { readable, promisify } from 'svelte-loadable-store';

const delay = (timeout: number) =>
	new Promise<number>((resolve) => setTimeout(() => resolve(performance.now() - start), timeout));

const store = readable(delay(100));

promisify(store).then(console.log);

/*
 * prints:
 * 101
 */

Acknowledgement

This library is inspired by @square/svelte-store. I have been using it myself before writing this one, but found it having quite a complex interface and faced some issues.

Inspiration for type definitions comes from this nanostores issue.

License

This project is licensed under the MIT License. See the LICENSE file for details.