var pl = require('pull-level')var pull = require('pull-stream')var db = require('levelup')('/tmp/pull-level-example')pull(pl.read(db), pull.collect(console.log))
read items in database, plus realtime changes
pull( pl.read(db, {live: true}), //log data as it comes, //because tail will keep the connection open //so we'll never see the end otherwise. pull.through(console.log), //note, pull-streams will not drain unless something is //pulling the data through, so we have to add drain //even though the data we want is coming from pull.through() pull.drain())
We want to keep a realtime count of everything in the database.
When ever something is inserted, we increment. But, we need
to check the records that are currently in the database.
Since it takes some time to scan the database, we need to make sure
we have done that before giving an answer. We can read it all with
one stream, using {sync: true} to be notified of when we have read out all the old records.
First all the old records are read from the non-live stream,
then you get one {sync: true} element, then all the new item.
var sum = 0, ready = false, waiting = []//call get count to know sfunction getSum (cb) { if(!ready) waiting.push(cb) else cb(null, sum)}pull( pl.read(db, {sync: true}), pull.drain(function (op) { if(op.sync) { //if we see a data element with this it means ready = true while(waiting.length) waiting.shift()(null, count) } //increment our counter! if(Number.isFinite