Skip to content

Commit 00ba294

Browse files
committed
fix: dispatches onInit now get caught by selector
1 parent 0fbe8f3 commit 00ba294

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

Diff for: projects/angular-redux/src/lib/inject-selector.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EqualityFn } from './types';
22
import {
33
assertInInjectionContext,
4+
DestroyRef,
45
effect,
56
inject,
67
Signal,
@@ -80,6 +81,7 @@ export function createSelectorInjection(): InjectSelector {
8081
): Signal<Selected> => {
8182
assertInInjectionContext(injectSelector);
8283
const reduxContext = inject(ReduxProvider);
84+
const destroyRef = inject(DestroyRef);
8385

8486
const { equalityFn = refEquality } =
8587
typeof equalityFnOrOptions === 'function'
@@ -90,19 +92,17 @@ export function createSelectorInjection(): InjectSelector {
9092

9193
const selectedState = signal(selector(store.getState()));
9294

93-
effect((onCleanup) => {
94-
const unsubscribe = subscription.addNestedSub(() => {
95-
const data = selector(store.getState());
96-
if (equalityFn(selectedState(), data)) {
97-
return;
98-
}
95+
const unsubscribe = subscription.addNestedSub(() => {
96+
const data = selector(store.getState());
97+
if (equalityFn(selectedState(), data)) {
98+
return;
99+
}
99100

100-
selectedState.set(data);
101-
});
101+
selectedState.set(data);
102+
});
102103

103-
onCleanup(() => {
104-
unsubscribe();
105-
});
104+
destroyRef.onDestroy(() => {
105+
unsubscribe();
106106
});
107107

108108
return selectedState;

Diff for: projects/angular-redux/src/tests/inject-selector-and-dispatch.spec.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Component } from '@angular/core';
2-
import { render } from '@testing-library/angular';
2+
import { render, waitFor } from '@testing-library/angular';
33
import '@testing-library/jest-dom';
44
import { configureStore, createSlice } from '@reduxjs/toolkit';
55
import { provideRedux, injectDispatch, injectSelector } from '../public-api';
66
import { userEvent } from '@testing-library/user-event';
7+
import { createStore } from 'redux';
78

89
const user = userEvent.setup();
910

@@ -70,3 +71,32 @@ it('injectSelector should work with reactivity', async () => {
7071

7172
expect(getByText('Count: 1')).toBeInTheDocument();
7273
});
74+
75+
it('should show a value dispatched during ngOnInit', async () => {
76+
const store = configureStore({
77+
reducer: {
78+
counter: counterSlice.reducer,
79+
},
80+
});
81+
82+
@Component({
83+
selector: 'app-root',
84+
standalone: true,
85+
template: '<p>Count: {{count()}}</p>',
86+
})
87+
class Comp {
88+
count = injectSelector((state: any) => state.counter.value);
89+
dispatch = injectDispatch();
90+
increment = counterSlice.actions.increment;
91+
92+
ngOnInit() {
93+
this.dispatch(this.increment());
94+
}
95+
}
96+
97+
const { getByText } = await render(Comp, {
98+
providers: [provideRedux({ store })],
99+
});
100+
101+
await waitFor(() => expect(getByText('Count: 1')).toBeInTheDocument());
102+
});

0 commit comments

Comments
 (0)