6.0.0 • Published 3 months ago

simplesignal v6.0.0

Weekly downloads
932
License
MIT
Repository
github
Last release
3 months ago

Simple Signal

npm Build Status Coverage Status Dependency Status

This is a super-simple signals class inspired by Robert Penner's AS3Signals.

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

npm install simplesignal

Usage

First, import your signal:

// 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:

public onSomethingHappened = new SimpleSignal();

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

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

Signals can then be dispatched with parameters:

onSomethingHappened.dispatch("some-id");

This will call all subscribed functions with the given parameter.

Full reference (JS)

// 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:

// 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.

6.0.0

3 months ago

5.1.0

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.1.7

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.0

8 years ago