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

@dhlx/signal

v0.0.1

Published

A lightweight reactive data library providing Signal and Computed objects for state management and automatic updates, tracking dependencies and simplifying data handling.

Downloads

73

Readme

Signal

A simple reactive data library using Signal and Computed to manage state and trigger updates on value changes.

Features

  • Signal: A reactive data object that allows tracking and responding to value changes.
  • Computed: A reactive computed value that automatically updates when its dependencies change.
  • Efficient and lightweight.
  • Supports effects to run functions when values change.

Installation

You can install this package via NPM:

npm install @dhlx/signal

Or via Yarn:

yarn add @dhlx/signal

Usage

Creating a Signal

A Signal is a reactive data object that holds a value and notifies listeners when the value changes.

import { signal } from '@dhlx/signal'
// Create a signal with an initial value
const temperature = signal(20)

// Access the value
console.log(temperature.value) // 20

// Update the value
temperature.value = 25

promise.resolve().then(() => {
  console.log(temperature.value) // 25
})

console.log(temperature.value) //20;

Using Effects

You can register effects to react to changes in a Signal value.

temperature.effect(() => {
  console.log(`Temperature changed to: ${temperature.value}`);
});

// Update the signal and trigger the effect
temperature.value = 30; // Output: Temperature changed to: 30

Creating a Computed Value

A Computed value depends on one or more Signal values and automatically updates when its dependencies change.

import { computed } from '@dhlx/signal';

// Create two signals
const temperature = signal(20);
const humidity = signal(50);

// Create a computed value based on the signals
const comfortIndex = computed((temp, humid) => temp + humid, [temperature, humidity]);

console.log(comfortIndex.value); // 70

// Update the signals and the computed value will automatically update
temperature.value = 25;

Promise.resolve().then(() => {
  console.log(comfortIndex.value); // 75
})

Removing Effects

You can remove effects using the function returned by effect.

const stopLogging = temperature.effect(() => {
  console.log(`Temperature is now: ${temperature.value}`);
});

// Stop listening for changes
stopLogging();

temperature.value = 35; // No output, effect has been removed

API

signal(initialValue)

Creates a new Signal object.

  • initialValue: The initial value of the signal.

Properties:

  • value: Get or set the value of the signal.

Methods:

  • effect(fn): Registers a function that runs when the signal value changes. Returns a function to remove the effect.

computed(fn, deps)

Creates a new Computed object that automatically updates based on its dependencies.

  • fn: A function that returns the computed value based on its dependencies.
  • deps: An array of Signal objects that the computed value depends on.

Properties:

  • value: The current computed value, updated automatically when dependencies change.

License

MIT License