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

@signe/reactive

v1.0.1

Published

## What is `@signe/reactive`?

Downloads

24

Readme

Reactive Library

What is @signe/reactive?

The @signe/reactive library is a lightweight and flexible library for creating reactive data structures in JavaScript. It provides utilities for creating reactive signals, computed properties, and effects, allowing you to build reactive applications with ease.


Installation

To install the @signe/reactive package, use the following command:

npm install @signe/reactive

Usage

Overview

The @signe/reactive library provides utilities for creating reactive signals, computed properties, and effects. It includes the following main functions:

  1. signal: Creates a reactive signal.
  2. computed: Creates a computed property that depends on other signals.
  3. effect: Creates an effect that runs a function whenever the signals it depends on change.
  4. isSignal: Checks if a value is a signal.

1. signal

Creates a reactive signal which can hold any type of value (primitive, array, object, etc.).

Syntax

function signal<T>(defaultValue: T): WritableSignal<T>;

Example

import { signal } from '@signe/reactive';

// Primitive signal
const count = signal(0);
console.log(count()); // Output: 0
count.set(1);
console.log(count()); // Output: 1

// Array signal
const numbers = signal([1, 2, 3]);
numbers.mutate(arr => arr.push(4));
console.log(numbers()); // Output: [1, 2, 3, 4]

// Object signal
const person = signal({ name: 'John', age: 30 });
person.mutate(obj => obj.age = 31);
console.log(person()); // Output: { name: 'John', age: 31 }

2. computed

Creates a computed property that re-evaluates whenever the signals it depends on change.

Syntax

function computed<T>(computeFunction: () => T, disposableFn?: () => void): ComputedSignal<T>;

Example

import { signal, computed } from '@signe/reactive';

const a = signal(1);
const b = signal(2);

const sum = computed(() => a() + b());
console.log(sum()); // Output: 3

a.set(2);
console.log(sum()); // Output: 4

3. effect

Creates an effect that runs a function whenever the signals it depends on change. It can also return a cleanup function to run when the effect is disposed or re-run.

Syntax

function effect(fn: () => void): Effect;

Example

import { signal, effect } from '@signe/reactive';

const count = signal(0);

const dispose = effect(() => {
  console.log('Count changed:', count());
  return () => console.log('Cleanup on count change');
});

count.set(1); // Output: "Count changed: 1"
dispose(); // Output: "Cleanup on count change"

4. isSignal

Checks if a given value is a signal created by the signal function.

Syntax

function isSignal(value: any): boolean;

Example

import { signal, isSignal } from '@signe/reactive';

const count = signal(0);
console.log(isSignal(count)); // Output: true

const notASignal = { value: 0 };
console.log(isSignal(notASignal)); // Output: false

Advanced Examples

Working with ArraySubject

The ArraySubject class allows you to create signals specifically for arrays, tracking changes like additions, removals, and updates.

import { signal } from '@signe/reactive';

const arraySignal = signal([1, 2, 3]);
arraySignal.observable.subscribe((change) => {
  console.log(change); // Outputs changes like { type: 'add', index: 3, items: [4] }
});

arraySignal.mutate(arr => arr.push(4));

Working with ObjectSubject

The ObjectSubject class allows you to create signals specifically for objects, tracking changes like property additions, removals, and updates.

import { signal } from '@signe/reactive';

const objectSignal = signal({ a: 1, b: 2 });
objectSignal.observable.subscribe((change) => {
  console.log(change); // Outputs changes like { type: 'update', key: 'a', value: 2 }
});

objectSignal.mutate(obj => obj.a = 2);

Running Tests

To run the tests for @signe/reactive, use the following command:

npx vitest

Ensure you have Vitest installed as a dev dependency:

npm install --save-dev vitest