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

@ersbeth/picosignal

v0.0.3

Published

Minimal signal/slot/observable library for JS/TS files

Downloads

5

Readme

PicoSignal

Minimal signal library written in typescript.

Signal programming is a particular type of event driven programming. This library provides:

  • signals and slots (aka. events emitting and callbacks)
  • observable values with auto-emitted signals on set

This library runs both in browser and on Node.js

Installation

npm install @ersbeth/picosignal

Usage

Signal

Basic signal

Declare a new signal and register a slot (aka. callback) to it. Then the slot will be called every time the signal is emitted :

let data = 0;
const signal = new Signal<void>();
signal.on(() => { data++ });

assert.equal(data, 0);
signal.emit();
assert.equal(data, 1);
signal.emit();
assert.equal(data, 2);

Signal with parameter

You can provide a parameter when emitting a signal. The parameter will be forwarded to the slot:

let data = 0;
const signal = new Signal<number>();
signal.on((value:number) => { data = value });

assert.equal(data, 0);
signal.emit(3);
assert.equal(data, 3);

Signal with multiple slots

You can provide multiple slots to a signal. They will be called by order of registration:

let data1 = 0;
let data2 = 2;
const signal = new Signal<number>();
signal.on((value:number) => { data1 += value });
signal.on((value:number) => { data2 *= value });

assert.equal(data1, 0);
assert.equal(data2, 2);
signal.emit(3);
assert.equal(data1, 3);
assert.equal(data2, 6);

Unsubscribe

The registering function returns an unregistering function which can be called at anytime to remove the connection:

    let data = 0;
    let signal = new Signal<void>();
    let unsubscribe = signal.on(() => { data++ });

    assert.equal(data, 0);
    signal.emit();
    assert.equal(data, 1);
    unsubscribe();
    signal.emit();
    assert.equal(data, 1); // data was not increased

Observable

Registration without execution

An Observable contains a value and a signal named onChanged that is automatically emitted each time value is set :

let computed = 0;
const observable = new Observable<number>(1);
observable.onChanged((value:number) => { computed = value * 2; });

assert.equal(computed, 0); // not 2, slot hasn't been called
observable.value = 2;
assert.equal(computed, 4);
observable.value = 5;
assert.equal(computed, 10);

Notice that the slot is NOT called with the current value upon registration. See subscribe below if you need such behaviour.

Registration with execution

You can use the subscribe method if you want both to:

  • register your slot to an observable
  • execute your slot with the current value of the observable
let computed = 0;
let observable = new Observable<number>(1);
observable.subscribe((value:number) => { computed = value * 2; });

assert.equal(computed, 2); // here slot has been called
observable.value = 2;
assert.equal(computed, 4);
observable.value = 5;
assert.equal(computed, 10);