# simplesignal

> Super-simple signals class

Latest version **6.0.0** (published 2024-01-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install simplesignal
pnpm add simplesignal
yarn add simplesignal
bun add simplesignal
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2024-01-31 |
| First published | 2016-03-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Zeh Fernando |
| Maintainers | zeh |
| Keywords | signals, events, callbacks, minimal, small, simple, tiny, typescript |

## Links

- npm: https://www.npmjs.com/package/simplesignal
- Repository: https://github.com/zeh/simplesignal
- Homepage: https://github.com/zeh/simplesignal#readme
- Issues: https://github.com/zeh/simplesignal/issues
- npm.io page: https://npm.io/package/simplesignal

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 6.0.0 (latest) — 2024-01-31
- 5.1.0 — 2021-03-02
- 5.0.1 — 2021-03-02
- 5.0.0 — 2021-03-02
- 4.0.4 — 2019-12-29
- 4.0.3 — 2019-12-29
- 4.0.2 — 2019-07-04
- 4.0.1 — 2018-12-14
- 4.0.0 — 2018-12-14
- 3.1.0 — 2017-05-24
- 3.0.0 — 2017-05-19
- 2.1.7 — 2017-04-03
- 2.1.6 — 2016-12-14
- 2.1.5 — 2016-11-22
- 2.1.4 — 2016-11-20
- … 9 more at https://npm.io/package/simplesignal/versions

## README

# Simple Signal

[![npm](https://img.shields.io/npm/v/simplesignal.svg)](https://www.npmjs.com/package/simplesignal)
[![Build Status](https://travis-ci.org/zeh/simplesignal.svg?branch=master)](https://travis-ci.org/zeh/simplesignal)
[![Coverage Status](https://coveralls.io/repos/github/zeh/simplesignal/badge.svg?branch=master)](https://coveralls.io/github/zeh/simplesignal?branch=master)
[![Dependency Status](https://david-dm.org/zeh/simplesignal.svg)](https://david-dm.org/zeh/simplesignal)

This is a super-simple signals class inspired by [Robert Penner's AS3Signals](http://github.com/robertpenner/as3-signals).

Signals are like *Events*, *Delegates*, or *Callbacks* on other languages or platforms. You can create a signal that other parts of the code can "listen" to. When that signal is *dispatched*, all listeners are called with the passed parameters.

SimpleSignal is created with TypeScript, but aimed to be used as a standard JavaScript library.

## Install


```shell
npm install simplesignal
```

## Usage

First, import your signal:

```javascript
// Import (JavaScript ES5)
var SimpleSignal = require('simplesignal');

// Import (JavaScript ES6 and TypeScript)
import SimpleSignal from 'simplesignal';
```

Then, you can create a signal. For example, inside a class:

```javascript
public onSomethingHappened = new SimpleSignal();
```

Then other parts of the code can subscribe (listen) to that signal:

```javascript
myClassObject.onSomethingHappened.add((id) => {
    console.log("Something happened with an id of " + id);
});
```

Signals can then be dispatched with parameters:

```javascript
onSomethingHappened.dispatch("some-id");
```

This will call all subscribed functions with the given parameter.

## Full reference (JS)

```javascript
// Create
onSomethingHappened = new SimpleSignal();

// Subscribe
onSomethingHappened.add(myFunc);

// Anonymous functions are, of course, fine
onSomethingHappened.add(function() { ... });
onSomethingHappened.add(() => { ... });

// Unsubscribe
onSomethingHappened.remove(myFunc);

// Clear subscribers
onSomethingHappened.removeAll();

// Number of subscribers
console.log(onSomethingHappened.numItems);

// Dispatch
onSomethingHappened.dispatch(...args)
```

## Additional TypeScript reference

If your project already uses TypeScript, it has the advantage of using SimpleSignal's definition files for strict typing.

In this case, one can use a generic interface to enforce the correct dispatch/listener parameters:

```javascript
// Create, with a given interface as a Generic
onSomethingHappened = new SimpleSignal<(action:string) => void>();

// The listeners must fulfill the interface
function myFunc(action:string) {
    console.log(action);
}

// Subscribe
onSomethingHappened.add(myFunc);

// Dispatch must also respect the interface
onSomethingHappened.dispatch("some-action")
```

## License

[MIT](LICENSE.md).

---
_Source: https://npm.io/package/simplesignal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
