This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Effect() | |
addCartItem: Observable<Action> = this.actions$.pipe( | |
ofType<cartActions.AddItem>(cartActions.ADD_ITEM), | |
concatMap(({ itemId }) => | |
this.cartService.addToCart(itemId).pipe( | |
// Notice, that nothing is passed to the Success. | |
map(() => new cartActions.AddItemSuccess()), | |
// passing the itemId to the Error, so it can be restored. | |
catchError(() => of(new cartActions.AddItemError(itemId))) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function productSync(reducer: ActionReducer<{ product: ProductState }>) { | |
return (state, action) => { | |
let reducedState = reducer(state, action); | |
if (action.type === INIT) { | |
const data = window.localStorage.getItem('productData'); | |
if (data) { | |
reducedState = { | |
...reducedState, | |
product: JSON.parse(data), | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function reducer( | |
state: ProductState = initState, | |
action: actions.All | |
): ProductState { | |
switch (action.type) { | |
... | |
case actions.FETCH_PRODUCTS_SUCCESS: { | |
return { | |
// addAll replaces current list of products with new list | |
products: productAdapter.addAll(action.payload, state.products), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Effect() | |
fetchProduct: Observable<Action> = this.actions$.pipe( | |
ofType(actions.FETCH_CURRENT_PRODUCT), | |
withLatestFrom( | |
this.store.select(selectors.getCurrentProductId) | |
), | |
switchMap(id => | |
this.productService.getProduct(id).pipe( | |
map(product => new actions.FetchProductSuccess(product)), | |
catchError(() => of(new actions.FetchProductError())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({...}) | |
export class ProductDetailsComponent { | |
product$ = this.store.select(selectors.getCurrentProduct); | |
constructor( | |
private readonly store: Store<{}>, | |
) { | |
this.store.dispatch(new actions.FetchCurrentProduct()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Effect() | |
fetchProducts: Observable<Action> = this.actions$.pipe( | |
ofType<actions.FetchProducts>(actions.FETCH_PRODUCTS), | |
switchMap(() => | |
this.productService.getProducts().pipe( | |
map(products => new actions.FetchProductsSuccess(products)), | |
catchError(() => of(new actions.FetchProductsError())) | |
) | |
) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("should handle a basic source that emits next then errors, count=3", () => { | |
testScheduler.run(({ expectObservable, cold, expectSubscriptions }) => { | |
const source = cold("--1-2-3-#"); | |
const subs = [ "^ ! ", | |
" ^ ! ", | |
" ^ !" | |
]; | |
const expected = "--1-2-3----1-2-3-----1-2-3-#"; | |
expectObservable( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message$ = of('Call me!').pipe( | |
switchMap(() => this.service.callBackend()), | |
retryBackoff({ | |
initialInterval: 100, | |
maxRetries: 12, | |
}), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateDelay(iteration, initialInterval) { | |
return Math.pow(2, iteration) * initialInterval; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message$ = of('Call me!').pipe( | |
switchMap(() => this.service.callBackend()), | |
retryBackoff(1000), | |
); |