Add 'stopped-changing' event to buffer. Fires 300ms after last change.

This will be used by the status bar and other expensive code that needs to respond to the buffer changing, but that we don't want running on every single keystroke when the user is typing quickly.
This commit is contained in:
Nathan Sobo 2012-11-14 15:33:44 -07:00
parent 632bba5609
commit 4a84c5f8f9
3 changed files with 31 additions and 0 deletions

View File

@ -727,3 +727,23 @@ describe 'Buffer', ->
expect(buffer.isEmpty()).toBeFalsy()
buffer.setText('\n')
expect(buffer.isEmpty()).toBeFalsy()
describe "stopped-changing event", ->
it "fires 'stoppedChangingDelay' ms after the last buffer change", ->
delay = buffer.stoppedChangingDelay
stoppedChangingHandler = jasmine.createSpy("stoppedChangingHandler")
buffer.on 'stopped-changing', stoppedChangingHandler
buffer.insert([0, 0], 'a')
expect(stoppedChangingHandler).not.toHaveBeenCalled()
advanceClock(delay / 2)
buffer.insert([0, 0], 'b')
expect(stoppedChangingHandler).not.toHaveBeenCalled()
advanceClock(delay / 2)
expect(stoppedChangingHandler).not.toHaveBeenCalled()
advanceClock(delay / 2)
expect(stoppedChangingHandler).toHaveBeenCalled()

View File

@ -41,6 +41,8 @@ class BufferChangeOperation
event = { oldRange, newRange, oldText, newText }
@buffer.trigger 'change', event
@buffer.scheduleStoppedChangingEvent()
anchor.handleBufferChange(event) for anchor in @buffer.getAnchors()
@buffer.trigger 'update-anchors-after-change'
newRange

View File

@ -13,6 +13,8 @@ Git = require 'git'
module.exports =
class Buffer
@idCounter = 1
stoppedChangingDelay: 300
stoppedChangingTimeout: null
undoManager: null
cachedDiskContents: null
cachedMemoryContents: null
@ -378,4 +380,11 @@ class Buffer
if @git?.checkoutHead(path)
@trigger 'git-status-change'
scheduleStoppedChangingEvent: ->
clearTimeout(@stoppedChangingTimeout) if @stoppedChangingTimeout
stoppedChangingCallback = =>
@stoppedChangingTimeout = null
@trigger 'stopped-changing'
@stoppedChangingTimeout = setTimeout(stoppedChangingCallback, @stoppedChangingDelay)
_.extend(Buffer.prototype, EventEmitter)