The ObservableObject
class provides a mechanism to observe changes to the properties of an object. It allows setting values on the object and watching for changes at specific paths, including nested properties, making it suitable for complex state management scenarios.
PropertyName
refers to the various ways you can specify a property name when using methods that deal with object properties.
It uses lodash's API internally, so its behavior is consistent with lodash.get and lodash.setWith and lodash.toPath methods when operating the target objects.
Creates a new ObservableObject
instance.
target
: The initial object to observe. This object is deeply cloned to avoid direct mutations.value
: Returns a deep clone of the target object.set
Sets a value at a specific path within the object and notifies watchers if the value changes.
values
: Set the full object to replace target
if just only one parameters.path
: The path where the value should be set (can be a string or an array of strings).value
: The new value to set at the specified path.customizer
(optional): The function to customize assigned values.Returns the new value at the specified path.
get
Gets the value at a specific path or the entire object if no path is provided.
path
(optional): The path to retrieve the value from (can be a string or an array of strings).Returns the deep clone value at the specified path or a deep clone of the entire object if no path is provided.
unset
Delete the value at a specific path.
path
: The path to delete.Returns the value at the specified path to delete, if the path not exists, it will be undefined
.
watch
Watches for changes at a specific path and executes a callback when changes occur.
path
: The path to watch for changes.callback
: The function to execute when a change occurs.options
(optional): An object with the following optional properties:
leading
: If true, the callback is executed immediately with the current value at the path.Returns a function to unsubscribe from the watch.
Note: If the value being set is equal to the existing value, watchers will not be triggered.
To watch for any change in the object:
The ObservableObject
automatically detects changes to parent and child paths. If a parent path is watched, any change to a child path will trigger the callback.
The ObservableObject
class provides a powerful way to manage and react to changes within an object. It supports deep observation of nested properties, making it suitable for complex state management scenarios. Watchers will not be triggered if the new value is equal to the old value, ensuring efficient change detection.