Skip to content

Commit

Permalink
release: 2.1.3
Browse files Browse the repository at this point in the history
* lift action value into handler functions in caseFn, caseFns, caseEff, and
  caseEffs.
  • Loading branch information
baetheus committed Mar 19, 2024
1 parent 96237fe commit 206b55f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
23 changes: 11 additions & 12 deletions contrib/dux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type ActionType = {
*/
export interface Action<P> extends ActionType {
readonly value: P;
readonly error: boolean;
}

/**
Expand Down Expand Up @@ -196,7 +195,7 @@ export function actionCreatorFactory<G extends string>(
*
* @since 2.1.0
*/
export type Reducer<S, A extends ActionType = ActionType> = (s: S, a: A) => S;
export type Reducer<S, A = ActionType> = (s: S, a: A) => S;

/**
* Case function matches ActionCreator to Reducer.
Expand All @@ -205,9 +204,9 @@ export type Reducer<S, A extends ActionType = ActionType> = (s: S, a: A) => S;
*/
export function caseFn<S, P>(
action: ActionCreator<P>,
reducer: Reducer<S, Action<P>>,
reducer: Reducer<S, P>,
): Reducer<S, ActionType> {
return (s, a) => (action.match(a) ? reducer(s, a) : s);
return (s, a) => (action.match(a) ? reducer(s, a.value) : s);
}

/**
Expand All @@ -217,11 +216,11 @@ export function caseFn<S, P>(
*/
export function casesFn<S, A extends ActionCreator<unknown>[]>(
actionCreators: A,
reducer: Reducer<S, ExtractAction<A>>,
reducer: Reducer<S, ExtractAction<A>["value"]>,
): Reducer<S, ActionType> {
return (s, a) =>
actionCreators.some(({ match }) => match(a))
? reducer(s, <ExtractAction<A>> a)
? reducer(s, (<ExtractAction<A>> a).value)
: s;
}

Expand Down Expand Up @@ -304,14 +303,14 @@ export function metaReducerFn<S, A extends ActionType>(
/**
* @since 2.1.2
*/
export type Effect<A extends ActionType = ActionType> = (
export type Effect<A = ActionType> = (
a: A,
) => Stream<ActionType>;

/**
* @since 2.1.2
*/
export type EffectWide<A extends ActionType = ActionType> = (
export type EffectWide<A = ActionType> = (
a: A,
) => ActionType | Promise<ActionType> | Stream<ActionType>;

Expand All @@ -332,21 +331,21 @@ function liftAction(
*/
export function caseEff<P>(
action: ActionCreator<P>,
effect: EffectWide<Action<P>>,
effect: EffectWide<P>,
): Effect<ActionType> {
return (a) => action.match(a) ? liftAction(effect(a)) : M.empty();
return (a) => action.match(a) ? liftAction(effect(a.value)) : M.empty();
}

/**
* @since 2.1.2
*/
export function caseEffs<A extends ActionCreator<unknown>[]>(
actionCreators: A,
effect: EffectWide<ExtractAction<A>>,
effect: EffectWide<ExtractAction<A>["value"]>,
): Effect<ActionType> {
return (a) =>
actionCreators.some(({ match }) => match(a))
? liftAction(effect(<ExtractAction<A>> a))
? liftAction(effect((<ExtractAction<A>> a).value))
: M.empty();
}

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@baetheus/fun",
"version": "2.1.2",
"version": "2.1.3",
"exports": {
"./applicable": "./applicable.ts",
"./array": "./array.ts",
Expand Down

0 comments on commit 206b55f

Please sign in to comment.