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

morphium

v0.0.4

Published

A framework-agnostic, type-safe, and mutable state management library

Downloads

5

Readme

Morphium

Morphium is a framework-agnostic, type-safe, and mutable state management library. It aims to simplify the state mutations compared to the existing immutable state management libraries like Redux & Zustand.

It is very similar to the existing Valtio library, it uses Proxy under the hood to track the state changes and notify the subscribers, and it was basically created for learning purposes.

Usage

In order to create a mutable state object, use the morph function:

import { morph } from 'morphium';

const state = morph({
  firstName: 'John',
  lastName: 'Doe',
  age: 42,
  address: {
    city: 'New York',
    country: 'USA',
  },
});

Then, you can listen to state changes using the subscribe function. It accepts the mutable state instance, and a subscriber function that will be called whenever the state changes. The subscriber function will receive an array of paths that represent the properties that have changed:

import { subscribe } from 'morphium';

const unsubscribe = subscribe(state, (paths) => {
  paths.forEach((path) => {
    console.log('state.' + path.join('.') + ' has changed');
  });
});

state.address.city = 'Los Angeles';
state.age++;

// Logs:
// 'state.address.city has changed'
// 'state.age has changed'

unsubscribe();

Subscriptions are batched by default, which is why you get an array of paths rather than a single one. If you want to get notified for each individual path, you can pass false as the third argument to the subscribe function:

import { subscribe } from 'morphium';

subscribe(
  state,
  (path) => {
    console.log('state.' + path.join('.') + ' has changed');
  },
  false,
);

state.address.city = 'Los Angeles'; // Logs: 'state.address.city has changed'
state.age++; // Logs: 'state.age has changed'

For convenience, there is also a type-safe get function that lets you read from the morphed object based on the path you get in the subscriber function:

import { subscribe, get } from 'morphium';

subscribe(state, (paths) => {
  paths.forEach((path) => {
    const pathAsString = path.join('.');
    const value = get(state, path);

    console.log(`state.${pathAsString} has changed to ${value}`);
  });
});

Usage with React

In order to use Morphium with React, use the useSnapshot hook. This hook will subscribe only to the properties that are accessed in the component, and it will re-render the component whenever those properties change:

import { morph } from 'morphium';
import { useSnapshot } from 'morphium/react';

const state = morph({
  name: 'counter',
  count: 0,
});

function Counter() {
  const snapshot = useSnapshot(state);

  // Will re-render the `Counter` component.
  return <button onClick={() => state.count++}>{snapshot.count}</button>;
}

function OtherComponent() {
  // Will NOT re-render the `Counter` component.
  return (
    <button onClick={() => (state.name = 'new-counter')}>Change name</button>
  );
}

Note that the useSnapshot hook is also batched, so your component will only re-render once even if multiple properties have changed.