Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing addMutatedBy when called multiple times with the same key #16

Merged
merged 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});