@libp2p/interface
Advanced tools
Comparing version 0.1.3-ab2c1f67 to 0.1.3-b5a808af
@@ -16,3 +16,14 @@ export interface EventCallback<EventType> { | ||
*/ | ||
export declare class EventEmitter<EventMap extends Record<string, any>> extends EventTarget { | ||
export interface TypedEventTarget<EventMap extends Record<string, any>> extends EventTarget { | ||
addEventListener<K extends keyof EventMap>(type: K, listener: EventHandler<EventMap[K]> | null, options?: boolean | AddEventListenerOptions): void; | ||
listenerCount(type: string): number; | ||
removeEventListener<K extends keyof EventMap>(type: K, listener?: EventHandler<EventMap[K]> | null, options?: boolean | EventListenerOptions): void; | ||
removeEventListener(type: string, listener?: EventHandler<Event>, options?: boolean | EventListenerOptions): void; | ||
safeDispatchEvent<Detail>(type: keyof EventMap, detail: CustomEventInit<Detail>): boolean; | ||
} | ||
/** | ||
* An implementation of a typed event target | ||
* etc | ||
*/ | ||
export declare class TypedEventEmitter<EventMap extends Record<string, any>> extends EventTarget implements TypedEventTarget<EventMap> { | ||
#private; | ||
@@ -29,2 +40,3 @@ listenerCount(type: string): number; | ||
}; | ||
export { TypedEventEmitter as EventEmitter }; | ||
//# sourceMappingURL=events.d.ts.map |
/** | ||
* Adds types to the EventTarget class. Hopefully this won't be necessary forever. | ||
* | ||
* https://github.com/microsoft/TypeScript/issues/28357 | ||
* https://github.com/microsoft/TypeScript/issues/43477 | ||
* https://github.com/microsoft/TypeScript/issues/299 | ||
* An implementation of a typed event target | ||
* etc | ||
*/ | ||
export class EventEmitter extends EventTarget { | ||
export class TypedEventEmitter extends EventTarget { | ||
#listeners = new Map(); | ||
@@ -70,2 +66,4 @@ listenerCount(type) { | ||
export const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill; | ||
// TODO: remove this in v1 | ||
export { TypedEventEmitter as EventEmitter }; | ||
//# sourceMappingURL=events.js.map |
@@ -18,3 +18,3 @@ /** | ||
import type { ContentRouting } from './content-routing/index.js'; | ||
import type { EventEmitter } from './events.js'; | ||
import type { TypedEventTarget } from './events.js'; | ||
import type { KeyChain } from './keychain/index.js'; | ||
@@ -275,3 +275,3 @@ import type { Metrics } from './metrics/index.js'; | ||
*/ | ||
export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, EventEmitter<Libp2pEvents<T>> { | ||
export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, TypedEventTarget<Libp2pEvents<T>> { | ||
/** | ||
@@ -278,0 +278,0 @@ * The PeerId is a unique identifier for a node on the network. |
@@ -1,2 +0,2 @@ | ||
import type { EventEmitter } from '../events.js'; | ||
import type { TypedEventTarget } from '../events.js'; | ||
import type { PeerInfo } from '../peer-info/index.js'; | ||
@@ -26,4 +26,4 @@ /** | ||
} | ||
export interface PeerDiscovery extends EventEmitter<PeerDiscoveryEvents> { | ||
export interface PeerDiscovery extends TypedEventTarget<PeerDiscoveryEvents> { | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
import type { Stream } from '../connection/index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
import type { TypedEventTarget } from '../events.js'; | ||
import type { PeerId } from '../peer-id/index.js'; | ||
@@ -58,3 +58,3 @@ import type { Pushable } from 'it-pushable'; | ||
} | ||
export interface PeerStreams extends EventEmitter<PeerStreamEvents> { | ||
export interface PeerStreams extends TypedEventTarget<PeerStreamEvents> { | ||
id: PeerId; | ||
@@ -130,3 +130,3 @@ protocol: string; | ||
} | ||
export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> { | ||
export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends TypedEventTarget<Events> { | ||
/** | ||
@@ -133,0 +133,0 @@ * The global signature policy controls whether or not we sill send and receive |
import type { Connection, MultiaddrConnection } from '../connection/index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
import type { TypedEventTarget } from '../events.js'; | ||
import type { AbortOptions } from '../index.js'; | ||
@@ -12,3 +12,3 @@ import type { StreamMuxerFactory } from '../stream-muxer/index.js'; | ||
} | ||
export interface Listener extends EventEmitter<ListenerEvents> { | ||
export interface Listener extends TypedEventTarget<ListenerEvents> { | ||
/** | ||
@@ -15,0 +15,0 @@ * Start a listener |
{ | ||
"name": "@libp2p/interface", | ||
"version": "0.1.3-ab2c1f67", | ||
"version": "0.1.3-b5a808af", | ||
"description": "The interface implemented by a libp2p node", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0 OR MIT", |
@@ -18,3 +18,19 @@ export interface EventCallback<EventType> { (evt: EventType): void } | ||
*/ | ||
export class EventEmitter<EventMap extends Record<string, any>> extends EventTarget { | ||
export interface TypedEventTarget <EventMap extends Record<string, any>> extends EventTarget { | ||
addEventListener<K extends keyof EventMap>(type: K, listener: EventHandler<EventMap[K]> | null, options?: boolean | AddEventListenerOptions): void | ||
listenerCount (type: string): number | ||
removeEventListener<K extends keyof EventMap>(type: K, listener?: EventHandler<EventMap[K]> | null, options?: boolean | EventListenerOptions): void | ||
removeEventListener (type: string, listener?: EventHandler<Event>, options?: boolean | EventListenerOptions): void | ||
safeDispatchEvent<Detail>(type: keyof EventMap, detail: CustomEventInit<Detail>): boolean | ||
} | ||
/** | ||
* An implementation of a typed event target | ||
* etc | ||
*/ | ||
export class TypedEventEmitter<EventMap extends Record<string, any>> extends EventTarget implements TypedEventTarget<EventMap> { | ||
#listeners = new Map<any, Listener[]>() | ||
@@ -102,1 +118,4 @@ | ||
export const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill | ||
// TODO: remove this in v1 | ||
export { TypedEventEmitter as EventEmitter } |
@@ -19,3 +19,3 @@ /** | ||
import type { ContentRouting } from './content-routing/index.js' | ||
import type { EventEmitter } from './events.js' | ||
import type { TypedEventTarget } from './events.js' | ||
import type { KeyChain } from './keychain/index.js' | ||
@@ -307,3 +307,3 @@ import type { Metrics } from './metrics/index.js' | ||
*/ | ||
export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, EventEmitter<Libp2pEvents<T>> { | ||
export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, TypedEventTarget<Libp2pEvents<T>> { | ||
/** | ||
@@ -310,0 +310,0 @@ * The PeerId is a unique identifier for a node on the network. |
@@ -1,2 +0,2 @@ | ||
import type { EventEmitter } from '../events.js' | ||
import type { TypedEventTarget } from '../events.js' | ||
import type { PeerInfo } from '../peer-info/index.js' | ||
@@ -29,2 +29,2 @@ | ||
export interface PeerDiscovery extends EventEmitter<PeerDiscoveryEvents> {} | ||
export interface PeerDiscovery extends TypedEventTarget<PeerDiscoveryEvents> {} |
import type { Stream } from '../connection/index.js' | ||
import type { EventEmitter } from '../events.js' | ||
import type { TypedEventTarget } from '../events.js' | ||
import type { PeerId } from '../peer-id/index.js' | ||
@@ -68,3 +68,3 @@ import type { Pushable } from 'it-pushable' | ||
export interface PeerStreams extends EventEmitter<PeerStreamEvents> { | ||
export interface PeerStreams extends TypedEventTarget<PeerStreamEvents> { | ||
id: PeerId | ||
@@ -156,3 +156,3 @@ protocol: string | ||
export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> { | ||
export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends TypedEventTarget<Events> { | ||
/** | ||
@@ -159,0 +159,0 @@ * The global signature policy controls whether or not we sill send and receive |
import type { Connection, MultiaddrConnection } from '../connection/index.js' | ||
import type { EventEmitter } from '../events.js' | ||
import type { TypedEventTarget } from '../events.js' | ||
import type { AbortOptions } from '../index.js' | ||
@@ -14,3 +14,3 @@ import type { StreamMuxerFactory } from '../stream-muxer/index.js' | ||
export interface Listener extends EventEmitter<ListenerEvents> { | ||
export interface Listener extends TypedEventTarget<ListenerEvents> { | ||
/** | ||
@@ -17,0 +17,0 @@ * Start a listener |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
256281
6150