Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use EventTarget instead of EventEmitter #27

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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