Skip to content

Commit

Permalink
Merge pull request #170 from milchopenchev/vieweffects-queueing-change
Browse files Browse the repository at this point in the history
Add LiveQueue method to enable ignoring of paused effects entirely
  • Loading branch information
togi committed Dec 6, 2023
2 parents 4c67db2 + 5af6d08 commit fb556b2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Expand Up @@ -52,6 +52,16 @@ public interface LiveQueue<T> {
void setObserver(
@Nonnull LifecycleOwner lifecycleOwner, @Nonnull Observer<T> liveEffectsObserver);

/**
* A utility method for calling {@link #setObserver(LifecycleOwner, Observer, Observer)} that
* substitutes null for the optional observer. This method will also cause any effects that occur
* in background to be ignored, and they will not besent if a new pausedEffectsObserver is added.
* The paused effects will not be added in a queue at all, so there will be no exceptions thrown
* due to queue size exceeding a limit.
*/
void setObserverIgnoringPausedEffects(
@Nonnull LifecycleOwner lifecycleOwner, @Nonnull Observer<T> liveEffectsObserver);

/**
* The <code>LiveQueue</code> supports only a single observer, so calling this method will
* override any previous observers set.<br>
Expand Down
Expand Up @@ -55,6 +55,7 @@ void onAny(LifecycleOwner source, Lifecycle.Event event) {
@Nullable private Observer<T> liveObserver = null;
@Nullable private Observer<Iterable<T>> pausedObserver = null;
private boolean lifecycleOwnerIsPaused = true;
private boolean ignoreBackgroundEffects = false;

MutableLiveQueue(WorkRunner effectsWorkRunner, int capacity) {
this.effectsWorkRunner = effectsWorkRunner;
Expand All @@ -76,6 +77,16 @@ public void setObserver(@Nonnull LifecycleOwner owner, @Nonnull Observer<T> live
setObserver(owner, liveEffectsObserver, null);
}

@Override
public void setObserverIgnoringPausedEffects(
@Nonnull LifecycleOwner owner, @Nonnull Observer<T> liveEffectsObserver) {
synchronized (lock) {
setObserver(owner, liveEffectsObserver, null);
ignoreBackgroundEffects = true;
pausedEffectsQueue.clear();
}
}

@Override
public void setObserver(
@Nonnull LifecycleOwner lifecycleOwner,
Expand All @@ -89,6 +100,7 @@ public void setObserver(
this.pausedObserver = pausedObserver;
this.lifecycleOwnerIsPaused = true;
lifecycleOwner.getLifecycle().addObserver(new LifecycleObserverHelper());
ignoreBackgroundEffects = false;
}
}

Expand All @@ -110,7 +122,7 @@ public void clearObserver() {
void post(@Nonnull final T data) {
synchronized (lock) {
if (lifecycleOwnerIsPaused) {
if (!pausedEffectsQueue.offer(data)) {
if (shouldQueuePausedEffects() && !pausedEffectsQueue.offer(data)) {
throw new IllegalStateException(
"Maximum effect queue size ("
+ pausedEffectsQueue.size()
Expand All @@ -123,6 +135,10 @@ void post(@Nonnull final T data) {
}
}

private boolean shouldQueuePausedEffects() {
return !ignoreBackgroundEffects;
}

private void onLifecycleChanged(Lifecycle.Event event) {
switch (event) {
case ON_RESUME:
Expand Down
Expand Up @@ -100,6 +100,19 @@ public void shouldQueueEventsWithNoObserver() {
assertThat(pausedObserver.valueCount(), equalTo(1));
}

@Test
public void shouldNotQueueEventsWhenSetToIgnoreBackgroundEventsAndNoObserver() {
mutableLiveQueue.setObserverIgnoringPausedEffects(fakeLifecycleOwner1, liveObserver);
fakeLifecycleOwner1.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
mutableLiveQueue.post("one");
mutableLiveQueue.post("two");
mutableLiveQueue.setObserver(fakeLifecycleOwner1, liveObserver, pausedObserver);
fakeLifecycleOwner1.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);

assertThat(liveObserver.valueCount(), equalTo(0));
assertThat(pausedObserver.valueCount(), equalTo(0));
}

@Test
public void shouldSendQueuedEventsWithValidPausedObserver() {
fakeLifecycleOwner1.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
Expand Down

0 comments on commit fb556b2

Please sign in to comment.