Skip to content

Commit da0c69f

Browse files
author
Alan Shaw
authored
fix: use EventTarget instead of EventEmitter (#27)
Remove use of node API where a pure-js alternative exists.
1 parent cfc852b commit da0c69f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/it-parallel/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
/* global EventTarget Event */
12
'use strict'
23

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

66
/**
77
* @template T
@@ -12,6 +12,8 @@ const EventEmitter = require('events').EventEmitter
1212
* @property {T} value
1313
*/
1414

15+
const CustomEvent = globalThis.CustomEvent || Event
16+
1517
/**
1618
* Takes an (async) iterator that emits promise-returning functions,
1719
* invokes them in parallel and emits the results as they become available but
@@ -32,7 +34,7 @@ async function * parallel (source, options = {}) {
3234
}
3335

3436
const ordered = options.ordered == null ? false : options.ordered
35-
const emitter = new EventEmitter()
37+
const emitter = new EventTarget()
3638

3739
/** @type {Operation<T>[]}} */
3840
const ops = []
@@ -42,7 +44,7 @@ async function * parallel (source, options = {}) {
4244
let sourceErr
4345
let opErred = false
4446

45-
emitter.on('task-complete', () => {
47+
emitter.addEventListener('task-complete', () => {
4648
resultAvailable.resolve()
4749
})
4850

@@ -71,19 +73,19 @@ async function * parallel (source, options = {}) {
7173
op.done = true
7274
op.ok = true
7375
op.value = result
74-
emitter.emit('task-complete')
76+
emitter.dispatchEvent(new CustomEvent('task-complete'))
7577
}, err => {
7678
op.done = true
7779
op.err = err
78-
emitter.emit('task-complete')
80+
emitter.dispatchEvent(new CustomEvent('task-complete'))
7981
})
8082
}
8183

8284
sourceFinished = true
83-
emitter.emit('task-complete')
85+
emitter.dispatchEvent(new CustomEvent('task-complete'))
8486
} catch (err) {
8587
sourceErr = err
86-
emitter.emit('task-complete')
88+
emitter.dispatchEvent(new CustomEvent('task-complete'))
8789
}
8890
})
8991

0 commit comments

Comments
 (0)