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

@tiny-lit/store

v2.0.0-alpha.8

Published

A framework-agnostic centralized state management based on the [Vuex](https://github.com/vuejs/vuex) pattern

Downloads

3

Readme

@tiny-lit/store

A framework-agnostic centralized state management based on the Vuex pattern

Installation

To install with npm

npm i --save @tiny-lit/store

or with yarn

yarn add @tiny-lit/store

Introduction

A store uses actions and mutations to bring changes to the state.

Actions

Action handlers are pure functions that accepts the store class and data as arguments. They can contain async operations and MUST NOT directly mutate the state, but they may commit mutations.

type ActionHandler = (store: StoreInterface, data: any) => void;

Mutations

Mutations handlers area responsible of muting the state. The first parameter is the state object and the second is the data. The returned object is the new state.

type MutationHandler = (state?: any, data?: any) => object;

Plugins

A plugin is a function, called just after the initialization of the store, that receive the store instance in the first argument.

type PluginHandler = (store: StoreInterface) => void;

Getting started

Options

You can create a store using createStore function passing a configuration object.

These are the options you can specify in the configuration:

  • actions an object containing the actions
  • mutations and object containing the mutations
  • initialState the initial state of your application
  • plugins and array of plugins

Methods

  • subscribe Registers a handler to the store listening for changes. It will return an unsubscribe function.
subscribe(callback: (state: any, mutation?: Mutation) => void) => Function
  • commit Applies the mutation provided.
commit(type: string, data:any) => object
commit(mutation: {
    type: string;
    data: any;
}) => object

Properties

  • mutations A map of the registered mutations
  • actions A map of the registered actions
  • state The state

Example

This is a basic example on how to create a store:

import { createStore } from '@tiny-lit/store';

const consolePlugin = store => store.subscribe(console.log);

const myStore = createStore({
    config = {
        actions: {
            incrementCounter(store, data) {
                store.commit('setCount', store.state + data)
            },
            decrementCounter(store, data) {
                store.commit('setCount', store.state - data)
            },
            asyncIncrement(store) {
                store.commit('setCount', 0);
                fetch('https://www.example.com')
                    .then(response => response.json())
                    .then(({ counter }) => {
                        store.commit('setCount', counter);
                    })
            }
        },
        mutations: {
            setCount(state, data) {
                return {
                    count: data
                };
            }
        },
        initialState: {
            count: 0
        },
        plugins: [consolePlugin]
    }
})

Use with custom elements

Provider

To use the store in conjuction with your custom elements, you need to create a Provider element for your store.

To do that, simply create an element extending the StoreProvider class and implementing a getter named config that will return your store configuration object.

Example

import { StoreProvider } from '@tiny-lit/store';

class MyStore extends StoreProvider {
    static get is() {
        return 'my-store';
    }

    get config() {
        return {
            // your config goes here
        };
    }
}
customElements.define(MyStore.is, MyStore);

Consumer

Now that you have a store provider, you need to connect your custom elements to it. Using the withStore mixin, it will subscribe/unsubscribe your elements to the store on connect/disconnected callbacks.

These are the lifecycle callbacks you can implement:

  • onStoreConnect Called when the element connected to a store
onStoreConnect() => void
  • onStateChange Called everytime the state changed
onStateChange(newState: any) => void

In addition, your element will implement a dispatch method for triggering changes. It is essentially a proxy to store.dispatch, the only difference is that using the withStore mixin everything is based on DOM events.

dispatch(type: string, data?: any) => void;

Note that if you don't provide a Provider, your element will continue to works as usual.

Example

import { withStore } from '@tiny-lit/store';

class MyCounter extends withStore(HTMLElement) {
    constructor() {
        super();
        this.count = 0;
    }

    onStateChange(state) {
        if (this.count !== state.count) {
            this.count = state.count;

            this.update();
        }
    }

    static get is() { return 'my-counter'; }

    update() {
        this.innerText = `Count: ${this.count}`;
    }
}
customElements.define(MyCounter.is, MyCounter);

class MyButton extends withStore(HTMLElement) {
    constructor() {
        super();
        
        this.addEventListener('click', () => {
            this.dispatch('increment');
        })
    }

    static get is() { return 'my-button'; }
}
customElements.define(MyButton.is, MyButton);

A DOM Example

<my-store>
    <my-consumer></my-consumer>
    <my-button>Click to increment</my-button>
</my-store>