Skip to content

Commit 3aa64ae

Browse files
feat(signals): rename withEffects to withEventHandlers (#5009)
BREAKING CHANGES: The withEffects feature from @ngrx/signals/events plugin is renamed to withEventHandlers. BEFORE: import { withEffects } from '@ngrx/signals/events'; export const CounterStore = signalStore( withState({ count: 0 }), withEffects((store, events = inject(Events)) => ({ logCount$: events.on(increment).pipe( tap(() => console.log(store.count())) ), })) ); AFTER: import { withEventHandlers } from '@ngrx/signals/events'; export const CounterStore = signalStore( withState({ count: 0 }), withEventHandlers((store, events = inject(Events)) => ({ logCount$: events.on(increment).pipe( tap(() => console.log(store.count())) ), })) );
1 parent 8994d92 commit 3aa64ae

File tree

7 files changed

+107
-74
lines changed

7 files changed

+107
-74
lines changed

modules/signals/events/spec/integration.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
Events,
3333
injectDispatch,
3434
on,
35-
withEffects,
35+
withEventHandlers,
3636
withReducer,
3737
} from '../src';
3838

@@ -92,7 +92,7 @@ describe('Integration Tests', () => {
9292
return { requestStatus: { error } };
9393
}
9494

95-
describe('withReducer and withEffects', () => {
95+
describe('withReducer and withEventHandlers', () => {
9696
const booksPageEvents = eventGroup({
9797
source: 'Books Page',
9898
events: {
@@ -113,7 +113,7 @@ describe('Integration Tests', () => {
113113
]),
114114
on(booksApiEvents.loadedFailure, ({ payload }) => setError(payload))
115115
),
116-
withEffects(
116+
withEventHandlers(
117117
(_, events = inject(Events), booksService = inject(BooksService)) => ({
118118
loadUsers$: events
119119
.on(booksPageEvents.opened, booksPageEvents.refreshed)
@@ -242,15 +242,15 @@ describe('Integration Tests', () => {
242242
const Store = signalStore(
243243
{ providedIn: 'root' },
244244
withState({ savedEvents: [] as string[] }),
245-
withEffects((_, events = inject(Events)) => ({
245+
withEventHandlers((_, events = inject(Events)) => ({
246246
emitSecond$: events.on(first).pipe(
247247
tap(({ type }) =>
248-
handledEventsLog.push(`emitSecond$ effect: ${type}`)
248+
handledEventsLog.push(`emitSecond$ handler: ${type}`)
249249
),
250250
map(() => second())
251251
),
252252
save$: events.on(first, second).pipe(
253-
tap(({ type }) => handledEventsLog.push(`save$ effect: ${type}`)),
253+
tap(({ type }) => handledEventsLog.push(`save$ handler: ${type}`)),
254254
map(({ type }) => save(type))
255255
),
256256
})),
@@ -272,17 +272,17 @@ describe('Integration Tests', () => {
272272
expect(store.savedEvents()).toEqual(['first', 'second']);
273273
expect(handledEventsLog).toEqual([
274274
'reducer: first',
275-
'emitSecond$ effect: first',
275+
'emitSecond$ handler: first',
276276
'reducer: second',
277-
'save$ effect: first',
277+
'save$ handler: first',
278278
'reducer: savefirst',
279-
'save$ effect: second',
279+
'save$ handler: second',
280280
'reducer: savesecond',
281281
]);
282282
});
283283
});
284284

285-
describe('custom withReducer and withEffects', () => {
285+
describe('custom withReducer and withEventHandlers', () => {
286286
const booksPageEvents = eventGroup({
287287
source: 'Books Page',
288288
events: {
@@ -312,10 +312,10 @@ describe('Integration Tests', () => {
312312
);
313313
}
314314

315-
function withBooksEffects() {
315+
function withBooksEventHandlers() {
316316
return signalStoreFeature(
317317
{ state: type<QueryState>() },
318-
withEffects(
318+
withEventHandlers(
319319
(
320320
{ query },
321321
events = inject(Events),
@@ -348,7 +348,7 @@ describe('Integration Tests', () => {
348348
withEntities<Book>(),
349349
withRequestStatus(),
350350
withBooksReducer(),
351-
withBooksEffects()
351+
withBooksEventHandlers()
352352
);
353353

354354
it('loads entities when queryChanged and refreshed events are dispatched', fakeAsync(() => {

modules/signals/events/spec/with-effects.spec.ts renamed to modules/signals/events/spec/with-event-handlers.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import {
1717
Events,
1818
mapToScope,
1919
toScope,
20-
withEffects,
20+
withEventHandlers,
2121
} from '../src';
2222
import { createLocalService } from '../../spec/helpers';
2323

24-
describe('withEffects', () => {
24+
describe('withEventHandlers', () => {
2525
const event1 = event('event1');
2626
const event2 = event('event2');
2727
const event3 = event('event3', type<string>());
@@ -42,7 +42,7 @@ describe('withEffects', () => {
4242
return 'n';
4343
},
4444
})),
45-
withEffects(({ k, l, m, n, ...store }) => {
45+
withEventHandlers(({ k, l, m, n, ...store }) => {
4646
values.push(k(), l(), m, n(), getState(store).k);
4747
return {};
4848
})
@@ -53,10 +53,10 @@ describe('withEffects', () => {
5353
expect(values).toEqual(['k', 'k l', 'm', 'n', 'k']);
5454
});
5555

56-
it('dispatches events returned by effects', () => {
56+
it('dispatches events returned by event handlers', () => {
5757
const Store = signalStore(
5858
{ providedIn: 'root' },
59-
withEffects((_, events = inject(Events)) => ({
59+
withEventHandlers((_, events = inject(Events)) => ({
6060
$: events.on(event1, event2).pipe(map(({ type }) => event3(type))),
6161
$$: events
6262
.on(event3)
@@ -84,10 +84,10 @@ describe('withEffects', () => {
8484
]);
8585
});
8686

87-
it('does not re-dispatch the same event when an effect does not return a new event', () => {
87+
it('does not re-dispatch the same event when an event handler does not return a new event', () => {
8888
const Store = signalStore(
8989
{ providedIn: 'root' },
90-
withEffects((_, events = inject(Events)) => ({
90+
withEventHandlers((_, events = inject(Events)) => ({
9191
$: events.on(event1).pipe(tap(() => {})),
9292
$$: events.on(event2).pipe(tap(() => {})),
9393
}))
@@ -106,12 +106,12 @@ describe('withEffects', () => {
106106
expect(emittedEvents).toEqual([{ type: 'event1' }, { type: 'event2' }]);
107107
});
108108

109-
it('re-dispatches the same event when it is explicitly returned from an effect', () => {
109+
it('re-dispatches the same event when it is explicitly returned from an event handler', () => {
110110
let executionCount = 0;
111111

112112
const Store = signalStore(
113113
{ providedIn: 'root' },
114-
withEffects((_, events = inject(Events)) => ({
114+
withEventHandlers((_, events = inject(Events)) => ({
115115
$: events.on(event1).pipe(
116116
map(() => (executionCount < 2 ? event1() : null)),
117117
tap(() => executionCount++)
@@ -138,7 +138,7 @@ describe('withEffects', () => {
138138
it('handles observables returned as an array', () => {
139139
const Store = signalStore(
140140
{ providedIn: 'root' },
141-
withEffects((_, events = inject(Events)) => [
141+
withEventHandlers((_, events = inject(Events)) => [
142142
events.on(event1, event2).pipe(map(({ type }) => event3(type))),
143143
events.on(event3).pipe(tap(() => {})),
144144
])
@@ -165,7 +165,7 @@ describe('withEffects', () => {
165165
it('dispatches an event with provided scope via toScope', () => {
166166
const Store = signalStore(
167167
{ providedIn: 'root' },
168-
withEffects((_, events = inject(Events)) => ({
168+
withEventHandlers((_, events = inject(Events)) => ({
169169
$: events.on(event1).pipe(map(() => [event2(), toScope('parent')])),
170170
}))
171171
);
@@ -184,7 +184,7 @@ describe('withEffects', () => {
184184
it('dispatches an event with provided scope via mapToScope', () => {
185185
const Store = signalStore(
186186
{ providedIn: 'root' },
187-
withEffects((_, events = inject(Events)) => ({
187+
withEventHandlers((_, events = inject(Events)) => ({
188188
$: events.on(event1).pipe(
189189
map(() => event3('ngrx')),
190190
mapToScope('global')
@@ -203,11 +203,11 @@ describe('withEffects', () => {
203203
});
204204
});
205205

206-
it('unsubscribes from effects when the store is destroyed', () => {
206+
it('unsubscribes from event handlers when the store is destroyed', () => {
207207
let executionCount = 0;
208208

209209
const Store = signalStore(
210-
withEffects((_, events = inject(Events)) => ({
210+
withEventHandlers((_, events = inject(Events)) => ({
211211
$: events.on(event1).pipe(tap(() => executionCount++)),
212212
}))
213213
);

modules/signals/events/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export {
1111
} from './event-scope';
1212
export { Events, ReducerEvents } from './events-service';
1313
export { injectDispatch } from './inject-dispatch';
14-
export { withEffects } from './with-effects';
14+
export { withEventHandlers } from './with-event-handlers';
1515
export { withReducer } from './with-reducer';

modules/signals/events/src/with-effects.ts renamed to modules/signals/events/src/with-event-handlers.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@ import { SOURCE_TYPE } from './events-service';
2020
* @experimental
2121
* @description
2222
*
23-
* SignalStore feature for defining side effects.
23+
* SignalStore feature for defining event handlers.
2424
*
2525
* @usageNotes
2626
*
2727
* ```ts
2828
* import { signalStore, withState } from '@ngrx/signals';
29-
* import { event, Events, withEffects } from '@ngrx/signals/events';
29+
* import { event, Events, withEventHandlers } from '@ngrx/signals/events';
3030
*
3131
* const increment = event('[Counter Page] Increment');
3232
* const decrement = event('[Counter Page] Decrement');
3333
*
3434
* const CounterStore = signalStore(
3535
* withState({ count: 0 }),
36-
* withEffects(({ count }, events = inject(Events)) => ({
36+
* withEventHandlers(({ count }, events = inject(Events)) => ({
3737
* logCount$: events.on(increment, decrement).pipe(
3838
* tap(({ type }) => console.log(type, count())),
3939
* ),
4040
* })),
4141
* );
4242
* ```
4343
*/
44-
export function withEffects<Input extends SignalStoreFeatureResult>(
45-
effectsFactory: (
44+
export function withEventHandlers<Input extends SignalStoreFeatureResult>(
45+
handlersFactory: (
4646
store: Prettify<
4747
StateSignals<Input['state']> &
4848
Input['props'] &
@@ -55,9 +55,9 @@ export function withEffects<Input extends SignalStoreFeatureResult>(
5555
type<Input>(),
5656
withHooks({
5757
onInit(store, dispatcher = inject(Dispatcher)) {
58-
const effectSources = effectsFactory(store);
59-
const effects = Object.values(effectSources).map((effectSource$) =>
60-
effectSource$.pipe(
58+
const handlerSources = handlersFactory(store);
59+
const handlers = Object.values(handlerSources).map((handlerSource$) =>
60+
handlerSource$.pipe(
6161
tap((result) => {
6262
const [potentialEvent, config] = Array.isArray(result)
6363
? result
@@ -73,7 +73,7 @@ export function withEffects<Input extends SignalStoreFeatureResult>(
7373
)
7474
);
7575

76-
merge(...effects)
76+
merge(...handlers)
7777
.pipe(takeUntilDestroyed())
7878
.subscribe();
7979
},
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import { inject, untracked } from '@angular/core';
2-
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
3-
import { merge, tap } from 'rxjs';
2+
import { tap } from 'rxjs';
43
import {
54
EmptyFeatureResult,
65
getState,
76
patchState,
87
SignalStoreFeature,
98
signalStoreFeature,
109
type,
11-
withHooks,
1210
} from '@ngrx/signals';
1311
import { CaseReducerResult } from './case-reducer';
1412
import { EventCreator } from './event-creator';
1513
import { ReducerEvents } from './events-service';
14+
import { withEventHandlers } from './with-event-handlers';
1615

1716
/**
1817
* @experimental
1918
* @description
2019
*
21-
* SignalStore feature for defining state changes based on dispatched events.
20+
* SignalStore feature for defining state transitions based on dispatched events.
2221
*
2322
* @usageNotes
2423
*
@@ -44,24 +43,18 @@ export function withReducer<State extends object>(
4443
> {
4544
return signalStoreFeature(
4645
{ state: type<State>() },
47-
withHooks({
48-
onInit(store, events = inject(ReducerEvents)) {
49-
const updates = caseReducers.map((caseReducer) =>
50-
events.on(...caseReducer.events).pipe(
51-
tap((event) => {
52-
const state = untracked(() => getState(store));
53-
const result = caseReducer.reducer(event, state);
54-
const updaters = Array.isArray(result) ? result : [result];
46+
withEventHandlers((store, events = inject(ReducerEvents)) =>
47+
caseReducers.map((caseReducer) =>
48+
events.on(...caseReducer.events).pipe(
49+
tap((event) => {
50+
const state = untracked(() => getState(store));
51+
const result = caseReducer.reducer(event, state);
52+
const updaters = Array.isArray(result) ? result : [result];
5553

56-
patchState(store, ...updaters);
57-
})
58-
)
59-
);
60-
61-
merge(...updates)
62-
.pipe(takeUntilDestroyed())
63-
.subscribe();
64-
},
65-
})
54+
patchState(store, ...updaters);
55+
})
56+
)
57+
)
58+
)
6659
);
6760
}
79.6 KB
Loading

0 commit comments

Comments
 (0)