Move batching management down into presentation layer

This commit is contained in:
Antonio Scandurra
2015-02-27 12:17:34 +01:00
parent c638c23e3f
commit af65849880
3 changed files with 32 additions and 53 deletions

View File

@@ -103,6 +103,8 @@ class TextEditorComponent
window.removeEventListener 'resize', @requestHeightAndWidthMeasurement
updateSync: ->
@presenter.exitBatchMode()
@oldState ?= {}
@newState = @presenter.state
@@ -148,6 +150,8 @@ class TextEditorComponent
@hostElement.__spacePenView.trigger 'selection:changed' if selectionChanged
@hostElement.__spacePenView.trigger 'editor:display-updated'
@presenter.enterBatchMode()
readAfterUpdateSync: =>
@linesComponent.measureCharactersInNewLines() if @isVisible() and not @newState.content.scrollingVertically

View File

@@ -26,6 +26,7 @@ class TextEditorPresenter
@observeConfig()
@buildState()
@startBlinkingCursors()
@enterBatchMode()
destroy: ->
@disposables.dispose()
@@ -90,9 +91,6 @@ class TextEditorPresenter
@shouldUpdateLineNumbersState = false
observeModel: ->
@disposables.add @model.onWillStartBatchOperation => @enterBatchMode()
@disposables.add @model.onDidFinishBatchOperation => @exitBatchMode()
@disposables.add @model.onDidChange =>
@updateContentDimensions()
@updateEndRow()
@@ -193,6 +191,7 @@ class TextEditorPresenter
updateFocusedState: ->
if @isInBatchMode()
@shouldUpdateFocusedState = true
@emitter.emit "did-update-state"
return
@state.focused = @focused
@@ -200,6 +199,7 @@ class TextEditorPresenter
updateHeightState: ->
if @isInBatchMode()
@shouldUpdateHeightState = true
@emitter.emit "did-update-state"
return
if @autoHeight
@@ -207,11 +207,10 @@ class TextEditorPresenter
else
@state.height = null
@emitter.emit 'did-update-state'
updateVerticalScrollState: ->
if @isInBatchMode()
@shouldUpdateVerticalScrollState = true
@emitter.emit "did-update-state"
return
@state.content.scrollHeight = @scrollHeight
@@ -222,11 +221,10 @@ class TextEditorPresenter
@state.gutter.scrollTop = @scrollTop
@state.verticalScrollbar.scrollTop = @scrollTop
@emitter.emit 'did-update-state'
updateHorizontalScrollState: ->
if @isInBatchMode()
@shouldUpdateHorizontalScrollState = true
@emitter.emit "did-update-state"
return
@state.content.scrollWidth = @scrollWidth
@@ -235,11 +233,10 @@ class TextEditorPresenter
@state.content.scrollLeft = @scrollLeft
@state.horizontalScrollbar.scrollLeft = @scrollLeft
@emitter.emit 'did-update-state'
updateScrollbarsState: ->
if @isInBatchMode()
@shouldUpdateScrollbarsState = true
@emitter.emit "did-update-state"
return
@state.horizontalScrollbar.visible = @horizontalScrollbarHeight > 0
@@ -250,11 +247,10 @@ class TextEditorPresenter
@state.verticalScrollbar.width = @measuredVerticalScrollbarWidth
@state.verticalScrollbar.bottom = @horizontalScrollbarHeight
@emitter.emit 'did-update-state'
updateHiddenInputState: ->
if @isInBatchMode()
@shouldUpdateHiddenInputState = true
@emitter.emit "did-update-state"
return
return unless lastCursor = @model.getLastCursor()
@@ -273,11 +269,10 @@ class TextEditorPresenter
@state.hiddenInput.height = height
@state.hiddenInput.width = Math.max(width, 2)
@emitter.emit 'did-update-state'
updateContentState: ->
if @isInBatchMode()
@shouldUpdateContentState = true
@emitter.emit "did-update-state"
return
@state.content.scrollWidth = @scrollWidth
@@ -285,11 +280,11 @@ class TextEditorPresenter
@state.content.indentGuidesVisible = not @model.isMini() and @showIndentGuide
@state.content.backgroundColor = if @model.isMini() then null else @backgroundColor
@state.content.placeholderText = if @model.isEmpty() then @model.getPlaceholderText() else null
@emitter.emit 'did-update-state'
updateLinesState: ->
if @isInBatchMode()
@shouldUpdateLinesState = true
@emitter.emit "did-update-state"
return
return unless @startRow? and @endRow? and @lineHeight?
@@ -316,8 +311,6 @@ class TextEditorPresenter
unless visibleLineIds.hasOwnProperty(id)
delete @state.content.lines[id]
@emitter.emit 'did-update-state'
updateLineState: (row, line) ->
lineState = @state.content.lines[line.id]
lineState.screenRow = row
@@ -340,18 +333,18 @@ class TextEditorPresenter
updateCursorsState: ->
if @isInBatchMode()
@shouldUpdateCursorsState = true
@emitter.emit "did-update-state"
return
@state.content.cursors = {}
@updateCursorState(cursor) for cursor in @model.cursors # using property directly to avoid allocation
@emitter.emit 'did-update-state'
updateCursorState: (cursor, destroyOnly = false) ->
if @isInBatchMode()
@shouldUpdateCursorsState = true
@emitter.emit "did-update-state"
return
delete @state.content.cursors[cursor.id]
@@ -367,6 +360,7 @@ class TextEditorPresenter
updateOverlaysState: ->
if @isInBatchMode()
@shouldUpdateOverlaysState = true
@emitter.emit "did-update-state"
return
return unless @hasPixelRectRequirements()
@@ -389,11 +383,10 @@ class TextEditorPresenter
for id of @state.content.overlays
delete @state.content.overlays[id] unless visibleDecorationIds[id]
@emitter.emit "did-update-state"
updateGutterState: ->
if @isInBatchMode()
@shouldUpdateGutterState = true
@emitter.emit "did-update-state"
return
@state.gutter.visible = not @model.isMini() and (@model.isGutterVisible() ? true) and @showLineNumbers
@@ -402,11 +395,11 @@ class TextEditorPresenter
@gutterBackgroundColor
else
@backgroundColor
@emitter.emit "did-update-state"
updateLineNumbersState: ->
if @isInBatchMode()
@shouldUpdateLineNumbersState = true
@emitter.emit "did-update-state"
return
return unless @startRow? and @endRow? and @lineHeight?
@@ -451,11 +444,10 @@ class TextEditorPresenter
for id of @state.gutter.lineNumbers
delete @state.gutter.lineNumbers[id] unless visibleLineNumberIds[id]
@emitter.emit 'did-update-state'
updateStartRow: ->
if @isInBatchMode()
@shouldUpdateStartRow = true
@emitter.emit "did-update-state"
return
return unless @scrollTop? and @lineHeight?
@@ -466,6 +458,7 @@ class TextEditorPresenter
updateEndRow: ->
if @isInBatchMode()
@shouldUpdateEndRow = true
@emitter.emit "did-update-state"
return
return unless @scrollTop? and @lineHeight? and @height?
@@ -499,9 +492,9 @@ class TextEditorPresenter
updateContentDimensions: ->
if @isInBatchMode()
@shouldUpdateContentDimensions = true
@emitter.emit "did-update-state"
return
if @lineHeight?
oldContentHeight = @contentHeight
@contentHeight = @lineHeight * @model.getScreenLineCount()
@@ -559,6 +552,7 @@ class TextEditorPresenter
updateScrollbarDimensions: ->
if @isInBatchMode()
@shouldUpdateScrollbarDimensions = true
@emitter.emit "did-update-state"
return
return unless @contentFrameWidth? and @height?
@@ -940,6 +934,7 @@ class TextEditorPresenter
updateDecorations: ->
if @isInBatchMode()
@shouldUpdateDecorations = true
@emitter.emit "did-update-state"
return
@lineDecorationsByScreenRow = {}
@@ -961,8 +956,6 @@ class TextEditorPresenter
unless visibleHighlights[id]
delete @state.content.highlights[id]
@emitter.emit 'did-update-state'
removeFromLineDecorationCaches: (decoration, range) ->
for row in [range.start.row..range.end.row] by 1
delete @lineDecorationsByScreenRow[row]?[decoration.id]

