From da0c69f7ab16ddf6dd488cfed5844d1e6fa10366 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 3 Oct 2022 03:00:00 -0700 Subject: [PATCH] fix: use EventTarget instead of EventEmitter (#27) Remove use of node API where a pure-js alternative exists. --- packages/it-parallel/index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/it-parallel/index.js b/packages/it-parallel/index.js index 9c46ebed..c3b32d77 100644 --- a/packages/it-parallel/index.js +++ b/packages/it-parallel/index.js @@ -1,7 +1,7 @@ +/* global EventTarget Event */ 'use strict' const defer = require('p-defer') -const EventEmitter = require('events').EventEmitter /** * @template T @@ -12,6 +12,8 @@ const EventEmitter = require('events').EventEmitter * @property {T} value */ +const CustomEvent = globalThis.CustomEvent || Event + /** * Takes an (async) iterator that emits promise-returning functions, * invokes them in parallel and emits the results as they become available but @@ -32,7 +34,7 @@ async function * parallel (source, options = {}) { } const ordered = options.ordered == null ? false : options.ordered - const emitter = new EventEmitter() + const emitter = new EventTarget() /** @type {Operation[]}} */ const ops = [] @@ -42,7 +44,7 @@ async function * parallel (source, options = {}) { let sourceErr let opErred = false - emitter.on('task-complete', () => { + emitter.addEventListener('task-complete', () => { resultAvailable.resolve() }) @@ -71,19 +73,19 @@ async function * parallel (source, options = {}) { op.done = true op.ok = true op.value = result - emitter.emit('task-complete') + emitter.dispatchEvent(new CustomEvent('task-complete')) }, err => { op.done = true op.err = err - emitter.emit('task-complete') + emitter.dispatchEvent(new CustomEvent('task-complete')) }) } sourceFinished = true - emitter.emit('task-complete') + emitter.dispatchEvent(new CustomEvent('task-complete')) } catch (err) { sourceErr = err - emitter.emit('task-complete') + emitter.dispatchEvent(new CustomEvent('task-complete')) } })