Only notify when changed key path is really sub path of observed path

Closes #3775
This commit is contained in:
Ben Ogle
2014-10-13 14:25:55 -07:00
parent 9481260f6f
commit 0588e14850
2 changed files with 18 additions and 4 deletions

View File

@@ -340,6 +340,14 @@ describe "Config", ->
atom.config.set('foo.bar.baz', "value 2")
expect(observeHandler).not.toHaveBeenCalled()
it 'does not fire the callback for a similarly named keyPath', ->
bazCatHandler = jasmine.createSpy("bazCatHandler")
observeSubscription = atom.config.observe "foo.bar.bazCat", bazCatHandler
bazCatHandler.reset()
atom.config.set('foo.bar.baz', "value 10")
expect(bazCatHandler).not.toHaveBeenCalled()
describe ".initializeConfigDirectory()", ->
beforeEach ->
if fs.existsSync(dotAtomPath)

View File

@@ -747,12 +747,18 @@ class Config
observeKeyPath: (keyPath, options, callback) ->
callback(_.clone(@get(keyPath))) unless options.callNow == false
@emitter.on 'did-change', (event) ->
callback(event.newValue) if keyPath? and keyPath.indexOf(event?.keyPath) is 0
@emitter.on 'did-change', (event) =>
callback(event.newValue) if keyPath? and @isSubKeyPath(keyPath, event?.keyPath)
onDidChangeKeyPath: (keyPath, callback) ->
@emitter.on 'did-change', (event) ->
callback(event) if not keyPath? or (keyPath? and keyPath.indexOf(event?.keyPath) is 0)
@emitter.on 'did-change', (event) =>
callback(event) if not keyPath? or (keyPath? and @isSubKeyPath(keyPath, event?.keyPath))
isSubKeyPath: (keyPath, subKeyPath) ->
return false unless keyPath? and subKeyPath?
pathSubTokens = subKeyPath.split('.')
pathTokens = keyPath.split('.').slice(0, pathSubTokens.length)
_.isEqual(pathTokens, pathSubTokens)
setRawDefault: (keyPath, value) ->
oldValue = _.clone(@get(keyPath))