added option for servers that want to propagate applied operations (aka the websockets connector)
This commit is contained in:
		
							parent
							
								
									ae12b087e7
								
							
						
					
					
						commit
						58a612eaa1
					
				@ -20,6 +20,7 @@ module.exports = function (Y) {
 | 
			
		||||
      } else {
 | 
			
		||||
        throw new Error("Role must be either 'master' or 'slave'!")
 | 
			
		||||
      }
 | 
			
		||||
      this.y.db.forwardAppliedOperations = opts.forwardAppliedOperations || false
 | 
			
		||||
      this.role = opts.role
 | 
			
		||||
      this.connections = {}
 | 
			
		||||
      this.isSynced = false
 | 
			
		||||
@ -221,6 +222,17 @@ module.exports = function (Y) {
 | 
			
		||||
            this.send(client, m)
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if (this.y.db.forwardAppliedOperations) {
 | 
			
		||||
          var delops = m.ops.filter(function (o) {
 | 
			
		||||
            return o.struct === 'Delete'
 | 
			
		||||
          })
 | 
			
		||||
          if (delops.length > 0) {
 | 
			
		||||
            this.broadcast({
 | 
			
		||||
              type: 'update',
 | 
			
		||||
              ops: delops
 | 
			
		||||
            })
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        this.y.db.apply(m.ops)
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -16,6 +16,8 @@ module.exports = function (Y) {
 | 
			
		||||
  class AbstractDatabase {
 | 
			
		||||
    constructor (y, opts) {
 | 
			
		||||
      this.y = y
 | 
			
		||||
      // whether to broadcast all applied operations (insert & delete hook)
 | 
			
		||||
      this.forwardAppliedOperations = false
 | 
			
		||||
      // E.g. this.listenersById[id] : Array<Listener>
 | 
			
		||||
      this.listenersById = {}
 | 
			
		||||
      // Execute the next time a transaction is requested
 | 
			
		||||
 | 
			
		||||
@ -104,7 +104,8 @@ module.exports = function (Y) {
 | 
			
		||||
        yield* this.store.tryExecute.call(this, op)
 | 
			
		||||
        send.push(Y.Struct[op.struct].encode(op))
 | 
			
		||||
      }
 | 
			
		||||
      if (!this.store.y.connector.isDisconnected()) {
 | 
			
		||||
      if (!this.store.y.connector.isDisconnected()) { // TODO: && !this.store.forwardAppliedOperations (but then i don't send delete ops)
 | 
			
		||||
        // is connected, and this is not going to be send in addOperation
 | 
			
		||||
        this.store.y.connector.broadcast({
 | 
			
		||||
          type: 'update',
 | 
			
		||||
          ops: send
 | 
			
		||||
@ -483,6 +484,15 @@ module.exports = function (Y) {
 | 
			
		||||
          yield* this.garbageCollectOperation(id)
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (this.store.forwardAppliedOperations) {
 | 
			
		||||
        var ops = deletions.map(function(d){
 | 
			
		||||
          return {struct: 'Delete', target: [d[0], d[1]]}
 | 
			
		||||
        })
 | 
			
		||||
        this.store.y.connector.broadcast({
 | 
			
		||||
          type: 'update',
 | 
			
		||||
          ops: ops
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    * isGarbageCollected (id) {
 | 
			
		||||
      var n = yield* this.ds.findWithUpperBound(id)
 | 
			
		||||
@ -517,6 +527,13 @@ module.exports = function (Y) {
 | 
			
		||||
    }
 | 
			
		||||
    * addOperation (op) {
 | 
			
		||||
      yield* this.os.put(op)
 | 
			
		||||
      if (!this.store.y.connector.isDisconnected() && this.store.forwardAppliedOperations) {
 | 
			
		||||
        // is connected, and this is not going to be send in addOperation
 | 
			
		||||
        this.store.y.connector.broadcast({
 | 
			
		||||
          type: 'update',
 | 
			
		||||
          ops: [op]
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    * getOperation (id) {
 | 
			
		||||
      return yield* this.os.find(id)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user