All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.launchdarkly.sdk.android.Debounce Maven / Gradle / Ivy

package com.launchdarkly.sdk.android;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Debounce {
    private volatile Callable pending;
    private volatile Callable inFlight = null;
    private final ExecutorService service = Executors.newSingleThreadExecutor();

    synchronized void call(Callable task) {
        pending = task;

        schedulePending();
    }

    private synchronized void schedulePending() {
        if (pending == null) {
            return;
        }

        if (inFlight == null) {
            inFlight = pending;
            service.submit(() -> {
                try {
                    inFlight.call();
                } finally {
                    inFlight = null;
                    schedulePending();
                }
                return null;
            });
            pending = null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy