diff --git a/spec/app/display-buffer-spec.coffee b/spec/app/display-buffer-spec.coffee index 274a2d4e8..0063bd791 100644 --- a/spec/app/display-buffer-spec.coffee +++ b/spec/app/display-buffer-spec.coffee @@ -557,17 +557,17 @@ describe "DisplayBuffer", -> expect(marker.setTailBufferPosition([1, 0])).toBeTruthy() expect(marker.setTailBufferPosition([1, 0])).toBeFalsy() - describe "marker observation", -> - [observeHandler, marker, subscription] = [] + describe "marker change events", -> + [changedHandler, marker] = [] beforeEach -> marker = displayBuffer.markScreenRange([[5, 4], [5, 10]]) - subscription = marker.observe(observeHandler = jasmine.createSpy("observeHandler")) + marker.on 'changed', changedHandler = jasmine.createSpy("changedHandler") - it "calls the callback whenever the markers head's screen position changes in the buffer or on screen", -> + it "triggers the 'changed' event whenever the markers head's screen position changes in the buffer or on screen", -> marker.setHeadScreenPosition([8, 20]) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [5, 10] oldHeadBufferPosition: [8, 10] newHeadScreenPosition: [8, 20] @@ -579,11 +579,11 @@ describe "DisplayBuffer", -> bufferChanged: false valid: true } - observeHandler.reset() + changedHandler.reset() buffer.insert([11, 0], '...') - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [8, 20] oldHeadBufferPosition: [11, 20] newHeadScreenPosition: [8, 23] @@ -595,11 +595,11 @@ describe "DisplayBuffer", -> bufferChanged: true valid: true } - observeHandler.reset() + changedHandler.reset() displayBuffer.destroyFoldsContainingBufferRow(4) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [8, 23] oldHeadBufferPosition: [11, 23] newHeadScreenPosition: [11, 23] @@ -611,11 +611,11 @@ describe "DisplayBuffer", -> bufferChanged: false valid: true } - observeHandler.reset() + changedHandler.reset() displayBuffer.createFold(4, 7) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [11, 23] oldHeadBufferPosition: [11, 23] newHeadScreenPosition: [8, 23] @@ -628,10 +628,10 @@ describe "DisplayBuffer", -> valid: true } - it "calls the callback whenever the marker tail's position changes in the buffer or on screen", -> + it "triggers the 'changed' event whenever the marker tail's position changes in the buffer or on screen", -> marker.setTailScreenPosition([8, 20]) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [5, 10] oldHeadBufferPosition: [8, 10] newHeadScreenPosition: [5, 10] @@ -643,11 +643,11 @@ describe "DisplayBuffer", -> bufferChanged: false valid: true } - observeHandler.reset() + changedHandler.reset() buffer.insert([11, 0], '...') - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [5, 10] oldHeadBufferPosition: [8, 10] newHeadScreenPosition: [5, 10] @@ -660,10 +660,10 @@ describe "DisplayBuffer", -> valid: true } - it "calls the callback whenever the marker is invalidated or revalidated", -> + it "triggers the 'changed' event whenever the marker is invalidated or revalidated", -> buffer.deleteRow(8) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [5, 10] oldHeadBufferPosition: [8, 10] newHeadScreenPosition: [5, 10] @@ -676,11 +676,11 @@ describe "DisplayBuffer", -> valid: false } - observeHandler.reset() + changedHandler.reset() buffer.undo() - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadScreenPosition: [5, 10] oldHeadBufferPosition: [8, 10] newHeadScreenPosition: [5, 10] @@ -695,18 +695,12 @@ describe "DisplayBuffer", -> it "does not call the callback for screen changes that don't change the position of the marker", -> displayBuffer.createFold(10, 11) - expect(observeHandler).not.toHaveBeenCalled() - - it "allows observation subscriptions to be cancelled", -> - subscription.cancel() - marker.setHeadScreenPosition([8, 20]) - displayBuffer.destroyFoldsContainingBufferRow(4) - expect(observeHandler).not.toHaveBeenCalled() + expect(changedHandler).not.toHaveBeenCalled() it "updates the position of markers before emitting buffer change events, but does not notify their observers until the change event", -> displayBuffer.on 'changed', changeHandler = jasmine.createSpy("changeHandler").andCallFake -> # calls change handler first - expect(observeHandler).not.toHaveBeenCalled() + expect(changedHandler).not.toHaveBeenCalled() # but still updates the markers expect(marker.getScreenRange()).toEqual [[5, 7], [5, 13]] expect(marker.getHeadScreenPosition()).toEqual [5, 13] @@ -715,12 +709,12 @@ describe "DisplayBuffer", -> buffer.insert([8, 1], "...") expect(changeHandler).toHaveBeenCalled() - expect(observeHandler).toHaveBeenCalled() + expect(changedHandler).toHaveBeenCalled() it "updates the position of markers before emitting change events that aren't caused by a buffer change", -> displayBuffer.on 'changed', changeHandler = jasmine.createSpy("changeHandler").andCallFake -> # calls change handler first - expect(observeHandler).not.toHaveBeenCalled() + expect(changedHandler).not.toHaveBeenCalled() # but still updates the markers expect(marker.getScreenRange()).toEqual [[8, 4], [8, 10]] expect(marker.getHeadScreenPosition()).toEqual [8, 10] @@ -729,7 +723,7 @@ describe "DisplayBuffer", -> displayBuffer.destroyFoldsContainingBufferRow(4) expect(changeHandler).toHaveBeenCalled() - expect(observeHandler).toHaveBeenCalled() + expect(changedHandler).toHaveBeenCalled() describe ".findMarkers(attributes)", -> it "allows the startBufferRow and endBufferRow to be specified", -> diff --git a/spec/app/text-buffer-spec.coffee b/spec/app/text-buffer-spec.coffee index 3b09591c3..68a63c4e3 100644 --- a/spec/app/text-buffer-spec.coffee +++ b/spec/app/text-buffer-spec.coffee @@ -843,18 +843,17 @@ describe 'Buffer', -> expect(marker.setTailPosition([6, 3])).toBeTruthy() expect(marker.setTailPosition([6, 3])).toBeFalsy() - describe ".observeMarker(marker, callback)", -> - [observeHandler, marker, subscription] = [] + describe "change events", -> + [changedHandler, marker] = [] beforeEach -> - observeHandler = jasmine.createSpy("observeHandler") marker = buffer.markRange([[4, 20], [4, 23]]) - subscription = marker.observe(observeHandler) + marker.on 'changed', changedHandler = jasmine.createSpy("changedHandler") - it "calls the callback when the marker's head position changes", -> + it "triggers 'changed' events when the marker's head position changes", -> marker.setHeadPosition([6, 2]) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadPosition: [4, 23] newHeadPosition: [6, 2] oldTailPosition: [4, 20] @@ -862,10 +861,10 @@ describe 'Buffer', -> bufferChanged: false valid: true } - observeHandler.reset() + changedHandler.reset() buffer.insert([6, 0], '...') - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler.argsForCall[0][0]).toEqual { oldTailPosition: [4, 20] newTailPosition: [4, 20] oldHeadPosition: [6, 2] @@ -876,8 +875,8 @@ describe 'Buffer', -> it "calls the given callback when the marker's tail position changes", -> marker.setTailPosition([6, 2]) - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadPosition: [4, 23] newHeadPosition: [4, 23] oldTailPosition: [4, 20] @@ -885,11 +884,11 @@ describe 'Buffer', -> bufferChanged: false valid: true } - observeHandler.reset() + changedHandler.reset() buffer.insert([6, 0], '...') - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadPosition: [4, 23] newHeadPosition: [4, 23] oldTailPosition: [6, 2] @@ -898,10 +897,10 @@ describe 'Buffer', -> valid: true } - it "calls the callback when the selection's tail is cleared", -> + it "triggers 'changed' events when the selection's tail is cleared", -> marker.clearTail() - expect(observeHandler).toHaveBeenCalled() - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler).toHaveBeenCalled() + expect(changedHandler.argsForCall[0][0]).toEqual { oldHeadPosition: [4, 23] newHeadPosition: [4, 23] oldTailPosition: [4, 20] @@ -910,10 +909,10 @@ describe 'Buffer', -> valid: true } - it "only calls the callback once when both the marker's head and tail positions change due to the same operation", -> + it "only triggers 'changed' events once when both the marker's head and tail positions change due to the same operation", -> buffer.insert([4, 0], '...') - expect(observeHandler.callCount).toBe 1 - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler.callCount).toBe 1 + expect(changedHandler.argsForCall[0][0]).toEqual { oldTailPosition: [4, 20] newTailPosition: [4, 23] oldHeadPosition: [4, 23] @@ -921,11 +920,11 @@ describe 'Buffer', -> bufferChanged: true valid: true } - observeHandler.reset() + changedHandler.reset() marker.setRange([[0, 0], [1, 1]]) - expect(observeHandler.callCount).toBe 1 - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler.callCount).toBe 1 + expect(changedHandler.argsForCall[0][0]).toEqual { oldTailPosition: [4, 23] newTailPosition: [0, 0] oldHeadPosition: [4, 26] @@ -934,10 +933,10 @@ describe 'Buffer', -> valid: true } - it "calls the callback with the valid flag set to false when the marker is invalidated", -> + it "triggers 'changed' events with the valid flag set to false when the marker is invalidated", -> buffer.deleteRow(4) - expect(observeHandler.callCount).toBe 1 - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler.callCount).toBe 1 + expect(changedHandler.argsForCall[0][0]).toEqual { oldTailPosition: [4, 20] newTailPosition: [4, 20] oldHeadPosition: [4, 23] @@ -946,10 +945,10 @@ describe 'Buffer', -> valid: false } - observeHandler.reset() + changedHandler.reset() buffer.undo() - expect(observeHandler.callCount).toBe 1 - expect(observeHandler.argsForCall[0][0]).toEqual { + expect(changedHandler.callCount).toBe 1 + expect(changedHandler.argsForCall[0][0]).toEqual { oldTailPosition: [4, 20] newTailPosition: [4, 20] oldHeadPosition: [4, 23] @@ -958,11 +957,6 @@ describe 'Buffer', -> valid: true } - it "allows the observation subscription to be cancelled", -> - subscription.cancel() - marker.setHeadPosition([6, 2]) - expect(observeHandler).not.toHaveBeenCalled() - describe ".findMarkers(attributes)", -> [marker1, marker2, marker3, marker4] = [] diff --git a/src/app/buffer-marker.coffee b/src/app/buffer-marker.coffee index 33d3eec91..eb9c2935f 100644 --- a/src/app/buffer-marker.coffee +++ b/src/app/buffer-marker.coffee @@ -153,15 +153,6 @@ class BufferMarker containsPoint: (point) -> @getRange().containsPoint(point) - # Public: Sets a callback to be fired whenever a marker is changed. - observe: (callback) -> - @on 'changed', callback - cancel: => @unobserve(callback) - - # Public: Removes the fired callback whenever a marker changes. - unobserve: (callback) -> - @off 'changed', callback - # Destroys the marker destroy: -> @buffer.destroyMarker(@id) diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index d35a03c55..ade42e347 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -20,7 +20,7 @@ class Cursor constructor: ({@editSession, @marker}) -> @updateVisibility() - @marker.observe (e) => + @marker.on 'changed', (e) => @updateVisibility() {oldHeadScreenPosition, newHeadScreenPosition} = e {oldHeadBufferPosition, newHeadBufferPosition} = e diff --git a/src/app/display-buffer-marker.coffee b/src/app/display-buffer-marker.coffee index 5c2b4d857..fc82ecfe8 100644 --- a/src/app/display-buffer-marker.coffee +++ b/src/app/display-buffer-marker.coffee @@ -15,7 +15,7 @@ class DisplayBufferMarker constructor: ({@bufferMarker, @displayBuffer}) -> @id = @bufferMarker.id - @bufferMarker.on 'destroyed', => @destroyed() + @observeBufferMarker() ### # Public # @@ -113,21 +113,6 @@ class DisplayBufferMarker clearTail: -> @bufferMarker.clearTail() - # Public: Sets a callback to be fired whenever the marker is changed. - # - # callback - A {Function} to execute - observe: (callback) -> - @observeBufferMarkerIfNeeded() - @on 'changed', callback - cancel: => @unobserve(callback) - - # Public: Removes the callback that's fired whenever the marker changes. - # - # callback - A {Function} to remove - unobserve: (callback) -> - @off 'changed', callback - @unobserveBufferMarkerIfNeeded() - # Returns whether the head precedes the tail in the buffer isReversed: -> @bufferMarker.isReversed() @@ -159,23 +144,19 @@ class DisplayBufferMarker destroyed: -> delete @displayBuffer.markers[@id] - observeBufferMarkerIfNeeded: -> - return if @subscriptionCount() + observeBufferMarker: -> + @bufferMarker.on 'destroyed', => @destroyed() + @getHeadScreenPosition() # memoize current value @getTailScreenPosition() # memoize current value - @bufferMarkerSubscription = - @bufferMarker.observe ({oldHeadPosition, newHeadPosition, oldTailPosition, newTailPosition, bufferChanged, valid}) => - @notifyObservers - oldHeadBufferPosition: oldHeadPosition - newHeadBufferPosition: newHeadPosition - oldTailBufferPosition: oldTailPosition - newTailBufferPosition: newTailPosition - bufferChanged: bufferChanged - valid: valid - - unobserveBufferMarkerIfNeeded: -> - return if @subscriptionCount() - @bufferMarkerSubscription.cancel() + @bufferMarker.on 'changed', ({oldHeadPosition, newHeadPosition, oldTailPosition, newTailPosition, bufferChanged, valid}) => + @notifyObservers + oldHeadBufferPosition: oldHeadPosition + newHeadBufferPosition: newHeadPosition + oldTailBufferPosition: oldTailPosition + newTailBufferPosition: newTailPosition + bufferChanged: bufferChanged + valid: valid notifyObservers: ({oldHeadBufferPosition, oldTailBufferPosition, bufferChanged, valid} = {}) -> oldHeadScreenPosition = @getHeadScreenPosition() diff --git a/src/app/selection.coffee b/src/app/selection.coffee index 7528692e8..1d4424ce2 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -19,7 +19,7 @@ class Selection constructor: ({@cursor, @marker, @editSession, @goalBufferRange}) -> @cursor.selection = this - @marker.observe => @screenRangeChanged() + @marker.on 'changed', => @screenRangeChanged() @cursor.on 'destroyed.selection', => @cursor = null @destroy() diff --git a/src/packages/spell-check/lib/misspelling-view.coffee b/src/packages/spell-check/lib/misspelling-view.coffee index 05ce0a800..0dae2e9a5 100644 --- a/src/packages/spell-check/lib/misspelling-view.coffee +++ b/src/packages/spell-check/lib/misspelling-view.coffee @@ -15,7 +15,7 @@ class MisspellingView extends View @misspellingValid = true @marker = @editSession.markScreenRange(range, invalidationStrategy: 'between') - @marker.observe ({newHeadScreenPosition, newTailScreenPosition, valid}) => + @marker.on 'changed', ({newHeadScreenPosition, newTailScreenPosition, valid}) => @startPosition = newTailScreenPosition @endPosition = newHeadScreenPosition @updateDisplayPosition = valid