Batch multiple view updates with Editor::batchUpdates

This commit is contained in:
Nathan Sobo 2014-04-15 13:13:53 -06:00
parent fe6a007774
commit ddc677fb30
2 changed files with 26 additions and 3 deletions

View File

@ -15,7 +15,8 @@ EditorCompont = React.createClass
pendingScrollTop: null
pendingScrollLeft: null
selectOnMouseMove: false
batchingUpdates: false
updateRequested: false
render: ->
{focused, fontSize, lineHeight, fontFamily, showIndentGuide} = @state
@ -74,6 +75,8 @@ EditorCompont = React.createClass
observeEditor: ->
{editor} = @props
@subscribe editor, 'batched-updates-started', @onBatchedUpdatesStarted
@subscribe editor, 'batched-updates-ended', @onBatchedUpdatesEnded
@subscribe editor, 'screen-lines-changed', @onScreenLinesChanged
@subscribe editor, 'selection-screen-range-changed', @requestUpdate
@subscribe editor, 'selection-added', @onSelectionAdded
@ -258,6 +261,16 @@ EditorCompont = React.createClass
event.preventDefault()
onBatchedUpdatesStarted: ->
@batchingUpdates = true
onBatchedUpdatesEnded: ->
updateRequested = @updateRequested
@updateRequested = false
@batchingUpdates = false
if updateRequested
@forceUpdate()
onScreenLinesChanged: ({start, end}) ->
{editor} = @props
@requestUpdate() if editor.intersectsVisibleRowRange(start, end + 1) # TODO: Use closed-open intervals for change events
@ -284,7 +297,10 @@ EditorCompont = React.createClass
clearVisibleRowOverridesAfterDelay: null
requestUpdate: ->
@forceUpdate()
if @batchingUpdates
@updateRequested = true
else
@forceUpdate()
updateModelDimensions: ->
@refs.scrollView.updateModelDimensions()

View File

@ -1781,7 +1781,9 @@ class Editor extends Model
# execution and revert any changes performed up to the abortion.
#
# fn - A {Function} to call inside the transaction.
transact: (fn) -> @buffer.transact(fn)
transact: (fn) ->
@batchUpdates =>
@buffer.transact(fn)
# Public: Start an open-ended transaction.
#
@ -1801,6 +1803,11 @@ class Editor extends Model
# within the transaction.
abortTransaction: -> @buffer.abortTransaction()
batchUpdates: (fn) ->
@emit 'batched-updates-started'
fn()
@emit 'batched-updates-ended'
inspect: ->
"<Editor #{@id}>"