Skip to content

Commit ad6e14a

Browse files
fix(store-devtools): add state signal to StateObservable (#3889)
1 parent b4431f7 commit ad6e14a

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

modules/store-devtools/spec/store.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,4 +885,17 @@ describe('Store Devtools', () => {
885885
expect(oldComputedStates).not.toBe(liftedState.computedStates);
886886
});
887887
});
888+
889+
describe('StateObservable', () => {
890+
it('should contain state signal that is updated on state changes', () => {
891+
const { state, store } = createStore(counter);
892+
expect(state.state()).toEqual({ state: 0 });
893+
894+
store.dispatch({ type: 'INCREMENT' });
895+
expect(store.state()).toEqual({ state: 1 });
896+
897+
store.dispatch({ type: 'DECREMENT' });
898+
expect(store.state()).toEqual({ state: 0 });
899+
});
900+
});
888901
});

modules/store-devtools/src/devtools.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Injectable, Inject, ErrorHandler } from '@angular/core';
2+
import { toSignal } from '@angular/core/rxjs-interop';
23
import {
34
Action,
45
ActionReducer,
56
ActionsSubject,
67
INITIAL_STATE,
78
ReducerObservable,
89
ScannedActionsSubject,
10+
StateObservable,
911
} from '@ngrx/store';
1012
import {
1113
merge,
@@ -36,7 +38,7 @@ export class StoreDevtools implements Observer<any> {
3638
private extensionStartSubscription: Subscription;
3739
public dispatcher: ActionsSubject;
3840
public liftedState: Observable<LiftedState>;
39-
public state: Observable<any>;
41+
public state: StateObservable;
4042

4143
constructor(
4244
dispatcher: DevtoolsDispatcher,
@@ -114,7 +116,10 @@ export class StoreDevtools implements Observer<any> {
114116

115117
const liftedState$ =
116118
liftedStateSubject.asObservable() as Observable<LiftedState>;
117-
const state$ = liftedState$.pipe(map(unliftState));
119+
const state$ = liftedState$.pipe(map(unliftState)) as StateObservable;
120+
Object.defineProperty(state$, 'state', {
121+
value: toSignal(state$, { manualCleanup: true, requireSync: true }),
122+
});
118123

119124
this.extensionStartSubscription = extensionStartSubscription;
120125
this.stateSubscription = liftedStateSubscription;

modules/store-devtools/src/instrument.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ModuleWithProviders, NgModule } from '@angular/core';
2-
import { Observable } from 'rxjs';
2+
import { StateObservable } from '@ngrx/store';
33
import { StoreDevtoolsOptions } from './config';
44
import { StoreDevtools } from './devtools';
55
import { provideStoreDevtools } from './provide-store-devtools';
66

77
export function createStateObservable(
88
devtools: StoreDevtools
9-
): Observable<any> {
9+
): StateObservable {
1010
return devtools.state;
1111
}
1212

0 commit comments

Comments
 (0)