@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
68 lines • 3.13 kB
TypeScript
import { Observable } from "rxjs";
import Transport from "@ledgerhq/hw-transport";
export declare const setErrorRemapping: (f: (arg0: Error) => Observable<never>) => void;
export declare const cancelDeviceAction: (transport: Transport) => void;
export type QueuedJob = {
job: Promise<void>;
id: number;
};
/**
* Manages queued jobs for each device (id)
*
* Careful: a USB-connected device has no unique id, and its `deviceId` will be an empty string.
*
* The queue object `queuedJobsByDevice` only stores, for each device, the latest void promise that will resolve
* when the device is ready to be opened again.
* They are scheduled to resolve whenever the job associated to the device is finished.
* When calling withDevice several times, the new promise will be chained to the "then" of the previous promise:
* open(device) -> execute job -> clean connection -> resolve promise -> next promise can start: open(device) -> etc.
* So a queue is indeed created for each device, by creating a chain of promises, but only the end of the queue is stored for each device.
*/
export declare class DeviceQueuedJobsManager {
private queuedJobsByDevice;
private static instance;
private static jobIdCounter;
private constructor();
/**
* Get the singleton instance
*/
static getInstance(): DeviceQueuedJobsManager;
/**
* Returns the latest queued job for a given device id
*
* @param deviceId
* @returns the latest QueuedJob. If none, return a queued job that can be resolved directly.
*/
getLastQueuedJob(deviceId: string): QueuedJob;
/**
* Sets the latest queue job for a given device id
*
* Also increments the job id counter and set the newly queued job id to this new incremented value.
*
* Note: we should be fine on race conditions when updating the jobId in our use cases.
*
* @param deviceId
* @param jobToQueue a Promise that resolve to void, representing an async job
* @returns the id of the queued job
*/
setLastQueuedJob(deviceId: string, jobToQueue: Promise<void>): number;
}
/**
* Provides a Transport instance to a given job
*
* @param deviceId
* @param options contains optional configuration
* - openTimeoutMs: optional timeout that limits in time the open attempt of the matching registered transport.
*/
export declare const withDevice: (deviceId: string, options?: {
openTimeoutMs?: number;
}) => <T>(job: (t: Transport) => Observable<T>) => Observable<T>;
/**
* Provides a Transport instance to the given function fn
* @see withDevice
*/
export declare const withDevicePromise: <T>(deviceId: string, fn: (Transport: any) => Promise<T>) => Promise<T>;
export declare const genericCanRetryOnError: (err: unknown) => boolean;
export declare const retryWhileErrors: (acceptError: (arg0: Error) => boolean) => (attempts: Observable<any>) => Observable<any>;
export declare const withDevicePolling: (deviceId: string) => <T>(job: (arg0: Transport) => Observable<T>, acceptError?: (arg0: Error) => boolean) => Observable<T>;
//# sourceMappingURL=deviceAccess.d.ts.map