Skip to content

Commit

Permalink
fix: use EventTarget instead of EventEmitter (#27)
Browse files Browse the repository at this point in the history
Remove use of node API where a pure-js alternative exists.
  • Loading branch information
Alan Shaw authored Oct 3, 2022
1 parent cfc852b commit da0c69f
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/it-parallel/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global EventTarget Event */
'use strict'

const defer = require('p-defer')
const EventEmitter = require('events').EventEmitter

/**
* @template T
Expand All @@ -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
Expand All @@ -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<T>[]}} */
const ops = []
Expand All @@ -42,7 +44,7 @@ async function * parallel (source, options = {}) {
let sourceErr
let opErred = false

emitter.on('task-complete', () => {
emitter.addEventListener('task-complete', () => {
resultAvailable.resolve()
})

Expand Down Expand Up @@ -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'))
}
})

Expand Down

0 comments on commit da0c69f

Please sign in to comment.