Listener #29
-
Hi Greg, Firstly, can you explain why
Do I need to use it on a capsule that do other stuff? My desired outcome is to listen So I think I have to create a |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
Also, should I do the listening inside |
Beta Was this translation helpful? Give feedback.
-
Also, should I initialize the listener at top level (just after create the What if I'm in Flutter where I don't have access to the |
Beta Was this translation helpful? Give feedback.
-
I removed some commentaries due to I think I understand now better the whole picture |
Beta Was this translation helpful? Give feedback.
-
I'm going to try to answer all of your questions/comments here:
Here's why it is needed. Take the following: int xCapsule(CapsuleHandle use) => /* impl hidden */;
void xLoggerCapsule(CapsuleHandle use) => print(use(xCapsule)); If we read This garbage collection optimization is exactly what:
As logging, and listening in general, is actually considered a side effect (as they interact with the outside world),
That just sounds like you should instead rely on ReArch's reactivity via: int Function(int) aFactory(CapsuleHandle use) => /* impl hidden */;
int Function(int) bFactory(CapsuleHandle use) {
final a = use(aFactory); // bFactory will be rebuilt with a new "a" whenever aFactory rebuilds
return (i) {
return a(i) * 2;
}
}
To initialize a listener/capsule with some side effect, you only need to read it once from the container at a time of your choosing. This is, however, often when a container is first created. In Dart only, just call In Flutter, you can do something similar, but it'd be better to do something like: @rearchWidget
Widget initListeners(WidgetHandle use, {required Widget child}) {
use.callonce(() {
use(myListener1);
use(myListener2);
});
return child;
} (I'm too lazy to write out a full widget with all that boilerplate, so just expand the rearchWidget above into your own RearchConsumer) |
Beta Was this translation helpful? Give feedback.
-
Can you please give a full example of what you’re doing? Having difficulty following exactly
But in short if you have a “proxy” capsule that uses some data from a source capsule and returns that data directly, anything dependent on that proxy capsule will rebuild (and then emit new data) whenever the source has new data.
|
Beta Was this translation helpful? Give feedback.
I'm going to try to answer all of your questions/comments here:
Here's why it is needed. Take the following:
If we read
xLoggerCapsule
from the container once (so the container knows of its existence), we may then expect for it to log all future values ofxCapsule
. Here's the catch: it won't. As soon asxCapsule
changes, the container will recognize that its only dependents have no "demand" (i.e., nothing downstream registers a side effect), and will garbage collect thexLoggerCapsule
without it ev…