Skip to content

Instantly share code, notes, and snippets.

@Ashok-Varma
Last active June 5, 2018 08:37
Show Gist options
  • Save Ashok-Varma/3563ec6d7aa0310d29d14c8a0e570922 to your computer and use it in GitHub Desktop.
Save Ashok-Varma/3563ec6d7aa0310d29d14c8a0e570922 to your computer and use it in GitHub Desktop.
Debouncer is a Util class for Android that Replicates Debounce Operator functionality in RxJava. Can be used for "libraries" or "light weight apps" or "apps that don't use RxJava".
import android.os.Handler;
import android.support.annotation.NonNull;
/**
* Class description
*
* @author ashok
* @version 1.0
* @since 04/06/18
*/
public class Debouncer<V> {
private final int mInterval;
private final Callback<V> mCallback;
private final Handler mHandler;
public Debouncer(int intervalInMills, @NonNull Callback<V> callback) {
mInterval = intervalInMills;
mCallback = callback;
mHandler = new Handler();
}
private Runnable currentRunnable;
public void consume(V key) {
mHandler.removeCallbacksAndMessages(null);
// if (currentRunnable != null) {
// mHandler.removeCallbacks(currentRunnable);// if running cancel
// }
currentRunnable = new Counter<>(key, mCallback);
mHandler.postDelayed(currentRunnable, mInterval);
}
public static class Counter<T> implements Runnable {
private final T mDeliverable;
private final Callback<T> mCallback;
Counter(T deliverable, Callback<T> callback) {
mDeliverable = deliverable;
mCallback = callback;
}
@Override
public void run() {
mCallback.onEmit(mDeliverable);
}
}
public interface Callback<T> {
void onEmit(T key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment