DependentCallback
A low-level function used when manually graphing e.g. with ecosystem.selectors.addDependent()
or atomInstance.addDependent()
. This function is called with special "GraphEdgeSignals" when Zedux needs to push some new information about the dependency to the dependent.
Example
const exampleAtom = atom('example', () => 'test')
let instance = ecosystem.getInstance(exampleAtom)
// keep the local `instance` reference up-to-date in case of force destruction:
const cleanup = instance.addDependent({
callback: (signal, val, reason) => {
if (signal === 'Destroyed') {
instance = ecosystem.getInstance(exampleAtom)
}
},
})
Definition
type DependentCallback = (
signal: GraphEdgeSignal,
val?: any,
reason?: EvaluationReason
) => void
A string. One of:
'Destroyed' | 'Updated'
A dependent can only receive the "Destroyed" signal when the dependency is force-destroyed, since this dependent would prevent destruction otherwise.
The "Updated" signal can mean either the dependency's state changed or its promise reference changed.
The current state of the dependency. If the dependency is an atom instance, this is the current state of the atom instance's store. If the dependency is an atom selector, this is the current .result
property of the atom selector's cache item.
The single EvaluationReason that triggered this signal. Note that if multiple updates were batched, only the first reason will be passed here - this callback will never receive the others.