0.2.2 • Published 2 years ago

@hmans/signal v0.2.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Tests Version Downloads Bundle Size

@hmans/signal

A super duper simple signal implementation that I use in many of my other projects. It doesn't do anything terribly exciting, but provides some API niceties for added convenience.

Signals represent distinct signals -- or events -- that allow interested parties to add callbacks to them that will be evoked whenever the signal is emitted. Unlike eventemitter and friends, distinct signals are expressed as separate instances -- there is no key-based routing (in fact, there aren't even any keys to begin with!)

Projects using @hmans/signal

Usage

import { Signal } from "@hmans/signal"

const signal = new Signal<number>()
signal.add((n) => console.log(n))
signal.emit()

Callbacks are added through add and removed through remove.

const callback = (n) => console.log(n)
signal.add(callback)
signal.remove(callback)

clear discards all registered listeners:

signal.clear()

Signals optionally accept a listener through their constructor (just a bit of syntactical sugar for convenience):

const signal = new Signal(() => console.log("I've been signalled!"))
signal.emit()

Interactions with Signal instances can be chained:

new Signal<string>()
  .add((name) => console.log(`Hello ${name}!`))
  .add((name) => console.log(`Hi again ${name}!`))
  .emit()
0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago