How to use the automerge.getVClock function in automerge

To help you get started, we’ve selected a few automerge examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github automerge / mpl / src / mpl / network / delta-router.js View on Github external
sendVectorClockToPeer(peer) {
    let state = this.getAutomergeCB()
    let myClock = Automerge.getVClock(state)
    console.log("send vector clock to peer",myClock)
    // we definitely shuoldn't be passing "boardTitle" like this
    peer.send({ docId: state.docId, docTitle: state.boardTitle, vectorClock: myClock })
  }
github automerge / mpl / src / mpl / network / delta-router.js View on Github external
sendDeltasToPeer(peer) {
    console.log("maybe send deltas")
    let state = this.getAutomergeCB()
    let myClock = Automerge.getVClock(state)
    let theirClock = this.clocks[peer.id];

    if (theirClock) {
      let deltas = Automerge.getDeltasAfter(state, theirClock)
      if (deltas.length > 0) {
        console.log("SEND DELTAS",deltas.length)
        // we definitely shuoldn't be passing "boardTitle" like this
        peer.send({docId: state.docId, docTitle: state.boardTitle, vectorClock: myClock, deltas:deltas})
        
        // update our estimate of their clock to assume the deltas we just sent will all arrive
        this.clocks[peer.id] = this.clockMax(myClock,theirClock)
      }
    }
  }
github automerge / mpl / src / mpl / network / delta-router.js View on Github external
// try and apply deltas we receive
      if (m.deltas && m.deltas.length > 0) {
        console.log("APPLY DELTAS",m.deltas.length)
        this.applyAutomergeDeltasCB(m.deltas)
        this.broadcastVectorClock()
      }

      // and if we get a vector clock, send the peer anything they're missing
      if (m.vectorClock) { // ignore acks for all but the last send
        console.log("got vector clock from", peer.id, m.vectorClock)

        // we maintain an estimated clock that assumes messages we sent will be applied by our peer
        // POSSIBLE BUG: i haven't checked but this clock should be reset after reconnect but probably isn't!
        //let theirEstimatedClock = this.clockMax(m.vectorClock, this.clocks[peer.id] || {})
        let theirEstimatedClock = m.vectorClock // clock estimation disabled for now
        let myClock = Automerge.getVClock(state)

        if (this.isAheadOf(myClock, theirEstimatedClock)) {
          console.log("We are ahead - send deltas",peer.id)
          this.sendDeltasToPeer(peer)
        }

        // it should be safe to use the estimated clock but for this purpose m.vectorClock would work too
        if (this.isAheadOf(theirEstimatedClock, myClock)) {
          console.log("We are behind - request deltas",peer.id)
          this.sendVectorClockToPeer(peer)
        }

        // update the clock after sending to prevent exceptions above from falsely moving our
        // estimated peer clock forward
        this.clocks[peer.id] = theirEstimatedClock