Skip to content

Commit

Permalink
Call LottieTask listeners synchronously when already on the main thre…
Browse files Browse the repository at this point in the history
…ad (#2470)

When we are already on the main thread, there is no need to post listeners to the main thread again.

As a side effect, this fixes #2449. The listener was called twice because it was called synchronously in addListener because it was already done and then because of the post, it was called again.

Fixes #2449
  • Loading branch information
gpeal committed Feb 27, 2024
1 parent af7fffc commit f0f24b9
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lottie/src/main/java/com/airbnb/lottie/LottieTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,24 @@ public LottieResult<T> getResult() {

private void notifyListeners() {
// Listeners should be called on the main thread.
handler.post(() -> {
// Local reference in case it gets set on a background thread.
LottieResult<T> result = LottieTask.this.result;
if (result == null) {
return;
}
if (result.getValue() != null) {
notifySuccessListeners(result.getValue());
} else {
notifyFailureListeners(result.getException());
}
});
if (Looper.myLooper() == Looper.getMainLooper()) {
notifyListenersInternal();
} else {
handler.post(this::notifyListenersInternal);
}
}

private void notifyListenersInternal() {
// Local reference in case it gets set on a background thread.
LottieResult<T> result = LottieTask.this.result;
if (result == null) {
return;
}
if (result.getValue() != null) {
notifySuccessListeners(result.getValue());
} else {
notifyFailureListeners(result.getException());
}
}

private synchronized void notifySuccessListeners(T value) {
Expand Down

0 comments on commit f0f24b9

Please sign in to comment.