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

@xeito/store

v0.3.5

Published

Reactive Stores for Xeito | Framework for building web applications

Downloads

5

Readme

Xeito Store

This is the Router library for Xeito Framework. Provides a simple yet powerful router for web applications built with Xeito.

To find the full documentation and more links go to the main README file.

Table of Contents

Exports

Classes

  • WriteStore: A store that can be written to and read from.
  • ReadStore: A store that can only be read from.
  • MixedStore: A store that depends on other stores.

Interfaces

  • Updater: A function that can be passed to a store to update its value.
  • Subscription: Object returned from a store's subscribe method. Contains a unsubscribe method to unsubscribe from the store.

What is a store?

At its core, a store is a class that holds a value that changes over time, allowing you to subscribe to it. Stores allow the state of your application to be shared across multiple components and services or even regular Typescript modules.

There are three main types of stores, each with a different purpose, but a very similar API.

WriteStore

The most basic form of a store is a WriteStore, which allows us to write to it and notify subscribers of the change. Let's create a simple store that holds a number:

import { WriteStore } from '@xeito/store';

const store = new WriteStore(0);

Now we can subscribe to the store and get notified when the value changes:

store.subscribe((value) => {
  console.log(value);
});

Stores are fully typed, so you can rewrite the above code as:

import { WriteStore } from '@xeito/store';

const store: WriteStore<number> = new WriteStore(0);

store.subscribe((value: number) => {
  console.log(value);
});

We can now write to the store and notify subscribers:

store.set(1); // Every subscriber will be notified with the new value

We can also get the current value of a store without subscribing to it:

const value = store.value;

Note: Reading the value of a store like this will not create a subscription and it won't be notified of any changes.

Updater function

We can also pass an updater function when creating a WriteStore, this function will be called with the current value of the store and the set method. This allows us to transform the value and write it to the store in a single operation:

const store = new WriteStore(0, (value, set) => {
  set(value + 1);
});

Updater functions can return return a value or a cleanup function. If a cleanup function is returned, it will be called when the store is destroyed.

const store = new WriteStore(0, (value, set) => {
  const interval = setInterval(() => {
    set(value + 1);
  }, 1000);

  return () => {
    clearInterval(interval);
  };
});

If the updater returns a value, it will be set as the value of the store.

const store = new WriteStore(0, (value, set) => {
  return value + 1;
});

// This can also be written as:
const store = new WriteStore(0, (value, set) => value + 1);

The updater function can be changed after the store is created by using the update method:

store.update((value, set) => {
  return value + 1;
});

ReadStore

A ReadStore is a store that can only be read from, it can't be written to. This is useful when you want to share a value across multiple components or services, but you don't want them to be able to change it. ReadStore therefore only has a subscribe method and a value property and relies on the updater function to change its value.

import { ReadStore } from '@xeito/store';

const store = new ReadStore(0);

store.subscribe((value) => {
  console.log(value); // Logs 0
});

This can be useful for tasks like fetching data from an API and sharing it across multiple components without them being able to change it:

import { ReadStore } from '@xeito/store';

const store = new ReadStore(0, async (value, set) => {
  const response = await fetch('https://example.com/api');
  const data = await response.json();

  set(data);
});

store.subscribe((value) => {
  console.log(value); // Logs the data from the API
});

Mixed Store

A MixedStore is a type of store that depends on other stores to calculate its value, it can be read from and written to. This is useful when you need to merge data from multiple stores or compute a value when any of the store passed as a dependency changes.

import { WriteStore, MixedStore } from '@xeito/store';

const count = new WriteStore(0);

const store = new MixedStore(count, (value, set) => {
  return value + 1;
});

store.subscribe((value) => {
  console.log(value); // Logs 1
});

As you can see, the MixedStore accepts a store as its first argument, this store will be used to calculate the value of the MixedStore, the updater function will be called with the value of the store and the set method. A third argument can be provided, this argument will be the initial value of the MixedStore:

const store = new MixedStore(count, (value, set) => {
  return value + 1;
}, 0); // The initial value of the store will be 0

If we need to use multiple stores to calculate the value of the MixedStore, we can pass them as an array:

const value1 = new WriteStore('Hello');
const value2 = new WriteStore('World');

const store = new MixedStore([value1, value2], (value, set) => {
  return value.join(' ');
});

store.subscribe((value) => {
  console.log(value); // Logs 'Hello World'
});

value1.set('Goodbye'); // Logs 'Goodbye World' in the subscriber