Add node modules and compiled JavaScript from main
This commit is contained in:
parent
8bccaeaf7c
commit
4181bfdf50
7465 changed files with 1775003 additions and 2 deletions
414
node_modules/emittery/index.d.ts
generated
vendored
Normal file
414
node_modules/emittery/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,414 @@
|
|||
/**
|
||||
Emittery accepts strings and symbols as event names.
|
||||
|
||||
Symbol event names can be used to avoid name collisions when your classes are extended, especially for internal events.
|
||||
*/
|
||||
type EventName = string | symbol;
|
||||
|
||||
/**
|
||||
Emittery also accepts an array of strings and symbols as event names.
|
||||
*/
|
||||
type EventNames = EventName | readonly EventName[];
|
||||
|
||||
declare class Emittery {
|
||||
/**
|
||||
In TypeScript, it returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
@Emittery.mixin('emittery')
|
||||
class MyClass {}
|
||||
|
||||
const instance = new MyClass();
|
||||
|
||||
instance.emit('event');
|
||||
```
|
||||
*/
|
||||
static mixin(emitteryPropertyName: string | symbol, methodNames?: readonly string[]): Function;
|
||||
|
||||
/**
|
||||
Fires when an event listener was added.
|
||||
|
||||
An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
|
||||
emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
|
||||
console.log(listener);
|
||||
//=> data => {}
|
||||
|
||||
console.log(eventName);
|
||||
//=> '🦄'
|
||||
});
|
||||
|
||||
emitter.on('🦄', data => {
|
||||
// Handle data
|
||||
});
|
||||
```
|
||||
*/
|
||||
static readonly listenerAdded: unique symbol;
|
||||
|
||||
/**
|
||||
Fires when an event listener was removed.
|
||||
|
||||
An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
|
||||
const off = emitter.on('🦄', data => {
|
||||
// Handle data
|
||||
});
|
||||
|
||||
emitter.on(Emittery.listenerRemoved, ({listener, eventName}) => {
|
||||
console.log(listener);
|
||||
//=> data => {}
|
||||
|
||||
console.log(eventName);
|
||||
//=> '🦄'
|
||||
});
|
||||
|
||||
off();
|
||||
```
|
||||
*/
|
||||
static readonly listenerRemoved: unique symbol;
|
||||
|
||||
/**
|
||||
Subscribe to one or more events.
|
||||
|
||||
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: EventNames, listener: (eventData?: unknown) => void): Emittery.UnsubscribeFn;
|
||||
|
||||
/**
|
||||
Get an async iterator which buffers data each time an event is emitted.
|
||||
|
||||
Call `return()` on the iterator to remove the subscription.
|
||||
|
||||
@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
|
||||
});
|
||||
```
|
||||
|
||||
In practice you would usually consume the events using the [for await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) statement. In that case, to revoke the subscription simply break the loop.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
const iterator = emitter.events('🦄');
|
||||
|
||||
emitter.emit('🦄', '🌈1'); // Buffered
|
||||
emitter.emit('🦄', '🌈2'); // Buffered
|
||||
|
||||
// In an async context.
|
||||
for await (const data of iterator) {
|
||||
if (data === '🌈2') {
|
||||
break; // Revoke the subscription when we see the value `🌈2`.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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: EventNames): AsyncIterableIterator<unknown>
|
||||
|
||||
/**
|
||||
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: EventNames, listener: (eventData?: unknown) => void): void;
|
||||
|
||||
/**
|
||||
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: EventNames): Promise<unknown>;
|
||||
|
||||
/**
|
||||
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
|
||||
|
||||
@returns A promise that resolves when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
|
||||
*/
|
||||
emit(eventName: EventName, eventData?: unknown): Promise<void>;
|
||||
|
||||
/**
|
||||
Same as `emit()`, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
|
||||
|
||||
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
|
||||
|
||||
@returns A promise that resolves when all the event listeners are done.
|
||||
*/
|
||||
emitSerial(eventName: EventName, eventData?: unknown): Promise<void>;
|
||||
|
||||
/**
|
||||
Subscribe to be notified about any event.
|
||||
|
||||
@returns A method to unsubscribe.
|
||||
*/
|
||||
onAny(listener: (eventName: EventName, eventData?: unknown) => unknown): Emittery.UnsubscribeFn;
|
||||
|
||||
/**
|
||||
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
|
||||
|
||||
Call `return()` on the iterator to remove the subscription.
|
||||
|
||||
In the same way as for `events`, you can subscribe by using the `for await` statement.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
const iterator = emitter.anyEvent();
|
||||
|
||||
emitter.emit('🦄', '🌈1'); // Buffered
|
||||
emitter.emit('🌟', '🌈2'); // Buffered
|
||||
|
||||
iterator.next()
|
||||
.then(({value, done}) => {
|
||||
// done is false
|
||||
// value is ['🦄', '🌈1']
|
||||
return iterator.next();
|
||||
})
|
||||
.then(({value, done}) => {
|
||||
// done is false
|
||||
// value is ['🌟', '🌈2']
|
||||
// revoke subscription
|
||||
return iterator.return();
|
||||
})
|
||||
.then(({done}) => {
|
||||
// done is true
|
||||
});
|
||||
```
|
||||
*/
|
||||
anyEvent(): AsyncIterableIterator<unknown>
|
||||
|
||||
/**
|
||||
Remove an `onAny` subscription.
|
||||
*/
|
||||
offAny(listener: (eventName: EventName, eventData?: unknown) => void): void;
|
||||
|
||||
/**
|
||||
Clear all event listeners on the instance.
|
||||
|
||||
If `eventName` is given, only the listeners for that event are cleared.
|
||||
*/
|
||||
clearListeners(eventName?: EventNames): void;
|
||||
|
||||
/**
|
||||
The number of listeners for the `eventName` or all events if not specified.
|
||||
*/
|
||||
listenerCount(eventName?: EventNames): number;
|
||||
|
||||
/**
|
||||
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const object = {};
|
||||
|
||||
new Emittery().bindMethods(object);
|
||||
|
||||
object.emit('event');
|
||||
```
|
||||
*/
|
||||
bindMethods(target: object, methodNames?: readonly string[]): void;
|
||||
}
|
||||
|
||||
declare namespace Emittery {
|
||||
/**
|
||||
Removes an event subscription.
|
||||
*/
|
||||
type UnsubscribeFn = () => void;
|
||||
type EventNameFromDataMap<EventDataMap> = Extract<keyof EventDataMap, EventName>;
|
||||
|
||||
/**
|
||||
Maps event names to their emitted data type.
|
||||
*/
|
||||
interface Events {
|
||||
// Blocked by https://github.com/microsoft/TypeScript/issues/1863, should be
|
||||
// `[eventName: EventName]: unknown;`
|
||||
}
|
||||
|
||||
/**
|
||||
The data provided as `eventData` when listening for `Emittery.listenerAdded` or `Emittery.listenerRemoved`.
|
||||
*/
|
||||
interface ListenerChangedData {
|
||||
/**
|
||||
The listener that was added or removed.
|
||||
*/
|
||||
listener: (eventData?: unknown) => void;
|
||||
|
||||
/**
|
||||
The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
|
||||
*/
|
||||
eventName?: EventName;
|
||||
}
|
||||
|
||||
/**
|
||||
Async event emitter.
|
||||
|
||||
You must list supported events and the data type they emit, if any.
|
||||
|
||||
@example
|
||||
```
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
|
||||
|
||||
emitter.emit('open');
|
||||
emitter.emit('value', 'foo\n');
|
||||
emitter.emit('value', 1); // TS compilation error
|
||||
emitter.emit('end'); // TS compilation error
|
||||
```
|
||||
*/
|
||||
class Typed<EventDataMap extends Events, EmptyEvents extends EventName = never> extends Emittery {
|
||||
on<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): Emittery.UnsubscribeFn;
|
||||
on<Name extends EmptyEvents>(eventName: Name, listener: () => void): Emittery.UnsubscribeFn;
|
||||
|
||||
events<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name): AsyncIterableIterator<EventDataMap[Name]>;
|
||||
|
||||
once<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name): Promise<EventDataMap[Name]>;
|
||||
once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
||||
|
||||
off<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): void;
|
||||
off<Name extends EmptyEvents>(eventName: Name, listener: () => void): void;
|
||||
|
||||
onAny(listener: (eventName: EventNameFromDataMap<EventDataMap> | EmptyEvents, eventData?: EventDataMap[EventNameFromDataMap<EventDataMap>]) => void): Emittery.UnsubscribeFn;
|
||||
anyEvent(): AsyncIterableIterator<[EventNameFromDataMap<EventDataMap>, EventDataMap[EventNameFromDataMap<EventDataMap>]]>;
|
||||
|
||||
offAny(listener: (eventName: EventNameFromDataMap<EventDataMap> | EmptyEvents, eventData?: EventDataMap[EventNameFromDataMap<EventDataMap>]) => void): void;
|
||||
|
||||
emit<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
|
||||
emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
||||
|
||||
emitSerial<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
|
||||
emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
||||
}
|
||||
}
|
||||
|
||||
export = Emittery;
|
415
node_modules/emittery/index.js
generated
vendored
Normal file
415
node_modules/emittery/index.js
generated
vendored
Normal file
|
@ -0,0 +1,415 @@
|
|||
'use strict';
|
||||
|
||||
const anyMap = new WeakMap();
|
||||
const eventsMap = new WeakMap();
|
||||
const producersMap = new WeakMap();
|
||||
const anyProducer = Symbol('anyProducer');
|
||||
const resolvedPromise = Promise.resolve();
|
||||
|
||||
const listenerAdded = Symbol('listenerAdded');
|
||||
const listenerRemoved = Symbol('listenerRemoved');
|
||||
|
||||
function assertEventName(eventName) {
|
||||
if (typeof eventName !== 'string' && typeof eventName !== 'symbol') {
|
||||
throw new TypeError('eventName must be a string or a symbol');
|
||||
}
|
||||
}
|
||||
|
||||
function assertListener(listener) {
|
||||
if (typeof listener !== 'function') {
|
||||
throw new TypeError('listener must be a function');
|
||||
}
|
||||
}
|
||||
|
||||
function getListeners(instance, eventName) {
|
||||
const events = eventsMap.get(instance);
|
||||
if (!events.has(eventName)) {
|
||||
events.set(eventName, new Set());
|
||||
}
|
||||
|
||||
return events.get(eventName);
|
||||
}
|
||||
|
||||
function getEventProducers(instance, eventName) {
|
||||
const key = typeof eventName === 'string' || typeof eventName === 'symbol' ? eventName : anyProducer;
|
||||
const producers = producersMap.get(instance);
|
||||
if (!producers.has(key)) {
|
||||
producers.set(key, new Set());
|
||||
}
|
||||
|
||||
return producers.get(key);
|
||||
}
|
||||
|
||||
function enqueueProducers(instance, eventName, eventData) {
|
||||
const producers = producersMap.get(instance);
|
||||
if (producers.has(eventName)) {
|
||||
for (const producer of producers.get(eventName)) {
|
||||
producer.enqueue(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
if (producers.has(anyProducer)) {
|
||||
const item = Promise.all([eventName, eventData]);
|
||||
for (const producer of producers.get(anyProducer)) {
|
||||
producer.enqueue(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function iterator(instance, eventNames) {
|
||||
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
||||
|
||||
let isFinished = false;
|
||||
let flush = () => {};
|
||||
let queue = [];
|
||||
|
||||
const producer = {
|
||||
enqueue(item) {
|
||||
queue.push(item);
|
||||
flush();
|
||||
},
|
||||
finish() {
|
||||
isFinished = true;
|
||||
flush();
|
||||
}
|
||||
};
|
||||
|
||||
for (const eventName of eventNames) {
|
||||
getEventProducers(instance, eventName).add(producer);
|
||||
}
|
||||
|
||||
return {
|
||||
async next() {
|
||||
if (!queue) {
|
||||
return {done: true};
|
||||
}
|
||||
|
||||
if (queue.length === 0) {
|
||||
if (isFinished) {
|
||||
queue = undefined;
|
||||
return this.next();
|
||||
}
|
||||
|
||||
await new Promise(resolve => {
|
||||
flush = resolve;
|
||||
});
|
||||
|
||||
return this.next();
|
||||
}
|
||||
|
||||
return {
|
||||
done: false,
|
||||
value: await queue.shift()
|
||||
};
|
||||
},
|
||||
|
||||
async return(value) {
|
||||
queue = undefined;
|
||||
|
||||
for (const eventName of eventNames) {
|
||||
getEventProducers(instance, eventName).delete(producer);
|
||||
}
|
||||
|
||||
flush();
|
||||
|
||||
return arguments.length > 0 ?
|
||||
{done: true, value: await value} :
|
||||
{done: true};
|
||||
},
|
||||
|
||||
[Symbol.asyncIterator]() {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function defaultMethodNamesOrAssert(methodNames) {
|
||||
if (methodNames === undefined) {
|
||||
return allEmitteryMethods;
|
||||
}
|
||||
|
||||
if (!Array.isArray(methodNames)) {
|
||||
throw new TypeError('`methodNames` must be an array of strings');
|
||||
}
|
||||
|
||||
for (const methodName of methodNames) {
|
||||
if (!allEmitteryMethods.includes(methodName)) {
|
||||
if (typeof methodName !== 'string') {
|
||||
throw new TypeError('`methodNames` element must be a string');
|
||||
}
|
||||
|
||||
throw new Error(`${methodName} is not Emittery method`);
|
||||
}
|
||||
}
|
||||
|
||||
return methodNames;
|
||||
}
|
||||
|
||||
const isListenerSymbol = symbol => symbol === listenerAdded || symbol === listenerRemoved;
|
||||
|
||||
class Emittery {
|
||||
static mixin(emitteryPropertyName, methodNames) {
|
||||
methodNames = defaultMethodNamesOrAssert(methodNames);
|
||||
return target => {
|
||||
if (typeof target !== 'function') {
|
||||
throw new TypeError('`target` must be function');
|
||||
}
|
||||
|
||||
for (const methodName of methodNames) {
|
||||
if (target.prototype[methodName] !== undefined) {
|
||||
throw new Error(`The property \`${methodName}\` already exists on \`target\``);
|
||||
}
|
||||
}
|
||||
|
||||
function getEmitteryProperty() {
|
||||
Object.defineProperty(this, emitteryPropertyName, {
|
||||
enumerable: false,
|
||||
value: new Emittery()
|
||||
});
|
||||
return this[emitteryPropertyName];
|
||||
}
|
||||
|
||||
Object.defineProperty(target.prototype, emitteryPropertyName, {
|
||||
enumerable: false,
|
||||
get: getEmitteryProperty
|
||||
});
|
||||
|
||||
const emitteryMethodCaller = methodName => function (...args) {
|
||||
return this[emitteryPropertyName][methodName](...args);
|
||||
};
|
||||
|
||||
for (const methodName of methodNames) {
|
||||
Object.defineProperty(target.prototype, methodName, {
|
||||
enumerable: false,
|
||||
value: emitteryMethodCaller(methodName)
|
||||
});
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
}
|
||||
|
||||
constructor() {
|
||||
anyMap.set(this, new Set());
|
||||
eventsMap.set(this, new Map());
|
||||
producersMap.set(this, new Map());
|
||||
}
|
||||
|
||||
on(eventNames, listener) {
|
||||
assertListener(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, eventNames, listener);
|
||||
}
|
||||
|
||||
off(eventNames, listener) {
|
||||
assertListener(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});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
once(eventNames) {
|
||||
return new Promise(resolve => {
|
||||
const off = this.on(eventNames, data => {
|
||||
off();
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
events(eventNames) {
|
||||
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
||||
for (const eventName of eventNames) {
|
||||
assertEventName(eventName);
|
||||
}
|
||||
|
||||
return iterator(this, eventNames);
|
||||
}
|
||||
|
||||
async emit(eventName, eventData) {
|
||||
assertEventName(eventName);
|
||||
|
||||
enqueueProducers(this, eventName, eventData);
|
||||
|
||||
const listeners = getListeners(this, eventName);
|
||||
const anyListeners = anyMap.get(this);
|
||||
const staticListeners = [...listeners];
|
||||
const staticAnyListeners = isListenerSymbol(eventName) ? [] : [...anyListeners];
|
||||
|
||||
await resolvedPromise;
|
||||
await Promise.all([
|
||||
...staticListeners.map(async listener => {
|
||||
if (listeners.has(listener)) {
|
||||
return listener(eventData);
|
||||
}
|
||||
}),
|
||||
...staticAnyListeners.map(async listener => {
|
||||
if (anyListeners.has(listener)) {
|
||||
return listener(eventName, eventData);
|
||||
}
|
||||
})
|
||||
]);
|
||||
}
|
||||
|
||||
async emitSerial(eventName, eventData) {
|
||||
assertEventName(eventName);
|
||||
|
||||
const listeners = getListeners(this, eventName);
|
||||
const anyListeners = anyMap.get(this);
|
||||
const staticListeners = [...listeners];
|
||||
const staticAnyListeners = [...anyListeners];
|
||||
|
||||
await resolvedPromise;
|
||||
/* eslint-disable no-await-in-loop */
|
||||
for (const listener of staticListeners) {
|
||||
if (listeners.has(listener)) {
|
||||
await listener(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
for (const listener of staticAnyListeners) {
|
||||
if (anyListeners.has(listener)) {
|
||||
await listener(eventName, eventData);
|
||||
}
|
||||
}
|
||||
/* eslint-enable no-await-in-loop */
|
||||
}
|
||||
|
||||
onAny(listener) {
|
||||
assertListener(listener);
|
||||
anyMap.get(this).add(listener);
|
||||
this.emit(listenerAdded, {listener});
|
||||
return this.offAny.bind(this, listener);
|
||||
}
|
||||
|
||||
anyEvent() {
|
||||
return iterator(this);
|
||||
}
|
||||
|
||||
offAny(listener) {
|
||||
assertListener(listener);
|
||||
this.emit(listenerRemoved, {listener});
|
||||
anyMap.get(this).delete(listener);
|
||||
}
|
||||
|
||||
clearListeners(eventNames) {
|
||||
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
||||
|
||||
for (const eventName of eventNames) {
|
||||
if (typeof eventName === 'string' || typeof eventName === 'symbol') {
|
||||
getListeners(this, eventName).clear();
|
||||
|
||||
const producers = getEventProducers(this, eventName);
|
||||
|
||||
for (const producer of producers) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
listenerCount(eventNames) {
|
||||
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
||||
let count = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (typeof eventName !== 'undefined') {
|
||||
assertEventName(eventName);
|
||||
}
|
||||
|
||||
count += anyMap.get(this).size;
|
||||
|
||||
for (const value of eventsMap.get(this).values()) {
|
||||
count += value.size;
|
||||
}
|
||||
|
||||
for (const value of producersMap.get(this).values()) {
|
||||
count += value.size;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
bindMethods(target, methodNames) {
|
||||
if (typeof target !== 'object' || target === null) {
|
||||
throw new TypeError('`target` must be an object');
|
||||
}
|
||||
|
||||
methodNames = defaultMethodNamesOrAssert(methodNames);
|
||||
|
||||
for (const methodName of methodNames) {
|
||||
if (target[methodName] !== undefined) {
|
||||
throw new Error(`The property \`${methodName}\` already exists on \`target\``);
|
||||
}
|
||||
|
||||
Object.defineProperty(target, methodName, {
|
||||
enumerable: false,
|
||||
value: this[methodName].bind(this)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const allEmitteryMethods = Object.getOwnPropertyNames(Emittery.prototype).filter(v => v !== 'constructor');
|
||||
|
||||
// Subclass used to encourage TS users to type their events.
|
||||
Emittery.Typed = class extends Emittery {};
|
||||
Object.defineProperty(Emittery.Typed, 'Typed', {
|
||||
enumerable: false,
|
||||
value: undefined
|
||||
});
|
||||
|
||||
Object.defineProperty(Emittery, 'listenerAdded', {
|
||||
value: listenerAdded,
|
||||
writable: false,
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
Object.defineProperty(Emittery, 'listenerRemoved', {
|
||||
value: listenerRemoved,
|
||||
writable: false,
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
|
||||
module.exports = Emittery;
|
9
node_modules/emittery/license
generated
vendored
Normal file
9
node_modules/emittery/license
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
67
node_modules/emittery/package.json
generated
vendored
Normal file
67
node_modules/emittery/package.json
generated
vendored
Normal file
|
@ -0,0 +1,67 @@
|
|||
{
|
||||
"name": "emittery",
|
||||
"version": "0.7.2",
|
||||
"description": "Simple and modern async event emitter",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/emittery",
|
||||
"funding": "https://github.com/sindresorhus/emittery?sponsor=1",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && nyc ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"event",
|
||||
"emitter",
|
||||
"eventemitter",
|
||||
"events",
|
||||
"async",
|
||||
"emit",
|
||||
"on",
|
||||
"once",
|
||||
"off",
|
||||
"listener",
|
||||
"subscribe",
|
||||
"unsubscribe",
|
||||
"pubsub",
|
||||
"tiny",
|
||||
"addlistener",
|
||||
"addeventlistener",
|
||||
"dispatch",
|
||||
"dispatcher",
|
||||
"observer",
|
||||
"trigger",
|
||||
"await",
|
||||
"promise",
|
||||
"typescript",
|
||||
"ts",
|
||||
"typed"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/node": "^13.7.5",
|
||||
"ava": "^2.4.0",
|
||||
"codecov": "^3.1.0",
|
||||
"delay": "^4.3.0",
|
||||
"nyc": "^15.0.0",
|
||||
"p-event": "^4.1.0",
|
||||
"tsd": "^0.11.0",
|
||||
"xo": "^0.25.4"
|
||||
},
|
||||
"nyc": {
|
||||
"reporter": [
|
||||
"html",
|
||||
"lcov",
|
||||
"text"
|
||||
]
|
||||
}
|
||||
}
|
394
node_modules/emittery/readme.md
generated
vendored
Normal file
394
node_modules/emittery/readme.md
generated
vendored
Normal file
|
@ -0,0 +1,394 @@
|
|||
# <img src="media/header.png" width="1000">
|
||||
|
||||
> Simple and modern async event emitter
|
||||
|
||||
[](https://travis-ci.org/sindresorhus/emittery) [](https://codecov.io/gh/sindresorhus/emittery) [](https://bundlephobia.com/result?p=emittery)
|
||||
|
||||
It works in Node.js and the browser (using a bundler).
|
||||
|
||||
Emitting events asynchronously is important for production code where you want the least amount of synchronous operations. Since JavaScript is single-threaded, no other code can run while doing synchronous operations. For Node.js, that means it will block other requests, defeating the strength of the platform, which is scalability through async. In the browser, a synchronous operation could potentially cause lags and block user interaction.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install emittery
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
|
||||
emitter.on('🦄', data => {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
const myUnicorn = Symbol('🦄');
|
||||
|
||||
emitter.on(myUnicorn, data => {
|
||||
console.log(`Unicorns love ${data}`);
|
||||
});
|
||||
|
||||
emitter.emit('🦄', '🌈'); // Will trigger printing '🌈'
|
||||
emitter.emit(myUnicorn, '🦋'); // Will trigger printing 'Unicorns love 🦋'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### eventName
|
||||
|
||||
Emittery accepts strings and symbols as event names.
|
||||
|
||||
Symbol event names can be used to avoid name collisions when your classes are extended, especially for internal events.
|
||||
|
||||
### emitter = new Emittery()
|
||||
|
||||
#### on(eventName | eventName[], listener)
|
||||
|
||||
Subscribe to one or more events.
|
||||
|
||||
Returns an unsubscribe method.
|
||||
|
||||
Using the same listener multiple times for the same event will result in only one method call per emitted event.
|
||||
|
||||
```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
|
||||
|
||||
Emittery exports some symbols which represent custom events that can be passed to `Emitter.on` and similar methods.
|
||||
|
||||
- `Emittery.listenerAdded` - Fires when an event listener was added.
|
||||
- `Emittery.listenerRemoved` - Fires when an event listener was removed.
|
||||
|
||||
```js
|
||||
const Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
|
||||
emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
|
||||
console.log(listener);
|
||||
//=> data => {}
|
||||
|
||||
console.log(eventName);
|
||||
//=> '🦄'
|
||||
});
|
||||
|
||||
emitter.on('🦄', data => {
|
||||
// Handle data
|
||||
});
|
||||
```
|
||||
|
||||
###### Listener data
|
||||
|
||||
- `listener` - The listener that was added.
|
||||
- `eventName` - The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
|
||||
|
||||
Only events that are not of this type are able to trigger these events.
|
||||
|
||||
##### listener(data)
|
||||
|
||||
#### off(eventName | eventName[], listener)
|
||||
|
||||
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 | eventName[])
|
||||
|
||||
Subscribe to one or more events only once. It will be unsubscribed after the first event.
|
||||
|
||||
Returns a promise for the event data when `eventName` is emitted.
|
||||
|
||||
```js
|
||||
const Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
|
||||
emitter.once('🦄').then(data => {
|
||||
console.log(data);
|
||||
//=> '🌈'
|
||||
});
|
||||
emitter.once(['🦄', '🐶']).then(data => {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
emitter.emit('🦄', '🌈'); // log => '🌈' x2
|
||||
emitter.emit('🐶', '🍖'); // nothing happens
|
||||
```
|
||||
|
||||
#### events(eventName)
|
||||
|
||||
Get an async iterator which buffers data each time an event is emitted.
|
||||
|
||||
Call `return()` on the iterator to remove the subscription.
|
||||
|
||||
```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
|
||||
});
|
||||
```
|
||||
|
||||
In practice, you would usually consume the events using the [for await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) statement. In that case, to revoke the subscription simply break the loop.
|
||||
|
||||
```js
|
||||
const Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
const iterator = emitter.events('🦄');
|
||||
|
||||
emitter.emit('🦄', '🌈1'); // Buffered
|
||||
emitter.emit('🦄', '🌈2'); // Buffered
|
||||
|
||||
// In an async context.
|
||||
for await (const data of iterator) {
|
||||
if (data === '🌈2') {
|
||||
break; // Revoke the subscription when we see the value '🌈2'.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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?)
|
||||
|
||||
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
|
||||
|
||||
Returns a promise that resolves when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
|
||||
|
||||
#### emitSerial(eventName, data?)
|
||||
|
||||
Same as above, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
|
||||
|
||||
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
|
||||
|
||||
#### onAny(listener)
|
||||
|
||||
Subscribe to be notified about any event.
|
||||
|
||||
Returns a method to unsubscribe.
|
||||
|
||||
##### listener(eventName, data)
|
||||
|
||||
#### offAny(listener)
|
||||
|
||||
Remove an `onAny` subscription.
|
||||
|
||||
#### anyEvent()
|
||||
|
||||
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
|
||||
|
||||
Call `return()` on the iterator to remove the subscription.
|
||||
|
||||
```js
|
||||
const Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery();
|
||||
const iterator = emitter.anyEvent();
|
||||
|
||||
emitter.emit('🦄', '🌈1'); // Buffered
|
||||
emitter.emit('🌟', '🌈2'); // Buffered
|
||||
|
||||
iterator.next()
|
||||
.then(({value, done}) => {
|
||||
// done === false
|
||||
// value is ['🦄', '🌈1']
|
||||
return iterator.next();
|
||||
})
|
||||
.then(({value, done}) => {
|
||||
// done === false
|
||||
// value is ['🌟', '🌈2']
|
||||
// Revoke subscription
|
||||
return iterator.return();
|
||||
})
|
||||
.then(({done}) => {
|
||||
// done === true
|
||||
});
|
||||
```
|
||||
|
||||
In the same way as for `events`, you can subscribe by using the `for await` statement
|
||||
|
||||
#### clearListeners(eventNames?)
|
||||
|
||||
Clear all event listeners on the instance.
|
||||
|
||||
If `eventNames` is given, only the listeners for that events are cleared.
|
||||
|
||||
#### listenerCount(eventNames?)
|
||||
|
||||
The number of listeners for the `eventNames` or all events if not specified.
|
||||
|
||||
#### bindMethods(target, methodNames?)
|
||||
|
||||
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
|
||||
|
||||
```js
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const object = {};
|
||||
|
||||
new Emittery().bindMethods(object);
|
||||
|
||||
object.emit('event');
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
The default `Emittery` class does not let you type allowed event names and their associated data. However, you can use `Emittery.Typed` with generics:
|
||||
|
||||
```ts
|
||||
import Emittery = require('emittery');
|
||||
|
||||
const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
|
||||
|
||||
emitter.emit('open');
|
||||
emitter.emit('value', 'foo\n');
|
||||
emitter.emit('value', 1); // TS compilation error
|
||||
emitter.emit('end'); // TS compilation error
|
||||
```
|
||||
|
||||
### Emittery.mixin(emitteryPropertyName, methodNames?)
|
||||
|
||||
A decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
|
||||
|
||||
```ts
|
||||
import Emittery = require('emittery');
|
||||
|
||||
@Emittery.mixin('emittery')
|
||||
class MyClass {}
|
||||
|
||||
const instance = new MyClass();
|
||||
|
||||
instance.emit('event');
|
||||
```
|
||||
|
||||
## Scheduling details
|
||||
|
||||
Listeners are not invoked for events emitted *before* the listener was added. Removing a listener will prevent that listener from being invoked, even if events are in the process of being (asynchronously!) emitted. This also applies to `.clearListeners()`, which removes all listeners. Listeners will be called in the order they were added. So-called *any* listeners are called *after* event-specific listeners.
|
||||
|
||||
Note that when using `.emitSerial()`, a slow listener will delay invocation of subsequent listeners. It's possible for newer events to overtake older ones.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How is this different than the built-in `EventEmitter` in Node.js?
|
||||
|
||||
There are many things to not like about `EventEmitter`: its huge API surface, synchronous event emitting, magic error event, flawed memory leak detection. Emittery has none of that.
|
||||
|
||||
### Isn't `EventEmitter` synchronous for a reason?
|
||||
|
||||
Mostly backwards compatibility reasons. The Node.js team can't break the whole ecosystem.
|
||||
|
||||
It also allows silly code like this:
|
||||
|
||||
```js
|
||||
let unicorn = false;
|
||||
|
||||
emitter.on('🦄', () => {
|
||||
unicorn = true;
|
||||
});
|
||||
|
||||
emitter.emit('🦄');
|
||||
|
||||
console.log(unicorn);
|
||||
//=> true
|
||||
```
|
||||
|
||||
But I would argue doing that shows a deeper lack of Node.js and async comprehension and is not something we should optimize for. The benefit of async emitting is much greater.
|
||||
|
||||
### Can you support multiple arguments for `emit()`?
|
||||
|
||||
No, just use [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment):
|
||||
|
||||
```js
|
||||
emitter.on('🦄', ([foo, bar]) => {
|
||||
console.log(foo, bar);
|
||||
});
|
||||
|
||||
emitter.emit('🦄', [foo, bar]);
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
|
Loading…
Add table
Add a link
Reference in a new issue