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/deep

v0.2.10

Published

Primitives for tracking and observing nested reactive objects in Solid.

Downloads

6,991

Readme

@solid-primitives/deep

turborepo size version stage

Primitives for tracking and observing nested reactive objects in Solid.

  • trackDeep - Tracks all properties of a store by iterating over them recursively.
  • trackStore - A more performant alternative to trackDeep utilizing specific store implementations.
  • captureStoreUpdates - A utility function that captures all updates to a store and returns them as an array.

Installation

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

trackDeep

Tracks all properties of a store by iterating over them recursively.

It's a slightly more performant alternative to tracing a store with JSON.stringify, that won't throw when encountering circular references or BigInt values.

Since it iterates over all properties of a store, it's not recommended to use this on large stores under rapid updates.

How to use it

You can call this function with any store under a tracking scope and it will iterate over all properties of the store and track them.

import { trackDeep } from "@solid-primitives/deep";

const [state, setState] = createStore({ name: "John", age: 42 });

createEffect(() => {
  trackDeep(state);
  /* execute some logic whenever the state changes */
});

Or since this has a composable design, you can create derivative functions and use them similar to derivative signals.

const deeplyTrackedStore = () => trackDeep(sign);
createEffect(() => {
  console.log("Store is: ", deeplyTrackedStore());
  //                        ^ this causes a re-execution of the effect on deep changes of properties
});

trackDeep will traverse any "wrappable" object (objects that solid stores will wrap with proxies), even if it's not a solid store.

createEffect(() => {
  // will also work:
  trackDeep({ myStore: state });
});

Warning If you unwrap a store, it will no longer be tracked by trackDeep nor trackStore!

const unwrapped = unwrap(state);

createEffect(() => {
  // This will NOT work:
  trackDeep(unwrapped);
});

trackStore

A much more performant alternative to trackDeep that is utilizing memoization and specific store implementations of solid stores.

You should consider using this instead of other tracking methods, for large stores, stores that are updated rapidly or tracked in many effects.

How to use it

It can be used in almost the same way as trackDeep, the only difference is that it requires a store to be directly passed in. So it won't work with objects that contain stores.

import { trackStore } from "@solid-primitives/deep";

const [state, setState] = createStore({ name: "John", age: 42 });

createEffect(() => {
  trackStore(state);
  /* execute some logic whenever the state changes */
});

captureStoreUpdates

Creates a function for tracking and capturing updates to a store.

It could be useful for implementing undo/redo functionality or for turning a store into a immutable stream of updates.

How to use it

Each execution of the returned function will return an array of updates to the store since the last execution.

const [state, setState] = createStore({ todos: [] });

const getDelta = captureStoreUpdates(state);

getDelta(); // [{ path: [], value: { todos: [] } }]

setState("todos", ["foo"]);

getDelta(); // [{ path: ["todos"], value: ["foo"] }]

The returned function will track all updates to a store (just like trackStore), so it can be used inside a tracking scope.

const [state, setState] = createStore({ todos: [] });

const getDelta = captureStoreUpdates(state);

createEffect(() => {
  const delta = getDelta();
  /* execute some logic whenever the state changes */
  console.log(delta);
});

The returned function is not a signal - it won't get updated by itself, it has to be called manually, or under a tracking scope to capture new updates.

But it can be turned into a signal by using createMemo:

const [state, setState] = createStore({ todos: [] });

const delta = createMemo(captureStoreUpdates(state));

// both of these effects will receive the same delta
createEffect(() => {
  console.log(delta());
});
createEffect(() => {
  console.log(delta());
});

Demo

See a demo of this primitive in action here.

Changelog

See CHANGELOG.md