Skip to content

Commit

Permalink
refactor(core): cleaned up trigger types
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Nov 18, 2022
1 parent 5952f3b commit fead81f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
8 changes: 3 additions & 5 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
} from './constants';

import {
Matchable,
Matcher,
matchGetFilter,
objectLock,
typeIsFunction,
Expand All @@ -30,8 +28,8 @@ import type {
PluginOptions,
PublicStore,
StoreOptions,
Trigger,
TriggerEventData,
TriggerHandler,
} from './types';

export {
Expand Down Expand Up @@ -148,8 +146,8 @@ export function createStore<TState extends BaseState, TExtensions extends Extens
store.emit(EVENTS.devtools.update, SENDER, state);
};

const getTrigger = (eventName: string) => {
return (matcher: Matcher | Matchable, handler: TriggerHandler) => {
const getTrigger = (eventName: string): Trigger => {
return (matcher, handler) => {
const filter = matchGetFilter(
typeIsMatchable(matcher)
? matcher
Expand Down
17 changes: 9 additions & 8 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Mutation<TPayload, TResult = void> = undefined extends TPayload ? (p
export type ActionBody<TState extends BaseState, TPayload = undefined, TResult = void> = (payload: TPayload, mutator: (mutate: Mutator<TState, undefined, void>) => void) => Promise<TResult>;
export type Action<TPayload, TResult = void> = undefined extends TPayload ? (payload?: TPayload) => Promise<TResult> : (payload: TPayload) => Promise<TResult>;
export type EventHandler<TData = any> = (payload?: EventPayload<TData>) => void;
export type Trigger = <TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>) => EventListener;
export type TriggerHandler<TPayload = any, TResult = any> = (data: TriggerEventData<TPayload, TResult>) => void;
export type BranchAccessor<TState extends BaseState, TValue> = (state: ReadState<TState>) => TValue;
export type InternalStores = Map<string, InternalStore<BaseState>>;
Expand Down Expand Up @@ -287,14 +288,14 @@ export interface Store<TState extends BaseState> extends StoreBase<TState> {
*/
state: ReadState<TState>;

onBeforeMutation<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onAfterMutation<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onMutationSuccess<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onMutationError<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onBeforeAction<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onAfterAction<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onActionSuccess<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onActionError<TPayload, TResult>(matcher: Matcher | Matchable, handler: TriggerHandler<TPayload, TResult>): EventListener;
onBeforeMutation: Trigger;
onAfterMutation: Trigger;
onMutationSuccess: Trigger;
onMutationError: Trigger;
onBeforeAction: Trigger;
onAfterAction: Trigger;
onActionSuccess: Trigger;
onActionError: Trigger;
}

export interface HarlemPlugin {
Expand Down

0 comments on commit fead81f

Please sign in to comment.