addMutatedBy
Register (one to many) mutation dependencies.
.addMutatedBy(observable, [key], observableOrDependencyRecord)
Register a mutation dependency between an observable and optional key
, to one or
many observables.
Mutation dependencies happen when an observable sets another observable's value,
the following example creates two simple observable
instances, then listens to the value event on the one
observable and sets the
value of the observable bound to the two
variable:
import canReflect from "can-reflect";
import SimpleObservable from "can-simple-observable";
import canReflectDeps from "can-reflect-dependencies";
const one = new SimpleObservable( "one" );
const two = new SimpleObservable( "two" );
canReflect.onValue( one, function() {
two.set( /* new value */ );
} );
In order to register this dependency (which is critical for can-debug to work
properly), you can use .addMutatedBy
like this:
/* code omitted for brevity */
canReflectDeps.addMutatedBy( two, one );
The previous example uses a shorthand to register a value dependency.
It's also possible to register keyDependencies
by passing a dependency record
object instead. Let's build upon the previous example by creating a simple map
instance that sets two
's value when its age
property changes:
import canReflect from "can-reflect";
import SimpleMap from "can-simple-map";
import SimpleObservable from "can-simple-observable";
import canReflectDeps from "can-reflect-dependencies";
const one = new SimpleObservable( "one" );
const two = new SimpleObservable( "two" );
const me = new SimpleMap( { age: 30 } );
canReflect.onValue( one, function() {
two.set( /* new value */ );
} );
canReflect.onKeyValue( me, "age", function() {
two.set( /* new value */ );
} );
Both me
and one
set the value of two
, and this can be registered as follows:
/* code omitted for brevity */
canReflectDeps.addMutatedBy( two, {
valueDependencies: new Set( [ one ] ),
keyDependencies: new Map( [ [ me, new Set( [ "age" ] ) ] ] )
} );
An optional key
can be passed as a second argument when registering map like
observable mutations, e.g:
import canReflect from "can-reflect";
import SimpleMap from "can-simple-map";
import canReflectDeps from "can-reflect-dependencies";
const me = new SimpleMap( { age: 30 } );
const month = new SimpleMap( { day: 10 } );
canReflect.onKeyValue( month, "day", function() {
me.set( age, /* new value */ );
} );
canReflectDeps.addMutatedBy( me, "age", {
keyDependencies: new Map( [ [ month, new Set( [ "day" ] ) ] ] )
} );
Parameters
- observable
{Object}
:The observable being set by other observable
- key
{String}
:The key on a map-like observable
- observableOrDependencyRecord
{Object}
:The value-like observable or dependency record with the observable(s) that set the value of the first argument.