
var levelup  = require('level')
//var SubLevel = require('level-sublevel')
var pull     = require('pull-stream')


var l = require('../')
var all = []

var h = require('./helper')
var tape = require('tape')

tape('live', function (t) {

  var path    = '/tmp/pull-level-read-live'
  require('rimraf').sync(path)
  var db      = levelup(path)

  var second = false

  h.timestamps(db, 10, function (err, all) {

    var i = 0
    var sync = false
    pull(
      l.read(db, {live: true, onSync: function () {
        console.log('SYNC')
        sync = true
      }}),
      h.exactly(20),
      pull.collect(function (err, ary) {
        process.nextTick(function () {
          t.notOk(err)
          t.ok(second)
          console.log("ALL", all)
          t.equal(ary.length, 20)
          t.equal(ary.length, all.length)
          t.deepEqual(ary, h.sort(ary.slice()))
          t.deepEqual(ary.map(function (e) {
            return {key: e.key, value: e.value}
          }), all)
          t.ok(sync)
          t.end()
        })
      })
    )


    setTimeout(function () {
      h.timestamps(db, 10, function (err, _all) {
        second = true
        all = all.concat(_all)
        console.log('all', all, all.length)
      })
    }, 100)
  })
})


tape('live2', function (t) {

  var path    = '/tmp/pull-level-read-live2'
  require('rimraf').sync(path)
  var db      = levelup(path)

  var second = false

  h.timestamps(db, 10, function (err, all) {

    var i = 0
    var sync = false
    pull(
      l.read(db, {tail: true, keys: false, onSync: function () {
        console.log('SYNC')
        sync = true
      }}),
      h.exactly(20),
      pull.collect(function (err, ary) {
        process.nextTick(function () {
          t.notOk(err)
          t.ok(second)
          console.log(ary)
          t.equal(ary.length, 20)
          t.equal(ary.length, all.length)

          var values = all.map(function (e) { return e.value })

          t.deepEqual(ary, values)
          t.ok(sync)
          t.end()
        })
      })
    )


    setTimeout(function () {
      h.timestamps(db, 10, function (err, _all) {
        second = true
        all = all.concat(_all)
//        console.log('all', all, all.length)
      })
    }, 100)
  })
})


@@ -2232,28 +2232,1076 @@
     %7D)%0A    %7D, 100)%0A  %7D)%0A%7D)%0A%0A
+%0Atape('live, sync:true', function (t) %7B%0A  var path    = '/tmp/pull-level-read-live3'%0A  require('rimraf').sync(path)%0A  var db      = levelup(path)%0A%0A  var second = false%0A%0A  h.timestamps(db, 10, function (err, all) %7B%0A%0A    var i = 0%0A    var sync = false%0A    pull(%0A      l.read(db, %7Btail: true, keys: false, sync: true%7D),%0A      pull.filter(function (data) %7B%0A        if(data.sync) sync = true%0A        else return true%0A      %7D),%0A      h.exactly(20),%0A      pull.collect(function (err, ary) %7B%0A        process.nextTick(function () %7B%0A          t.notOk(err)%0A          t.ok(second)%0A          console.log(ary)%0A          t.equal(ary.length, 20)%0A          t.equal(ary.length, all.length)%0A%0A          var values = all.map(function (e) %7B return e.value %7D)%0A%0A          t.deepEqual(ary, values)%0A          t.ok(sync)%0A          t.end()%0A        %7D)%0A      %7D)%0A    )%0A%0A%0A    setTimeout(function () %7B%0A      h.timestamps(db, 10, function (err, _all) %7B%0A        second = true%0A        all = all.concat(_all)%0A//        console.log('all', all, all.length)%0A      %7D)%0A    %7D, 100)%0A  %7D)%0A%0A%7D)%0A

test for sync: true