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): export EffectReturnFn interface #2555

Merged
merged 1 commit into from
Jun 3, 2020
Merged
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
23 changes: 11 additions & 12 deletions modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { debounceSync } from './debounceSync';
* Return type of the effect, that behaves differently based on whether the
* argument is passed to the callback.
*/
interface EffectReturnFn<T> {
export interface EffectReturnFn<T> {
(): void;
(t: T | Observable<T>): Subscription;
}
Expand All @@ -36,7 +36,7 @@ export class ComponentStore<T extends object> {
private readonly stateSubject$ = new ReplaySubject<T>(1);
private isInitialized = false;
// Needs to be after destroy$ is declared because it's used in select.
readonly state$: Observable<T> = this.select(s => s);
readonly state$: Observable<T> = this.select((s) => s);

constructor(defaultState?: T) {
// State can be initialized either through constructor, or initState or
Expand Down Expand Up @@ -80,22 +80,21 @@ export class ComponentStore<T extends object> {
: of(observableOrValue);
const subscription = observable$
.pipe(
concatMap(
value =>
this.isInitialized
? of(value).pipe(withLatestFrom(this.stateSubject$))
: // If state was not initialized, we'll throw an error.
throwError(
Error(`${this.constructor.name} has not been initialized`)
)
concatMap((value) =>
this.isInitialized
? of(value).pipe(withLatestFrom(this.stateSubject$))
: // If state was not initialized, we'll throw an error.
throwError(
Error(`${this.constructor.name} has not been initialized`)
)
),
takeUntil(this.destroy$)
)
.subscribe({
next: ([value, currentState]) => {
this.stateSubject$.next(updaterFn(currentState, value!));
},
error: error => {
error: (error) => {
initializationError = error;
this.stateSubject$.error(error);
},
Expand Down Expand Up @@ -211,7 +210,7 @@ export class ComponentStore<T extends object> {
const observable$ = isObservable(observableOrValue)
? observableOrValue
: of(observableOrValue);
return observable$.pipe(takeUntil(this.destroy$)).subscribe(value => {
return observable$.pipe(takeUntil(this.destroy$)).subscribe((value) => {
// any new 👇 value is pushed into a stream
origin$.next(value);
});
Expand Down