Skip to content
Marcus Hitchins edited this page Mar 28, 2018 · 10 revisions

Thoughts: get

  • check gundb
  • Fire API calls for all missing data
  • Put all returned data into gundb
  • return relevant data

set

  • check gundb if diff
    • set gundb
    • add to postList
    • onIdle(5000,1000).then(APIsync(postList))
function onIdle (itime, failAfter) {
  return new Promise(function (resolve, reject) {
    var trys = 0
    var onIdleTest = function() {
      var t = performance.now()
      setTimeout(function(){ 
        if (failAfter && trys++ > failAfter) {
          reject()
        }
        if (Math.round(performance.now() - t) === Math.round(itime)) {
          resolve()
        } else {
          onIdleTest()
        }
      }, itime)
    }
    onIdleTest()
  })
}
onIdle(5000,1000).then(APIsync)
var gun = Gun()

getter = query => {
  return new Promise(function (resolve, reject) {
    query.split(".").reduce((db,val) => {
      return db.get(val)
    }, gun).val(resolve)
  })
}
setter = (query, valueToSet) => {
  return new Promise(function (resolve, reject) {
    var oldValue
    var newValue
    getter(query).then(data => {
      oldValue = data
    }).then( _ => {
      return query.split(".").reduce((db,val) => {
        return db.get(val)
      }, gun).put(valueToSet)
    }).then( _ => {
      return getter(query).then(data => {
        newValue = data
      })
    }).then( _ => {
      if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
        // add to postList
        console.log("push to server", newValue)
      } else {
        console.log("Do not push", newValue)
      }
    }).then( _ => {
      return newValue
    })
  })
}

setter('marcus',{
  name: "Marcus",
  email: "[email protected]",
})
var russell = {name: "Russell", is: "Full stack butterfly"}
var marcus = {boss: russell}
russell.slave = marcus

// partial updates merge with existing data!
setter('marcus', marcus)
setter('marcus.is',"making a cache")

getter("marcus.boss")
getter("marcus.boss.slave.boss.is")
getter("marcus.boss.slave.is")
Clone this wiki locally