View File

@@ -85,7 +85,6 @@ class TextEditor extends Model
@cursors = []
@selections = []
@batchCount = 0
buffer ?= new TextBuffer
@displayBuffer ?= new DisplayBuffer({buffer, tabLength, softWrapped})
@buffer = @displayBuffer.buffer
@@ -93,10 +92,9 @@ class TextEditor extends Model
@updateInvisibles()
@batch =>
for marker in @findMarkers(@getSelectionMarkerAttributes())
marker.setProperties(preserveFolds: true)
@addSelection(marker)
for marker in @findMarkers(@getSelectionMarkerAttributes())
marker.setProperties(preserveFolds: true)
@addSelection(marker)
@subscribeToBuffer()
@subscribeToDisplayBuffer()
@@ -996,7 +994,7 @@ class TextEditor extends Model
# selections to create multiple single-line selections that cumulatively cover
# the same original area.
splitSelectionsIntoLines: ->
@batch => @mergeIntersectingSelections =>
@mergeIntersectingSelections =>
for selection in @getSelections()
range = selection.getBufferRange()
continue if range.isSingleLine()
@@ -1145,22 +1143,7 @@ class TextEditor extends Model
# still 'groupable', the two transactions are merged with respect to undo and redo.
# * `fn` A {Function} to call inside the transaction.
transact: (groupingInterval, fn) ->
@batch => @buffer.transact(groupingInterval, fn)
batch: (fn) ->
@batchCount++
@emitter.emit "will-start-batch-operation" if @batchCount == 1
value = fn()
@emitter.emit "did-finish-batch-operation" if @batchCount == 1
@batchCount--
value
onWillStartBatchOperation: (callback) ->
@emitter.on "will-start-batch-operation", callback
onDidFinishBatchOperation: (callback) ->
@emitter.on "did-finish-batch-operation", callback
@buffer.transact(groupingInterval, fn)
# Deprecated: Start an open-ended transaction.
beginTransaction: (groupingInterval) -> @buffer.beginTransaction(groupingInterval)
@@ -1808,9 +1791,8 @@ class TextEditor extends Model
@emitter.emit 'did-remove-cursor', cursor
moveCursors: (fn) ->
@batch =>
fn(cursor) for cursor in @getCursors()
@mergeCursors()
fn(cursor) for cursor in @getCursors()
@mergeCursors()
cursorMoved: (event) ->
@emit 'cursor-moved', event
@@ -2207,13 +2189,13 @@ class TextEditor extends Model
# Calls the given function with each selection, then merges selections
expandSelectionsForward: (fn) ->
@batch => @mergeIntersectingSelections =>
@mergeIntersectingSelections =>
fn(selection) for selection in @getSelections()
# Calls the given function with each selection, then merges selections in the
# reversed orientation
expandSelectionsBackward: (fn) ->
@batch => @mergeIntersectingSelections reversed: true, =>
@mergeIntersectingSelections reversed: true, =>
fn(selection) for selection in @getSelections()
finalizeSelections: ->