From 11731f98efebb63106a54f3a1f5deeae38a8c735 Mon Sep 17 00:00:00 2001 From: Kevin Phillips Date: Thu, 22 Mar 2018 18:56:52 -0500 Subject: [PATCH] fixing addMutatedBy when called multiple times with the same key --- src/add-mutated-by.js | 6 ++++-- test.js | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/add-mutated-by.js b/src/add-mutated-by.js index c998a5f..3064047 100644 --- a/src/add-mutated-by.js +++ b/src/add-mutated-by.js @@ -42,11 +42,13 @@ module.exports = function(mutatedByMap) { mutatedByMap.set(mutated, root); } - // retrieve or create a [key] DependencyRecord, if [key] was provided - if (gotKey) { + // create a [key] DependencyRecord if [key] was provided + // and Record does not already exist + if (gotKey && !root.mutateDependenciesForKey.get(key)) { root.mutateDependenciesForKey.set(key, makeDependencyRecord()); } + // retrieve DependencyRecord var dependencyRecord = gotKey ? root.mutateDependenciesForKey.get(key) : root.mutateDependenciesForValue; diff --git a/test.js b/test.js index 89b02de..4f51163 100644 --- a/test.js +++ b/test.js @@ -129,3 +129,52 @@ QUnit.test("key - key & value dependencies", function(assert) { "undefined" ); }); + +QUnit.test("key - two value dependencies (#15)", function(assert) { + var source = new SimpleMap({ + key: "keyValue" + }); + var one = new SimpleObservable("one"); + var two = new SimpleObservable("two"); + + // canReflect.onValue(one, _ => source.set('key', 'three')); + canReflectDeps.addMutatedBy(source, "key", one); + + assert.deepEqual( + canReflectDeps + .getDependencyDataOf(source, "key") + .whatChangesMe + .mutate + .valueDependencies, + new Set([ one ]), + "key -> Set([ one ])" + ); + + // canReflect.onValue(two, _ => source.set('key', 'three')); + canReflectDeps.addMutatedBy(source, "key", two); + + assert.deepEqual( + canReflectDeps + .getDependencyDataOf(source, "key") + .whatChangesMe + .mutate + .valueDependencies, + new Set([ one, two ]), + "key -> Set([ one, two ])" + ); + + canReflectDeps.deleteMutatedBy(source, "key", one); + + assert.deepEqual( + canReflectDeps + .getDependencyDataOf(source, "key") + .whatChangesMe + .mutate + .valueDependencies, + new Set([ two ]), + "key -> Set([ two ])" + ); + + canReflectDeps.deleteMutatedBy(source, "key", two); + assert.equal(typeof canReflectDeps.getDependencyDataOf(source), "undefined"); +});