Watches for changes of the specified object
Watches for changes of the specified object
additional options
Watches for changes of the specified object
additional options
Watches for changes of the specified object
path to a property to watch
Watches for changes of the specified object
path to a property to watch
additional options
Watches for changes of the specified object
path to a property to watch
additional options
The function temporarily mutes all mutation events for the specified proxy object
Sets a new watchable value for a proxy object by the specified path. The function is actual when using an engine based on accessors to add new properties to the watchable object. Or when you want to restore watching for a property after deleting it.
Sets a new watchable value for a proxy object by the specified path. The function is actual when using an engine based on accessors to add new properties to the watchable object. Or when you want to restore watching for a property after deleting it.
The function unmutes all mutation events for the specified proxy object
Deletes a watchable value from a proxy object by the specified path
Deletes a watchable value from a proxy object by the specified path.
To restore watching for this property, use set
.
Wraps the specified object with unwatchable proxy, i.e. any mutations of this proxy can’t be watched
core/object/watch
The module provides API to watch changes of JS objects, like, maps, arrays, etc. The watching supports different strategies:
By default, if runtime supports Proxy objects, then will be used an approach based on these objects. Otherwise, will be used a strategy based on accessors. Also, you can manually define a strategy to use for each watcher.
Known limitations
The assessors' based engine doesn't watch array indices. To add or remove new elements to an array, use array methods. Or, you can use
set/unset
methods from a watcher to add or remove elements by a path.The assessors' based engine doesn't watch newly added elements. To add watching for these properties, use
set/unset
methods from a watcher to add or remove elements by a path.To watch invoking of mutating methods, like
add
ordelete
, the watcher wraps the original methods of the passed object.How it works?
The module provides a function to watch changes. It takes an object to watch, optionally some watching options, and a callback function that accumulates mutations and invokes on the next tick after the first mutation. After this, the function returns API to watch changes. The API has an interface below.
The function to watch supports: objects, arrays, Map-s, Set-s.
Notice, the function creates a new object that wraps the original and adds the watching functionality. The new object is connected to the original, and if you change the value of some property of the proxy object, it will affect the original object. The connection works with the reverted direction too, when you change the original object, but in this case, you can't watch these mutations.
Also, the API provides a function to remove watching from the proxy object.
The rest two methods of the API allow adding or removing properties to the proxy object. If your environment supports Proxy objects, you can add new properties without invoking
set
, but the invoking is necessary for a strategy based on accessors.To delete a property from the proxy object, we can set it to
undefined
, or use thedelete
operator, or invoke thedelete
method of the watcher. All of these methods have different semantic and work the same with any engine. Let's watch these in action.Parameters of a mutation handler
A function that handles mutations can take a list of mutations or a single mutation. The list of mutations contains sub-arrays, where the first two parameters refer to new and old values of the mutated property. The third parameter refers to an object that contains some information about a particular mutation, like, where the mutation has occurred. In case when the function takes a single mutation, the function takes tree arguments instead of one.
Watching for the specific path
We can set watching not to the whole object properties, but only the property by the specified path. To do it, just provide a path as the second parameter of the watching function.
There are some nuances of using this approach:
Because we watch the specific path, the callback function will take not a list of mutations, but just a single mutation.
Mutations of nested properties that match the path also invoke the callback. To get a path of the mutated property, use
info.originalPath
, becauseinfo.path
always refers to the path that we watched.By default, all mutations that occur on the same tick are accumulated within a mutation list. The provided handler function is invoked on the next tick and takes the last value from this list as an argument, i.e., it works lazily. To force a watcher to invoke its handler immediately after the occurred mutation, provide the
immediate
option. To watch the whole list of mutations, provide thecollapse
option tofalse
.The provided handler function takes new and old values of a property by the provided path. Notice, if a mutation of some nested property occurs, the new and old values will be equal because they refer to the same object. To get values of changed nested properties, provide the
collapse
option tofalse
.Separated and shared watchers
The important point is that the watch function doesn't mutate the passed object but creates a new object based on the original and returns it. Only mutation of this new object will create events of modifications, and when we make another one watcher based on the original object, they can't watch mutations of each other.
If we want to share mutations between different watchers, we should invoke the watch function by providing the previous proxy object instead of the original.
Options of watching
deep
By default, are watched only mutations from the top object properties, i.e., all nested mutations, are ignored. To enable watching of nested properties, provide the
deep
option.withProto
By default, all mutations of properties from a prototype of the proxy object are ignored. To enable watching of prototype properties, provide the
withProto
option.immediate
By default, all mutations that occur on the same tick are accumulated within a mutation list. The provided handler function is invoked on the next tick and takes the list of mutations as an argument, i.e., it works lazily. To force a watcher to invoke its handler immediately after the occurred mutation, provide the
immediate
option. In this case, the callback function doesn't take a list of mutations but parameters of the single mutation.collapse
The option enables or disables collapsing of mutation events. When it toggles to
true
, all mutation events fire as if they occur on top properties of the watchable object.When it toggles to
false,
and the watcher binds to the specified path, the callback takes a list of mutations. Otherwise, the callback takes only the last mutation.pathModifier
A function that takes a path of the mutation event and returns a new path. The function is used when you want to mask one mutation to another one.
eventFilter
A filter function for mutation events. The function allows skipping some mutation events.
tiedWith
A link to an object that should connect with the watched object, i.e., changing of properties of the tied object, will also emit mutation events.
prefixes
A list of prefixes for paths to watch. This parameter can help to watch accessors.
postfixes
A list of postfixes for paths to watch. This parameter can help to watch accessors.
dependencies
When providing the specific path to watch, this parameter can contain a list of dependencies for the watching path. This parameter can help to watch accessors.
When providing the specific path to watch, this parameter can contain an object or
Map
with lists of dependencies to watch.engine
A watch engine to use. By default, will be used proxy if supported, otherwise accessors.
Global API
The module provides a bunch of additional helper functions.
mute
The function temporarily mutes all mutation events for the specified proxy object.
unwatchable
Wraps the specified object with unwatchable proxy, i.e. any mutations of this proxy can’t be watched.
unmute
The function unmutes all mutation events for the specified proxy object.
set
The function sets a new watchable value for a proxy object by the specified path. It is actual when using an engine based on accessors to add new properties to the watchable object. Or when you want to restore watching for a property after deleting it.
unset
The function deletes a watchable value from a proxy object by the specified path. To restore watching for this property, use
set
.