Skip to content

Commit

Permalink
Merge branch 'main' into unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Jan 19, 2024
2 parents 4d8c94a + b85c3b6 commit 07ef013
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ jobs:

strategy:
matrix:
node-version: [16.17.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [16.17.1, 18.17.1, 20.x]

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 4 additions & 1 deletion lib/multi-core-index-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ class MultiCoreIndexStream extends Readable {
#handleDrained() {
let drained = true
for (const stream of this.#streams.keys()) {
if (!stream.drained) drained = false
if (!stream.drained) {
drained = false
break
}
}
if (drained === this.#drained && !drained) return
this.#drained = drained
Expand Down
25 changes: 20 additions & 5 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ram = require('random-access-memory')
const BLOCK_LENGTH = Buffer.from('block000000').byteLength

/** @typedef {import('../../lib/types').Entry<'binary'>} Entry */
/** @typedef {import('node:events').EventEmitter} EventEmitter */

module.exports = {
create,
Expand All @@ -14,6 +15,7 @@ module.exports = {
generateFixtures,
createMultiple,
throttledDrain,
throttledIdle,
sortEntries,
logEntries,
blocksToExpected,
Expand Down Expand Up @@ -87,24 +89,37 @@ async function generateFixtures(cores, count) {
* The index stream can become momentarily drained between reads and
* appends/downloads of new data. This throttle drained will resolve only when
* the stream has remained drained for > 10ms
* @param {import('events').EventEmitter} emitter
* @param {EventEmitter} emitter
* @returns {Promise<void>}
*/
async function throttledDrain(emitter) {
function throttledDrain(emitter) {
return throttledStreamEvent(emitter, 'drained')
}

function throttledIdle(emitter) {
return throttledStreamEvent(emitter, 'idle')
}

/**
* @param {EventEmitter} emitter
* @param {string} eventName
* @returns {Promise<void>}
*/
function throttledStreamEvent(emitter, eventName) {
return new Promise((resolve) => {
/** @type {ReturnType<setTimeout>} */
let timeoutId

function onDrained() {
function onEvent() {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
emitter.off('drained', onDrained)
emitter.off(eventName, onEvent)
emitter.off('indexing', onIndexing)
resolve()
}, 10)
}

emitter.on('drained', onDrained)
emitter.on(eventName, onEvent)
emitter.on('indexing', onIndexing)
function onIndexing() {
clearTimeout(timeoutId)
Expand Down

0 comments on commit 07ef013

Please sign in to comment.