Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #233 from atom/improve-stopped-changing-event-sche…
Browse files Browse the repository at this point in the history
…duling

Don't reset the "did-stop-changing" timeout for each buffer change
  • Loading branch information
Antonio Scandurra authored May 11, 2017
2 parents 68012ba + 4e55ddf commit 23d3b07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ exports.regexIsSingleLine = function (regex) {
return !MULTI_LINE_REGEX_REGEX.test(regex.source)
}

exports.debounce = function debounce (fn, wait) {
let timestamp, timeout

function later () {
const last = Date.now() - timestamp
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last)
} else {
timeout = null
fn()
}
}

return function () {
timestamp = Date.now()
if (!timeout) timeout = setTimeout(later, wait)
}
}

exports.spliceArray = function (array, start, removedCount, insertedItems = []) {
const oldLength = array.length
const insertedCount = insertedItems.length
Expand Down
29 changes: 14 additions & 15 deletions src/text-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ History = require './history'
MarkerLayer = require './marker-layer'
MatchIterator = require './match-iterator'
DisplayLayer = require './display-layer'
{spliceArray, newlineRegex, normalizePatchChanges, regexIsSingleLine} = require './helpers'
{spliceArray, newlineRegex, normalizePatchChanges, regexIsSingleLine, debounce} = require './helpers'
{traversal} = require './point-helpers'

class TransactionAbortedError extends Error
Expand Down Expand Up @@ -105,6 +105,7 @@ class TextBuffer
@lines = ['']
@lineEndings = ['']
@offsetIndex = new BufferOffsetIndex()
@debouncedEmitDidStopChangingEvent = debounce(@emitDidStopChangingEvent.bind(this), @stoppedChangingDelay)
@textDecorationLayers = new Set()
@setTextInRange([[0, 0], [0, 0]], text ? params?.text ? '', normalizeLineEndings: false)
maxUndoEntries = params?.maxUndoEntries ? @defaultMaxUndoEntries
Expand Down Expand Up @@ -1567,7 +1568,6 @@ class TextBuffer
@emitter.emit 'did-destroy'
@emitter.clear()

@cancelStoppedChangingTimeout()
@fileSubscriptions?.dispose()
for id, markerLayer of @markerLayers
markerLayer.destroy()
Expand Down Expand Up @@ -1658,7 +1658,7 @@ class TextBuffer
if hunks.length > 0
@emitter.emit 'did-change-text', {changes: Object.freeze(normalizePatchChanges(hunks))}
@patchesSinceLastStoppedChangingEvent.push(patch)
@scheduleDidStopChangingEvent()
@debouncedEmitDidStopChangingEvent()

# Identifies if the buffer belongs to multiple editors.
#
Expand All @@ -1667,18 +1667,17 @@ class TextBuffer
# Returns a {Boolean}.
hasMultipleEditors: -> @refcount > 1

cancelStoppedChangingTimeout: ->
clearTimeout(@stoppedChangingTimeout) if @stoppedChangingTimeout

scheduleDidStopChangingEvent: ->
@cancelStoppedChangingTimeout()
stoppedChangingCallback = =>
@stoppedChangingTimeout = null
modifiedStatus = @isModified()
@emitter.emit 'did-stop-changing', {changes: Object.freeze(normalizePatchChanges(Patch.compose(@patchesSinceLastStoppedChangingEvent).getHunks()))}
@patchesSinceLastStoppedChangingEvent = []
@emitModifiedStatusChanged(modifiedStatus)
@stoppedChangingTimeout = setTimeout(stoppedChangingCallback, @stoppedChangingDelay)
emitDidStopChangingEvent: ->
return if @destroyed

modifiedStatus = @isModified()
composedChanges = Patch.compose(@patchesSinceLastStoppedChangingEvent).getHunks()
@emitter.emit(
'did-stop-changing',
{changes: Object.freeze(normalizePatchChanges(composedChanges))}
)
@patchesSinceLastStoppedChangingEvent = []
@emitModifiedStatusChanged(modifiedStatus)

emitModifiedStatusChanged: (modifiedStatus) ->
return if modifiedStatus is @previousModifiedStatus
Expand Down

0 comments on commit 23d3b07

Please sign in to comment.