google/agera

What is the main function of eventSources

zjutkz opened this issue · 1 comments

I know there is a function observe() in RepositoryCompilerStates.It notes that Specifies the event source of the compiled repository. But I have no idea about how to use it.
I was using Agera to do some works during the past few mouths,like develop an AgeraBus just like RxBus or EventBus,and build an App based on Agera,but I've never using observe() function.
I just using Agera like:
.repositoryWithInitialValue(def) .observe() .onUpdatesPerLoop() .goTo(Executors.newSingleThreadExecutor()) .getFrom(.....) .thenTransform(.......) .compile();
I read the source code of this part and I got the point is eventSource will update the repository when observableActivated called:
@Override protected void observableActivated() { eventSource.addUpdatable(this); maybeStartFlow(); }
But is it useful?Adding an eventSource will just let the repository process the data twice,but the data is same so repository will not trigger the updatable to do updating.I cannot find any circumstance to use this function.

A compiled repository without any event source will only react to the event that it's activated. This could be useful in many scenarios, but typically when all the data sources are static, for example, when you're building a data chain from some static data.

A compiled repository with event source will also react to events from those sources and attempt to update its value.

External event sources typically provide values or signals to the compiled repository. For example, if this repository gets a value from another repository, then if that value changes, this repository should refresh. In this case you'll want to put "that repository" as one of the event sources of "this repository".

An example of "signal" event source: a network connectivity checker. If "this repository" depends on data from the network, then it needs network connectivity to function correctly. If it's activated during the time the device is not connected to a useful network (totally disconnected, or connected to a WiFi that has a paywall), then the computation will likely fail. In this case, you'll want to use the network connectivity checker as one of the event sources, so "this repository" can retry the computation.

Another example "signal" event source: a "refresh trigger". This trigger fires the event when the user selects "refresh" from the UI. Then you'll want the repository to redo the calculation to get fresh data from its data source(s).

Agera is built for responsive UI and is typically used to provide data to the UI and reacts to user interactions and other environment changes that mandate a change of the UI. The event source function ties neatly to this type of event-driven approach.