Skip to content

Commit

Permalink
feat: export EventMap
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantiago committed Jan 19, 2023
1 parent 04a3e4d commit 97e71ad
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
29 changes: 10 additions & 19 deletions src/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
export interface TrackingTime {
passed: number;
remaining: number;
}

type Events<Payload> = {
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<Payload, K extends keyof Events<Payload>> = {
type Receivers<Payload, K extends keyof EventMap<Payload>> = {
event: K;
listener: Events<Payload>[K];
listener: EventMap<Payload>[K];
}[];

export abstract class EventEmitter<Payload> {
private receivers: Receivers<Payload, keyof Events<Payload>> = [];
private receivers: Receivers<Payload, keyof EventMap<Payload>> = [];

on<K extends keyof Events<Payload>>(event: K, listener: Events<Payload>[K]) {
on<K extends keyof EventMap<Payload>>(
event: K,
listener: EventMap<Payload>[K]
) {
this.receivers.push({ event, listener });

return () => {
Expand All @@ -30,9 +21,9 @@ export abstract class EventEmitter<Payload> {
};
}

protected emit<K extends keyof Events<Payload>>(
protected emit<K extends keyof EventMap<Payload>>(
event: K,
...args: Parameters<Events<Payload>[K]>
...args: Parameters<EventMap<Payload>[K]>
) {
const listeners = this.receivers.filter((r) => r.event === event);
listeners.forEach(({ listener }) => listener.apply(this, args));
Expand Down
13 changes: 13 additions & 0 deletions src/EventMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface TrackingTime {
passed: number;
remaining: number;
}

export type EventMap<Payload> = {
start: (trackingTime: TrackingTime) => void;
success: (payload: Payload) => void;
error: (err: unknown) => void;
timeout: () => void;
second: (trackingTime: TrackingTime) => void;
close: () => void;
};
3 changes: 2 additions & 1 deletion src/PollingConnection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter, TrackingTime } from "./EventEmitter";
import { EventEmitter } from "./EventEmitter";
import { TrackingTime } from "./EventMap";

export interface TaskOptions<Payload> {
done: (data: Payload) => void;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./EventMap";
export * from "./PollingConnection";

0 comments on commit 97e71ad

Please sign in to comment.