Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
simplesignal
Advanced tools
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.
npm install simplesignal
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.
// 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)
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")
MIT.
FAQs
Super-simple signals class
The npm package simplesignal receives a total of 23,108 weekly downloads. As such, simplesignal popularity was classified as popular.
We found that simplesignal demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.