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

@avidian/state

v1.3.7

Published

Library for handling state in LocalStorage or AsyncStorage.

Downloads

43

Readme

@avidianity/state

GitHub license coverage

Library for handling state in LocalStorage or AsyncStorage

Installation

npm:

npm install @avidian/state

yarn:

yarn add @avidian/state

Usage

State and AsyncState are storage-agnostic so that providing custom storage drivers can be supported.

Synchonous Storage

import State from '@avidian/state';

const state = new State(); // defaults to window.localStorage if in browser

// add values
state.set('string', 'value');
state.set('array', []);
state.set('object', {});

// removing values
state.remove('string');
state.remove('array');
state.remove('object');

// remove all values
state.clear();

Specifying the storage is also supported

// use sessionStorage instead
const state = new State(window.sessionStorage, ?key);

Or implement your own synchonous storage

Example (typescript)

import State, { Storage } from '@avidian/state';

class CustomStorage implements Storage {
    readonly length: number;
    
    clear(): void;
    
    getItem(key: string): string | null;
    
    key(index: number): string | null;
    
    removeItem(key: string): void;
    
    setItem(key: string, value: string): void;
}

const state = new State(new CustomStorage());

Asynchronous Storage

Asynchronous state is also supported as long as you provide a storage implements the AsyncStorage interface. The API is also the same just like with State except that it returns promises.

Example (typescript)

import { AsyncState, AsyncStorage } from '@avidian/state';

class CustomAsyncStorage implements AsyncStorage {
    readonly length: number;
    
    clear(): Promise<this>;
    
    getItem(key: string): Promise<string | null>;
    
    key(index: number): Promise<string | null>;
    
    removeItem(key: string): Promise<this>;
    
    setItem(key: string, value: string): Promise<this>;
}

const state = new AsyncState(new CustomAsyncStorage(), ?key);

await state.set('key', value);
await state.remove('key');
await state.clear();

Usage in Node

Both State and AsyncState will work on the Node enviroment as long as a custom storage implementing their respective interface is provided.

Events

Each individual state has their own event handlers. Both State and AsyncState work the same.

import State from '@avidian/state';

const state = new State();

const key = state.listen('key', value => {
    // called everytime `state.set('key', value)` or `state.dispatch('key', value) is called
    console.log(value);
});

state.set('key', 'value');

// unregister the event listener
state.unlisten(key);

// dispatch your own value
state.dispatch('key', 'value');

Drivers

Some drivers are provided out of the box.

Synchronous Drivers

MemoryStorage
import State, { MemoryStorage } from '@avidian/state';

const state = new State(new MemoryStorage());

Asynchronous Drivers

AsyncMemoryStorage
import { AsyncState, AsyncMemoryStorage } from '@avidian/state';

const state = new AsyncState(new AsyncMemoryStorage());