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

tinysignals

v1.1.1

Published

Simple observable data structure

Downloads

1

Readme

tinysignals

Simple observable data structure

Introduction

Tinysignals are like regular variables that allow you to subscribe to changes on them. They provide a simple, lightweight alternative to more complex observables or event listeners.

Installation

With Yarn:

yarn add tinysignals

Using NPM:

npm install --save tinysignals

Signal types

Signal

Signal is the base signal class. You seed it with an initial value, and then can subscribe to follow it, as well as getting or setting the value at any time.

import { Signal } from 'tinysignals'

const signal = new Signal(1)
signal.follow(value => console.log(value))
signal.set(2) // console logs 2
signal.set(signal.get() + 1) // console logs 3

Signal methods

  • constructor(initial: Type) creates a new signal with the given initial value
  • get(): Type returns the current value of the signal
  • set(value: Type): void sets a new value, and notifies any observers
  • follow(callback: (type: Type) => void, runNow: boolean = false): () => void adds a new observer callback that will be notified when the signal value changes. If the optional runNow parameter is true, the callback will be fired immediately with the current value of the signal. The return value is an unfollow function that can be called to remove the observer
  • dispose() removes all observers

MappedSignal

MappedSignal allows you to created a new signal that "maps" an existing signal through a function, updating the result of the function when the mapped signal changes.

import { Signal, MappedSignal } from 'tinysignals'

const signal = new Signal(2)
const mapped = new MappedSignal(signal, (value) => value * 2)
mapped.get() // returns 4
mapped.follow(value => console.log(value))
signal.set(5) // console logs 10

MergedSignal

MergedSignal is like a MappedSignal, but it accepts more than 1 input. The result is updated whenever any of the input signals change.

import { Signal, MergedSignal } from 'tinysignals'

const signalA = new Signal('foo')
const signalB = new Signal('bar')
const merged = new MergedSignal({ a: signalA, b: signalB }, ({ a, b }) => a + b)
sum.get() // returns 'foobar'
sum.follow(value => console.log(value))
signalA.set('tiny') // console logs 'tinybar'
signalB.set('signal') // console logs 'tinysignal'

PromiseSignal

PromiseSignal wraps the status of a Promise as a signal.

import { PromiseSignal } from 'tinysignals'

const signal = new PromiseSignal(myPromise)
signal.get() // returns { status: 'pending' }
signal.follow(value => console.log(value))
// when promise resolves, console logs { status: 'resolved', value: 'someValue' }

Event type

Event lets you define a simple observable event without the value storage provided by a Signal.

import { Event } from 'tinysignals'

const event = new Event()
event.follow((name) => console.log(`Hello ${name}`))
event.call('World') // console logs "Hello World"

Event methods

  • constructor() creates a new event
  • call(...args: CallbackArgs) calls all observer callbacks with the arguments provided
  • follow(callback: (...CallbackArgs) => void): () => void adds a new callback that will be run when the event fires. The return value is an unfollow function that can be called to remove the callback
  • dispose() removes all callbacks