Skip to content

Instantly share code, notes, and snippets.

View alex-okrushko's full-sized avatar

Alex Okrushko alex-okrushko

View GitHub Profile
@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)))
)
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),
};
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),
@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()))
@Component({...})
export class ProductDetailsComponent {
product$ = this.store.select(selectors.getCurrentProduct);
constructor(
private readonly store: Store<{}>,
) {
this.store.dispatch(new actions.FetchCurrentProduct());
}
}
@alex-okrushko
alex-okrushko / basic_effects.ts
Created December 6, 2018 03:39
most basic API call
@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()))
)
)
);
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(
message$ = of('Call me!').pipe(
switchMap(() => this.service.callBackend()),
retryBackoff({
initialInterval: 100,
maxRetries: 12,
}),
);
@alex-okrushko
alex-okrushko / calculate_delay.ts
Created May 29, 2018 22:14
example of a function that calculated delay for each iteration in exponential backoff
function calculateDelay(iteration, initialInterval) {
 return Math.pow(2, iteration) * initialInterval;
}
@alex-okrushko
alex-okrushko / retry_backoff_simple.ts
Last active May 30, 2018 13:27
simple example of exponential backoff usage
message$ = of('Call me!').pipe(
switchMap(() => this.service.callBackend()),
retryBackoff(1000),
);