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

signals.ts

v1.0.4

Published

![Tests](https://github.com/cleverplatypus/signals-ts/actions/workflows/test.yml/badge.svg)

Downloads

80

Readme

Signals.ts

Tests

Signals implementation for TypeScript.

It's a simple pub-sub message bus architecture that's completely decoupled from any UI or other component.

Inspired by JS-Signals which was inspired by AS3-Signals.

Why another implementation

I used many implementations through the years, manly Miller Medeiros' one.

What I though was missing was:

  • TypeScript support
  • Two way decoupled communication

Installation

$ npm i --save signals.ts

or

$ yarn add signal.ts

or

$ echo 'whatever package manager you use... the package is on npm.com 🙂'

Basic usage

//signals.ts --------------------------

import {Signal} from 'signals.ts';
export const MY_SIGNAL = new Signal();
export const MY_OTHER_SIGNAL = new Signal();
//...
// dispatcher.ts ----------------------
import {MY_SIGNAL} from './signals';

MY_SIGNAL.dispatch();
// listener.ts ------------------------

MY_SIGNAL.add(() => {
  //do something about it
})

// or

MY_SIGNAL.addOnce(() => {
  //do something about it just the first time the signal is received
})

Sending data

const SHOW_NOTIFICATION = new Signal<{title : string, body : string}>();

SHOW_NOTIFICATION.dispatch({title : 'Error', `There's a snake in your boot`});

Receiving data (asynchronously)

const SHOW_DIALOG = new Signal<{title : string, body : string, buttons : Array<'ok' | 'cancel' | 'yes' | 'no'>}>();
// the enquirer
const [response] = //response are always an array
  await SHOW_DIALOG.dispatch({title : 'Danger', body : 'This button will destroy the world. Do you want to proceed?', buttons : ['yes', 'no']})
// the dialog view component
SHOW_DIALOG.add((config) => {
  return new Promise(resolve => {
  this.show(config)
    .onResponse(resolve)
  });
})

Controlling propagation

//any failed response, according to the listenerSuccessTest will stop propagation
const APP_WILL_CLOSE = new Signal({propagate : 'any-fail', listenerSuccessTest: val => val === true})
// The app controller
const responses = await APP_WILL_CLOSE.dispatch()

const shouldProceed = !response.some(dirty => dirty = false)


// any listening editors

APP_WILL_CLOSE.add(() => {
  if(this.inABadMood) {
    return false;
  }
});

Retaining early signal dispatchment

export const APP_WAS_INITIALIZED = new Signal({memoize : true});
// app bootstrapping controller

await this.bootstrap()
APP_WAS_INITIALIZED.dispatch();
// some component we're not sure will be listening in time

// listener will be called even if addOnce is called after the 
// signal was already dispatched
APP_WAS_INITIALIZED.addOnce(() => {
  this.doSomething()
})

Detaching/Suspending a listener

Adding a listener returns a binding object that can be used to temporarily suspend or permanently detach the listener


const fn1Binding = SOME_SIGNAL.add(functionOne);
SOME_SIGNAL.add(functionTwo);
SOME_SIGNAL.dispatch() //will reach functionOne and functionTwo
fn1Binding.suspend()
SOME_SIGNAL.dispatch() //will reach functionTwo only
fn1Binding.resume()
SOME_SIGNAL.dispatch() //will reach functionOne and functionTwo

fn1Binding.detatch()
SOME_SIGNAL.dispatch() //will reach functionTwo only

fn1Binding.suspend() //will throw Error
fn1Binding.resume() //will throw Error

License

Running Tests

$ yarn
$ yarn test