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

@adbl/cells

v0.0.6

Published

A simple implementation of reactive updates for JavaScript

Downloads

204

Readme

@adbl/cells

npm version License: MIT

Cells is a powerful yet lightweight library for reactive state management in JavaScript applications. It offers an intuitive API that simplifies the complexities of managing and propagating state changes throughout your application.

Features

  • Simple API: Easy to learn and use, even for developers new to reactive programming.
  • Lightweight: No external dependencies, keeping your project lean.
  • Flexible: Works seamlessly with any JavaScript framework or vanilla JS.
  • Type-safe: Built with TypeScript, providing excellent type inference and checking.
  • Performant: Optimized for efficiency, with features like batched updates to minimize unnecessary computations.

Installation

Get started with Cells in your project:

npm install @adbl/cells

Or if you prefer Yarn:

yarn add @adbl/cells

Core Concepts

1. Source Cells

Source cells are the building blocks of your reactive state. They hold values that can change over time, automatically notifying dependents when updates occur.

import { Cell } from '@adbl/cells';

const count = Cell.source(0);
console.log(count.value); // Output: 0

count.value = 5;
console.log(count.value); // Output: 5

2. Derived Cells

Derived cells allow you to create computed values based on other cells. They update automatically when their dependencies change, ensuring your derived state is always in sync.

const count = Cell.source(0);
const doubledCount = Cell.derived(() => count.value * 2);

console.log(doubledCount.value); // Output: 0

count.value = 5;
console.log(doubledCount.value); // Output: 10

3. Reactive Effects

Easily set up listeners to react to changes in cell values, allowing you to create side effects or update your UI in response to state changes.

const count = Cell.source(0);

count.listen((newValue) => {
  console.log(`Count changed to: ${newValue}`);
});

count.value = 3; // Output: "Count changed to: 3"
count.value = 7; // Output: "Count changed to: 7"

4. Global Effects

Cells allows you to set up global effects that run before or after any cell is updated, giving you fine-grained control over your application's reactive behavior.

Cell.beforeUpdate((value) => {
  console.log(`About to update a cell with value: ${value}`);
});

Cell.afterUpdate((value) => {
  console.log(`Just updated a cell with value: ${value}`);
});

5. Batch Updates

When you need to perform multiple updates but only want to trigger effects once, you can use batch updates to optimize performance:

const cell1 = Cell.source(0);
const cell2 = Cell.source(0);

Cell.afterUpdate(() => {
  console.log('Update occurred');
});

Cell.batch(() => {
  cell1.value = 1;
  cell2.value = 2;
});
// Output: "Update occurred" (only once)

6. Async Operations

Cells provides utilities for handling asynchronous operations, making it easy to manage loading states, data, and errors:

const fetchUser = Cell.async(async (userId) => {
  const response = await fetch(`https://api.example.com/users/${userId}`);
  return response.json();
});

const { pending, data, error, run } = fetchUser;

pending.listen((isPending) => {
  console.log(isPending ? 'Loading...' : 'Done!');
});

data.listen((userData) => {
  if (userData) {
    console.log('User data:', userData);
  }
});

run(123); // Triggers the async operation

7. Flattening

Cells offers utility functions to work with nested cell structures, making it easier to handle complex state shapes:

const nestedCell = Cell.source(Cell.source(5));
const flattenedValue = Cell.flatten(nestedCell);
console.log(flattenedValue); // Output: 5

const arrayOfCells = [Cell.source(1), Cell.source(2), Cell.source(3)];
const flattenedArray = Cell.flattenArray(arrayOfCells);
console.log(flattenedArray); // Output: [1, 2, 3]

const objectWithCells = { a: Cell.source(1), b: Cell.source(2) };
const flattenedObject = Cell.flattenObject(objectWithCells);
console.log(flattenedObject); // Output: { a: 1, b: 2 }

8. Custom Equality Checks

For more complex objects, you can provide custom equality functions to determine when a cell's value has truly changed:

const userCell = Cell.source(
  { name: 'Alice', age: 30 },
  {
    equals: (a, b) => a.name === b.name && a.age === b.age,
  }
);

9. Named Effects

To aid in debugging, you can name your effects, making it easier to track and manage them:

const count = Cell.source(0);

count.listen((value) => console.log(`Count is now: ${value}`), {
  name: 'countLogger',
});

console.log(count.isListeningTo('countLogger')); // Output: true

count.stopListeningTo('countLogger');

Advanced Features and API Details

Cell Options

When creating a source cell, you have fine-grained control over its behavior:

const cell = Cell.source(initialValue, {
  immutable: boolean, // If true, the cell will not allow updates
  deep: boolean, // By default, the cell only reacts to changes at the top level of objects. Setting deep to true will proxy the cell to all nested properties and trigger updates when they change as well.
  equals: (oldValue, newValue) => boolean, // Custom equality function
});

Effect Options

When setting up listeners or effects, you can customize their behavior:

cell.listen(callback, {
  once: boolean, // If true, the effect will only run once
  signal: AbortSignal, // An AbortSignal to cancel the effect
  name: string, // A name for the effect (useful for debugging)
  priority: number, // The priority of the effect (higher priority effects run first)
});