package info.bitrich.xchangestream.service;

import io.reactivex.Completable;

/**
 * Base class of streaming services, declares connect() method including before connection logic
 */
public abstract class ConnectableService {

    /**
     * Exchange specific parameter is used for providing {@link Runnable} action which is caused before setup new connection.
     * For example adding throttle control for limiting too often opening connections:
     * <pre>
     * {@code
     * static final TimedSemaphore limiter = new TimedSemaphore(1, MINUTES, 15);
     * ExchangeSpecification spec = exchange.getDefaultExchangeSpecification();
     * spec.setExchangeSpecificParameters(ImmutableMap.of(
     *   {@link ConnectableService#BEFORE_CONNECTION_HANDLER}, () -> limiter.acquire()
     * ));
     * }
     * </pre>
     */
    public static final String BEFORE_CONNECTION_HANDLER = "Before_Connection_Event_Handler";

    /**
     * {@link Runnable} handler is called before opening new socket connection.
     */
    private Runnable beforeConnectionHandler = () -> {
    };

    public void setBeforeConnectionHandler(Runnable beforeConnectionHandler) {
        if (beforeConnectionHandler != null) {
            this.beforeConnectionHandler = beforeConnectionHandler;
        }
    }

    protected abstract Completable openConnection();

    public Completable connect() {
        beforeConnectionHandler.run();
        return openConnection();
    }


}

@@ -820,87 +820,1418 @@
    *
-/%0A    public static final String BEFORE_CONNECTION_HANDLER = %22Before_Connection
+%0A     * @see #BEFORE_API_CALL_HANDLER for a similar hook for stream-initiated API calls.%0A     */%0A    public static final String BEFORE_CONNECTION_HANDLER = %22Before_Connection_Event_Handler%22;%0A%0A    /**%0A     * Exchange-specific parameter used for providing a %7B@link Runnable%7D action to be run%0A     * prior to any API calls initiated during the course of maintaining a streamed connection.%0A     *%0A     * %3Cp%3EWhile some exchanges allow bidirectional streamed communication, such that a socket%0A     * can be opened and then authenticated, others perform authentication by means of a%0A     * separate API call which can count towards API rate limits.  In addition, many exchanges%0A     * require API calls to obtain initial snapshots for streamed data such as order books%0A     * or account updates. XChange requires that the developer manage rate limits themselves,%0A     * but this is not possible when xchange-stream has to initiate these API calls%0A     * automatically. This parameter provides a means for the developer to get a callback%0A     * prior to these API calls, principally to apply such rate limiting globally.  However,%0A     * in principle there are wider potential uses.%3C/p%3E%0A     *%0A     * @see #BEFORE_CONNECTION_HANDLER which provides the same sort of hook for socket%0A     * reconnections. This also includes example usage.%0A     */%0A    public static final String BEFORE_API_CALL_HANDLER = %22Before_API_Call
 _Eve

Add BEFORE_API_CALL_HANDLER hook and documentation