Skip to content
Logo

Equal states

A state that lists itself in its transitions can transition to its own kind:

.transitions({
  Ready: ["Ready"],
  // ...
})

When the new state is equal to the current one, finity skips the transition. The state isn't replaced, so effects don't rerun and nothing rerenders. A skipped transition is still valid: it isn't reported to onInvalidTransition, and since the state didn't change, the same transition can be called again.

What counts as equal

States of different kinds are never equal. For states of the same kind, isEqualState decides, and transition is left out of both before they're compared.

The default is shallowEqual, from @ghostry/finity-core: two states are equal when they have the same properties, and each value is the same by Object.is. A property holding a new array or object is a change, even if its contents are the same.

Changing the comparison

Pass isEqualState to initialize:

import { equals } from "ramda";
 
export const finity = initialize({ isEqualState: equals });

It receives the two states and returns whether they're equal. It's only called for states of the same kind.

To always transition, even to an equal state:

export const finity = initialize({ isEqualState: () => false });