Skip to content

Commit

Permalink
Export action and event handler types (#987)
Browse files Browse the repository at this point in the history
The `ActionHandler`, `EventHandler` and related types were previously
not exported. We use these in `snaps-monorepo`, but currently have to
copy them over.
  • Loading branch information
Mrtenz committed Nov 23, 2022
1 parent 4dbce9e commit fe822ff
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/base-controller/src/ControllerMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
type ActionHandler<Action, ActionType> = (
export type ActionHandler<Action, ActionType> = (
...args: ExtractActionParameters<Action, ActionType>
) => ExtractActionResponse<Action, ActionType>;
type ExtractActionParameters<Action, T> = Action extends {
export type ExtractActionParameters<Action, T> = Action extends {
type: T;
handler: (...args: infer H) => any;
}
? H
: never;
type ExtractActionResponse<Action, T> = Action extends {
export type ExtractActionResponse<Action, T> = Action extends {
type: T;
handler: (...args: any) => infer H;
}
? H
: never;

type ExtractEventHandler<Event, T> = Event extends { type: T; payload: infer P }
export type ExtractEventHandler<Event, T> = Event extends {
type: T;
payload: infer P;
}
? P extends unknown[]
? (...payload: P) => void
: never
: never;
type ExtractEventPayload<Event, T> = Event extends { type: T; payload: infer P }
export type ExtractEventPayload<Event, T> = Event extends {
type: T;
payload: infer P;
}
? P
: never;

type GenericEventHandler = (...args: unknown[]) => void;
export type GenericEventHandler = (...args: unknown[]) => void;

type SelectorFunction<Args extends unknown[], ReturnValue> = (
export type SelectorFunction<Args extends unknown[], ReturnValue> = (
...args: Args
) => ReturnValue;
type SelectorEventHandler<SelectorReturnValue> = (
export type SelectorEventHandler<SelectorReturnValue> = (
newValue: SelectorReturnValue,
previousValue: SelectorReturnValue | undefined,
) => void;
Expand Down

0 comments on commit fe822ff

Please sign in to comment.