Creating from objects
From.object wraps an object, emitting finely-grained updates when changes are made to it. This can be useful to maintaining a ‘shape’ of data and using just one reactive instead of making lots of reactives for each property of an object.
It’s important that you manipulate the wrapped object using the provided set
, update
and updateField
functions rather than manipulating the original object.
With set
you have to provide a complete object with the same shape as the original.
update
‘patches’ the object with a partial set of key-value pairs, overwriting what’s already there.
updateField
sets a single field:
The onValue
event always emits the whole object. There are two additional event handlers, onDiff
& onField
. onDiff
emits a change set rather than whole object. This allows the event listener to get fine-grained information on what field has changed and how. For example:
onField
allows subscribing to particular field changes.
Object proxy
An alternative to From.object
is From.objectProxy . In this case, a proxy of the source object is created such that changes made on the proxy flow through and are emitted from the reactive. This can be useful if you want a reactive object to interoperate with other parts of code that shouldn’t know anything about reactives.
The proxy object appears to be the same as the input object: { colour: 'red', x: 10, y: 20 }
. But if we change fields, the reactive will magically pick it up and emit change events.
Keep in mind only change made via the proxy object returned by objectProxy
will make the magic happen.
A twist on From.objectProxy
is From.objectProxySymbol . This returns the reactive as symbol on the original object, rather than returning the proxy and the reactive.
This can be more ergonomic because you don’t need to juggle two return values, the proxied object and the stream.