I'm trying Rxjs 5 to simplify some Node.js nested callbacks. I need to read a directory (fs.readdir), then read stats of each file (fs.stats) and parse them if they were modified since last sync.
The following code works but I find it a bit odd and not "the rxjs way" because of the first switchMap which is too big!
const fs = require('fs');
const path = require('path');
const { Observable } = require('rxjs');
const lastSync = new Date(2017, 01, 01);
const pathToFolder = '/any/path/';
Observable.bindNodeCallback(fs.readdir)(pathToFolder)
.switchMap((files) => {
const array = files.map((fileName) => {
return Observable.zip(
Observable.of(fileName),
Observable.bindNodeCallback(fs.stat)(path.join(pathToFolder, fileName))
);
});
return Observable.concat(...array);
})
.filter(([fileName, stats]) => stats.mtime.getTime() > lastSync.getTime())
.subscribe(([fileName, stats]) => parseFile(fileName));
function parseFile(fileName) { /* ... */ }
How can I improve it?