Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(signal-store): imperative state read returns raw state #215

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 6 additions & 11 deletions libs/signal-store/src/lib/component-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DestroyRef, inject, Signal, signal, WritableSignal } from '@angular/core';
import { DestroyRef, inject, signal, WritableSignal } from '@angular/core';
import {
Action,
calculateExtensions,
Expand Down Expand Up @@ -34,14 +34,9 @@ export class ComponentStore<StateType extends object> implements ComponentStoreL
private actionsOnQueue = createActionsOnQueue();

private _state: WritableSignal<StateType> = signal(this.initialState);
private selectableState = createSelectableSignalState(this._state);

/**
* @deprecated
* Use the `select` method without arguments to return a state Signal
* the `state` property will be replaced with a getter function which returns the raw state (like in the original MiniRx Store)
*/
state: Signal<StateType> = this.selectableState.select();
get state(): StateType {
return this._state();
}

private updateState: UpdateStateCallback<StateType> = (
stateOrCallback: StateOrCallback<StateType>,
Expand Down Expand Up @@ -69,7 +64,7 @@ export class ComponentStore<StateType extends object> implements ComponentStoreL

const subSink = createSignalStoreSubSink();
subSink.sink = this.actionsOnQueue.actions$.subscribe((action) => {
const newState: StateType = reducer(this.state(), action);
const newState: StateType = reducer(this.state, action);
this._state.set(newState);
});

Expand All @@ -87,7 +82,7 @@ export class ComponentStore<StateType extends object> implements ComponentStoreL
setState = createUpdateFn(this.updateState);
connect = createConnectFn(this.updateState);
rxEffect = createRxEffectFn();
select = this.selectableState.select;
select = createSelectableSignalState(this._state).select;

private destroy(): void {
// Dispatch an action really just for logging via LoggerExtension
Expand Down
12 changes: 5 additions & 7 deletions libs/signal-store/src/lib/feature-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ export class FeatureStore<StateType extends object> implements ComponentStoreLik
return this._featureKey;
}

/**
* @deprecated
* Use the `select` method without arguments to return a state Signal
* the `state` property will be replaced with a getter function which returns the raw state (like in the original MiniRx Store)
*/
state: Signal<StateType> = select((state) => state[this.featureKey]);
private _state: Signal<StateType> = select((state) => state[this.featureKey]);
get state(): StateType {
return this._state();
}

private updateState: UpdateStateCallback<StateType> = (
stateOrCallback: StateOrCallback<StateType>,
Expand Down Expand Up @@ -66,7 +64,7 @@ export class FeatureStore<StateType extends object> implements ComponentStoreLik
setState = createUpdateFn(this.updateState);
connect = createConnectFn(this.updateState);
rxEffect = createRxEffectFn();
select = createSelectableSignalState(this.state).select;
select = createSelectableSignalState(this._state).select;

private destroy(): void {
removeFeature(this._featureKey);
Expand Down
2 changes: 1 addition & 1 deletion libs/signal-store/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Observable } from 'rxjs';
import { Action, StateOrCallback } from '@mini-rx/common';

export interface ComponentStoreLike<StateType> {
state: Signal<StateType>;
get state(): StateType;
spierala marked this conversation as resolved.
Show resolved Hide resolved
setState(stateOrCallback: StateOrCallback<StateType>, name?: string): void;
connect(dict: Record<string, Observable<any> | Signal<any>>): void;
rxEffect(effectFn: (origin$: Observable<any>) => Observable<any>): () => void;
Expand Down
10 changes: 10 additions & 0 deletions libs/signal-store/src/lib/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,14 @@ describe('ComponentStore', () => {

expect(selectedState()).toBe(1);
});

it('should read state imperatively', () => {
const cs = setup({ counter: 0 });

expect(cs.state).toEqual({ counter: 0 });

cs.setState((state) => ({ counter: state.counter + 1 }));

expect(cs.state).toEqual({ counter: 1 });
});
});
12 changes: 11 additions & 1 deletion libs/signal-store/src/lib/spec/feature-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const getSomeFeatureSelector = createFeatureStateSelector<CounterState>('someFea
class UserFeatureStore extends FeatureStore<UserState> {
private injector = inject(EnvironmentInjector);

state$ = toObservable(this.state);
state$ = toObservable(this.select());
firstName = this.select((state) => state.firstName);
firstName$ = toObservable(this.firstName, { injector: this.injector });
lastName = this.select((state) => state.lastName);
Expand Down Expand Up @@ -562,4 +562,14 @@ describe('FeatureStore', () => {

expect(selectedState()).toBe(0);
});

it('should read state imperatively', () => {
setupCounterFeatureStore();

expect(counterFeatureStore.state).toEqual({ counter: 0 });

counterFeatureStore.increment();

expect(counterFeatureStore.state).toEqual({ counter: 1 });
});
});
Loading