jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.44k stars 1.63k forks source link

Thoughts on RxInterface #740

Closed sheepdreamofandroids closed 4 years ago

sheepdreamofandroids commented 4 years ago

I want to build my own RxFun subclass of Rx. The idea is that you define it like this:

RxInt a = 1.obs;
RxInt b = 2.obs;
Rx<Int> sum = RxFun(()=> a.value + b.value);

For a simple addition this is obviously not useful but imagine fetching something from a database or another heavy calculation. I looked into the implementation of Obx and noticed that it uses a "dummy" Rx to listen to changes in the Rx's in the builder. It seems to make more sense to listen to those Rx directly instead of using a proxy.

I think this is caused by having only one RxInterface. This interface is responsible for both sending and receiving change events. A simply Rx though should not listen to anything, it's value should be set and it should notify listeners but no more. The Obx should be able to accept streams to listen to but not notify listeners. My RxFun should actually do both of those things.

So, to get to a point, I propose splitting RxInterface into two: one for sending (for Rx) and one for receiving (Obx).

Before diving into a PR which might be rather hard because of backward compatibility I would love to hear some thoughts!

jpkontreras commented 4 years ago

from https://github.com/jonataslaw/getx/blob/master/documentation/en_US/state_management.md#using-the-values-in-the-view

final count1 = 0.obs;
final count2 = 0.obs;
int get sum => count1.value + count2.value;
jonataslaw commented 4 years ago

NotifyManager https://github.com/jonataslaw/getx/blob/master/lib/get_rx/src/rx_types/rx_core/rx_impl.dart#L126 only addListeners to RxInterface.

RxObjectMixin https://github.com/jonataslaw/getx/blob/master/lib/get_rx/src/rx_types/rx_core/rx_impl.dart#L8 Trigger changes

It doesn't make sense to create a compute class (that's what you're trying to do with the description) using GetX, because it automatically takes care of that by attaching listeners when the getter is accessed.

Well, as this has nothing to do with the current structure of the package, and there is no reason to change the current structure, I am closing this.