1.0.0 • Published 6 years ago

signal-notifier-ts v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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