UNPKG

repond-events

Version:
160 lines (159 loc) 6.81 kB
import { ItemType, StatePath } from "repond"; import { RepondEventsTypes } from "./declarations"; type WithRequiredProps<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>; type StringWithAutocomplete<T> = T | (string & Record<never, never>); export type KnownChainId = RepondEventsTypes<any, any, any>["KnownChainIds"]; export type ChainId = StringWithAutocomplete<KnownChainId>; export type RunMode = "add" | "start" | "end" | "pause" | "unpause" | "suspend" | "unsuspend" | "cancel" | "skip"; export type RunModeExtraOptions = { runBy?: string; }; export type RunModeOptions = RunModeExtraOptions & { runMode: RunMode; }; export type EventBlockBase = { group: string; name: string; params?: Record<any, any>; }; export type EventBlockOptions = { chainId?: ChainId; liveId?: string; addedBy?: string; isParallel?: boolean; timePath?: StatePath<ItemType>; hasPriority?: boolean; duration?: number; isFast?: boolean; parentChainId?: ChainId; }; export type EventBlockOptionsWithChainId = WithRequiredProps<EventBlockOptions, "chainId">; export type EventBlockOptionsWithIds = WithRequiredProps<EventBlockOptions, "chainId" | "liveId">; export type EventBlock = EventBlockBase & { options: EventBlockOptions; type?: "event"; }; export type EventBlockWithChainId = EventBlock & { options: EventBlockOptionsWithChainId; }; export type EventBlockWithIds = EventBlock & { options: EventBlockOptionsWithIds; }; export type EventRunLiveInfo = { liveId: string; chainId: ChainId; parentChainId?: ChainId; runBy: string; addedBy: string; runMode: RunMode; isFast: boolean; didStart: boolean; remainingTime: number; elapsedTime: number; isUnpausing: boolean; isUnsuspending: boolean; isUnfreezing: boolean; isFreezing: boolean; isFirstAdd: boolean; isFirstStart: boolean; isFirstPause: boolean; isFirstSuspend: boolean; addTime: number; startTime: number; pauseTime: number; unpauseTime: number; suspendTime: number; unsuspendTime: number; goalEndTime: number; }; export type EventTypeDefinition<T_Params extends Record<any, any>> = { run: (params: T_Params, liveInfo: EventRunLiveInfo) => void; params: T_Params; isParallel?: boolean; id?: string; duration?: number; timePath?: StatePath<ItemType>; }; type OriginalEventGroups = RepondEventsTypes<any, any, any>["EventGroups"]; export type EmojiKeys = RepondEventsTypes<any, any, any>["EmojiKeys"] extends never ? Record<string, string> : RepondEventsTypes<any, any, any>["EmojiKeys"]; type OriginalValueGroups = RepondEventsTypes<any, any, any>["ValueGroups"]; export type ValueEmojiKeys = RepondEventsTypes<any, any, any>["EmojiKeys"] extends never ? Record<string, string> : RepondEventsTypes<any, any, any>["EmojiKeys"]; type RemoveEventsSuffix<T extends string> = T extends `${infer Prefix}Events` ? Prefix : T; export type RefinedEventGroups = { [K in keyof OriginalEventGroups as RemoveEventsSuffix<K>]: OriginalEventGroups[K]; }; export type EventGroupName = keyof RefinedEventGroups & string; export type EventName<T extends EventGroupName> = keyof RefinedEventGroups[T] & string; export type CustomEventParams<T_Group, T_Event, T_GenericParamA> = RepondEventsTypes<T_Group, T_Event, T_GenericParamA>["EventParameters"]; export type DefaultEventParams<T_Group extends EventGroupName & string, T_Name extends EventName<T_Group> & string> = RefinedEventGroups[T_Group][T_Name] extends { params: infer P; } ? P : RefinedEventGroups[T_Group][T_Name] extends {} ? undefined : never; export type MakeOptionalWhereUndefined<T> = Partial<T> & { [P in keyof T as undefined extends T[P] ? never : P]: T[P]; }; type Evaluate<T> = T extends infer U ? { [K in keyof U]: U[K]; } : never; export type EventParams<T_Group extends EventGroupName, T_Event extends EventName<T_Group>, T_GenericParamA> = CustomEventParams<T_Group, T_Event, T_GenericParamA> extends never ? MakeOptionalWhereUndefined<Evaluate<DefaultEventParams<T_Group, T_Event>>> : CustomEventParams<T_Group, T_Event, T_GenericParamA>; export type EventBlockTuple = { [G in EventGroupName]: { [N in EventName<G>]: [G, N, DefaultEventParams<G, N>] | [G, N, DefaultEventParams<G, N>, EventBlockOptions]; }[EventName<G>]; }[EventGroupName]; export type EventBlockTupleLoose = [string, string, Record<string, any>, EventBlockOptions]; export type AllOptional<T> = { [P in keyof T]-?: undefined extends T[P] ? never : P; } extends { [key: string]: never; } ? true : false; export type TypeOrUndefinedIfAllOptional<T> = AllOptional<T> extends true ? T | undefined : T; export type ParamMap = Record<string, any>; export type RawValue = number | string | boolean | null | undefined | Record<any, any> | any[]; export type ValueBlockOptions = { chainId?: ChainId; liveId?: string; addedBy?: string; isParallel?: boolean; timePath?: StatePath<ItemType>; hasPriority?: boolean; duration?: number; isFast?: boolean; parentChainId?: ChainId; }; export type ValueBlock = { group: string; name: string; params?: Record<any, any>; options: ValueBlockOptions; type: "value"; }; export type EvaluateParamsInfo = { valueIdBase: string; parentChainId?: ChainId; runBy?: string; addedBy?: string; }; export type ValueRunInfo = { valueId: string; parentChainId?: ChainId; runBy?: string; addedBy?: string; isFast?: boolean; }; export type ValueTypeDefinition<T_Params extends Record<any, any>> = { run: (evaluatedParams: T_Params, liveInfo: ValueRunInfo) => any; params: T_Params; id?: string; }; type RemoveValuesSuffix<T extends string> = T extends `${infer Prefix}Values` ? Prefix : T; export type RefinedValueGroups = { [K in keyof OriginalValueGroups as RemoveValuesSuffix<K>]: OriginalValueGroups[K]; }; export type ValueGroupName = keyof RefinedValueGroups & string; export type ValueName<T extends ValueGroupName> = keyof RefinedValueGroups[T] & string; export type CustomValueParams<T_Group, T_Event, T_GenericParamA> = RepondEventsTypes<T_Group, T_Event, T_GenericParamA>["EventParameters"]; export type DefaultValueParams<T_Group extends ValueGroupName & string, T_Name extends ValueName<T_Group> & string> = RefinedValueGroups[T_Group][T_Name] extends { params: infer P; } ? P : RefinedValueGroups[T_Group][T_Name] extends {} ? undefined : never; export type ValueParams<T_Group extends ValueGroupName, T_Value extends ValueName<T_Group>, T_GenericParamA> = CustomValueParams<T_Group, T_Value, T_GenericParamA> extends never ? MakeOptionalWhereUndefined<Evaluate<DefaultValueParams<T_Group, T_Value>>> : CustomValueParams<T_Group, T_Value, T_GenericParamA>; export {};