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

Make read ahead configurable #84

Merged
merged 5 commits into from
Feb 17, 2024
Merged

Make read ahead configurable #84

merged 5 commits into from
Feb 17, 2024

Conversation

mafintosh
Copy link
Owner

@mafintosh mafintosh commented Feb 17, 2024

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))
}

@mafintosh mafintosh changed the title Read ahead Make read ahead configurable Feb 17, 2024
@vweevers
Copy link

Doesn't highWaterMark: 0 achieve that?

@mafintosh
Copy link
Owner Author

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

@mafintosh
Copy link
Owner Author

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

@vweevers
Copy link

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
Copy link
Owner Author

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

@mafintosh mafintosh merged commit fb7678b into master Feb 17, 2024
3 checks passed
@mafintosh mafintosh deleted the read-ahead branch February 17, 2024 18:06
@mafintosh
Copy link
Owner Author

Thanks for the assist @vweevers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants