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

fix(component-store): move isInitialized check to queueScheduler context on state update #3492

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
15 changes: 14 additions & 1 deletion modules/component-store/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('Component Store', () => {
);

it(
'does not throws an Error when updater is called with async Observable' +
'does not throw an Error when updater is called with async Observable' +
' before initialization, that emits the value after initialization',
marbles((m) => {
const componentStore = new ComponentStore();
Expand Down Expand Up @@ -236,6 +236,19 @@ describe('Component Store', () => {
);
})
);

it(
'does not throw an Error when ComponentStore initialization and' +
' state update are scheduled via queueScheduler',
() => {
expect(() => {
queueScheduler.schedule(() => {
const componentStore = new ComponentStore({ foo: false });
componentStore.patchState({ foo: true });
});
}).not.toThrow();
Comment on lines +244 to +249
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test would fail before this PR

}
);
});

describe('updates the state', () => {
Expand Down
32 changes: 16 additions & 16 deletions modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
EMPTY,
} from 'rxjs';
import {
concatMap,
takeUntil,
withLatestFrom,
map,
Expand All @@ -22,6 +21,7 @@ import {
take,
tap,
catchError,
observeOn,
} from 'rxjs/operators';
import { debounceSync } from './debounce-sync';
import {
Expand Down Expand Up @@ -61,9 +61,6 @@ export class ComponentStore<T extends object> implements OnDestroy {

private readonly stateSubject$ = new ReplaySubject<T>(1);
private isInitialized = false;
private notInitializedErrorMessage =
`${this.constructor.name} has not been initialized yet. ` +
`Please make sure it is initialized before updating/getting.`;
// Needs to be after destroy$ is declared because it's used in select.
readonly state$: Observable<T> = this.select((s) => s);
private ɵhasProvider = false;
Expand Down Expand Up @@ -125,15 +122,11 @@ export class ComponentStore<T extends object> implements OnDestroy {
: of(observableOrValue);
const subscription = observable$
.pipe(
concatMap((value) =>
this.isInitialized
? // Push the value into queueScheduler
scheduled([value], queueScheduler).pipe(
withLatestFrom(this.stateSubject$)
)
: // If state was not initialized, we'll throw an error.
throwError(() => new Error(this.notInitializedErrorMessage))
),
// Push the value into queueScheduler
observeOn(queueScheduler),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was very confident that observeOn was deprecated in favour of scheduled. Apparently I was dreaming?

Copy link
Member Author

@markostanimirovic markostanimirovic Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁 AFAIK, the signature of the of function that accepts scheduler is deprecated in favor of scheduled.

// If the state is not initialized yet, we'll throw an error.
tap(() => this.assertStateIsInitialized()),
withLatestFrom(this.stateSubject$),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
map(([value, currentState]) => updaterFn(currentState, value!)),
tap((newState) => this.stateSubject$.next(newState)),
Expand Down Expand Up @@ -209,9 +202,7 @@ export class ComponentStore<T extends object> implements OnDestroy {
protected get(): T;
protected get<R>(projector: (s: T) => R): R;
protected get<R>(projector?: (s: T) => R): R | T {
if (!this.isInitialized) {
throw new Error(this.notInitializedErrorMessage);
}
this.assertStateIsInitialized();
let value: R | T;

this.stateSubject$.pipe(take(1)).subscribe((state) => {
Expand Down Expand Up @@ -353,6 +344,15 @@ export class ComponentStore<T extends object> implements OnDestroy {
}
});
}

private assertStateIsInitialized(): void {
if (!this.isInitialized) {
throw new Error(
`${this.constructor.name} has not been initialized yet. ` +
`Please make sure it is initialized before updating/getting.`
);
}
}
}

function processSelectorArgs<
Expand Down