Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion modules/signals/spec/with-feature.spec.ts
Original file line number Diff line number Diff line change
@@ -117,7 +117,29 @@ describe('withFeature', () => {
expect(store.createUrl('docs')).toBe('https://www.ngrx.io/docs');
});

it('can be cominbed with inputs', () => {
it('provides writable state source', () => {
const withCallback = (cb: () => void) =>
signalStoreFeature(
withMethods(() => ({
executeCallBack: () => cb(),
}))
);

const Store = signalStore(
{ providedIn: 'root' },
withState({ counter: 1 }),
withFeature((store) =>
withCallback(() => patchState(store, { counter: 2 }))
)
);

const store = TestBed.inject(Store);
expect(getState(store)).toEqual({ counter: 1 });
store.executeCallBack();
expect(getState(store)).toEqual({ counter: 2 });
});

it('can be combined with inputs', () => {
function withLoadEntities<Entity extends { id: number }, Filter>(config: {
filter: Signal<Filter>;
loader: (filter: Filter) => Observable<Entity[]>;
7 changes: 6 additions & 1 deletion modules/signals/src/with-feature.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import {
SignalStoreFeatureResult,
StateSignals,
} from './signal-store-models';
import { STATE_SOURCE, WritableStateSource } from './state-source';
import { Prettify } from './ts-helpers';

/**
@@ -33,12 +34,16 @@ export function withFeature<
>(
featureFactory: (
store: Prettify<
StateSignals<Input['state']> & Input['props'] & Input['methods']
StateSignals<Input['state']> &
Input['props'] &
Input['methods'] &
WritableStateSource<Prettify<Input['state']>>
>
) => SignalStoreFeature<Input, Output>
): SignalStoreFeature<Input, Output> {
return (store) => {
const storeForFactory = {
[STATE_SOURCE]: store[STATE_SOURCE],
...store['stateSignals'],
...store['props'],
...store['methods'],