From 0588e148501ea47b113e00a0bafc4a4bc00460e6 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 13 Oct 2014 14:25:55 -0700 Subject: [PATCH] Only notify when changed key path is really sub path of observed path Closes #3775 --- spec/config-spec.coffee | 8 ++++++++ src/config.coffee | 14 ++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 8f6dad245..fcd2e44c3 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -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) diff --git a/src/config.coffee b/src/config.coffee index cd2476fbe..53c966d90 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -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))