Skip to content

Commit

Permalink
feat(core): added matcher option for trigger event name
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Nov 17, 2022
1 parent 4afe572 commit 7ca96fa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
17 changes: 14 additions & 3 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from './constants';

import {
Matchable,
Matcher,
matchGetFilter,
objectLock,
typeIsMatchable,
} from '@harlem/utilities';

import type {
Expand Down Expand Up @@ -147,11 +151,18 @@ export function createStore<TState extends BaseState, TExtensions extends Extens
};

const getTrigger = <TEventData extends TriggerEventData>(eventName: string, prop: keyof TEventData): Trigger<TEventData> => {
return (name: string | string[], handler: TriggerHandler<TEventData>) => {
const mutations = ([] as string[]).concat(name);
return (matcher: Matcher | Matchable, handler: TriggerHandler<TEventData>) => {
const filter = matchGetFilter(
typeIsMatchable(matcher)
? matcher
: {
include: matcher,
exclude: [],
}
);

return store.on(eventName, (event?: EventPayload<TEventData>) => {
if (event && mutations.includes(event.data[prop] as unknown as string)) {
if (event && !filter(event.data[prop] as unknown as string)) {
handler(event.data);
}
});
Expand Down
6 changes: 3 additions & 3 deletions core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ export default class Store<TState extends BaseState = BaseState> implements Inte
const snapshot = objectClone(this.state);

const {
value: stateTrace,
value,
getNodes,
resetNodes,
} = objectTrace<TState>();
} = objectTrace<ReadState<TState>>();

const apply = <TValue>(
branchAccessor: BranchAccessor<TState, TValue> = functionIdentity,
Expand All @@ -307,7 +307,7 @@ export default class Store<TState extends BaseState = BaseState> implements Inte
}

resetNodes();
branchAccessor(stateTrace);
branchAccessor(value);

const nodes = getNodes();
const source = objectFromPath(snapshot, nodes);
Expand Down
10 changes: 7 additions & 3 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type {
DeepReadonly,
} from 'vue';

type UnionToIntersection<TValue> = (TValue extends any ? (arg: TValue) => any : never) extends ((arg: infer I) => void) ? I : never;
import type {
Matchable,
Matcher,
UnionToIntersection,
} from '@harlem/utilities';

export type BaseState = Record<PropertyKey, any>;
export type StoreProvider<TState extends BaseState> = keyof StoreProviders<TState>;
Expand All @@ -20,8 +24,8 @@ export type ActionBody<TState extends BaseState, TPayload = undefined, 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 TriggerHandler<TEventData extends TriggerEventData> = (data: TEventData) => void;
export type Trigger<TEventData extends TriggerEventData> = (name: string | string[], handler: TriggerHandler<TEventData>) => EventListener;
export type BranchAccessor<TState extends BaseState, TValue> = (state: ReadState<TState> | WriteState<TState>) => TValue;
export type Trigger<TEventData extends TriggerEventData> = (matcher: Matcher | Matchable, handler: TriggerHandler<TEventData>) => EventListener;
export type BranchAccessor<TState extends BaseState, TValue> = (state: ReadState<TState>) => TValue;
export type InternalStores = Map<string, InternalStore<BaseState>>;
export type Extension<TState extends BaseState> = (store: InternalStore<TState>) => Record<string, any>;
export type ExtensionAPIs<TExtensions extends Extension<BaseState>[]> = UnionToIntersection<ReturnType<TExtensions[number]>>;
Expand Down

0 comments on commit 7ca96fa

Please sign in to comment.