-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import { | |
EMPTY, | ||
} from 'rxjs'; | ||
import { | ||
concatMap, | ||
takeUntil, | ||
withLatestFrom, | ||
map, | ||
|
@@ -22,6 +21,7 @@ import { | |
take, | ||
tap, | ||
catchError, | ||
observeOn, | ||
} from 'rxjs/operators'; | ||
import { debounceSync } from './debounce-sync'; | ||
import { | ||
|
@@ -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; | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was very confident that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😁 AFAIK, the signature of the |
||
// 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)), | ||
|
@@ -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) => { | ||
|
@@ -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< | ||
|
There was a problem hiding this comment.
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