https://github.com/zeh/simplesignal
Super-simple signals (C#-style events) for JavaScript and TypeScript.
https://github.com/zeh/simplesignal
event listen npm-module signal typescript
Last synced: 14 days ago
JSON representation
Super-simple signals (C#-style events) for JavaScript and TypeScript.
- Host: GitHub
- URL: https://github.com/zeh/simplesignal
- Owner: zeh
- License: mit
- Created: 2016-03-28T18:22:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-07T22:35:58.000Z (about 1 year ago)
- Last Synced: 2025-04-04T20:46:05.453Z (6 months ago)
- Topics: event, listen, npm-module, signal, typescript
- Language: JavaScript
- Homepage:
- Size: 536 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Simple Signal
[](https://www.npmjs.com/package/simplesignal)
[](https://travis-ci.org/zeh/simplesignal)
[](https://coveralls.io/github/zeh/simplesignal?branch=master)
[](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).