Skip to content

Commit

Permalink
feat(redux): add private/public feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Dec 18, 2023
1 parent 22157c9 commit 284da58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
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 @@ -2,35 +2,35 @@ import { signalStore } from '@ngrx/signals';
import { inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, switchMap } from 'rxjs';
import { createActions, payload, withRedux } from './with-redux';
import { TestBed } from '@angular/core/testing';
import { noPayload, payload, withRedux } from './with-redux';

type Flight = { id: number };

describe('with redux', () => {
it('should load flights', () => {
const FlightsStore = signalStore(
withRedux({
actions: createActions({
actions: {
public: {
loadFlights: payload<{ from: string; to: string }>(),
delayFirst: noPayload,
},
private: {
flightsLoaded: payload<{ flights: Flight[] }>(),
},
}),
},

reducer: (on, actions) => {
on(actions.loadFlights, (action, state) => state);
on(actions.flightsLoaded, (action, state) => state);
},

effects: (actions, create) => {
const httpCLient = inject(HttpClient);
const httpClient = inject(HttpClient);

create(actions.loadFlights).pipe(
switchMap(({ from, to }) =>
httpCLient.get<Flight[]>('www.angulararchitects.io', {
httpClient.get<Flight[]>('www.angulararchitects.io', {
params: { from, to },
})
),
Expand Down
41 changes: 18 additions & 23 deletions libs/ngrx-toolkit/src/lib/with-redux.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { map, Observable, of, pipe, switchMap, tap } from 'rxjs';
import { rxMethod } from '@ngrx/signals/rxjs-interop';
import { HttpClient } from '@angular/common/http';
import { inject } from '@angular/core';
import { signalStore, SignalStoreFeature } from '@ngrx/signals';
import { Observable } from 'rxjs';
import { SignalStoreFeature } from '@ngrx/signals';
import {
EmptyFeatureResult,
SignalStoreFeatureResult,
Expand All @@ -23,15 +20,17 @@ type ActionsCreator<Spec extends ActionsSpec> = Extract<
'private' | 'public'
> extends never
? {
[ActionName in keyof Spec]: Spec[ActionName] & { type: ActionName };
[ActionName in keyof Spec]: Spec[ActionName] & {
type: ActionName & string;
};
}
: {
[ActionName in keyof Spec['private']]: Spec['private'][ActionName] & {
type: ActionName;
type: ActionName & string;
};
} & {
[ActionName in keyof Spec['public']]: Spec['public'][ActionName] & {
type: ActionName;
type: ActionName & string;
};
};

Expand All @@ -40,11 +39,13 @@ type PublicActions<Spec extends ActionsSpec> = Extract<
'private' | 'public'
> extends never
? {
[ActionName in keyof Spec]: Spec[ActionName] & { type: ActionName };
[ActionName in keyof Spec]: Spec[ActionName] & {
type: ActionName & string;
};
}
: {
[ActionName in keyof Spec['public']]: Spec['public'][ActionName] & {
type: ActionName;
type: ActionName & string;
};
};

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

type ActionsFactory<StateActions extends Actions> = () => StateActions;

type ReducerFn<A extends Action = Action> = (
action: A,
reducerFn: (action: A, state: State) => State
) => void;

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

type EffectFn<A extends Action = Action> = {
(action: A, effect: (action: Observable<A>) => Observable<Action>): void;
};

type EffectsFactory<StateActions extends Actions> = (
actions: StateActions,
forAction: <EffectAction extends Action>(
Expand All @@ -90,14 +85,14 @@ type EffectsFactory<StateActions extends Actions> = (
*/
export declare function withRedux<
Spec extends ActionsSpec,
StateActions extends Actions,
Input extends SignalStoreFeatureResult
Input extends SignalStoreFeatureResult,
StoreActions extends ActionsCreator<Spec> = ActionsCreator<Spec>,
PublicStoreActions extends PublicActions<Spec> = PublicActions<Spec>
>(redux: {
actions: StateActions;
// actions: (actionsCreator: (spec: ActionsSpec) => StateActions) => void;
reducer: ReducerFactory<StateActions>;
effects: EffectsFactory<StateActions>;
actions: Spec;
reducer: ReducerFactory<StoreActions>;
effects: EffectsFactory<StoreActions>;
}): SignalStoreFeature<
Input,
EmptyFeatureResult & { methods: { actions: () => StateActions } }
EmptyFeatureResult & { methods: { actions: () => PublicStoreActions } }
>;

0 comments on commit 284da58

Please sign in to comment.