mafintosh / streamx

An iteration of the Node.js core streams with a series of improvements.
MIT License
226 stars 16 forks source link

Make read ahead configurable #84

Closed mafintosh closed 9 months ago

mafintosh commented 9 months ago

Allows for disabling read-ahead meaning a _read only happens when someone asks for it and gets a cache-miss.

This is useful for interactive async iterators like

import { Readable } from 'streamx'

const stream = new Readable({
  readAhead: false,
  read (cb) {
    this.push(new Date())
    cb(null)
  }
})

for await (const time of stream) {
  console.log(time) // poll is requested just in time rather than ahead of time
  await new Promise(resolve => setTimeout(resolve, 5000))
}
vweevers commented 9 months ago

Doesn't highWaterMark: 0 achieve that?

mafintosh commented 9 months ago

hwm 0 makes it not read anything ever as you are telling it to never grow the buffer

mafintosh commented 9 months ago

But if that’s what node makes it mean we can change the flag to that for sure

vweevers commented 9 months ago

if that’s what node makes it mean

Yeah, I often use that pattern in tests. Demo:

const { Readable } = require('stream')

async function main () {
  const stream = new Readable({
    objectMode: true,
    highWaterMark: 0,
    read (size) {
      console.log('read', size)
      this.push(new Date())
    }
  })

  for await (const time of stream) {
    console.log(time)
    await new Promise(resolve => setTimeout(resolve, 5000))
  }
}

main()
> node test.js
read 0
read 0
2024-02-17T12:39:31.308Z
2024-02-17T12:39:31.309Z
read 0
2024-02-17T12:39:41.332Z
read 0
2024-02-17T12:39:46.348Z
read 0
2024-02-17T12:39:51.351Z

Except for the first read, apparently.

mafintosh commented 9 months ago

Ya thats fair, i'll just change the option to that.

mafintosh commented 9 months ago

Thanks for the assist @vweevers