privatevoidconsiderNotify(ObserverWrapperobserver){if(!observer.mActive){return;}// Check latest state b4 dispatch. Maybe it changed state but we didn't get the event yet.
//
// we still first check observer.active to keep it as the entrance for events. So even if
// the observer moved to an active state, if we've not received that event, we better not
// notify for a more predictable notification order.
if(!observer.shouldBeActive()){observer.activeStateChanged(false);return;}if(observer.mLastVersion>=mVersion){return;}observer.mLastVersion=mVersion;observer.mObserver.onChanged((T)mData);}
publicclassMutableLiveData<T>extendsLiveData<T>{/**
* Creates a MutableLiveData initialized with the given {@code value}.
*
* @param value initial value
*/publicMutableLiveData(Tvalue){super(value);}/**
* Creates a MutableLiveData with no value assigned to it.
*/publicMutableLiveData(){super();}@OverridepublicvoidpostValue(Tvalue){super.postValue(value);}@OverridepublicvoidsetValue(Tvalue){super.setValue(value);}}
/**
* {@link LiveData} subclass which may observe other {@code LiveData} objects and react on
* {@code OnChanged} events from them.
* <p>
* This class correctly propagates its active/inactive states down to source {@code LiveData}
* objects.
* <p>
* Consider the following scenario: we have 2 instances of {@code LiveData}, let's name them
* {@code liveData1} and {@code liveData2}, and we want to merge their emissions in one object:
* {@code liveDataMerger}. Then, {@code liveData1} and {@code liveData2} will become sources for
* the {@code MediatorLiveData liveDataMerger} and every time {@code onChanged} callback
* is called for either of them, we set a new value in {@code liveDataMerger}.
*
* <pre>
* LiveData<Integer> liveData1 = ...;
* LiveData<Integer> liveData2 = ...;
*
* MediatorLiveData<Integer> liveDataMerger = new MediatorLiveData<>();
* liveDataMerger.addSource(liveData1, value -> liveDataMerger.setValue(value));
* liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value));
* </pre>
* <p>
* Let's consider that we only want 10 values emitted by {@code liveData1}, to be
* merged in the {@code liveDataMerger}. Then, after 10 values, we can stop listening to {@code
* liveData1} and remove it as a source.
* <pre>
* liveDataMerger.addSource(liveData1, new Observer<Integer>() {
* private int count = 1;
*
* {@literal @}Override public void onChanged(@Nullable Integer s) {
* count++;
* liveDataMerger.setValue(s);
* if (count > 10) {
* liveDataMerger.removeSource(liveData1);
* }
* }
* });
* </pre>
*
* @param <T> The type of data hold by this instance
*/