Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

emittery

Package Overview
Dependencies
Maintainers
2
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emittery - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

108

index.d.ts

@@ -8,2 +8,7 @@ /**

/**
Emittery also accepts an array of strings and symbols as event names.
*/
type EventNames = EventName | readonly EventName[];
declare class Emittery {

@@ -82,3 +87,3 @@ /**

/**
Subscribe to an event.
Subscribe to one or more events.

@@ -88,5 +93,22 @@ Using the same listener multiple times for the same event will result in only one method call per emitted event.

@returns An unsubscribe method.
@example
```
import Emittery = require('emittery');
const emitter = new Emittery();
emitter.on('🦄', data => {
console.log(data);
});
emitter.on(['🦄', '🐶'], data => {
console.log(data);
});
emitter.emit('🦄', '🌈'); // log => '🌈' x2
emitter.emit('🐶', '🍖'); // log => '🍖'
```
*/
on(eventName: typeof Emittery.listenerAdded | typeof Emittery.listenerRemoved, listener: (eventData: Emittery.ListenerChangedData) => void): Emittery.UnsubscribeFn
on(eventName: EventName, listener: (eventData?: unknown) => void): Emittery.UnsubscribeFn;
on(eventName: EventNames, listener: (eventData?: unknown) => void): Emittery.UnsubscribeFn;

@@ -145,18 +167,86 @@ /**

```
It accepts multiple event names.
@example
```
import Emittery = require('emittery');
const emitter = new Emittery();
const iterator = emitter.events(['🦄', '🦊']);
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦊', '🌈2'); // Buffered
iterator
.next()
.then(({value, done}) => {
// done === false
// value === '🌈1'
return iterator.next();
})
.then(({value, done}) => {
// done === false
// value === '🌈2'
// Revoke subscription
return iterator.return();
})
.then(({done}) => {
// done === true
});
```
*/
events(eventName: EventName): AsyncIterableIterator<unknown>
events(eventName: EventNames): AsyncIterableIterator<unknown>
/**
Remove an event subscription.
Remove one or more event subscriptions.
@example
```
import Emittery = require('emittery');
const emitter = new Emittery();
const listener = data => console.log(data);
(async () => {
emitter.on(['🦄', '🐶', '🦊'], listener);
await emitter.emit('🦄', 'a');
await emitter.emit('🐶', 'b');
await emitter.emit('🦊', 'c');
emitter.off('🦄', listener);
emitter.off(['🐶', '🦊'], listener);
await emitter.emit('🦄', 'a'); // nothing happens
await emitter.emit('🐶', 'b'); // nothing happens
await emitter.emit('🦊', 'c'); // nothing happens
})();
```
*/
off(eventName: EventName, listener: (eventData?: unknown) => void): void;
off(eventName: EventNames, listener: (eventData?: unknown) => void): void;
/**
Subscribe to an event only once. It will be unsubscribed after the first
Subscribe to one or more events only once. It will be unsubscribed after the first
event.
@returns The event data when `eventName` is emitted.
@example
```
import Emittery = require('emittery');
const emitter = new Emittery();
emitter.once('🦄').then(data => {
console.log(data);
//=> '🌈'
});
emitter.once(['🦄', '🐶']).then(data => {
console.log(data);
});
emitter.emit('🦄', '🌈'); // Logs `🌈` twice
emitter.emit('🐶', '🍖'); // Nothing happens
```
*/
once(eventName: typeof Emittery.listenerAdded | typeof Emittery.listenerRemoved): Promise<Emittery.ListenerChangedData>
once(eventName: EventName): Promise<unknown>;
once(eventName: EventNames): Promise<unknown>;

@@ -232,3 +322,3 @@ /**

*/
clearListeners(eventName?: EventName): void;
clearListeners(eventName?: EventNames): void;

@@ -238,3 +328,3 @@ /**

*/
listenerCount(eventName?: EventName): number;
listenerCount(eventName?: EventNames): number;

@@ -241,0 +331,0 @@ /**

132

index.js

@@ -59,3 +59,5 @@ 'use strict';

function iterator(instance, eventName) {
function iterator(instance, eventNames) {
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
let isFinished = false;

@@ -76,3 +78,5 @@ let flush = () => {};

getEventProducers(instance, eventName).add(producer);
for (const eventName of eventNames) {
getEventProducers(instance, eventName).add(producer);
}

@@ -106,3 +110,7 @@ return {

queue = undefined;
getEventProducers(instance, eventName).delete(producer);
for (const eventName of eventNames) {
getEventProducers(instance, eventName).delete(producer);
}
flush();

@@ -193,29 +201,35 @@

on(eventName, listener) {
assertEventName(eventName);
on(eventNames, listener) {
assertListener(listener);
getListeners(this, eventName).add(listener);
if (!isListenerSymbol(eventName)) {
this.emit(listenerAdded, {eventName, listener});
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
for (const eventName of eventNames) {
assertEventName(eventName);
getListeners(this, eventName).add(listener);
if (!isListenerSymbol(eventName)) {
this.emit(listenerAdded, {eventName, listener});
}
}
return this.off.bind(this, eventName, listener);
return this.off.bind(this, eventNames, listener);
}
off(eventName, listener) {
assertEventName(eventName);
off(eventNames, listener) {
assertListener(listener);
if (!isListenerSymbol(eventName)) {
this.emit(listenerRemoved, {eventName, listener});
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
for (const eventName of eventNames) {
assertEventName(eventName);
getListeners(this, eventName).delete(listener);
if (!isListenerSymbol(eventName)) {
this.emit(listenerRemoved, {eventName, listener});
}
}
getListeners(this, eventName).delete(listener);
}
once(eventName) {
once(eventNames) {
return new Promise(resolve => {
assertEventName(eventName);
const off = this.on(eventName, data => {
const off = this.on(eventNames, data => {
off();

@@ -227,5 +241,9 @@ resolve(data);

events(eventName) {
assertEventName(eventName);
return iterator(this, eventName);
events(eventNames) {
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
for (const eventName of eventNames) {
assertEventName(eventName);
}
return iterator(this, eventNames);
}

@@ -244,3 +262,3 @@

await resolvedPromise;
return Promise.all([
await Promise.all([
...staticListeners.map(async listener => {

@@ -300,21 +318,11 @@ if (listeners.has(listener)) {

clearListeners(eventName) {
if (typeof eventName === 'string') {
getListeners(this, eventName).clear();
clearListeners(eventNames) {
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
const producers = getEventProducers(this, eventName);
for (const eventName of eventNames) {
if (typeof eventName === 'string') {
getListeners(this, eventName).clear();
for (const producer of producers) {
producer.finish();
}
const producers = getEventProducers(this, eventName);
producers.clear();
} else {
anyMap.get(this).clear();
for (const listeners of eventsMap.get(this).values()) {
listeners.clear();
}
for (const producers of producersMap.get(this).values()) {
for (const producer of producers) {

@@ -325,2 +333,16 @@ producer.finish();

producers.clear();
} else {
anyMap.get(this).clear();
for (const listeners of eventsMap.get(this).values()) {
listeners.clear();
}
for (const producers of producersMap.get(this).values()) {
for (const producer of producers) {
producer.finish();
}
producers.clear();
}
}

@@ -330,20 +352,26 @@ }

listenerCount(eventName) {
if (typeof eventName === 'string') {
return anyMap.get(this).size + getListeners(this, eventName).size +
getEventProducers(this, eventName).size + getEventProducers(this).size;
}
listenerCount(eventNames) {
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
let count = 0;
if (typeof eventName !== 'undefined') {
assertEventName(eventName);
}
for (const eventName of eventNames) {
if (typeof eventName === 'string') {
count += anyMap.get(this).size + getListeners(this, eventName).size +
getEventProducers(this, eventName).size + getEventProducers(this).size;
continue;
}
let count = anyMap.get(this).size;
if (typeof eventName !== 'undefined') {
assertEventName(eventName);
}
for (const value of eventsMap.get(this).values()) {
count += value.size;
}
count += anyMap.get(this).size;
for (const value of producersMap.get(this).values()) {
count += value.size;
for (const value of eventsMap.get(this).values()) {
count += value.size;
}
for (const value of producersMap.get(this).values()) {
count += value.size;
}
}

@@ -350,0 +378,0 @@

{
"name": "emittery",
"version": "0.6.0",
"version": "0.7.0",
"description": "Simple and modern async event emitter",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -48,5 +48,5 @@ # <img src="media/header.png" width="1000">

#### on(eventName, listener)
#### on(eventName | eventName[], listener)
Subscribe to an event.
Subscribe to one or more events.

@@ -57,2 +57,18 @@ Returns an unsubscribe method.

```js
const Emittery = require('emittery');
const emitter = new Emittery();
emitter.on('🦄', data => {
console.log(data);
});
emitter.on(['🦄', '🐶'], data => {
console.log(data);
});
emitter.emit('🦄', '🌈'); // log => '🌈' x2
emitter.emit('🐶', '🍖'); // log => '🍖'
```
##### Custom subscribable events

@@ -92,11 +108,30 @@

#### off(eventName, listener)
#### off(eventName | eventName[], listener)
Remove an event subscription.
Remove one or more event subscriptions.
```js
const Emittery = require('emittery');
const emitter = new Emittery();
const listener = data => console.log(data);
(async () => {
emitter.on(['🦄', '🐶', '🦊'], listener);
await emitter.emit('🦄', 'a');
await emitter.emit('🐶', 'b');
await emitter.emit('🦊', 'c');
emitter.off('🦄', listener);
emitter.off(['🐶', '🦊'], listener);
await emitter.emit('🦄', 'a'); // nothing happens
await emitter.emit('🐶', 'b'); // nothing happens
await emitter.emit('🦊', 'c'); // nothing happens
})();
```
##### listener(data)
#### once(eventName)
#### once(eventName | eventName[])
Subscribe to an event only once. It will be unsubscribed after the first event.
Subscribe to one or more events only once. It will be unsubscribed after the first event.

@@ -114,4 +149,8 @@ Returns a promise for the event data when `eventName` is emitted.

});
emitter.once(['🦄', '🐶']).then(data => {
console.log(data);
});
emitter.emit('🦄', '🌈');
emitter.emit('🦄', '🌈'); // log => '🌈' x2
emitter.emit('🐶', '🍖'); // nothing happens
```

@@ -171,2 +210,31 @@

It accepts multiple event names.
```js
const Emittery = require('emittery');
const emitter = new Emittery();
const iterator = emitter.events(['🦄', '🦊']);
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦊', '🌈2'); // Buffered
iterator
.next()
.then(({value, done}) => {
// done === false
// value === '🌈1'
return iterator.next();
})
.then(({value, done}) => {
// done === false
// value === '🌈2'
// Revoke subscription
return iterator.return();
})
.then(({done}) => {
// done === true
});
```
#### emit(eventName, data?)

@@ -230,11 +298,11 @@

#### clearListeners()
#### clearListeners(eventNames?)
Clear all event listeners on the instance.
If `eventName` is given, only the listeners for that event are cleared.
If `eventNames` is given, only the listeners for that events are cleared.
#### listenerCount(eventName?)
#### listenerCount(eventNames?)
The number of listeners for the `eventName` or all events if not specified.
The number of listeners for the `eventNames` or all events if not specified.

@@ -241,0 +309,0 @@ #### bindMethods(target, methodNames?)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc