From e36887900f82db305cc9fcd1de0b33543c914ba5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki & Nathan Sobo Date: Tue, 26 Feb 2013 14:31:42 -0800 Subject: [PATCH] Notify observers when markers are invalidated/revalidated --- spec/app/buffer-spec.coffee | 31 ++++++++++++++++++++++++++ src/app/buffer-change-operation.coffee | 3 +-- src/app/buffer-marker.coffee | 24 +++++++++++++++----- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/spec/app/buffer-spec.coffee b/spec/app/buffer-spec.coffee index 635f7d519..e355dd6a9 100644 --- a/spec/app/buffer-spec.coffee +++ b/spec/app/buffer-spec.coffee @@ -741,6 +741,7 @@ describe 'Buffer', -> oldTailPosition: [4, 20] newTailPosition: [4, 20] bufferChanged: false + valid: true } observeHandler.reset() @@ -751,6 +752,7 @@ describe 'Buffer', -> oldHeadPosition: [6, 2] newHeadPosition: [6, 5] bufferChanged: true + valid: true } it "calls the given callback when the marker's tail position changes", -> @@ -762,6 +764,7 @@ describe 'Buffer', -> oldTailPosition: [4, 20] newTailPosition: [6, 2] bufferChanged: false + valid: true } observeHandler.reset() @@ -773,6 +776,7 @@ describe 'Buffer', -> oldTailPosition: [6, 2] newTailPosition: [6, 5] bufferChanged: true + valid: true } it "calls the callback when the selection's tail is cleared", -> @@ -784,6 +788,7 @@ describe 'Buffer', -> oldTailPosition: [4, 20] newTailPosition: [4, 23] bufferChanged: false + valid: true } it "only calls the callback once when both the marker's head and tail positions change due to the same operation", -> @@ -795,6 +800,7 @@ describe 'Buffer', -> oldHeadPosition: [4, 23] newHeadPosition: [4, 26] bufferChanged: true + valid: true } observeHandler.reset() @@ -806,6 +812,31 @@ describe 'Buffer', -> oldHeadPosition: [4, 26] newHeadPosition: [1, 1] bufferChanged: false + valid: true + } + + it "calls the callback 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 { + oldTailPosition: [4, 20] + newTailPosition: [4, 20] + oldHeadPosition: [4, 23] + newHeadPosition: [4, 23] + bufferChanged: true + valid: false + } + + observeHandler.reset() + buffer.undo() + expect(observeHandler.callCount).toBe 1 + expect(observeHandler.argsForCall[0][0]).toEqual { + oldTailPosition: [4, 20] + newTailPosition: [4, 20] + oldHeadPosition: [4, 23] + newHeadPosition: [4, 23] + bufferChanged: true + valid: true } it "allows the observation subscription to be cancelled", -> diff --git a/src/app/buffer-change-operation.coffee b/src/app/buffer-change-operation.coffee index 928b4a07b..b3ffda35e 100644 --- a/src/app/buffer-change-operation.coffee +++ b/src/app/buffer-change-operation.coffee @@ -107,5 +107,4 @@ class BufferChangeOperation if validMarker = @buffer.validMarkers[id] validMarker.setRange(previousRange) else if invalidMarker = @buffer.invalidMarkers[id] - @buffer.validMarkers[id] = invalidMarker - + invalidMarker.revalidate() diff --git a/src/app/buffer-marker.coffee b/src/app/buffer-marker.coffee index c8693372e..e3206ee9e 100644 --- a/src/app/buffer-marker.coffee +++ b/src/app/buffer-marker.coffee @@ -113,23 +113,31 @@ class BufferMarker [newRow, newColumn] observe: (callback) -> - @on 'position-changed', callback + @on 'changed', callback cancel: => @unobserve(callback) unobserve: (callback) -> - @off 'position-changed', callback + @off 'changed', callback containsPoint: (point) -> @getRange().containsPoint(point) - notifyObservers: ({oldHeadPosition, newHeadPosition, oldTailPosition, newTailPosition, bufferChanged}) -> + notifyObservers: ({oldHeadPosition, newHeadPosition, oldTailPosition, newTailPosition, bufferChanged} = {}) -> return if @suppressObserverNotification - return if _.isEqual(newHeadPosition, oldHeadPosition) and _.isEqual(newTailPosition, oldTailPosition) + + if newHeadPosition? and newTailPosition? + return if _.isEqual(newHeadPosition, oldHeadPosition) and _.isEqual(newTailPosition, oldTailPosition) + else if newHeadPosition? + return if _.isEqual(newHeadPosition, oldHeadPosition) + else if newTailPosition? + return if _.isEqual(newTailPosition, oldTailPosition) + oldHeadPosition ?= @getHeadPosition() newHeadPosition ?= @getHeadPosition() oldTailPosition ?= @getTailPosition() newTailPosition ?= @getTailPosition() - @trigger 'position-changed', {oldHeadPosition, newHeadPosition, oldTailPosition, newTailPosition, bufferChanged} + valid = @buffer.validMarkers[@id]? + @trigger 'changed', {oldHeadPosition, newHeadPosition, oldTailPosition, newTailPosition, bufferChanged, valid} consolidateObserverNotifications: (bufferChanged, fn) -> @suppressObserverNotification = true @@ -144,5 +152,11 @@ class BufferMarker invalidate: -> delete @buffer.validMarkers[@id] @buffer.invalidMarkers[@id] = this + @notifyObservers(bufferChanged: true) + + revalidate: -> + delete @buffer.invalidMarkers[@id] + @buffer.validMarkers[@id] = this + @notifyObservers(bufferChanged: true) _.extend BufferMarker.prototype, EventEmitter