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

@solid-primitives/mutable

v1.0.2

Published

A primitive for creating a mutable store, an alternative to Solid's createStore.

Downloads

5,034

Readme

@solid-primitives/mutable

turborepo size version stage

A primitive for creating a mutable store proxy object. An alternative to createStore from "solid-js/store".

  • createMutable - Creates a mutable store proxy object.
  • modifyMutable - Helper for applying store mutation utilities - like produce or reconcile from "solid-js/store" - to a mutable store.

Installation

npm install @solid-primitives/mutable
# or
pnpm add @solid-primitives/mutable
# or
yarn add @solid-primitives/mutable

createMutable

import { createMutable } from "@solid-primitives/mutable";

declare function createMutable<T extends StoreNode>(state: T): T;

Creates a new mutable Store proxy object. Stores only trigger updates on values changing. Tracking is done by intercepting property access and automatically tracks deep nesting via proxy.

Useful for integrating external systems or as a compatibility layer with MobX/Vue.

Note: A mutable state can be passed around and mutated anywhere, which can make it harder to follow and easier to break unidirectional flow. It is generally recommended to use createStore instead. The produce modifier can give many of the same benefits without any of the downsides.

const state = createMutable(initialValue);

// read value
state.someValue;

// set value
state.someValue = 5;

state.list.push(anotherValue);

Mutables support setters along with getters.

const user = createMutable({
  firstName: "John",
  lastName: "Smith",
  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  },
  set fullName(value) {
    [this.firstName, this.lastName] = value.split(" ");
  },
});

modifyMutable

import { modifyMutable } from "@solid-primitives/mutable";

declare function modifyMutable<T>(mutable: T, modifier: (state: T) => T): void;

This helper function simplifies making multiple changes to a mutable Store (as returned by createMutable) in a single batch, so that dependant computations update just once instead of once per update.

The first argument is the mutable Store to modify, and the second argument is a Store modifier such as those returned by reconcile or produce. (If you pass in your own modifier function, beware that its argument is an unwrapped version of the Store.)

For example, suppose we have a UI depending on multiple fields of a mutable:

const state = createMutable({
  user: {
    firstName: "John",
    lastName: "Smith",
  },
});

<h1>Hello {state.user.firstName + " " + state.user.lastName}</h1>;

Modifying n fields in sequence will cause the UI to update n times:

state.user.firstName = "Jake"; // triggers update
state.user.lastName = "Johnson"; // triggers another update

To trigger just a single update, we could modify the fields in a batch:

batch(() => {
  state.user.firstName = "Jake";
  state.user.lastName = "Johnson";
});

modifyMutable combined with reconcile or produce provides two alternate ways to do similar things:

// Replace state.user with the specified object (deleting any other fields)
modifyMutable(state.user, reconcile({
  firstName: "Jake",
  lastName: "Johnson",
});

// Modify two fields in batch, triggering just one update
modifyMutable(state.user, produce((u) => {
  u.firstName = "Jake";
  u.lastName = "Johnson";
});

Demo

Deployed example | Source code

Changelog

See CHANGELOG.md