Fix logic error when exception is thrown in config observer

This commit is contained in:
Max Brunsfeld 2014-12-23 16:20:37 -08:00
parent f439d0d996
commit 388428b074
2 changed files with 19 additions and 14 deletions

View File

@ -493,13 +493,19 @@ describe "Config", ->
expect(observeHandler).not.toHaveBeenCalled() expect(observeHandler).not.toHaveBeenCalled()
it "fires the callback every time the observed value changes", -> it "fires the callback every time the observed value changes", ->
observeHandler.reset() # clear the initial call
atom.config.set('foo.bar.baz', "value 2") atom.config.set('foo.bar.baz', "value 2")
expect(observeHandler).toHaveBeenCalledWith({newValue: 'value 2', oldValue: 'value 1'}) expect(observeHandler).toHaveBeenCalledWith({newValue: 'value 2', oldValue: 'value 1'})
observeHandler.reset() observeHandler.reset()
atom.config.set('foo.bar.baz', "value 1") observeHandler.andCallFake -> throw new Error("oops")
expect(-> atom.config.set('foo.bar.baz', "value 1")).toThrow("oops")
expect(observeHandler).toHaveBeenCalledWith({newValue: 'value 1', oldValue: 'value 2'}) expect(observeHandler).toHaveBeenCalledWith({newValue: 'value 1', oldValue: 'value 2'})
observeHandler.reset()
# Regression: exception in earlier handler shouldn't put observer
# into a bad state.
atom.config.set('something.else', "new value")
expect(observeHandler).not.toHaveBeenCalled()
describe 'when a keyPath is not specified', -> describe 'when a keyPath is not specified', ->
beforeEach -> beforeEach ->
@ -567,7 +573,7 @@ describe "Config", ->
atom.config.set("foo.bar.baz", "i'm back") atom.config.set("foo.bar.baz", "i'm back")
expect(observeHandler).toHaveBeenCalledWith("i'm back") expect(observeHandler).toHaveBeenCalledWith("i'm back")
it "does not fire the callback once the observe subscription is off'ed", -> it "does not fire the callback once the subscription is disposed", ->
observeHandler.reset() # clear the initial call observeHandler.reset() # clear the initial call
observeSubscription.dispose() observeSubscription.dispose()
atom.config.set('foo.bar.baz', "value 2") atom.config.set('foo.bar.baz', "value 2")

View File

@ -882,13 +882,12 @@ class Config
onDidChangeKeyPath: (keyPath, callback) -> onDidChangeKeyPath: (keyPath, callback) ->
oldValue = @get(keyPath) oldValue = @get(keyPath)
@emitter.on 'did-change', =>
didChange = =>
newValue = @get(keyPath) newValue = @get(keyPath)
callback({oldValue, newValue}) unless _.isEqual(oldValue, newValue) unless _.isEqual(oldValue, newValue)
oldValue = newValue event = {oldValue, newValue}
oldValue = newValue
@emitter.on 'did-change', didChange callback(event)
isSubKeyPath: (keyPath, subKeyPath) -> isSubKeyPath: (keyPath, subKeyPath) ->
return false unless keyPath? and subKeyPath? return false unless keyPath? and subKeyPath?
@ -1003,12 +1002,12 @@ class Config
onDidChangeScopedKeyPath: (scope, keyPath, callback) -> onDidChangeScopedKeyPath: (scope, keyPath, callback) ->
oldValue = @get(keyPath, {scope}) oldValue = @get(keyPath, {scope})
didChange = => @emitter.on 'did-change', =>
newValue = @get(keyPath, {scope}) newValue = @get(keyPath, {scope})
callback({oldValue, newValue}) unless _.isEqual(oldValue, newValue) unless _.isEqual(oldValue, newValue)
oldValue = newValue event = {oldValue, newValue}
oldValue = newValue
@emitter.on 'did-change', didChange callback(event)
# TODO: figure out how to change / remove this. The return value is awkward. # TODO: figure out how to change / remove this. The return value is awkward.
# * language mode uses it for one thing. # * language mode uses it for one thing.