All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2601_2700.s2694_event_emitter.solution.ts Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
// #Medium #2023_07_29_Time_45_ms_(99.58%)_Space_44.6_MB_(72.08%)

type Callback = (...args: any[]) => any
type Subscription = {
    unsubscribe: () => void
}

class EventEmitter {
    subs: Record = {}

    subscribe(eventName: string, callback: Callback): Subscription {
        if (!this.subs[eventName]) this.subs[eventName] = []
        const idx = this.subs[eventName].push(callback) - 1
        return {
            unsubscribe: () => this.subs[eventName].splice(idx, 1),
        }
    }

    emit(eventName: string, args: any[] = []): any[] {
        return this.subs[eventName]?.map((callback) => callback(...args)) || []
    }
}

/*
 * const emitter = new EventEmitter();
 *
 * // Subscribe to the onClick event with onClickCallback
 * function onClickCallback() { return 99 }
 * const sub = emitter.subscribe('onClick', onClickCallback);
 *
 * emitter.emit('onClick'); // [99]
 * sub.unsubscribe(); // undefined
 * emitter.emit('onClick'); // []
 */

export { EventEmitter }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy