Add tests. Add exponential backoff contexts. Update error handling. Imrove Event Parser compatibility. Simplify SSE Session. Simplify Async Iterator.

This commit is contained in:
2026-07-21 12:44:33 +10:00
parent fb64b1b2ea
commit e12698ab6f
20 changed files with 3123 additions and 408 deletions

View File

@@ -1,8 +1,3 @@
// TODO: You'll probably want to use WeakRef's here.
// NOTE: Looked into the WeakRefs, but they are extremely challenging to work well without side-effects.
// Things like anonymous functions and closures will get cleaned up immediately and the side-effects from a developer's perspective
// are likely more painful than leaving event listener cleanup to the developer.
// - Harvmaster 2026-05-24
export type EventMap = Record<string, unknown>;
type Listener<T> = (detail: T) => void;
@@ -181,13 +176,14 @@ export class EventEmitter<T extends EventMap> {
let timeoutId: ReturnType<typeof setTimeout> | undefined;
// Create a listener function.
const listener = (payload: T[K]) => {
const listener = (payload: T[K]): void => {
if (predicate(payload)) {
// Clean up
this.off(type, listener);
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
resolve(payload);
}
};
@@ -219,10 +215,11 @@ export class EventEmitter<T extends EventMap> {
let timeout: ReturnType<typeof setTimeout>;
return (detail: T[K]) => {
// If the timeout is not null, clear it.
if (timeout !== null) {
// If a debounce timer is already pending, clear it before scheduling the next one.
if (timeout !== undefined) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
func(detail);
}, wait);