Skip to content

Commit a16c713

Browse files
authored
Merge pull request #408 from studiohyperdrive/feat/store/dispatch-data-to-store-void-return
feat(store): change dispatchDataToStore return type to void
2 parents 1dfdf2b + b8e646f commit a16c713

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

apps/docs/src/app/pages/docs/angular/store/implementation/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ A short example can be found here
106106
});
107107
```
108108

109-
### dispatchDataToSTore
109+
### dispatchDataToStore
110110

111111
An additional util that works in tandem with the aforementioned store assets is the `dispatchDataToStore` util. Using the assets, the util will automatically handle the loading and error state of the provided data.
112112

apps/store-test/src/services/courses.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { Store } from '@ngrx/store';
33
import { of } from 'rxjs';
4+
import { switchMap } from 'rxjs/operators';
45
import { CoursesStore, actions, selectors } from '../store/courses.store';
56
import { StoreService, dispatchDataToStore } from '@ngx/store';
67

@@ -15,6 +16,8 @@ export class CoursesService extends StoreService<CoursesStore> {
1516
}
1617

1718
dispatchCourses() {
18-
return dispatchDataToStore(actions.courses, of(['hello', 'world']), this.store);
19+
return dispatchDataToStore(actions.courses, of(['hello', 'world']), this.store).pipe(
20+
switchMap(() => this.state.courses$)
21+
);
1922
}
2023
}

libs/angular/store/src/lib/store/spec/store-service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { Store } from '@ngrx/store';
33
import { Observable } from 'rxjs';
4+
import { switchMap } from 'rxjs/operators';
45

56
import { HttpClient } from '@angular/common/http';
67
import { StoreService } from '../abstracts';
@@ -49,7 +50,7 @@ export class SpecStoreService extends StoreService {
4950
actions.channel,
5051
this.httpClient.get<DataType>('test'),
5152
this.store
52-
);
53+
).pipe(switchMap(() => this.channel$));
5354
}
5455

5556
public clearChannel(): void {
@@ -62,7 +63,7 @@ export class SpecStoreService extends StoreService {
6263
this.httpClient.get<DataType[]>('test'),
6364
this.store,
6465
actionType
65-
);
66+
).pipe(switchMap(() => this.videos$));
6667
}
6768

6869
public clearVideos(): void {

libs/angular/store/src/lib/store/spec/store-state.service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { Store } from '@ngrx/store';
3-
import { Observable, of, throwError } from 'rxjs';
3+
import { Observable, of, throwError, switchMap } from 'rxjs';
44
import { BaseStoreAssets, EntityStoreAssets, StoreFlowAssets } from '../interfaces';
55
import {
66
createBaseStoreAssets,
@@ -38,14 +38,18 @@ export class StoreStateService extends StoreService<StoreState> {
3838
actions.data,
3939
throwError(() => new Error('This is an error')),
4040
this.store
41-
);
41+
).pipe(switchMap(() => throwError(() => new Error('This is an error'))));
4242
}
4343

4444
setData(payload: string[]): Observable<string[]> {
45-
return dispatchDataToStore(actions.data, of(payload), this.store);
45+
return dispatchDataToStore(actions.data, of(payload), this.store).pipe(
46+
switchMap(() => this.state.data$)
47+
);
4648
}
4749

4850
setCompleted(payload: boolean): Observable<boolean> {
49-
return dispatchDataToStore(actions.isCompleted, of(payload), this.store);
51+
return dispatchDataToStore(actions.isCompleted, of(payload), this.store).pipe(
52+
switchMap(() => this.state.isCompleted$)
53+
);
5054
}
5155
}

libs/angular/store/src/lib/store/utils/dispatch-data-to-store/dispatch-data-to-store.util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Store } from '@ngrx/store';
22
import { Observable, throwError } from 'rxjs';
3-
import { catchError, finalize, tap } from 'rxjs/operators';
3+
import { catchError, finalize, tap, map } from 'rxjs/operators';
44
/**
55
* Dispatches data to the store based on a provided action and Observable. Loading and error state will be handled by default.
66
*
@@ -15,7 +15,7 @@ export const dispatchDataToStore = <DataType>(
1515
data: Observable<DataType>,
1616
store: Store,
1717
dispatchType: 'set' | 'add' | 'update' = 'set'
18-
): Observable<DataType> => {
18+
): Observable<void> => {
1919
// Iben: Set the loading state to true and the error state to false to start a new set
2020
store.dispatch(dispatchAction.loading({ payload: true }));
2121
store.dispatch(dispatchAction.error({ payload: false }));
@@ -40,6 +40,7 @@ export const dispatchDataToStore = <DataType>(
4040
// Iben: Set the loading state to false
4141
finalize(() => {
4242
store.dispatch(dispatchAction.loading({ payload: false }));
43-
})
43+
}),
44+
map(() => void 0)
4445
);
4546
};

0 commit comments

Comments
 (0)