Document.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-undef,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS001: Remove Babel/TypeScript constructor workaround
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS103: Rewrite code to no longer use __guard__
  15. * DS205: Consider reworking code to avoid use of IIFEs
  16. * DS206: Consider reworking classes to avoid initClass
  17. * DS207: Consider shorter variations of null checks
  18. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  19. */
  20. define([
  21. 'utils/EventEmitter',
  22. 'ide/editor/ShareJsDoc',
  23. 'ide/review-panel/RangesTracker'
  24. ], function(EventEmitter, ShareJsDoc, RangesTracker) {
  25. let Document
  26. return (Document = (function() {
  27. Document = class Document extends EventEmitter {
  28. static initClass() {
  29. this.prototype.MAX_PENDING_OP_SIZE = 64
  30. }
  31. static getDocument(ide, doc_id) {
  32. if (!this.openDocs) {
  33. this.openDocs = {}
  34. }
  35. if (this.openDocs[doc_id] == null) {
  36. sl_console.log(
  37. `[getDocument] Creating new document instance for ${doc_id}`
  38. )
  39. this.openDocs[doc_id] = new Document(ide, doc_id)
  40. } else {
  41. sl_console.log(
  42. `[getDocument] Returning existing document instance for ${doc_id}`
  43. )
  44. }
  45. return this.openDocs[doc_id]
  46. }
  47. static hasUnsavedChanges() {
  48. const object = this.openDocs || {}
  49. for (let doc_id in object) {
  50. const doc = object[doc_id]
  51. if (doc.hasBufferedOps()) {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. static flushAll() {
  58. return (() => {
  59. const result = []
  60. for (let doc_id in this.openDocs) {
  61. const doc = this.openDocs[doc_id]
  62. result.push(doc.flush())
  63. }
  64. return result
  65. })()
  66. }
  67. constructor(ide, doc_id) {
  68. super()
  69. this.ide = ide
  70. this.doc_id = doc_id
  71. this.connected = this.ide.socket.socket.connected
  72. this.joined = false
  73. this.wantToBeJoined = false
  74. this._checkAceConsistency = _.bind(
  75. this._checkConsistency,
  76. this,
  77. this.ace
  78. )
  79. this._checkCMConsistency = _.bind(this._checkConsistency, this, this.cm)
  80. this._bindToEditorEvents()
  81. this._bindToSocketEvents()
  82. }
  83. attachToAce(ace) {
  84. this.ace = ace
  85. if (this.doc != null) {
  86. this.doc.attachToAce(this.ace)
  87. }
  88. const editorDoc = this.ace.getSession().getDocument()
  89. editorDoc.on('change', this._checkAceConsistency)
  90. return this.ide.$scope.$emit('document:opened', this.doc)
  91. }
  92. detachFromAce() {
  93. if (this.doc != null) {
  94. this.doc.detachFromAce()
  95. }
  96. const editorDoc =
  97. this.ace != null ? this.ace.getSession().getDocument() : undefined
  98. if (editorDoc != null) {
  99. editorDoc.off('change', this._checkAceConsistency)
  100. }
  101. return this.ide.$scope.$emit('document:closed', this.doc)
  102. }
  103. attachToCM(cm) {
  104. this.cm = cm
  105. if (this.doc != null) {
  106. this.doc.attachToCM(this.cm)
  107. }
  108. if (this.cm != null) {
  109. this.cm.on('change', this._checkCMConsistency)
  110. }
  111. return this.ide.$scope.$emit('document:opened', this.doc)
  112. }
  113. detachFromCM() {
  114. if (this.doc != null) {
  115. this.doc.detachFromCM()
  116. }
  117. if (this.cm != null) {
  118. this.cm.off('change', this._checkCMConsistency)
  119. }
  120. return this.ide.$scope.$emit('document:closed', this.doc)
  121. }
  122. submitOp(...args) {
  123. return this.doc != null
  124. ? this.doc.submitOp(...Array.from(args || []))
  125. : undefined
  126. }
  127. _checkConsistency(editor) {
  128. return () => {
  129. // We've been seeing a lot of errors when I think there shouldn't be
  130. // any, which may be related to this check happening before the change is
  131. // applied. If we use a timeout, hopefully we can reduce this.
  132. return setTimeout(() => {
  133. const editorValue = editor != null ? editor.getValue() : undefined
  134. const sharejsValue =
  135. this.doc != null ? this.doc.getSnapshot() : undefined
  136. if (editorValue !== sharejsValue) {
  137. return this._onError(
  138. new Error('Editor text does not match server text')
  139. )
  140. }
  141. }, 0)
  142. }
  143. }
  144. getSnapshot() {
  145. return this.doc != null ? this.doc.getSnapshot() : undefined
  146. }
  147. getType() {
  148. return this.doc != null ? this.doc.getType() : undefined
  149. }
  150. getInflightOp() {
  151. return this.doc != null ? this.doc.getInflightOp() : undefined
  152. }
  153. getPendingOp() {
  154. return this.doc != null ? this.doc.getPendingOp() : undefined
  155. }
  156. getRecentAck() {
  157. return this.doc != null ? this.doc.getRecentAck() : undefined
  158. }
  159. getOpSize(op) {
  160. return this.doc != null ? this.doc.getOpSize(op) : undefined
  161. }
  162. hasBufferedOps() {
  163. return this.doc != null ? this.doc.hasBufferedOps() : undefined
  164. }
  165. setTrackingChanges(track_changes) {
  166. return (this.doc.track_changes = track_changes)
  167. }
  168. getTrackingChanges() {
  169. return !!this.doc.track_changes
  170. }
  171. setTrackChangesIdSeeds(id_seeds) {
  172. return (this.doc.track_changes_id_seeds = id_seeds)
  173. }
  174. _bindToSocketEvents() {
  175. this._onUpdateAppliedHandler = update => this._onUpdateApplied(update)
  176. this.ide.socket.on('otUpdateApplied', this._onUpdateAppliedHandler)
  177. this._onErrorHandler = (error, update) => this._onError(error, update)
  178. this.ide.socket.on('otUpdateError', this._onErrorHandler)
  179. this._onDisconnectHandler = error => this._onDisconnect(error)
  180. return this.ide.socket.on('disconnect', this._onDisconnectHandler)
  181. }
  182. _bindToEditorEvents() {
  183. const onReconnectHandler = update => {
  184. return this._onReconnect(update)
  185. }
  186. return (this._unsubscribeReconnectHandler = this.ide.$scope.$on(
  187. 'project:joined',
  188. onReconnectHandler
  189. ))
  190. }
  191. _unBindFromEditorEvents() {
  192. return this._unsubscribeReconnectHandler()
  193. }
  194. _unBindFromSocketEvents() {
  195. this.ide.socket.removeListener(
  196. 'otUpdateApplied',
  197. this._onUpdateAppliedHandler
  198. )
  199. this.ide.socket.removeListener('otUpdateError', this._onErrorHandler)
  200. return this.ide.socket.removeListener(
  201. 'disconnect',
  202. this._onDisconnectHandler
  203. )
  204. }
  205. leaveAndCleanUp() {
  206. return this.leave(error => {
  207. return this._cleanUp()
  208. })
  209. }
  210. join(callback) {
  211. if (callback == null) {
  212. callback = function(error) {}
  213. }
  214. this.wantToBeJoined = true
  215. this._cancelLeave()
  216. if (this.connected) {
  217. return this._joinDoc(callback)
  218. } else {
  219. if (!this._joinCallbacks) {
  220. this._joinCallbacks = []
  221. }
  222. return this._joinCallbacks.push(callback)
  223. }
  224. }
  225. leave(callback) {
  226. if (callback == null) {
  227. callback = function(error) {}
  228. }
  229. this.wantToBeJoined = false
  230. this._cancelJoin()
  231. if (this.doc != null && this.doc.hasBufferedOps()) {
  232. sl_console.log(
  233. '[leave] Doc has buffered ops, pushing callback for later'
  234. )
  235. if (!this._leaveCallbacks) {
  236. this._leaveCallbacks = []
  237. }
  238. return this._leaveCallbacks.push(callback)
  239. } else if (!this.connected) {
  240. sl_console.log('[leave] Not connected, returning now')
  241. return callback()
  242. } else {
  243. sl_console.log('[leave] Leaving now')
  244. return this._leaveDoc(callback)
  245. }
  246. }
  247. flush() {
  248. return this.doc != null ? this.doc.flushPendingOps() : undefined
  249. }
  250. chaosMonkey(line, char) {
  251. if (line == null) {
  252. line = 0
  253. }
  254. if (char == null) {
  255. char = 'a'
  256. }
  257. const orig = char
  258. let copy = null
  259. let pos = 0
  260. var timer = () => {
  261. if (copy == null || !copy.length) {
  262. copy = orig.slice() + ' ' + new Date() + '\n'
  263. line += Math.random() > 0.1 ? 1 : -2
  264. if (line < 0) {
  265. line = 0
  266. }
  267. pos = 0
  268. }
  269. char = copy[0]
  270. copy = copy.slice(1)
  271. this.ace.session.insert({ row: line, column: pos }, char)
  272. pos += 1
  273. return (this._cm = setTimeout(
  274. timer,
  275. 100 + (Math.random() < 0.1 ? 1000 : 0)
  276. ))
  277. }
  278. return (this._cm = timer())
  279. }
  280. clearChaosMonkey() {
  281. return clearTimeout(this._cm) // pending ops bigger than this are always considered unsaved
  282. }
  283. pollSavedStatus() {
  284. // returns false if doc has ops waiting to be acknowledged or
  285. // sent that haven't changed since the last time we checked.
  286. // Otherwise returns true.
  287. let saved
  288. const inflightOp = this.getInflightOp()
  289. const pendingOp = this.getPendingOp()
  290. const recentAck = this.getRecentAck()
  291. const pendingOpSize = pendingOp != null && this.getOpSize(pendingOp)
  292. if (inflightOp == null && pendingOp == null) {
  293. // there's nothing going on, this is ok.
  294. saved = true
  295. sl_console.logOnce('[pollSavedStatus] no inflight or pending ops')
  296. } else if (inflightOp != null && inflightOp === this.oldInflightOp) {
  297. // The same inflight op has been sitting unacked since we
  298. // last checked, this is bad.
  299. saved = false
  300. sl_console.log('[pollSavedStatus] inflight op is same as before')
  301. } else if (
  302. pendingOp != null &&
  303. recentAck &&
  304. pendingOpSize < this.MAX_PENDING_OP_SIZE
  305. ) {
  306. // There is an op waiting to go to server but it is small and
  307. // within the flushDelay, this is ok for now.
  308. saved = true
  309. sl_console.log(
  310. '[pollSavedStatus] pending op (small with recent ack) assume ok',
  311. pendingOp,
  312. pendingOpSize
  313. )
  314. } else {
  315. // In any other situation, assume the document is unsaved.
  316. saved = false
  317. sl_console.log(
  318. `[pollSavedStatus] assuming not saved (inflightOp?: ${inflightOp !=
  319. null}, pendingOp?: ${pendingOp != null})`
  320. )
  321. }
  322. this.oldInflightOp = inflightOp
  323. return saved
  324. }
  325. _cancelLeave() {
  326. if (this._leaveCallbacks != null) {
  327. return delete this._leaveCallbacks
  328. }
  329. }
  330. _cancelJoin() {
  331. if (this._joinCallbacks != null) {
  332. return delete this._joinCallbacks
  333. }
  334. }
  335. _onUpdateApplied(update) {
  336. this.ide.pushEvent('received-update', {
  337. doc_id: this.doc_id,
  338. remote_doc_id: update != null ? update.doc : undefined,
  339. wantToBeJoined: this.wantToBeJoined,
  340. update,
  341. hasDoc: this.doc != null
  342. })
  343. if (
  344. window.disconnectOnAck != null &&
  345. Math.random() < window.disconnectOnAck
  346. ) {
  347. sl_console.log('Disconnecting on ack', update)
  348. window._ide.socket.socket.disconnect()
  349. // Pretend we never received the ack
  350. return
  351. }
  352. if (window.dropAcks != null && Math.random() < window.dropAcks) {
  353. if (update.op == null) {
  354. // Only drop our own acks, not collaborator updates
  355. sl_console.log('Simulating a lost ack', update)
  356. return
  357. }
  358. }
  359. if (
  360. (update != null ? update.doc : undefined) === this.doc_id &&
  361. this.doc != null
  362. ) {
  363. this.ide.pushEvent('received-update:processing', {
  364. update
  365. })
  366. // FIXME: change this back to processUpdateFromServer when redis fixed
  367. this.doc.processUpdateFromServerInOrder(update)
  368. if (!this.wantToBeJoined) {
  369. return this.leave()
  370. }
  371. }
  372. }
  373. _onDisconnect() {
  374. sl_console.log('[onDisconnect] disconnecting')
  375. this.connected = false
  376. this.joined = false
  377. return this.doc != null
  378. ? this.doc.updateConnectionState('disconnected')
  379. : undefined
  380. }
  381. _onReconnect() {
  382. sl_console.log('[onReconnect] reconnected (joined project)')
  383. this.ide.pushEvent('reconnected:afterJoinProject')
  384. this.connected = true
  385. if (
  386. this.wantToBeJoined ||
  387. (this.doc != null ? this.doc.hasBufferedOps() : undefined)
  388. ) {
  389. sl_console.log(
  390. `[onReconnect] Rejoining (wantToBeJoined: ${
  391. this.wantToBeJoined
  392. } OR hasBufferedOps: ${
  393. this.doc != null ? this.doc.hasBufferedOps() : undefined
  394. })`
  395. )
  396. return this._joinDoc(error => {
  397. if (error != null) {
  398. return this._onError(error)
  399. }
  400. this.doc.updateConnectionState('ok')
  401. this.doc.flushPendingOps()
  402. return this._callJoinCallbacks()
  403. })
  404. }
  405. }
  406. _callJoinCallbacks() {
  407. for (let callback of Array.from(this._joinCallbacks || [])) {
  408. callback()
  409. }
  410. return delete this._joinCallbacks
  411. }
  412. _joinDoc(callback) {
  413. if (callback == null) {
  414. callback = function(error) {}
  415. }
  416. if (this.doc != null) {
  417. this.ide.pushEvent('joinDoc:existing', {
  418. doc_id: this.doc_id,
  419. version: this.doc.getVersion()
  420. })
  421. return this.ide.socket.emit(
  422. 'joinDoc',
  423. this.doc_id,
  424. this.doc.getVersion(),
  425. { encodeRanges: true },
  426. (error, docLines, version, updates, ranges) => {
  427. if (error != null) {
  428. return callback(error)
  429. }
  430. this.joined = true
  431. this.doc.catchUp(updates)
  432. this._decodeRanges(ranges)
  433. this._catchUpRanges(
  434. ranges != null ? ranges.changes : undefined,
  435. ranges != null ? ranges.comments : undefined
  436. )
  437. return callback()
  438. }
  439. )
  440. } else {
  441. this.ide.pushEvent('joinDoc:new', {
  442. doc_id: this.doc_id
  443. })
  444. return this.ide.socket.emit(
  445. 'joinDoc',
  446. this.doc_id,
  447. { encodeRanges: true },
  448. (error, docLines, version, updates, ranges) => {
  449. if (error != null) {
  450. return callback(error)
  451. }
  452. this.joined = true
  453. this.ide.pushEvent('joinDoc:inited', {
  454. doc_id: this.doc_id,
  455. version
  456. })
  457. this.doc = new ShareJsDoc(
  458. this.doc_id,
  459. docLines,
  460. version,
  461. this.ide.socket
  462. )
  463. this._decodeRanges(ranges)
  464. this.ranges = new RangesTracker(
  465. ranges != null ? ranges.changes : undefined,
  466. ranges != null ? ranges.comments : undefined
  467. )
  468. this._bindToShareJsDocEvents()
  469. return callback()
  470. }
  471. )
  472. }
  473. }
  474. _decodeRanges(ranges) {
  475. const decodeFromWebsockets = text => decodeURIComponent(escape(text))
  476. try {
  477. for (let change of Array.from(ranges.changes || [])) {
  478. if (change.op.i != null) {
  479. change.op.i = decodeFromWebsockets(change.op.i)
  480. }
  481. if (change.op.d != null) {
  482. change.op.d = decodeFromWebsockets(change.op.d)
  483. }
  484. }
  485. return (() => {
  486. const result = []
  487. for (let comment of Array.from(ranges.comments || [])) {
  488. if (comment.op.c != null) {
  489. result.push((comment.op.c = decodeFromWebsockets(comment.op.c)))
  490. } else {
  491. result.push(undefined)
  492. }
  493. }
  494. return result
  495. })()
  496. } catch (err) {
  497. return console.log(err)
  498. }
  499. }
  500. _leaveDoc(callback) {
  501. if (callback == null) {
  502. callback = function(error) {}
  503. }
  504. sl_console.log('[_leaveDoc] Sending leaveDoc request')
  505. return this.ide.socket.emit('leaveDoc', this.doc_id, error => {
  506. if (error != null) {
  507. return callback(error)
  508. }
  509. this.joined = false
  510. for (callback of Array.from(this._leaveCallbacks || [])) {
  511. sl_console.log('[_leaveDoc] Calling buffered callback', callback)
  512. callback(error)
  513. }
  514. delete this._leaveCallbacks
  515. return callback(error)
  516. })
  517. }
  518. _cleanUp() {
  519. if (Document.openDocs[this.doc_id] === this) {
  520. sl_console.log(
  521. `[_cleanUp] Removing self (${this.doc_id}) from in openDocs`
  522. )
  523. delete Document.openDocs[this.doc_id]
  524. } else {
  525. // It's possible that this instance has error, and the doc has been reloaded.
  526. // This creates a new instance in Document.openDoc with the same id. We shouldn't
  527. // clear it because it's not this instance.
  528. sl_console.log(
  529. `[_cleanUp] New instance of (${this.doc_id}) created. Not removing`
  530. )
  531. }
  532. this._unBindFromEditorEvents()
  533. return this._unBindFromSocketEvents()
  534. }
  535. _bindToShareJsDocEvents() {
  536. this.doc.on('error', (error, meta) => this._onError(error, meta))
  537. this.doc.on('externalUpdate', update => {
  538. this.ide.pushEvent('externalUpdate', { doc_id: this.doc_id })
  539. return this.trigger('externalUpdate', update)
  540. })
  541. this.doc.on('remoteop', (...args) => {
  542. this.ide.pushEvent('remoteop', { doc_id: this.doc_id })
  543. return this.trigger('remoteop', ...Array.from(args))
  544. })
  545. this.doc.on('op:sent', op => {
  546. this.ide.pushEvent('op:sent', {
  547. doc_id: this.doc_id,
  548. op
  549. })
  550. return this.trigger('op:sent')
  551. })
  552. this.doc.on('op:acknowledged', op => {
  553. this.ide.pushEvent('op:acknowledged', {
  554. doc_id: this.doc_id,
  555. op
  556. })
  557. this.ide.$scope.$emit('ide:opAcknowledged', {
  558. doc_id: this.doc_id,
  559. op
  560. })
  561. return this.trigger('op:acknowledged')
  562. })
  563. this.doc.on('op:timeout', op => {
  564. this.ide.pushEvent('op:timeout', {
  565. doc_id: this.doc_id,
  566. op
  567. })
  568. this.trigger('op:timeout')
  569. return this._onError(new Error('op timed out'), { op })
  570. })
  571. this.doc.on('flush', (inflightOp, pendingOp, version) => {
  572. return this.ide.pushEvent('flush', {
  573. doc_id: this.doc_id,
  574. inflightOp,
  575. pendingOp,
  576. v: version
  577. })
  578. })
  579. this.doc.on('change', (ops, oldSnapshot, msg) => {
  580. this._applyOpsToRanges(ops, oldSnapshot, msg)
  581. return this.ide.$scope.$emit('doc:changed', { doc_id: this.doc_id })
  582. })
  583. this.doc.on('flipped_pending_to_inflight', () => {
  584. return this.trigger('flipped_pending_to_inflight')
  585. })
  586. return this.doc.on('saved', () => {
  587. return this.ide.$scope.$emit('doc:saved', { doc_id: this.doc_id })
  588. })
  589. }
  590. _onError(error, meta) {
  591. if (meta == null) {
  592. meta = {}
  593. }
  594. meta.doc_id = this.doc_id
  595. sl_console.log('ShareJS error', error, meta)
  596. if (error.message === 'no project_id found on client') {
  597. sl_console.log('ignoring error, will wait to join project')
  598. return
  599. }
  600. if (typeof ga === 'function') {
  601. // sanitise the error message before sending (the "delete component"
  602. // error in public/js/libs/sharejs.js includes the some document
  603. // content).
  604. let message = error.message
  605. if (/^Delete component/.test(message)) {
  606. message = 'Delete component does not match deleted text'
  607. }
  608. ga(
  609. 'send',
  610. 'event',
  611. 'error',
  612. 'shareJsError',
  613. `${message} - ${this.ide.socket.socket.transport.name}`
  614. )
  615. }
  616. if (this.doc != null) {
  617. this.doc.clearInflightAndPendingOps()
  618. }
  619. this.trigger('error', error, meta)
  620. // The clean up should run after the error is triggered because the error triggers a
  621. // disconnect. If we run the clean up first, we remove our event handlers and miss
  622. // the disconnect event, which means we try to leaveDoc when the connection comes back.
  623. // This could intefere with the new connection of a new instance of this document.
  624. return this._cleanUp()
  625. }
  626. _applyOpsToRanges(ops, oldSnapshot, msg) {
  627. let old_id_seed
  628. if (ops == null) {
  629. ops = []
  630. }
  631. let track_changes_as = null
  632. const remote_op = msg != null
  633. if (__guard__(msg != null ? msg.meta : undefined, x => x.tc) != null) {
  634. old_id_seed = this.ranges.getIdSeed()
  635. this.ranges.setIdSeed(msg.meta.tc)
  636. }
  637. if (remote_op && (msg.meta != null ? msg.meta.tc : undefined)) {
  638. track_changes_as = msg.meta.user_id
  639. } else if (!remote_op && this.track_changes_as != null) {
  640. ;({ track_changes_as } = this)
  641. }
  642. this.ranges.track_changes = track_changes_as != null
  643. for (let op of Array.from(ops)) {
  644. this.ranges.applyOp(op, { user_id: track_changes_as })
  645. }
  646. if (old_id_seed != null) {
  647. this.ranges.setIdSeed(old_id_seed)
  648. }
  649. if (remote_op) {
  650. // With remote ops, Ace hasn't been updated when we receive this op,
  651. // so defer updating track changes until it has
  652. return setTimeout(() => this.emit('ranges:dirty'))
  653. } else {
  654. return this.emit('ranges:dirty')
  655. }
  656. }
  657. _catchUpRanges(changes, comments) {
  658. // We've just been given the current server's ranges, but need to apply any local ops we have.
  659. // Reset to the server state then apply our local ops again.
  660. if (changes == null) {
  661. changes = []
  662. }
  663. if (comments == null) {
  664. comments = []
  665. }
  666. this.emit('ranges:clear')
  667. this.ranges.changes = changes
  668. this.ranges.comments = comments
  669. this.ranges.track_changes = this.doc.track_changes
  670. for (var op of Array.from(this.doc.getInflightOp() || [])) {
  671. this.ranges.setIdSeed(this.doc.track_changes_id_seeds.inflight)
  672. this.ranges.applyOp(op, { user_id: this.track_changes_as })
  673. }
  674. for (op of Array.from(this.doc.getPendingOp() || [])) {
  675. this.ranges.setIdSeed(this.doc.track_changes_id_seeds.pending)
  676. this.ranges.applyOp(op, { user_id: this.track_changes_as })
  677. }
  678. return this.emit('ranges:redraw')
  679. }
  680. }
  681. Document.initClass()
  682. return Document
  683. })())
  684. })
  685. function __guard__(value, transform) {
  686. return typeof value !== 'undefined' && value !== null
  687. ? transform(value)
  688. : undefined
  689. }