Document.js 22 KB

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