diff --git a/package.json b/package.json index 94ec2ff..6820896 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "polling-connection", - "version": "0.0.1", + "version": "0.0.2", "description": "Event-based polling module written in TS with time tracking and aborting support.", "files": [ "dist" diff --git a/src/EventEmitter.ts b/src/EventEmitter.ts index c384cd9..376bb86 100644 --- a/src/EventEmitter.ts +++ b/src/EventEmitter.ts @@ -1,26 +1,17 @@ -export interface TrackingTime { - passed: number; - remaining: number; -} - -type Events = { - start: (trackingTime: TrackingTime) => void; - success: (payload: Payload) => void; - error: (err: unknown) => void; - timeout: () => void; - second: (trackingTime: TrackingTime) => void; - close: () => void; -}; +import { EventMap } from "./EventMap"; -type Receivers> = { +type Receivers> = { event: K; - listener: Events[K]; + listener: EventMap[K]; }[]; export abstract class EventEmitter { - private receivers: Receivers> = []; + private receivers: Receivers> = []; - on>(event: K, listener: Events[K]) { + on>( + event: K, + listener: EventMap[K] + ) { this.receivers.push({ event, listener }); return () => { @@ -30,9 +21,9 @@ export abstract class EventEmitter { }; } - protected emit>( + protected emit>( event: K, - ...args: Parameters[K]> + ...args: Parameters[K]> ) { const listeners = this.receivers.filter((r) => r.event === event); listeners.forEach(({ listener }) => listener.apply(this, args)); diff --git a/src/EventMap.ts b/src/EventMap.ts new file mode 100644 index 0000000..6c378c8 --- /dev/null +++ b/src/EventMap.ts @@ -0,0 +1,13 @@ +export interface TrackingTime { + passed: number; + remaining: number; +} + +export type EventMap = { + start: (trackingTime: TrackingTime) => void; + success: (payload: Payload) => void; + error: (err: unknown) => void; + timeout: () => void; + second: (trackingTime: TrackingTime) => void; + close: () => void; +}; diff --git a/src/PollingConnection.ts b/src/PollingConnection.ts index 05676a7..7f0bf00 100644 --- a/src/PollingConnection.ts +++ b/src/PollingConnection.ts @@ -1,4 +1,5 @@ -import { EventEmitter, TrackingTime } from "./EventEmitter"; +import { EventEmitter } from "./EventEmitter"; +import { TrackingTime } from "./EventMap"; export interface TaskOptions { done: (data: Payload) => void; diff --git a/src/index.ts b/src/index.ts index fa2c18f..0da7c0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ +export * from "./EventMap"; export * from "./PollingConnection";