all files / model/data/ IncrementalData.js

84.21% Statements 64/76
70.83% Branches 34/48
100% Functions 6/6
85.33% Lines 64/75
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                              4440× 1652×   4440× 4440× 4440×                   123× 123× 123× 123× 123× 123×   123×                                     1321× 1321× 1321× 1321×                     27× 27× 27× 27×                 7223× 7223×   5297× 1926× 175× 1751× 1710× 1710× 1710× 1317×       1317× 393× 379×     379× 379× 14× 14×     14×       41× 41×       7223×                       1321× 1321×     1321× 1321× 1321×   1321× 212×   85× 85×     127× 127×         1109× 1102×   59× 59×     1043× 1043×                       1321×     1321×            
import isString from '../../util/isString'
import isArray from '../../util/isArray'
import cloneDeep from '../../util/cloneDeep'
import Data from './Data'
import ObjectOperation from './ObjectOperation'
import ArrayOperation from './ArrayOperation'
import TextOperation from './TextOperation'
import CoordinateOperation from './CoordinateOperation'
 
/**
  Incremental data storage implemention.
 
  @internal
 */
class IncrementalData extends Data {
 
  /**
    Create a new node.
 
    @param {object} nodeData
    @returns {ObjectOperation} The applied operation.
   */
  create(nodeData) {
    if (nodeData._isNode) {
      nodeData = nodeData.toJSON()
    }
    let op = ObjectOperation.Create([nodeData.id], nodeData)
    this.apply(op)
    return op
  }
 
  /**
    Delete a node.
 
    @param {String} nodeId
    @returns {ObjectOperation} The applied operation.
   */
  delete(nodeId) {
    var op = null
    var node = this.get(nodeId)
    Eif (node) {
      var nodeData = node.toJSON()
      op = ObjectOperation.Delete([nodeId], nodeData)
      this.apply(op)
    }
    return op
  }
 
  /**
    Update a property incrementally.
 
    The diff can be of the following forms (depending on the updated property type):
      - String:
        - `{ insert: { offset: Number, value: Object } }`
        - `{ delete: { start: Number, end: Number } }`
      - Array:
        - `{ insert: { offset: Number, value: Object } }`
        - `{ delete: { offset: Number } }`
 
    @param {array} path
    @param {object} diff
    @returns {ObjectOperation} The applied operation.
  */
  update(path, diff) {
    var diffOp = this._getDiffOp(path, diff)
    var op = ObjectOperation.Update(path, diffOp)
    this.apply(op)
    return op
  }
 
  /**
    Set a property to a new value
 
    @param {Array} path
    @param {Object} newValue
    @returns {ObjectOperation} The applied operation.
   */
  set(path, newValue) {
    var oldValue = this.get(path)
    var op = ObjectOperation.Set(path, oldValue, newValue)
    this.apply(op)
    return op
  }
 
  /**
    Apply a given operation.
 
    @param {ObjectOperation} op
   */
  apply(op) {
    Iif (op.type === ObjectOperation.NOP) return
    else if (op.type === ObjectOperation.CREATE) {
      // clone here as the operations value must not be changed
      super.create(cloneDeep(op.val))
    } else if (op.type === ObjectOperation.DELETE) {
      super.delete(op.val.id)
    } else if (op.type === ObjectOperation.UPDATE) {
      var oldVal = this.get(op.path)
      var diff = op.diff
      if (op.propertyType === 'array') {
        Iif (! (diff._isArrayOperation) ) {
          diff = ArrayOperation.fromJSON(diff)
        }
        // array ops work inplace
        diff.apply(oldVal)
      } else if (op.propertyType === 'string') {
        Iif (!(diff._isTextOperation) ) {
          diff = TextOperation.fromJSON(diff)
        }
        var newVal = diff.apply(oldVal)
        super.set(op.path, newVal)
      } else Eif (op.propertyType === 'coordinate') {
        Iif (!(diff._isCoordinateOperation) ) {
          diff = CoordinateOperation.fromJSON(diff)
        }
        diff.apply(oldVal)
      } else {
        throw new Error("Unsupported type for operational update.")
      }
    } else Eif (op.type === ObjectOperation.SET) {
      super.set(op.path, op.val)
    } else {
      throw new Error("Illegal state.")
    }
    this.emit('operation:applied', op, this)
  }
 
  /**
    Creates proper operation based on provided node path and diff.
 
    @param {Array} path
    @param {Object} diff
    @returns {ObjectOperation} operation.
    @private
  */
  _getDiffOp(path, diff) {
    var diffOp = null
    Iif (diff.isOperation) {
      diffOp = diff
    } else {
      var value = this.get(path)
      diff = this._normalizeDiff(value, diff)
      Iif (value === null || value === undefined) {
        throw new Error('Property has not been initialized: ' + JSON.stringify(path))
      } else if (isString(value)) {
        switch (diff.type) {
          case 'delete': {
            diffOp = TextOperation.Delete(diff.start, value.substring(diff.start, diff.end))
            break
          }
          case 'insert': {
            diffOp = TextOperation.Insert(diff.start, diff.text)
            break
          }
          default:
            throw new Error('Unknown diff type')
        }
      } else if (isArray(value)) {
        switch (diff.type) {
          case 'delete': {
            diffOp = ArrayOperation.Delete(diff.pos, value[diff.pos])
            break
          }
          case 'insert': {
            diffOp = ArrayOperation.Insert(diff.pos, diff.value)
            break
          }
          default:
            throw new Error('Unknown diff type')
        }
      } else Eif (value._isCoordinate) {
        switch (diff.type) {
          case 'shift': {
            diffOp = CoordinateOperation.Shift(diff.value)
            break
          }
          default:
            throw new Error('Unknown diff type')
        }
      }
    }
    Iif (!diffOp) {
      throw new Error('Unsupported diff: ' + JSON.stringify(diff))
    }
    return diffOp
  }
 
}
 
export default IncrementalData