Skip to content

Commit

Permalink
fix(redux): fix never type in reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Feb 26, 2024
1 parent 4294a56 commit 164585b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion apps/demo/src/app/flight-search/flight-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const FlightStore = signalStore(
},

reducer: (actions, on) => {
on(actions.flightsLoaded, ({ flights }, state) => {
on(actions.flightsLoaded, (state, { flights }) => {
patchState(state, 'flights loaded', { flights });
});
},
Expand Down
12 changes: 6 additions & 6 deletions libs/ngrx-toolkit/src/lib/with-redux.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
HttpParams,
provideHttpClient,
} from '@angular/common/http';
import { map, switchMap, tap } from 'rxjs';
import { map, switchMap } from 'rxjs';
import { noPayload, payload, withRedux } from './with-redux';
import { TestBed } from '@angular/core/testing';
import {
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('with redux', () => {
},

reducer: (actions, on) => {
on(actions.flightsLoaded, ({ flights }, state) => {
on(actions.flightsLoaded, (state, { flights }) => {
patchState(state, { flights });
});
},
Expand All @@ -73,22 +73,22 @@ describe('with redux', () => {
'https://www.angulararchitects.io',
{
params: new HttpParams().set('from', from).set('to', to),
}
},
);
}),
map((flights) => actions.flightsLoaded({ flights }))
map((flights) => actions.flightsLoaded({ flights })),
),
};
},
})
}),
);

const flightsStore = new FlightsStore();
flightsStore.loadFlights({ from: 'Vienna', to: 'London' });
const flight = createFlight();
controller
.expectOne((req) =>
req.url.startsWith('https://www.angulararchitects.io')
req.url.startsWith('https://www.angulararchitects.io'),
)
.flush([flight]);

Expand Down
25 changes: 12 additions & 13 deletions libs/ngrx-toolkit/src/lib/with-redux.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Observable, Subject, Subscription } from 'rxjs';
import { Observable, Subject } from 'rxjs';
import { SignalStoreFeature } from '@ngrx/signals';
import {
EmptyFeatureResult,
SignalStoreFeatureResult,
} from '@ngrx/signals/src/signal-store-models';
import { StateSignal } from '@ngrx/signals/src/state-signal';
import { assertActionFnSpecs } from './assertions/assertions';
import { effect } from '@angular/core';

/** Actions **/

Expand Down Expand Up @@ -55,15 +54,15 @@ export const noPayload = {};
/** Reducer **/

type ReducerFunction<ReducerAction, State> = (
action: ActionFnPayload<ReducerAction>,
state: State
state: State,
action: ActionFnPayload<ReducerAction>
) => void;

type ReducerFactory<StateActionFns extends ActionFns, State> = (
actions: StateActionFns,
on: <ReducerAction extends { type: string }>(
action: ReducerAction,
reducerFn: ReducerFunction<ActionFnPayload<ReducerAction>, State>
reducerFn: ReducerFunction<ReducerAction, State>
) => void
) => void;

Expand All @@ -80,7 +79,7 @@ function createActionFns<Spec extends ActionsFnSpecs>(
actionFnSpecs: Spec,
reducerRegistry: Record<
string,
(payload: ActionFnPayload<unknown>, state: unknown) => void
(state: unknown, payload: ActionFnPayload<unknown>) => void
>,
effectsRegistry: Record<string, Subject<ActionFnPayload<unknown>>>,
state: unknown
Expand All @@ -92,9 +91,9 @@ function createActionFns<Spec extends ActionsFnSpecs>(
const fullPayload = { ...payload, type };
const reducer = reducerRegistry[type];
if (reducer) {
(reducer as (payload: unknown, state: unknown) => void)(
fullPayload as unknown,
state
(reducer as (state: unknown, payload: unknown) => void)(
state,
fullPayload as unknown
);
}
const effectSubject = effectsRegistry[type];
Expand All @@ -114,7 +113,7 @@ function createPublicAndAllActionsFns<Spec extends ActionsFnSpecs>(
actionFnSpecs: Spec,
reducerRegistry: Record<
string,
(payload: ActionFnPayload<unknown>, state: unknown) => void
(state: unknown, payload: ActionFnPayload<unknown>) => void
>,
effectsRegistry: Record<string, Subject<ActionFnPayload<unknown>>>,
state: unknown
Expand Down Expand Up @@ -160,12 +159,12 @@ function fillReducerRegistry(
actionFns: ActionFns,
reducerRegistry: Record<
string,
(payload: ActionFnPayload<unknown>, state: unknown) => void
(state: unknown, payload: ActionFnPayload<unknown>) => void
>
) {
function on(
action: { type: string },
reducerFn: (payload: ActionFnPayload<unknown>, state: unknown) => void
reducerFn: (state: unknown, payload: ActionFnPayload<unknown>) => void
) {
reducerRegistry[action.type] = reducerFn;
}
Expand Down Expand Up @@ -202,7 +201,7 @@ function processRedux<Spec extends ActionsFnSpecs, ReturnType>(
) {
const reducerRegistry: Record<
string,
(payload: ActionFnPayload<unknown>, state: unknown) => void
(state: unknown, payload: ActionFnPayload<unknown>) => void
> = {};
const effectsRegistry: Record<string, Subject<ActionFnPayload<unknown>>> = {};
const actionsMap = createPublicAndAllActionsFns(
Expand Down

0 comments on commit 164585b

Please sign in to comment.