@stolbivi/pirojok
Version:
Some minimalistic library used to build chrome extensions, covers some popular Chrome Extension API
44 lines (43 loc) • 2.09 kB
TypeScript
/// <reference types="chrome" />
import Port = chrome.runtime.Port;
import MessageSender = chrome.runtime.MessageSender;
export type Handler<Payload, Response> = (payload: Payload, sender?: MessageSender) => Promise<Response>;
export type RequestCreator<Payload, Response> = {
(payload?: Payload): Request<Payload, Response>;
type: string;
};
export type ActionCreator<Payload, Response> = {
(payload?: Payload): Action<Payload, Response>;
type: string;
};
export interface Request<Payload, Response> {
type: string;
payload?: Payload;
toAction: () => Action<Payload, Response>;
}
export interface Action<Payload, Response> {
type: string;
payload?: Payload;
handler: Handler<Payload, Response>;
}
export type Error = {
error: string;
};
export declare function createRequest<Payload, Response>(type: string): RequestCreator<Payload, Response>;
export declare function createAction<Payload, Response>(type: string, handler: Handler<Payload, Response>): ActionCreator<Payload, Response>;
export declare function createFromRequest<Payload, Response>(requestCreator: RequestCreator<Payload, Response>, handler: Handler<Payload, Response>): ActionCreator<Payload, Response>;
export type OnConnectListener = (port: Port) => void;
/**
* Upgraded version of messaging API wrapper. Designed for one time request-response style of communication.
* Internally cleans up listeners everywhere after receiving response or failing to receive it.
* You are still responsible to remove connection listener manually on the listening side, see #removeListener
*/
export declare class MessagesV2 {
private readonly _verbose;
constructor(verbose?: boolean);
request<Payload, Response>(request: Request<Payload, Response>): Promise<Response & Error>;
requestTab<Payload, Response>(tabId: number, request: Request<Payload, Response>): Promise<Response & Error>;
private handleRequest;
listen<Payload, Response>(actionCreator: ActionCreator<Payload, Response>): OnConnectListener;
removeListener(onConnect: OnConnectListener): void;
}