From 6fa85f3d1ec6e9368bf2ee981a0c08c8dc898c19 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sun, 6 Mar 2022 07:38:05 +0100 Subject: [PATCH] Extract emitter and improve coverage --- src/watch/WatchEmitter.ts | 48 +++++++++++++++++++++++++++++++++++++++ src/watch/watch-proxy.ts | 45 +----------------------------------- 2 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 src/watch/WatchEmitter.ts diff --git a/src/watch/WatchEmitter.ts b/src/watch/WatchEmitter.ts new file mode 100644 index 00000000000..edc68f85471 --- /dev/null +++ b/src/watch/WatchEmitter.ts @@ -0,0 +1,48 @@ +import { EventEmitter } from 'events'; + +type PromiseReturn any> = ( + ...args: Parameters +) => Promise>; + +export class WatchEmitter< + T extends { [event: string]: (...args: any) => any } +> extends EventEmitter { + private awaitedHandlers: { + [K in keyof T]?: PromiseReturn[]; + } = Object.create(null); + + constructor() { + super(); + // Allows more than 10 bundles to be watched without + // showing the `MaxListenersExceededWarning` to the user. + this.setMaxListeners(Infinity); + } + + // Will be overwritten by Rollup + async close(): Promise {} + + emitAndAwait( + event: K, + ...args: Parameters + ): Promise[]> { + this.emit(event as string, ...(args as any[])); + return Promise.all(this.getHandlers(event).map(handler => handler(...args))); + } + + onCurrentAwaited( + event: K, + listener: (...args: Parameters) => Promise> + ): this { + this.getHandlers(event).push(listener); + return this; + } + + removeAwaited(): this { + this.awaitedHandlers = {}; + return this; + } + + private getHandlers(event: K): PromiseReturn[] { + return this.awaitedHandlers[event] || (this.awaitedHandlers[event] = []); + } +} diff --git a/src/watch/watch-proxy.ts b/src/watch/watch-proxy.ts index 487a3365708..b7371b0398a 100644 --- a/src/watch/watch-proxy.ts +++ b/src/watch/watch-proxy.ts @@ -1,53 +1,10 @@ -import { EventEmitter } from 'events'; import type { RollupWatcher } from '../rollup/types'; import { ensureArray } from '../utils/ensureArray'; import { errInvalidOption, error } from '../utils/error'; import type { GenericConfigObject } from '../utils/options/options'; +import { WatchEmitter } from './WatchEmitter'; import { loadFsEvents } from './fsevents-importer'; -class WatchEmitter any }> extends EventEmitter { - private awaitedHandlers: { - [K in keyof T]?: ((...args: Parameters) => Promise>)[]; - } = {}; - - constructor() { - super(); - // Allows more than 10 bundles to be watched without - // showing the `MaxListenersExceededWarning` to the user. - this.setMaxListeners(Infinity); - } - - // Will be overwritten by Rollup - async close(): Promise {} - - emitAndAwait( - event: K, - ...args: Parameters - ): Promise[]> { - this.emit(event as string, ...(args as any[])); - const handlers = this.awaitedHandlers[event]; - if (!handlers) return Promise.resolve([]); - return Promise.all(handlers.map(handler => handler(...args))); - } - - onCurrentAwaited( - event: K, - listener: (...args: Parameters) => Promise> - ): this { - let handlers = this.awaitedHandlers[event]; - if (!handlers) { - handlers = this.awaitedHandlers[event] = []; - } - handlers.push(listener); - return this; - } - - removeAwaited(): this { - this.awaitedHandlers = {}; - return this; - } -} - export default function watch(configs: GenericConfigObject[] | GenericConfigObject): RollupWatcher { const emitter = new WatchEmitter() as RollupWatcher; const configArray = ensureArray(configs);