@Test public void addSourceDuringOnActive() { mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); mSource.setValue("a"); mMediator.addSource(mSource, new Observer<String>() { @Override public void onChanged(@Nullable String s) { MutableLiveData<String> source = new MutableLiveData<>(); source.setValue("b"); mMediator.addSource(source, new Observer<String>() { @Override public void onChanged(@Nullable String s) { mMediator.setValue("c"); } }); } }); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); assertThat(mMediator.getValue(), is("c")); }
@MainThread public <S> void addSource(LiveData<S> source, Observer<S> onChanged) { Source<S> e = new Source<>(source, onChanged); Source<?> existing = mSources.putIfAbsent(source, e); if (existing != null && existing.mObserver != onChanged) { throw new IllegalArgumentException( "This source was already added with the different observer"); } if (existing != null) { return; } if (hasActiveObservers()) { e.plug(); } }
MediatorLiveData extends MutableLiveData<T> { @MainThread public <S> void addSource(LiveData<S> source, Observer<S> onChanged) { Source<S> e = new Source<>(source, onChanged); Source<?> existing = mSources.putIfAbsent(source, e); if (existing != null && existing.mObserver != onChanged) { throw new IllegalArgumentException( "This source was already added with the different observer"); } if (existing != null) { return; } if (hasActiveObservers()) { e.plug(); } } }
MediatorLiveData extends MutableLiveData<T> { @MainThread public <S> void addSource(LiveData<S> source, Observer<S> onChanged) { Source<S> e = new Source<>(source, onChanged); Source<?> existing = mSources.putIfAbsent(source, e); if (existing != null && existing.mObserver != onChanged) { throw new IllegalArgumentException( "This source was already added with the different observer"); } if (existing != null) { return; } if (hasActiveObservers()) { e.plug(); } }  }
MediatorLiveData extends MutableLiveData<T> { @MainThread public <S> void addSource(LiveData<S> source, Observer<S> onChanged) { Source<S> e = new Source<>(source, onChanged); Source<?> existing = mSources.putIfAbsent(source, e); if (existing != null && existing.mObserver != onChanged) { throw new IllegalArgumentException( "This source was already added with the different observer"); } if (existing != null) { return; } if (hasActiveObservers()) { e.plug(); } }  @MainThread void addSource(LiveData<S> source, Observer<S> onChanged); @MainThread void removeSource(LiveData<S> toRemote); }
MediatorLiveData extends MutableLiveData<T> { @MainThread public <S> void addSource(LiveData<S> source, Observer<S> onChanged) { Source<S> e = new Source<>(source, onChanged); Source<?> existing = mSources.putIfAbsent(source, e); if (existing != null && existing.mObserver != onChanged) { throw new IllegalArgumentException( "This source was already added with the different observer"); } if (existing != null) { return; } if (hasActiveObservers()) { e.plug(); } }  @MainThread void addSource(LiveData<S> source, Observer<S> onChanged); @MainThread void removeSource(LiveData<S> toRemote);  }