I am facing some trouble to use Meteor code inside of a stream.
Usually I would just use Future / wait, and pull all the info in need from the stream back into the “main level” and just code normally in there like this.
Future = Meteor.npmRequire('fibers/future')
Readdirp = Meteor.npmRequire('readdirp') // Recursive read dir
console.log("reading files in", dir)
const files = []
const future = new Future ()
const files_stream = Readdirp ({ root : "\tmp", entryType : 'files' })
files_stream.on('data', function (info) { files.push({ name : info.name, file : info.fullPath }) })
files_stream.on('end', function () { future['return']() })
future.wait()
console.log(files.length, "files to be processed")
But in this case I need the stream to check data from a meteor collection and act accordingly. The library I am using has an option to specify a filter function
// ERROR : Can't run without a fiber
const files_stream = Readdirp ({
root : "\tmp",
entryType : 'files',
fileFilter : info => FilesAlreadyRead.findOne(info.name)
})
I thought about moving the filter to the ‘data’ event manager but I don’t know what code to add there to get the collection to run on the “main level”
// ERROR : Can't run without a fiber
files_stream.on('data', function (info) {
if (!FilesAlreadyRead.findOne(info.name)) files.push({ name : info.name, file : info.fullPath })
})