2022-08-11 12:03:51 +00:00
|
|
|
import { Values } from "./helpers";
|
|
|
|
|
|
|
|
export type Version = 1;
|
|
|
|
|
|
|
|
export const EventType = {
|
|
|
|
handshake: "handshake",
|
|
|
|
response: "response",
|
|
|
|
redirect: "redirect",
|
|
|
|
theme: "theme",
|
2022-09-13 09:59:30 +00:00
|
|
|
localeChanged: "localeChanged",
|
2022-08-11 12:03:51 +00:00
|
|
|
} as const;
|
2022-08-11 21:14:41 +00:00
|
|
|
|
2022-08-11 12:03:51 +00:00
|
|
|
export type EventType = Values<typeof EventType>;
|
|
|
|
|
|
|
|
type Event<Name extends EventType, Payload extends {}> = {
|
|
|
|
payload: Payload;
|
|
|
|
type: Name;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type HandshakeEvent = Event<
|
|
|
|
"handshake",
|
|
|
|
{
|
|
|
|
token: string;
|
|
|
|
version: Version;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type DispatchResponseEvent = Event<
|
|
|
|
"response",
|
|
|
|
{
|
|
|
|
actionId: string;
|
|
|
|
ok: boolean;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type RedirectEvent = Event<
|
|
|
|
"redirect",
|
|
|
|
{
|
|
|
|
path: string;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type ThemeType = "light" | "dark";
|
|
|
|
export type ThemeEvent = Event<
|
|
|
|
"theme",
|
|
|
|
{
|
|
|
|
theme: ThemeType;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
2022-09-13 09:59:30 +00:00
|
|
|
export type LocaleChangedEvent = Event<
|
|
|
|
"localeChanged",
|
|
|
|
{
|
|
|
|
locale: string;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type Events =
|
|
|
|
| HandshakeEvent
|
|
|
|
| DispatchResponseEvent
|
|
|
|
| RedirectEvent
|
|
|
|
| ThemeEvent
|
|
|
|
| LocaleChangedEvent;
|
2022-08-11 12:03:51 +00:00
|
|
|
|
|
|
|
export type PayloadOfEvent<
|
|
|
|
TEventType extends EventType,
|
|
|
|
TEvent extends Events = Events
|
|
|
|
// @ts-ignore TODO - why this is not working with this tsconfig? Fixme
|
2022-09-02 14:53:40 +00:00
|
|
|
> = TEvent extends Event<TEventType, unknown> ? TEvent["payload"] : never;
|