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

signal-notifier-ts

v1.0.0

Published

Signal Notifier written in Typescript.

Downloads

2

Readme

Build Status npm version

TypeScript Signal Notifier

Notifier and Signal Pattern pattern written in Typescript.

This module signal-notifier-ts uses typed Signals, also written in TypeScript.

Installation

npm install signal-notifier-ts

Usage

Creating a Notifier

Create an instance of the Notifier.

import {Signal, Notifier} from "signal-notifier-ts";

let notifier: Notifier = new Notifier();

Create Signal(s) with the specified type, i.e, string, number, etc.

let onCompleted: Signal<number> = new Signal();

Add function callback

Register a callback with add().

onCompleted.add((n: number) => {
     console.log("got a number", n);
});

Emitting signals with the Notifier

The Notifier allows for controling signals flow through a queue of notifications.

You can call notifier.pause() to pause emitting notifications or notifier.resume() to process the queue of notifications immediately. For example, you can pause() while waiting for an operation to complete and resume emitting from the queue with resume().

To emit a signal invoke the notify() method and emit data to the signal:

notifier.notify(onCompleted).emit(299792458);

Usage Sample

Automated Teller Machine

class ATMProcessor {
     constructor(atm: ATM) {
         // Subscribe to Signals
          atm.onPinEntered.add((pin: number) => {
               console.log('pin entered:', pin);
          });
          atm.onWithdrawAmount.add((amount: number) => {
               console.log('withdraw amount:', amount);
          });
          atm.onBalanceRequested.add((pin: number) => {
               console.log('balance requested w/pin:', pin);
          });
     }
}

// Extend the Notifier and create signals
class ATM extends Notifier {
     onPinEntered: Signal<number> = new Signal();
     onWithdrawAmount: Signal<number> = new Signal();
     onBalanceRequested: Signal<number> = new Signal();

     constructor() {
          super();
          new ATMProcessor(this);
     }
     // Notify and Emit signals
     enterPin(pin: number): void {
          this.notify(this.onPinEntered).emit(pin);
     }
     withdrawFunds(amount: number): void {
          this.notify(this.onWithdrawAmount).emit(amount);
     }
     balanceRequest(pin: number): void {
          this.notify(this.onBalanceRequested).emit(pin);
     }
     // Pause notifications
     connectionDropped(): void {
         console.log('ATM disconnected');
          this.pause();
     }
     // Resume notifications
     connectionOk(): void {
          console.log('ATM online');
          this.resume();
     }
}

Use Case

let atm = new ATM();
// User authorized by entering pin
atm.enterPin(555);

// Connection drops
atm.connectionDropped();

// User requests withdrawal
atm.withdrawFunds(100);

// User requests balance
atm.balanceRequest(555);

// Connection is back online
atm.connectionOk();

Expected Output

pin entered: 555
ATM disconnected
ATM online
withdraw amount: 100
balance requested w/pin: 555

License

MIT License