on

Calls back when a node changes, either the whole node or one of its properties.

Parameter

Description

lex

A lex query with the node id as "#", and optionally a property to watch as ".".

callback

Function called each time the node, or the watched property, changes.

get

Optional. When true, the callback is also called straight away with the current data.

options

Optional object passed to the initial read when get is true, see get().

The callback is called without any data when a change happens. It's a signal that something changed, so read the new value with get(). The exception is the immediate call made when get is true, which receives a message in the same format as get().

Examples

// Read the node each time it changes.
holster.wire.on({"#": "root"}, () => {
  holster.wire.get({"#": "root"}, msg => console.log(msg.put))
})

// Only changes to one property.
holster.wire.on({"#": "root", ".": "key"}, () => {
  holster.wire.get({"#": "root", ".": "key"}, msg => {
    console.log(msg.put.root.key)
  })
})

// Also called straight away, with a message like get() returns.
holster.wire.on(
  {"#": "root"},
  msg => {
    if (msg) console.log("current:", msg.put)
    else console.log("root changed")
  },
  true,
)

Keep a reference to the callback to remove it later with off().