Class: DynamicValue

declarative.DynamicValue

A dynamic value is a variable that may hold any value and watchers can subscribe for monitoring changes.

The stored value is accessible via the val property.

new declarative.DynamicValue (value)

Creates a new dynamic value.

Name Type Description
value any

The initial value.

Properties:
Name Type Description
setter function

The overridable setter function.

getter function

The overridable getter function.

val any

Gives access to the stored value.

Example
const dynVal = new declarative.DynamicValue(42);
console.log(dynVal.val);  // prints 42
dynVal.val = 25;          // changes the value to 25 and notifies all watchers.
console.log(dynVal.val);  // prints 25

Methods

unwatched (callback)

Registers a callback for when the last watcher was removed, meaning that this dynamic value is now completely unwatched.

Name Type Description
callback function

The callback to invoke.

Notifies all watchers of this dynamic value about a value change. This method must be called explicitly after changing a value directly.

Registers a callback for watching this dynamic value for changes and returns a watch handle.

The watch handle has a method unwatch to stop watching the value for changes.

Name Type Description
watchCallback function

The callback to invoke on changes.

Returns:
Type Description
declarative.DynamicValue.WatchHandle The watch handle.
Example

Watch for changes

const dynVal = new declarative.DynamicValue(42);
dynVal.watch(v => console.log("The value changed to " + v.val);
++(dynval.val);
dynVal.update();           // prints "The value changed to 43"

watched (callback)

Registers a callback for when the first watcher was added, meaning that this dynamic value is now being watched.

Name Type Description
callback function

The callback to invoke.

Type Definitions

declarative.DynamicValue.WatchHandle

A dynamic value watch handle.

Properties:
Name Type Description
setEnabled function

Enables or disables this watcher.

unwatch function

Stops watching the dynamic value.

value function

Returns the current value.