Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Merge branch 'stable' to 'dev' #4

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> **⚠️ Warning:** The @discordiumjs/emitter package will be released alongside discordium.js and is not currently available on npm. For now, it cannot be downloaded from npm.

# @discordiumjs/emitter

✨ A modern, powerful, and lightweight EventEmitter designed for seamless integration with TypeScript projects, built to complement **discordium.js**.
Expand All @@ -23,6 +25,9 @@ yarn add @discordiumjs/emitter

# pnpm
pnpm add @discordiumjs/emitter

# bun
bun add @discordiumjs/emitter
```

---
Expand Down
57 changes: 28 additions & 29 deletions lib/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,36 +160,35 @@ export default class Emitter<L extends ListenerSignature<L> = ListenerMap> {
* @param args - The arguments to pass to the listener callbacks.
* @returns A set of return values from the listener callbacks.
*/
public emit<K extends keyof L>(
eventName: K,
...args: Parameters<L[K]>
): Set<L[keyof L]> {
let returnVal: Set<L[keyof L]> = new Set();
if (this.listeners.has(eventName)) {
for (const callback of this.listeners.get(eventName) as Set<L[keyof L]>) {
if (callback.listenerData?.suspended) continue;

const callbackRes: any = callback(...args);

public emit<K extends keyof L>(eventName: K, ...args: Parameters<L[K]>): Set<ReturnType<L[keyof L]>>{
let returnVal: Set<ReturnType<L[keyof L]>> = new Set();
if(this.listeners.has(eventName)){
for(const callback of this.listeners.get(eventName) as Set<L[keyof L]>){
if(callback.listenerData?.suspended) continue;

const callbackRes: ReturnType<L[keyof L]> = callback(...args);

if(isPromiseLike(callbackRes)){
callbackRes.then(() => {
Object.defineProperty(callback.listenerData, "listened", {
value: true
})
if(callback.listenerData?.once) this.off(eventName, callback);
returnVal.add(callbackRes);
})
} else {
Object.defineProperty(callback.listenerData, "listened", {
value: true
})
if(callback.listenerData?.once) this.off(eventName, callback);
returnVal.add(callbackRes);
}
}
}
return returnVal;
}

if (isPromiseLike(callbackRes)) {
callbackRes.then(() => {
Object.defineProperty(callback.listenerData, 'listened', {
value: true,
});
if (callback.listenerData?.once) this.off(eventName, callback);
returnVal.add(callbackRes);
});
} else {
Object.defineProperty(callback.listenerData, 'listened', {
value: true,
});
if (callback.listenerData?.once) this.off(eventName, callback);
returnVal.add(callbackRes);
}
}
}
return returnVal;
}

/**
* Checks if the specified listener exists for a given event.
Expand Down