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

Commit

Permalink
Introduce MarkerLayer.prototype.getLastMarker()
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Scandurra committed May 4, 2016
1 parent 63d47f3 commit f87f806
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/marker-layer-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ describe "MarkerLayer", ->
expect(createEventCount).toBe 1
expect(updateEventCount).toBe 2

describe "::getLastMarker()", ->
it "returns the last (in terms of time) non-destroyed marker added to the layer", ->
marker1 = layer1.markRange([[0, 0], [0, 3]])
marker2 = layer1.markRange([[0, 2], [0, 6]])
marker3 = layer1.markRange([[0, 8], [0, 10]])
marker4 = layer1.markRange([[1, 0], [1, 10]])
expect(layer1.getLastMarker()).toBe(marker4)

marker4.destroy()
expect(layer1.getLastMarker()).toBe(marker3)

marker1.destroy()
expect(layer1.getLastMarker()).toBe(marker3)

marker3.destroy()
expect(layer1.getLastMarker()).toBe(marker2)

marker2.destroy()
expect(layer1.getLastMarker()).toBeFalsy()

describe "when destroyInvalidatedMarkers is enabled for the layer", ->
it "destroys markers when they are invalidated via a splice", ->
layer3 = buffer.addMarkerLayer(destroyInvalidatedMarkers: true)
Expand Down
6 changes: 6 additions & 0 deletions src/display-marker-layer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ class DisplayMarkerLayer
getMarkers: ->
@bufferMarkerLayer.getMarkers().map ({id}) => @getMarker(id)

# Public: Get the last (in terms of time) non-destroyed marker added to this layer.
#
# Returns a {DisplayMarker}.
getLastMarker: ->
@getMarker(@bufferMarkerLayer.getLastMarker()?.id)

# Public: Get the number of markers in the marker layer.
#
# Returns a {Number}.
Expand Down
22 changes: 22 additions & 0 deletions src/marker-layer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class MarkerLayer
getMarker: (id) ->
@markersById.get(id)

# Public: Get the last (in terms of time) non-destroyed marker added to this layer.
#
# Returns a {Marker}.
getLastMarker: ->
@getMarker(@markers[@markers.length - 1])

# Public: Get all existing markers on the marker layer.
#
# Returns an {Array} of {Marker}s.
Expand Down Expand Up @@ -334,6 +340,8 @@ class MarkerLayer
destroyMarker: (id) ->
if @markersById.has(id)
@markersById.delete(id)
index = @indexForMarkerId(id)
@markers.splice(index, 1) if index isnt -1
@markersIdsWithChangeSubscriptions.delete(id)
@index.delete(id)
@destroyedMarkers.add(id)
Expand Down Expand Up @@ -382,8 +390,22 @@ class MarkerLayer
@index.insert(id, range.start, range.end)
marker = new Marker(id, this, range, params)
@markersById.set(id, marker)
@markers.push(id)
marker

indexForMarkerId: (id) ->
low = 0
high = @markers.length - 1
while low <= high
index = low + ((high - low) >> 1)
if id < @markers[index]
high = index - 1
else if id is @markers[index]
return index
else
low = index + 1
-1

setDisableDidUpdateEvent: (@didUpdateEventDisabled) ->

emitDidUpdateEvent: ->
Expand Down

0 comments on commit f87f806

Please sign in to comment.