@discoveryjs/discovery
Version:
Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
183 lines (182 loc) • 10 kB
TypeScript
import type { PageParams, PageRef, PageHashState, PageHashStateWithAnchor, PageAnchor } from '../main/model.js';
import type { EmbedClientToHostMessage, EmbedHostToClientMessage, EmbedHostToPreinitMessage, EmbedPreinitToHostMessage, NavInsertPosition } from './embed-message.types.js';
import type { NavItemConfig } from '../nav/index.js';
import type { LoadDataState } from '../core/utils/load-data.js';
import type { ProgressbarState } from '../core/utils/progressbar.js';
import type { ColorSchemeState, SerializedColorSchemeValue } from '../core/color-scheme.js';
import { Emitter } from '../core/emitter.js';
import { Observer } from '../core/observer.js';
import { extractResourceMetadata } from '../core/utils/load-data.js';
import { loadStages, decodeStageProgress } from '../core/utils/progressbar.js';
import { LocationSync } from '../core/utils/location-sync.js';
export type BaseAppEvents = {
destroy: [];
};
export interface EmbedPreinitAppEvents extends BaseAppEvents {
loadingStateChanged: [state: LoadDataState];
}
export interface EmbedAppEvents extends BaseAppEvents {
colorSchemeChanged: [
value: {
state: ColorSchemeState;
value: SerializedColorSchemeValue;
}
];
loadingStateChanged: [state: ProgressbarState];
pageHashChanged: [hash: string, replace: boolean];
unloadData: [];
data: [];
}
export type onDisconnectCallback = () => void;
export type onPreinitCallback = (api: typeof EmbedPreinitApp.prototype.publicApi) => onDisconnectCallback | void;
export type onConnectCallback = (api: typeof EmbedApp.prototype.publicApi) => onDisconnectCallback | void;
export type ActionsMap = Map<string, (...args: unknown[]) => unknown>;
declare class BaseApp<Message extends EmbedHostToPreinitMessage | EmbedHostToClientMessage, Events extends BaseAppEvents = BaseAppEvents> extends Emitter<Events> {
window: Window;
id: string;
actions: ActionsMap;
dataLoadToken: string | null;
constructor(window: Window, id: string, actions: ActionsMap);
sendMessage<T extends Message['type']>(type: T, payload: Extract<Message, {
type: T;
}>['payload'], transfer?: Transferable[]): void;
destroy(): void;
}
declare class EmbedPreinitApp extends BaseApp<EmbedHostToPreinitMessage, EmbedPreinitAppEvents> {
publicApi: ReturnType<typeof EmbedPreinitApp.createPublicApi>;
static createPublicApi(app: EmbedPreinitApp): Readonly<{
on: EmbedPreinitApp["on"];
once: EmbedPreinitApp["once"];
off: EmbedPreinitApp["off"];
defineAction(name: string, fn: (...args: unknown[]) => unknown): void;
setPageHash(hash: string, replace?: boolean): void;
setRouterPreventLocationUpdate(allow?: boolean): void;
}>;
constructor(window: Window, id: string, actions: ActionsMap);
processMessage(message: EmbedPreinitToHostMessage): void;
}
declare class EmbedApp extends BaseApp<EmbedHostToClientMessage, EmbedAppEvents> {
commandMap: Map<string, (...args: unknown[]) => unknown>;
dataLoadToken: string | null;
pageHash: Observer<string>;
pageId: Observer<string>;
pageRef: Observer<PageRef>;
pageParams: Observer<PageParams>;
pageAnchor: Observer<PageAnchor>;
locationSync: LocationSync | null;
colorScheme: Observer<{
state: ColorSchemeState | 'unknown';
value: SerializedColorSchemeValue | 'unknown';
}>;
publicApi: ReturnType<typeof EmbedApp.createPublicApi>;
static createPublicApi(app: EmbedApp): Readonly<{
pageHash: {
subscribe: (callback: (value: string, unsubscribe: () => void) => void | Promise<void>) => () => void;
subscribeSync: (callback: (value: string, unsubscribe: () => void) => void | Promise<void>) => () => void;
unsubscribe: (callback: (value: string, unsubscribe: () => void) => void | Promise<void>) => void;
readonly value: string;
};
pageId: {
subscribe: (callback: (value: string, unsubscribe: () => void) => void | Promise<void>) => () => void;
subscribeSync: (callback: (value: string, unsubscribe: () => void) => void | Promise<void>) => () => void;
unsubscribe: (callback: (value: string, unsubscribe: () => void) => void | Promise<void>) => void;
readonly value: string;
};
pageRef: {
subscribe: (callback: (value: PageRef, unsubscribe: () => void) => void | Promise<void>) => () => void;
subscribeSync: (callback: (value: PageRef, unsubscribe: () => void) => void | Promise<void>) => () => void;
unsubscribe: (callback: (value: PageRef, unsubscribe: () => void) => void | Promise<void>) => void;
readonly value: PageRef;
};
pageAnchor: {
subscribe: (callback: (value: PageAnchor, unsubscribe: () => void) => void | Promise<void>) => () => void;
subscribeSync: (callback: (value: PageAnchor, unsubscribe: () => void) => void | Promise<void>) => () => void;
unsubscribe: (callback: (value: PageAnchor, unsubscribe: () => void) => void | Promise<void>) => void;
readonly value: PageAnchor;
};
pageParams: {
subscribe: (callback: (value: PageParams, unsubscribe: () => void) => void | Promise<void>) => () => void;
subscribeSync: (callback: (value: PageParams, unsubscribe: () => void) => void | Promise<void>) => () => void;
unsubscribe: (callback: (value: PageParams, unsubscribe: () => void) => void | Promise<void>) => void;
readonly value: PageParams;
};
colorScheme: {
subscribe: (callback: (value: {
state: ColorSchemeState | "unknown";
value: SerializedColorSchemeValue | "unknown";
}, unsubscribe: () => void) => void | Promise<void>) => () => void;
subscribeSync: (callback: (value: {
state: ColorSchemeState | "unknown";
value: SerializedColorSchemeValue | "unknown";
}, unsubscribe: () => void) => void | Promise<void>) => () => void;
unsubscribe: (callback: (value: {
state: ColorSchemeState | "unknown";
value: SerializedColorSchemeValue | "unknown";
}, unsubscribe: () => void) => void | Promise<void>) => void;
readonly value: {
state: ColorSchemeState | "unknown";
value: SerializedColorSchemeValue | "unknown";
};
};
on: EmbedApp["on"];
once: EmbedApp["once"];
off: EmbedApp["off"];
nav: {
insert(config: NavItemConfig, position: NavInsertPosition, name: string): void;
prepend(config: NavItemConfig): void;
append(config: NavItemConfig): void;
before(name: string, config: NavItemConfig): void;
after(name: string, config: NavItemConfig): void;
replace(name: string, config: NavItemConfig): void;
remove(name: string): void;
} & {
primary: {
insert(config: NavItemConfig, position: NavInsertPosition, name: string): void;
prepend(config: NavItemConfig): void;
append(config: NavItemConfig): void;
before(name: string, config: NavItemConfig): void;
after(name: string, config: NavItemConfig): void;
replace(name: string, config: NavItemConfig): void;
remove(name: string): void;
};
secondary: {
insert(config: NavItemConfig, position: NavInsertPosition, name: string): void;
prepend(config: NavItemConfig): void;
append(config: NavItemConfig): void;
before(name: string, config: NavItemConfig): void;
after(name: string, config: NavItemConfig): void;
replace(name: string, config: NavItemConfig): void;
remove(name: string): void;
};
menu: {
insert(config: NavItemConfig, position: NavInsertPosition, name: string): void;
prepend(config: NavItemConfig): void;
append(config: NavItemConfig): void;
before(name: string, config: NavItemConfig): void;
after(name: string, config: NavItemConfig): void;
replace(name: string, config: NavItemConfig): void;
remove(name: string): void;
};
};
notify(name: string, details: any): void;
defineAction(name: string, fn: (...args: unknown[]) => unknown): void;
setPageHash(hash: string, replace?: boolean): void;
setPageHashState(pageState: Partial<PageHashState>, replace?: boolean): void;
setPageHashStateWithAnchor(pageStateWithAnchor: Partial<PageHashStateWithAnchor>, replace?: boolean): void;
setPage(id: string, ref: PageRef, params: PageParams, replace?: boolean): void;
setPageRef(ref: PageRef, replace?: boolean): void;
setPageParams(params: PageParams, replace?: boolean): void;
setPageAnchor(anchor: PageAnchor, replace?: boolean): void;
setColorSchemeState(value: ColorSchemeState): void;
setRouterPreventLocationUpdate(allow?: boolean): void;
setLocationSync(enabled?: boolean): void;
unloadData(): void;
uploadData(source: unknown, getResourceMetadataFromSource: typeof extractResourceMetadata): Promise<void>;
}>;
constructor(window: Window, id: string, actions: ActionsMap);
processMessage(message: EmbedClientToHostMessage): Promise<void>;
destroy(): void;
}
export { loadStages, decodeStageProgress };
export declare function connectToEmbedApp(iframe: HTMLIFrameElement, onConnect: onConnectCallback): onDisconnectCallback;
export declare function connectToEmbedApp(iframe: HTMLIFrameElement, onPreinit: onPreinitCallback | void, onConnect: onConnectCallback): onDisconnectCallback;