Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
typed-emitter
Advanced tools
The 'typed-emitter' npm package provides a strongly-typed event emitter for TypeScript. It allows developers to define and use event emitters with type safety, ensuring that the events and their associated data are correctly typed.
Basic Event Emission
This feature allows you to define and emit basic events with type safety. The event 'event1' is defined to accept a string message, and the emitter ensures that only strings are passed when emitting this event.
const { TypedEmitter } = require('typed-emitter');
interface MyEvents {
event1: (message: string) => void;
}
const emitter = new TypedEmitter<MyEvents>();
emitter.on('event1', (message) => {
console.log(message);
});
emitter.emit('event1', 'Hello, World!');
Multiple Event Listeners
This feature demonstrates how you can attach multiple listeners to a single event. Both listeners will be called when 'event1' is emitted, and they will receive the same message.
const { TypedEmitter } = require('typed-emitter');
interface MyEvents {
event1: (message: string) => void;
event2: (count: number) => void;
}
const emitter = new TypedEmitter<MyEvents>();
emitter.on('event1', (message) => {
console.log('Listener 1:', message);
});
emitter.on('event1', (message) => {
console.log('Listener 2:', message);
});
emitter.emit('event1', 'Hello, World!');
Removing Event Listeners
This feature shows how to remove an event listener. The listener is first added to 'event1', then removed using the 'off' method. The second emission of 'event1' does not trigger the listener.
const { TypedEmitter } = require('typed-emitter');
interface MyEvents {
event1: (message: string) => void;
}
const emitter = new TypedEmitter<MyEvents>();
const listener = (message: string) => {
console.log(message);
};
emitter.on('event1', listener);
emitter.emit('event1', 'Hello, World!');
emitter.off('event1', listener);
emitter.emit('event1', 'This will not be logged');
EventEmitter3 is a high-performance event emitter for Node.js and the browser. It provides a similar API to Node.js's built-in EventEmitter but with better performance and additional features. Unlike 'typed-emitter', it does not provide built-in TypeScript support, so type safety must be manually managed.
Mitt is a tiny (~200 bytes) functional event emitter. It is designed to be extremely lightweight and simple to use. While it is not as feature-rich as 'typed-emitter' and does not provide built-in TypeScript support, it is a good choice for projects where size and simplicity are critical.
Nanoevents is a tiny (around 200 bytes) event emitter library for JavaScript. It is similar to 'mitt' in its focus on minimalism and simplicity. Like 'mitt', it does not provide built-in TypeScript support, so type safety must be manually managed.
FAQs
Strictly typed event emitter interface for TypeScript 3.
The npm package typed-emitter receives a total of 54,223 weekly downloads. As such, typed-emitter popularity was classified as popular.
We found that typed-emitter demonstrated a not healthy version release cadence and project activity because the last version was released 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.