Saltearse al contenido

UseCompute

Esta página aún no está disponible en tu idioma.

UseCompute is a hook that allows you to define a computation method ( computeValue ) to derive a value based on the current state of one or more dependencies. When any of these dependencies notify to change, the hook recalculates the value and provides the updated result.

Syntax

UseCompute<T>(
T computeValue(),
List<RtState> dependencies,
{ String? debugLabel },
);

UseCompute accepts these parameters:

  • computeValue : A function that returns a value based on the current state of the dependencies.
  • dependencies: A list of state( RtState , learn about it here) that the computation depends on. When any of these dependencies trigger Lifecycle.didUpdate event, the value is recalculated.
  • debugLabel: (optional) A label to identify the hook in the DevTools extension.

Properties & Methods

UseCompute provides the following properties and methods:

  • value: A getter that allows you to read the computed value.
Properties and methods inherited from RtState
  • debugLabel: A string that represents the label of the state object for debugging purposes.
  • debugInfo: A map that contains debug information about the state object.
  • update : Executes a callback function and notify its observers that the state has changed. When it is invoked, it emits two lifecycle events to signal the state transition:
    • Lifecycle.willUpdate is emitted first, indicating the impending update.
    • Lifecycle.didUpdate is emitted once the update process is complete.
  • notify : Forces the state to notify its observers. Unlike update , it emits only the Lifecycle.didUpdate event, as it doesn’t involve any preparatory steps before the notification.
  • bind : Establishes a connection between the state and a specific instance. This connection allows the instance to reactively update based on changes to the state. By binding the state, the instance becomes aware of changes to the state and can appropriately reflect those changes in its behavior.
  • unbind : Releases the connection between the state and the instance. When unbinding, the instance will no longer receive updates from the state. This can be useful when an instance is no longer actively using the state or when it needs to detach from the state temporarily or permanently.
  • dispose : Is responsible for cleaning up the state and any associated observers or resources. Disposing of the state ensures that it is properly released and no longer consumes memory or processing resources unnecessarily.

Usage

Declaration

UseCompute can be initialized using the constructor class:

3 collapsed lines
final uStateA = UseState<int>(0);
final uStateB = UseState<int>(0);
final uTotal = UseCompute<int>(
() => uStateA.value + uStateB.value,
[uStateA, uStateB],
);

Reading the computed value

UseCompute has a value property that allows you to read the computed value:

print("Total: ${uTotal.value}");

Computing the value

When any of the dependencies change, the value property recalculates the result based on the computeValue function, e.g.:

main.dart
1
final uStateA = UseState(1);
2
final uStateB = UseState(2);
3
4
final uResult = UseCompute(
5
// The `clamp` is a method that returns this num clamped
6
// to be in the range lowerLimit-upperLimit(e.g., 5-10).
7
() => addAB().clamp(5, 10),
8
[uStateA, uStateB],
9
);
10
11
int addAB() => uStateA.value + uStateB.value;
12
void printResult() => print("${addAB()} -> ${uResult.value}");
13
14
void main() {
15
Rt.on(
16
uResult,
17
Lifecycle.didUpdate,
18
(_, __) => print("uResult changed with: ${uResult.value}"),
19
);
20
21
printResult(); // 3 -> 5
22
uStateA.value += 1; // Will not notify change
23
printResult(); // 4 -> 5
24
uStateB.value += 2; // Will notify change
25
printResult(); // 6 -> 6
26
uStateA.value += 6; // Will notify change
27
printResult(); // 12 -> 10
28
uStateB.value -= 1; // Will not notify change
29
printResult(); // 13 -> 10
30
uStateA.value -= 5; // Will notify change
31
printResult(); // 8 -> 8
32
}

In above example, the value of uResult will be recalculated when the value of uStateA or uStateB changes. But when the calculated value is same as the previous value, it will not notify the change.

Using with Memo

UseCompute does not cache the computed value, meaning that it will computing the value every time the dependencies is changed, potentially impacting performance, especially if the computeValue is expensive. In this case, you should consider using  Memo  to cache the computed value, e.g.:

final addAB = Memo(
(Args2 args) => args.arg1 + args.arg2,
);
final uResult = UseCompute(
() => addAB(
Args2(uStateA.value, uStateB.value),
),
[uStateA, uStateB],
);

Updating the value

Use update method to notify changes after run a set of instructions:

uTotal.update((value) {
uTotal.value = 10;
});

Use refresh method to force to notify changes.

uTotal.refresh();

Listening to changes

When value has changed, the UseCompute will emit the following events(learn about it here):

  • Lifecycle.willUpdate event is triggered before the change in value or update method have been invoked.
  • Lifecycle.didUpdate event is triggered after the change in value or after update or notify methods have been invoked.

Example of listening to changes:

1
Rt.on(
2
uTotal,
3
Lifecycle.didUpdate,
4
(_, state) => print("State value has changed to: ${state.value}"),
5
);