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

@elucidata/pulse

v2.2.3

Published

Microscopic signals with automatic dependency tracking.

Downloads

327

Readme

Min+Gzip

Pulse

Microscopic signals with automatic dependency tracking.

Pulse signals are straightforward containers for values—no magic, no proxies, no weird tricks. They hold anything you throw at them: scalars, objects, DOM elements, functions, classes, instances... Whatever.

No need to worry about tracking dependencies in your computed values or effects—Pulse handles that automatically.

At its core, it's a simple API:

function signal<T>(value: T): IMutableSignal<T>;
function effect(fn: EffectFunction, onError?: ErrorCallback): CleanupFunction;
function computed<T>(fn: () => T, onError?: ErrorCallback, type?: 'eager' | 'lazy'): ISignal<T>; // default is "lazy"
function batch(fn: () => void): void;

type EffectFunction = () => void | (() => void);
type CleanupFunction = () => void;
type ErrorCallback = (error: any, source: any) => void;

interface ISignal<T> {
    readonly value: T;
    get(): T;
    peek(): T;
    subscribe(run: (value: T) => void): CleanupFunction;
}

interface IMutableSignal<T> extends ISignal<T> {
    get value(): T;
    set value(newValue: T);
    set(newValue: T): void;
    update(updater: (value: T) => T): void;
}

Usage

import { signal, computed, effect } from "@elucidata/pulse";

const counter = signal(0);
const doubled = computed(() => counter.get() * 2);

effect(() => {
  // You can use .get() or .value
  console.log("Counter:", counter.value, "Doubled:", doubled.value);
});

counter.set(1);
// or counter.value = 1
// or counter.update(c => c + 1);

Installation

Pulse has no dependencies or prerequisites other than a JavaScript runtime. Bun is used for development.

To install Pulse:

bun add @elucidata/pulse

React/Preact

There are two ways to react to signal changes: using a higher-order function or a hook.

Higher-Order Function

import { signal } from "@elucidata/pulse";
import { observer } from "@elucidata/pulse/react";

const counter = signal(1);

const ExampleView = observer((props) => {
  return <div>{counter.value}</div>;
});

Hook

import { signal } from "@elucidata/pulse";
import { useComputed } from "@elucidata/pulse/react";

const counter = signal(1);

const ExampleView = (props) => {
  const count = useComputed(() => counter.value);

  return <div>{count}</div>;
};

Local State

While not recommended, you could do something like this:

import { signal } from "@elucidata/pulse";
import { useComputed } from "@elucidata/pulse/react";
import { useEffect, useMemo } from "react";

const ExampleView = (props) => {
  const counter = useMemo(() => signal(1), []);
  const count = useComputed(() => counter.value);

  useEffect(() => {
    const interval = setInterval(() => {
      counter.update((prev) => prev + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return <div>{count}</div>;
};

Svelte

Signals comply with Svelte's store contract:

<script>
  import { signal } from "@elucidata/pulse";

  const counter = signal(1);
</script>

<div>{$counter}</div>

Pulse View (Experimental)

Included is a tiny experimental view engine you can play with. Feedback is welcome. Pulse View is a declarative, statement-based, reactive UI toolkit.

  • No Build Step: No compiler/transpiler required.
  • No Virtual DOM: Live DOM nodes are created and deleted by builders.
  • Signal-Driven Updates: Updates are reactions to signal values only.
  • Reactive Sections: Parts of a view can be reactive when denoted with when, each, or live.

The Tao of Pulse View

Pulse View is guided by a set of high-level principles:

  1. Dynamic View Management: Views in Pulse View are dynamically built and removed as needed.
  2. Signal-Driven Updates: All updates within Pulse View are driven by signal reactions.
  3. Modularity and Reusability: Components within Pulse View are designed to be modular and reusable.
  4. Performance Optimization: Pulse View is optimized for performance, ensuring that even with dynamic view management and signal-driven updates, the application remains fast and responsive.

Demo Usage

import { view, signal, render, tags, text } from '@elucidata/pulse/view';

const { div, button } = tags;

const counter = signal(0);

const CounterView = view(() => {
    Root(() => {
        Button('Increment', () => counter.value++);
        Button('Decrement', () => counter.value--);

        div(() => {
            text('Count: ');
            text(counter);
        });
    });
});

const Root = div.design.css`
  padding: 1rem;
  border: 0.1rem solid dodgerblue;
`;

const Button = (children, onclick) => {
  button({ onclick }, children);
};

const dispose = render(CounterView(), document.getElementById('app'));

Utilities

persistedSignal

declare function persistedSignal<T>(
  key: string,
  initialValue: T,
  options?: Partial<PersistedSignalOptions<T>>
): Signal<T>;

interface PersistedSignalOptions<T> {
  storage: IStorage; // Defaults to globalThis.localStorage
  toStorage: (value: T) => string; // Defaults to JSON.stringify
  fromStorage: (value: string) => T; // Defaults to JSON.parse
}

interface IStorage {
  getItem(key: string): string | null;
  setItem(key: string, value: string): void;
}

Creates a signal that persists its value in storage (using localStorage by default). The value is serialized to a string before being stored and deserialized when retrieved. If storage is unavailable, a regular signal will be returned instead.

update

declare function update<T>(
  state: Signal<T>,
  updater: Merger<T> | Updater<T>,
  reportChanges?: boolean
): boolean | (keyof T)[];

type Merger<T> = Partial<T> | ((v: T) => Partial<T>);
type Updater<T> = Required<T> | ((v: T) => T);

Updates a signal with a new value or partial value. If a function is provided as the updater, it receives the current value and should return the updated value. If an object is returned, it will be merged with the current value. When reportChanges is set to true, the function returns an array of keys that were changed.

How Is This Different Than Others?

KISS — Keep It Simple, Signals. Think of it like nanostores but with automatic dependency tracking. If you need more advanced signal implementations, check out:

  • Preact Signals
  • MobX
  • SolidJS

Contributing

We happily review contributions from the community. To be eligible for merging into Pulse, please follow these guidelines and project goals:

  • Keep It Simple
  • Keep It Small
  • Keep It Fast
  • Err on the Side of Less Code

License

Pulse is licensed under the MIT License.