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 🙏

© 2026 – Pkg Stats / Ryan Hefner

state-sync-light

v1.0.1

Published

A lightweight StateSync: Reactive state management with derived states and DOM binding.

Downloads

5

Readme

StateSync

A reactive state management solution that elegantly synchronizes data across components without frameworks, offering derived states and DOM binding with zero dependencies.

Features

  • Ultra-lightweight (only 50 lines of code)
  • Zero dependencies
  • Reactive state management
  • Derived states that auto-update
  • Direct DOM binding
  • Support for nested state paths
  • Simple subscription model
  • Framework-agnostic

Installation

npm install state-sync

Usage

Basic State Management

// Create a store with initial state
const userStore = StateSync.createStore('user', {
  name: 'Guest',
  isLoggedIn: false,
  preferences: { theme: 'light', language: 'en' }
});

// Get state or nested properties
const userName = userStore.get('name');
const theme = userStore.get('preferences.theme');
const fullState = userStore.get();

// Update state
userStore.set({ 
  name: 'John Doe', 
  isLoggedIn: true 
});

// Update nested state with function updater
userStore.set(state => ({
  ...state,
  preferences: {
    ...state.preferences,
    theme: 'dark'
  }
}));

// Subscribe to changes
const unsubscribe = userStore.subscribe((newState, oldState) => {
  console.log('User state changed:', newState);
});

// Later, unsubscribe if needed
unsubscribe();

Derived States

// Create multiple stores
const userStore = StateSync.createStore('user', { name: 'Guest', isLoggedIn: false });
const cartStore = StateSync.createStore('cart', { items: [], total: 0 });

// Create a derived state that depends on other stores
const uiStore = StateSync.derive('ui', ['user', 'cart'], 
  (userState, cartState) => ({
    displayName: userState.isLoggedIn ? userState.name : 'Guest',
    cartCount: cartState.items.length,
    isEmpty: cartState.items.length === 0
  })
);

// Derived state automatically updates when dependencies change
userStore.set({ name: 'John', isLoggedIn: true });
cartStore.set(state => ({
  items: [...state.items, { id: 1, name: 'Product', price: 100 }],
  total: state.total + 100
}));

// Get the computed state
console.log(uiStore.get()); // { displayName: 'John', cartCount: 1, isEmpty: false }

DOM Binding

// Bind state to DOM elements
userStore.bind('#user-name', { 
  prop: 'textContent', 
  transform: state => state.isLoggedIn ? state.name : 'Guest' 
});

cartStore.bind('#cart-badge', { 
  prop: 'textContent', 
  transform: state => state.items.length 
});

uiStore.bind('body', { 
  attr: 'data-theme', 
  transform: state => state.theme 
});

// Elements automatically update when state changes
userStore.set({ name: 'Jane', isLoggedIn: true });

Complete Example

// Create stores
const userStore = StateSync.createStore('user', {
  name: 'Guest',
  isLoggedIn: false,
  preferences: { theme: 'light' }
});

const todoStore = StateSync.createStore('todos', {
  items: [],
  filter: 'all'
});

// Create derived state
const uiState = StateSync.derive('ui', ['user', 'todos'], 
  (user, todos) => ({
    theme: user.preferences.theme,
    displayName: user.isLoggedIn ? user.name : 'Guest',
    todoCount: todos.items.length,
    activeTodoCount: todos.items.filter(todo => !todo.completed).length
  })
);

// Bind to DOM
uiState.bind('#user-display', { 
  prop: 'textContent', 
  transform: state => `Hello, ${state.displayName}` 
});

uiState.bind('#todo-count', { 
  prop: 'textContent', 
  transform: state => `${state.activeTodoCount} items left` 
});

uiState.bind('body', { 
  attr: 'data-theme', 
  transform: state => state.theme 
});

// Add a todo
document.querySelector('#add-todo').addEventListener('click', () => {
  const input = document.querySelector('#new-todo');
  const text = input.value.trim();
  
  if (text) {
    todoStore.set(state => ({
      ...state,
      items: [...state.items, { id: Date.now(), text, completed: false }]
    }));
    input.value = '';
  }
});

// Toggle theme
document.querySelector('#toggle-theme').addEventListener('click', () => {
  userStore.set(state => ({
    ...state,
    preferences: {
      ...state.preferences,
      theme: state.preferences.theme === 'light' ? 'dark' : 'light'
    }
  }));
});

// Login
document.querySelector('#login-button').addEventListener('click', () => {
  userStore.set({ name: 'John Doe', isLoggedIn: true });
});

API Reference

StateSync.createStore(name, initialState) Creates a new store with the given name and initial state.

Returns an object with the following methods:

  • get(path?) - Get the current state or a nested property
  • set(updater, options?) - Update the state
  • subscribe(callback) - Subscribe to state changes
  • bind(selector, options) - Bind state to DOM elements

StateSync.derive(name, dependencies, computeFn) Creates a derived state that automatically updates when its dependencies change.

Parameters:

  • name - Unique name for the derived store
  • dependencies - Array of store names this derived state depends on
  • computeFn - Function that computes the derived state from dependencies

License

MIT

Made with by Michael Ilyash