How to add all-capsules/container listener or observer for logging #107
-
How to add all-capsules/container listener for app-wide capsule state logging or some sort of Riverpod style ProviderObserver to container? The following part that probably deals with this in documentation is empty: https://rearch.gsconrad.com/paradigms/listeners |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@Kypsis Yea, I never got around to actually finishing that page. Maybe I can tackle it this evening. The idea is to just create a new capsule and opt-in to the capsules you want to listen to, knowing that they will never be freed from the container when you use listeners. Normally, ReArch will garbage-collect any capsules that don't use side effects when their dependencies change, but if you are adding a listener to those capsules, then they won't be freed. This doesn't matter at all if you are listening to a stateful capsule, but it can matter if you have a series of capsules that just do some simple data transformations that should be freed when no longer in use. Here's a quick example: (int, void Function(int)) aCapsule(CapsuleHandle use) => use.state(0);
void aListener(CapsuleHandle use) {
use.asListener(); // mark this capsule as a listener (so it won't be automatically garbage-collected & not receive updates
print(use(aCapsule)); // listen to whatever you want
}
final container = CapsuleContainer();
container.read(aListener); // instantiate the listener (which will call it once) |
Beta Was this translation helpful? Give feedback.
@Kypsis Yea, I never got around to actually finishing that page. Maybe I can tackle it this evening.
The idea is to just create a new capsule and opt-in to the capsules you want to listen to, knowing that they will never be freed from the container when you use listeners. Normally, ReArch will garbage-collect any capsules that don't use side effects when their dependencies change, but if you are adding a listener to those capsules, then they won't be freed. This doesn't matter at all if you are listening to a stateful capsule, but it can matter if you have a series of capsules that just do some simple data transformations that should be freed when no longer in use.
Here's a quick example:
(…