Skip to content

Commit 284da58

Browse files
feat(redux): add private/public feature
1 parent 22157c9 commit 284da58

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

libs/ngrx-toolkit/src/lib/with-redux.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ import { signalStore } from '@ngrx/signals';
22
import { inject } from '@angular/core';
33
import { HttpClient } from '@angular/common/http';
44
import { map, switchMap } from 'rxjs';
5-
import { createActions, payload, withRedux } from './with-redux';
6-
import { TestBed } from '@angular/core/testing';
5+
import { noPayload, payload, withRedux } from './with-redux';
76

87
type Flight = { id: number };
98

109
describe('with redux', () => {
1110
it('should load flights', () => {
1211
const FlightsStore = signalStore(
1312
withRedux({
14-
actions: createActions({
13+
actions: {
1514
public: {
1615
loadFlights: payload<{ from: string; to: string }>(),
16+
delayFirst: noPayload,
1717
},
1818
private: {
1919
flightsLoaded: payload<{ flights: Flight[] }>(),
2020
},
21-
}),
21+
},
2222

2323
reducer: (on, actions) => {
2424
on(actions.loadFlights, (action, state) => state);
2525
on(actions.flightsLoaded, (action, state) => state);
2626
},
2727

2828
effects: (actions, create) => {
29-
const httpCLient = inject(HttpClient);
29+
const httpClient = inject(HttpClient);
3030

3131
create(actions.loadFlights).pipe(
3232
switchMap(({ from, to }) =>
33-
httpCLient.get<Flight[]>('www.angulararchitects.io', {
33+
httpClient.get<Flight[]>('www.angulararchitects.io', {
3434
params: { from, to },
3535
})
3636
),

libs/ngrx-toolkit/src/lib/with-redux.ts

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { map, Observable, of, pipe, switchMap, tap } from 'rxjs';
2-
import { rxMethod } from '@ngrx/signals/rxjs-interop';
3-
import { HttpClient } from '@angular/common/http';
4-
import { inject } from '@angular/core';
5-
import { signalStore, SignalStoreFeature } from '@ngrx/signals';
1+
import { Observable } from 'rxjs';
2+
import { SignalStoreFeature } from '@ngrx/signals';
63
import {
74
EmptyFeatureResult,
85
SignalStoreFeatureResult,
@@ -23,15 +20,17 @@ type ActionsCreator<Spec extends ActionsSpec> = Extract<
2320
'private' | 'public'
2421
> extends never
2522
? {
26-
[ActionName in keyof Spec]: Spec[ActionName] & { type: ActionName };
23+
[ActionName in keyof Spec]: Spec[ActionName] & {
24+
type: ActionName & string;
25+
};
2726
}
2827
: {
2928
[ActionName in keyof Spec['private']]: Spec['private'][ActionName] & {
30-
type: ActionName;
29+
type: ActionName & string;
3130
};
3231
} & {
3332
[ActionName in keyof Spec['public']]: Spec['public'][ActionName] & {
34-
type: ActionName;
33+
type: ActionName & string;
3534
};
3635
};
3736

@@ -40,11 +39,13 @@ type PublicActions<Spec extends ActionsSpec> = Extract<
4039
'private' | 'public'
4140
> extends never
4241
? {
43-
[ActionName in keyof Spec]: Spec[ActionName] & { type: ActionName };
42+
[ActionName in keyof Spec]: Spec[ActionName] & {
43+
type: ActionName & string;
44+
};
4445
}
4546
: {
4647
[ActionName in keyof Spec['public']]: Spec['public'][ActionName] & {
47-
type: ActionName;
48+
type: ActionName & string;
4849
};
4950
};
5051

@@ -58,19 +59,13 @@ export declare function createActions<Spec extends ActionsSpec>(
5859
spec: Spec
5960
): ActionsCreator<Spec>;
6061

61-
type ActionsFactory<StateActions extends Actions> = () => StateActions;
62-
6362
type ReducerFn<A extends Action = Action> = (
6463
action: A,
6564
reducerFn: (action: A, state: State) => State
6665
) => void;
6766

6867
type ReducerFactory<A extends Actions> = (on: ReducerFn, actions: A) => void;
6968

70-
type EffectFn<A extends Action = Action> = {
71-
(action: A, effect: (action: Observable<A>) => Observable<Action>): void;
72-
};
73-
7469
type EffectsFactory<StateActions extends Actions> = (
7570
actions: StateActions,
7671
forAction: <EffectAction extends Action>(
@@ -90,14 +85,14 @@ type EffectsFactory<StateActions extends Actions> = (
9085
*/
9186
export declare function withRedux<
9287
Spec extends ActionsSpec,
93-
StateActions extends Actions,
94-
Input extends SignalStoreFeatureResult
88+
Input extends SignalStoreFeatureResult,
89+
StoreActions extends ActionsCreator<Spec> = ActionsCreator<Spec>,
90+
PublicStoreActions extends PublicActions<Spec> = PublicActions<Spec>
9591
>(redux: {
96-
actions: StateActions;
97-
// actions: (actionsCreator: (spec: ActionsSpec) => StateActions) => void;
98-
reducer: ReducerFactory<StateActions>;
99-
effects: EffectsFactory<StateActions>;
92+
actions: Spec;
93+
reducer: ReducerFactory<StoreActions>;
94+
effects: EffectsFactory<StoreActions>;
10095
}): SignalStoreFeature<
10196
Input,
102-
EmptyFeatureResult & { methods: { actions: () => StateActions } }
97+
EmptyFeatureResult & { methods: { actions: () => PublicStoreActions } }
10398
>;

0 commit comments

Comments
 (0)