Skip to content

Commit

Permalink
Implemented Type Integration for TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
HoloPanio committed Jul 21, 2021
1 parent 556c6be commit f167c2c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 45 deletions.
12 changes: 10 additions & 2 deletions dist/eventra.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ class Eventra {
}
on(eventName, listener) {
this._listeners.add(eventName, listener);
return;
}
once(eventName, listener) {
this._singularListeners.add(eventName, listener);
return;
}
prependListener(eventName, listener) {
this._listeners.prepend(eventName, listener);
return;
}
prependOnceListener(eventName, listener) {
this._singularListeners.prepend(eventName, listener);
return;
}
removeAllListeners(eventName) {
const listeners = typeof eventName == 'string' ? [eventName] : eventName;
removeAllListeners(...eventName) {
const listeners = [...eventName];
listeners.map(en => {
this._listeners.removeEvent(en);
this._singularListeners.removeEvent(en);
Expand All @@ -76,6 +80,10 @@ class Eventra {
}
}
exports.Eventra = Eventra;
const ev = new Eventra();
ev.on("hello", (msg) => {
console.log("Recieved greeting:", msg);
});


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/eventra.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions scripts/deploy.mjs

This file was deleted.

33 changes: 19 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import ListenerArray from "./lib/ListenerArray";
import { ListenerCallback } from "./types";
import { DefaultListener, ListenerSignature } from "./types/events";
import { ListenerCallback } from "./types/types";

export class Eventra {
export class Eventra<Events extends ListenerSignature<Events> = DefaultListener> {

private _listeners = new ListenerArray({ mode: "recurring" });
private _singularListeners = new ListenerArray({ mode: "once" });

addListener = this.on;
off = this.removeListener;

emit(event: string, ...args) {
emit<E extends keyof Events>(event: E, ...args: Parameters<Events[E]>) {
this._listeners.executeEvent(event, ...args);
this._singularListeners.executeEvent(event, ...args)
}

eventNames(): string[] {
let finalNamesArray: string[] = []
eventNames<E extends keyof Events>(): E[] {
let finalNamesArray: E[] = []

this._listeners.storage.map((val, key) => {
if (!finalNamesArray.includes(key)) finalNamesArray.push(key);
Expand All @@ -29,14 +30,14 @@ export class Eventra {
}


listenerCount(eventName: string): number {
listenerCount<E extends keyof Events>(eventName: E): number {
const recurring = this._listeners.countListeners(eventName);
const singular = this._singularListeners.countListeners(eventName);

return (recurring + singular);
}

listeners(eventName: string) {
listeners<E extends keyof Events>(eventName: E) {
let recurring = this._listeners.fetchListeners(eventName);
let singular = this._singularListeners.fetchListeners(eventName);

Expand All @@ -47,26 +48,30 @@ export class Eventra {
}


on(eventName: string, listener: ListenerCallback) {
on<E extends keyof Events>(eventName: E, listener: Events[E]): void {
this._listeners.add(eventName, listener);
return;
}

once(eventName: string, listener: ListenerCallback) {
once<E extends keyof Events>(eventName: E, listener: Events[E]): void {
this._singularListeners.add(eventName, listener);
return;
}


prependListener(eventName: string, listener: ListenerCallback) {
prependListener<E extends keyof Events>(eventName: E, listener: Events[E]): void {
this._listeners.prepend(eventName, listener);
return;
}

prependOnceListener(eventName: string, listener: ListenerCallback) {
prependOnceListener<E extends keyof Events>(eventName: E, listener: Events[E]): void {
this._singularListeners.prepend(eventName, listener);
return;
}


removeAllListeners(eventName: string | string[]): this {
const listeners = typeof eventName == 'string' ? [ eventName ] : eventName;
removeAllListeners<E extends keyof Events>(...eventName: E[]): this {
const listeners: E[] = [...eventName];

listeners.map(en => {
this._listeners.removeEvent(en);
Expand All @@ -76,7 +81,7 @@ export class Eventra {
return this;
}

removeListener(eventName: string, listener: ListenerCallback): this {
removeListener<E extends keyof Events>(eventName: E, listener: Events[E]): this {
this._listeners.removeListener(eventName, listener);
this._singularListeners.removeListener(eventName, listener);

Expand Down
Loading

0 comments on commit f167c2c

Please sign in to comment.