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

@aegisjsproject/state

v1.0.3

Published

A simple state manager library

Downloads

476

Readme

@aegisjsproject/state

A lightweight state management library that persists state across navigation within the same session. It leverages history.state for easy integration into web applications and synchronizes state across tabs or windows within the same origin using BroadcastChannel.

CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Features

  • Session persistence: State persists across navigation, staying in sync with the browser's session history.
  • Multi-tab synchronization: Keeps state in sync across multiple tabs or windows on the same origin using BroadcastChannel.
  • Efficient state updates: Only updates when necessary, ensuring minimal performance overhead.
  • Flexible API: Includes utilities for observing state changes, managing key-specific states, and clearing state.

Installation

npm install @aegisjsproject/state

CDN and importmap

You do not even need to npm install this or use any build process. You may either import it directly from a CDN or use an importmap.

<script type="importmap">
{
  "imports": {
    "@aegisjsproject/state": "https://unpkg.com/@aegisjsproject/state[@version]/state.js"
  }
}
</script>

Example

import { setState, getState, observeStateChanges, manageState, watchState } from '@aegisjsproject/state';

// Enables syncing of state across tabs/windows
watchState();

// Set state
setState('key', 'value');

// Get state
const value = getState('key');

// Observe state changes
observeStateChanges(({ diff, state }) => {
  console.log('State changed:', diff, state);
});

// Observe state changes to only specific state keys
observeStateChanges(({ state }) => {
  console.log('State changed:', {
    foo: state.foo,
    bar: state.bar,
  });
}, 'foo', 'bar');

// Manage state with reactive pattern
const [state, setStateValue] = manageState('key', 'defaultValue');
console.log(state.valueOf());  // Logs the state value from the wrapper object
setStateValue('newValue');     // Updates the state

[!IMPORTANT] This is NOT to be confused with eg useState() in React or anything like that. There is no transpilation step involved. Although manageState() does return a tuple of [value, setValue], it takes a key and an initial value, and the value is an object which wraps the current value. You can use the value via things like setValue(value + 1) thanks to [Symbol.toPrimitive], however.