Skip to content

Commit

Permalink
fixing addMutatedBy when called multiple times with the same key
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipskevin committed Mar 22, 2018
1 parent 4b332ec commit 69402ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/add-mutated-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'four'));
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");
});

0 comments on commit 69402ac

Please sign in to comment.