Skip to content
New issue

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

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

Already on GitHub? # to your account

Question: match events + pause #291

Closed
phated opened this issue Sep 16, 2016 · 3 comments
Closed

Question: match events + pause #291

phated opened this issue Sep 16, 2016 · 3 comments

Comments

@phated
Copy link
Contributor

phated commented Sep 16, 2016

Hey @isaacs, I'm reimplementing a bunch of logic we have in glob-stream and I was thinking about using pause and resume inside a Readable stream, which has led me to this question. Is it possible to get multiple match events if you pause immediately inside the listener?

This is what I am thinking about doing:

var globber = new glob.Glob('**/*.js');
globber.on('match', function(filepath) {
  globber.pause();
  // push filepath into stream
});

// globber.resume() whenever read is called

Appreciate any insight you can give.

@isaacs
Copy link
Owner

isaacs commented Sep 16, 2016

Something like this?

// gs.js
var Readable = require('stream').Readable
var util = require('util')
var glob = require('./')

module.exports = GlobStream

util.inherits(GlobStream, Readable)

function GlobStream (pattern, options) {
  Readable.call(this, { objectMode: true })
  this._glob = new glob.Glob(pattern, options)
  this._glob.on('match', function (filename) {
    if (!this.push(filename)) {
      this._glob.pause()
    }
  }.bind(this))
  this._glob.on('end', function () {
    this.push(null)
  }.bind(this))
}

GlobStream.prototype._read = function () {
  this._glob.resume()
}

Usage:

var GS = require('./gs.js')
var gs = new GS('**/*.js')
gs.on('data', function (filename) {
  console.log('%j', filename)
})
gs.on('end', function () {
  console.log('END')
})

@isaacs
Copy link
Owner

isaacs commented Sep 16, 2016

Anyway, yes, you can pause() whenever, and you should get no match events while paused. But if your goal is to push it into a stream, then the stream can handle a few pushes while it's buffering.

@phated
Copy link
Contributor Author

phated commented Sep 16, 2016

@isaacs awesome! thanks!

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

No branches or pull requests

2 participants