ConcatManager.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. strInject = (s1, pos, s2) -> s1[...pos] + s2 + s1[pos..]
  2. strRemove = (s1, pos, length) -> s1[...pos] + s1[(pos + length)..]
  3. module.exports = ConcatManager =
  4. normalizeUpdate: (update) ->
  5. updates = []
  6. for op in update.op
  7. updates.push
  8. op: [op]
  9. meta:
  10. start_ts: update.meta.ts
  11. end_ts: update.meta.ts
  12. user_id: update.meta.user_id
  13. return updates
  14. MAX_TIME_BETWEEN_UPDATES: oneMinute = 60 * 1000
  15. concatTwoUpdates: (firstUpdate, secondUpdate) ->
  16. firstUpdate =
  17. op: firstUpdate.op
  18. meta:
  19. user_id: firstUpdate.meta.user_id or null
  20. start_ts: firstUpdate.meta.start_ts or firstUpdate.meta.ts
  21. end_ts: firstUpdate.meta.end_ts or firstUpdate.meta.ts
  22. secondUpdate =
  23. op: secondUpdate.op
  24. meta:
  25. user_id: secondUpdate.meta.user_id or null
  26. start_ts: secondUpdate.meta.start_ts or secondUpdate.meta.ts
  27. end_ts: secondUpdate.meta.end_ts or secondUpdate.meta.ts
  28. if firstUpdate.meta.user_id != secondUpdate.meta.user_id
  29. return [firstUpdate, secondUpdate]
  30. if secondUpdate.meta.start_ts - firstUpdate.meta.end_ts > ConcatManager.MAX_TIME_BETWEEN_UPDATES
  31. return [firstUpdate, secondUpdate]
  32. firstOp = firstUpdate.op[0]
  33. secondOp = secondUpdate.op[0]
  34. # Two inserts
  35. if firstOp.i? and secondOp.i? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  36. return [
  37. meta:
  38. start_ts: firstUpdate.meta.start_ts
  39. end_ts: secondUpdate.meta.end_ts
  40. user_id: firstUpdate.meta.user_id
  41. op: [
  42. p: firstOp.p
  43. i: strInject(firstOp.i, secondOp.p - firstOp.p, secondOp.i)
  44. ]
  45. ]
  46. # Two deletes
  47. else if firstOp.d? and secondOp.d? and secondOp.p <= firstOp.p <= (secondOp.p + secondOp.d.length)
  48. return [
  49. meta:
  50. start_ts: firstUpdate.meta.start_ts
  51. end_ts: secondUpdate.meta.end_ts
  52. user_id: firstUpdate.meta.user_id
  53. op: [
  54. p: secondOp.p
  55. d: strInject(secondOp.d, firstOp.p - secondOp.p, firstOp.d)
  56. ]
  57. ]
  58. # An insert and then a delete
  59. else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  60. offset = secondOp.p - firstOp.p
  61. insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
  62. if insertedText == secondOp.d
  63. insert = strRemove(firstOp.i, offset, secondOp.d.length)
  64. return [] if insert == ""
  65. return [
  66. meta:
  67. start_ts: firstUpdate.meta.start_ts
  68. end_ts: secondUpdate.meta.end_ts
  69. user_id: firstUpdate.meta.user_id
  70. op: [
  71. p: firstOp.p
  72. i: insert
  73. ]
  74. ]
  75. else
  76. # This shouldn't be possible!
  77. return [firstUpdate, secondUpdate]
  78. else if firstOp.d? and secondOp.i? and firstOp.p == secondOp.p
  79. offset = firstOp.d.indexOf(secondOp.i)
  80. if offset == -1
  81. return [firstUpdate, secondUpdate]
  82. headD = firstOp.d.slice(0, offset)
  83. inserted = firstOp.d.slice(offset, secondOp.i.length)
  84. tailD = firstOp.d.slice(offset + secondOp.i.length)
  85. headP = firstOp.p
  86. tailP = firstOp.p + secondOp.i.length
  87. updates = []
  88. if headD != ""
  89. updates.push
  90. meta:
  91. start_ts: firstUpdate.meta.start_ts
  92. end_ts: secondUpdate.meta.end_ts
  93. user_id: firstUpdate.meta.user_id
  94. op: [
  95. p: headP
  96. d: headD
  97. ]
  98. if tailD != ""
  99. updates.push
  100. meta:
  101. start_ts: firstUpdate.meta.start_ts
  102. end_ts: secondUpdate.meta.end_ts
  103. user_id: firstUpdate.meta.user_id
  104. op: [
  105. p: tailP
  106. d: tailD
  107. ]
  108. if updates.length == 2
  109. updates[0].meta.start_ts = updates[0].meta.end_ts = firstUpdate.meta.start_ts
  110. updates[1].meta.start_ts = updates[1].meta.end_ts = secondUpdate.meta.end_ts
  111. return updates
  112. else
  113. return [firstUpdate, secondUpdate]