Add ability to cancel marker head position observations

This commit is contained in:
Nathan Sobo
2013-01-31 14:11:28 -07:00
parent bc44540b10
commit ec521b3fd3
4 changed files with 38 additions and 4 deletions

View File

@@ -53,6 +53,8 @@ class BufferMarker
observeHeadPosition: (callback) ->
@headPositionObservers.push(callback)
cancel: =>
_.remove(@headPositionObservers, callback)
tryToInvalidate: (oldRange) ->
containsStart = oldRange.containsPoint(@getStartPosition(), exclusive: true)

View File

@@ -355,10 +355,18 @@ class DisplayBuffer
@markerScreenPositionObservers[id] ?= { head: [], tail: [] }
@cacheMarkerScreenPositions(id) unless @markerScreenPositions[id]
@markerScreenPositionObservers[id].head.push(callback)
@buffer.observeMarkerHeadPosition id, (bufferPosition) =>
subscription = @buffer.observeMarkerHeadPosition id, (bufferPosition) =>
@cacheMarkerScreenPositions(id)
callback(@screenPositionForBufferPosition(bufferPosition))
cancel: =>
subscription.cancel()
{ head, tail } = @markerScreenPositionObservers[id]
_.remove(head, callback)
unless head.length + tail.length
delete @markerScreenPositionObservers[id]
delete @markerScreenPositions[id]
cacheMarkerScreenPositions: (id) ->
@markerScreenPositions[id] = { head: @getMarkerHeadScreenPosition(id), tail: @getMarkerTailScreenPosition }