httpgd
Version: 
R httpgd GraphicsDevice API and connection handler
112 lines (111 loc) • 3.31 kB
TypeScript
import { fetch_plot } from './api';
import { HttpgdIdResponse, HttpgdInfoResponse, HttpgdPlotRequest, HttpgdPlotsResponse, HttpgdRemoveRequest, HttpgdRendererResponse } from './types';
/**
 * Manages httpgd connection and API access.
 */
export declare class Httpgd {
    private backend;
    private connection;
    private data;
    private plotsChanged;
    private deviceActiveChanged;
    /**
     * Setup httpgd backend and connection.
     *
     * Note that Httpgd.connect() needs to be called after to open the connection
     * and start listening for remote changes.
     *
     * @param host Httpgd host
     * @param token Security token
     * @param allowWebsockets Allow WebSocket connection
     */
    constructor(host: string, token?: string, allowWebsockets?: boolean);
    /**
     * Open the connection to the httpgd backend.
     *
     * This will also cause a renderer list update and return a promise that will
     * be resolved once the renderers are updated.
     *
     * @returns Promise that will be resolved once the server information is
     *   updated (server info, list of renderers).
     */
    connect(): Promise<void>;
    /**
     * Disconnect httpgd backend.
     */
    disconnect(): void;
    /**
     * Listen to connection changes.
     *
     * A connection change occurs when the server goes offline or the connection
     * is interrupted.
     *
     * @param fun
     */
    onConnectionChanged(fun: (newState: boolean, oldState?: boolean) => void): void;
    private remoteStateChanged;
    private localStateChanged;
    private updateInfo;
    /**
     * Update plot ID list data. This will trigger `Httpgd.onPlotsChanged()` listeners.
     */
    updatePlots(): Promise<void>;
    /**
     * Get plot ID list.
     *
     * @returns
     */
    getPlots(): HttpgdIdResponse[];
    /**
     * Get renderer list.
     *
     * @returns
     */
    getInfo(): HttpgdInfoResponse | undefined;
    /**
     * Get renderer list.
     *
     * @returns
     */
    getRenderers(): HttpgdRendererResponse[];
    /**
     * Get the URL of a plot.
     *
     * @param r Plot request object.
     * @returns URL string
     */
    getPlotURL(r: HttpgdPlotRequest): string | undefined;
    /**
     * Get rendered plot data.
     *
     * @param r Plot request object.
     * @returns Plot data
     */
    getPlot(r: HttpgdPlotRequest): ReturnType<typeof fetch_plot> | undefined;
    /**
     * Listen to plot changes.
     *
     * Plot changes occur when a plot changes or a new plot is added remotely in
     * the R session.
     */
    onPlotsChanged(fun: (newState: HttpgdPlotsResponse, oldState?: HttpgdPlotsResponse) => void): void;
    /**
     * Listen to device active changes.
     *
     * The R GraphicsDevice becomes inactive when a other graphics device is
     * created remotely in the R session.
     *
     * @param fun
     */
    onDeviceActiveChanged(fun: (newState: boolean, oldState?: boolean) => void): void;
    /**
     * Remove a specific plot.
     *
     * @param r Remove request object
     */
    removePlot(r: HttpgdRemoveRequest): Promise<void>;
    /**
     * Clear all plots.
     */
    clearPlots(): Promise<void>;
}