ShareJsDoc.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. define [
  2. "utils/EventEmitter"
  3. "libs/sharejs"
  4. ], (EventEmitter, ShareJs) ->
  5. SINGLE_USER_FLUSH_DELAY = 1000 #ms
  6. class ShareJsDoc extends EventEmitter
  7. constructor: (@doc_id, docLines, version, @socket) ->
  8. # Dencode any binary bits of data
  9. # See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
  10. @type = "text"
  11. docLines = for line in docLines
  12. if line.text?
  13. @type = "json"
  14. line.text = decodeURIComponent(escape(line.text))
  15. else
  16. @type = "text"
  17. line = decodeURIComponent(escape(line))
  18. line
  19. if @type == "text"
  20. snapshot = docLines.join("\n")
  21. else if @type == "json"
  22. snapshot = { lines: docLines }
  23. else
  24. throw new Error("Unknown type: #{@type}")
  25. @connection = {
  26. send: (update) =>
  27. @_startInflightOpTimeout(update)
  28. @socket.emit "applyOtUpdate", @doc_id, update
  29. state: "ok"
  30. id: @socket.socket.sessionid
  31. }
  32. @_doc = new ShareJs.Doc @connection, @doc_id,
  33. type: @type
  34. @_doc.setFlushDelay(SINGLE_USER_FLUSH_DELAY)
  35. @_doc.on "change", () =>
  36. @trigger "change"
  37. @_doc.on "acknowledge", () =>
  38. @trigger "acknowledge"
  39. @_doc.on "remoteop", () =>
  40. # As soon as we're working with a collaborator, start sending
  41. # ops as quickly as possible for low latency.
  42. @_doc.setFlushDelay(0)
  43. @trigger "remoteop"
  44. @_doc.on "error", (e) =>
  45. @_handleError(e)
  46. @_bindToDocChanges(@_doc)
  47. @processUpdateFromServer
  48. open: true
  49. v: version
  50. snapshot: snapshot
  51. submitOp: (args...) -> @_doc.submitOp(args...)
  52. processUpdateFromServer: (message) ->
  53. try
  54. @_doc._onMessage message
  55. catch error
  56. # Version mismatches are thrown as errors
  57. @_handleError(error)
  58. if message?.meta?.type == "external"
  59. @trigger "externalUpdate", message
  60. catchUp: (updates) ->
  61. for update, i in updates
  62. update.v = @_doc.version
  63. update.doc = @doc_id
  64. @processUpdateFromServer(update)
  65. getSnapshot: () -> @_doc.snapshot
  66. getVersion: () -> @_doc.version
  67. getType: () -> @type
  68. clearInflightAndPendingOps: () ->
  69. @_doc.inflightOp = null
  70. @_doc.inflightCallbacks = []
  71. @_doc.pendingOp = null
  72. @_doc.pendingCallbacks = []
  73. flushPendingOps: () ->
  74. # This will flush any ops that are pending.
  75. # If there is an inflight op it will do nothing.
  76. @_doc.flush()
  77. updateConnectionState: (state) ->
  78. @connection.state = state
  79. @connection.id = @socket.socket.sessionid
  80. @_doc.autoOpen = false
  81. @_doc._connectionStateChanged(state)
  82. hasBufferedOps: () ->
  83. @_doc.inflightOp? or @_doc.pendingOp?
  84. getInflightOp: () -> @_doc.inflightOp
  85. getPendingOp: () -> @_doc.pendingOp
  86. attachToAce: (ace) -> @_doc.attach_ace(ace, false, window.maxDocLength)
  87. detachFromAce: () -> @_doc.detach_ace?()
  88. INFLIGHT_OP_TIMEOUT: 10000
  89. _startInflightOpTimeout: (update) ->
  90. meta =
  91. v: update.v
  92. op_sent_at: new Date()
  93. timer = setTimeout () =>
  94. @trigger "op:timeout", update
  95. , @INFLIGHT_OP_TIMEOUT
  96. @_doc.inflightCallbacks.push () =>
  97. clearTimeout timer
  98. _handleError: (error, meta = {}) ->
  99. @trigger "error", error, meta
  100. _bindToDocChanges: (doc) ->
  101. submitOp = doc.submitOp
  102. doc.submitOp = (args...) =>
  103. @trigger "op:sent", args...
  104. doc.pendingCallbacks.push () =>
  105. @trigger "op:acknowledged", args...
  106. submitOp.apply(doc, args)
  107. flush = doc.flush
  108. doc.flush = (args...) =>
  109. @trigger "flush", doc.inflightOp, doc.pendingOp, doc.version
  110. flush.apply(doc, args)