Skip to content

Instantly share code, notes, and snippets.

@Sequoia
Created November 3, 2016 17:59
Show Gist options
  • Save Sequoia/0ade5d3475a784ad90fdf08b27213ed0 to your computer and use it in GitHub Desktop.
Save Sequoia/0ade5d3475a784ad90fdf08b27213ed0 to your computer and use it in GitHub Desktop.
PoC for a static blog script written in RX.js
const Rx = require('rxjs');
const debug = require('debug');
const chokidar = require('chokidar');
const dirWatcher = chokidar.watch('./_data/*.md');
const readFile = require('fs').readFile;
const readFileAsObservable = Rx.Observable.bindNodeCallback(readFile);
const newFiles = Rx.Observable.fromEvent(dirWatcher, 'add');
const changedFiles = Rx.Observable.fromEvent(dirWatcher, 'change');
const changesPlusCreation = newFiles.map(filename => {
return changedFiles
.filter(name => name === filename)
.startWith(filename);
}).mergeAll();
const fileContents = changesPlusCreation.map(f => {
return Rx.Observable.zip(
Rx.Observable.of(f),
readFileAsObservable(f, 'utf-8'),
function(name, content){
return {name, content: format(content)};
}
);
});
const contentsOfAll = fileContents.concatAll().scan(function(acc, val){
acc[val.name] = val.content;
return acc;
}, {});
contentsOfAll.debounceTime(100)
.subscribe(writeIndexPage);
function writeIndexPage(pages){
const contents = '\nINDEX\n=====\n';
const indexPage = Object.keys(pages)
.map(key => pages[key])
.reduce((out, cur) => {
return `${out}* ${cur}`;
},contents);
debug('write-index')(indexPage);
}
function format(cont){
return 'CONTENTS: ' + cont;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment