Skip to content

Instantly share code, notes, and snippets.

View alex-okrushko's full-sized avatar

Alex Okrushko alex-okrushko

View GitHub Profile
@alex-okrushko
alex-okrushko / app.component.ts
Created May 23, 2018 03:13
Exponential backoff retry - example with fake service
import { Component } from '@angular/core';
import { of } from 'rxjs';
import { tap, switchMap} from 'rxjs/operators';
import { retryBackoff } from 'backoff-rxjs';
import { BackendService, HttpError } from './backend.service';
export const INIT_INTERVAL_MS = 100; // 100 ms
export const MAX_INTERVAL_MS = 20 * 1000; // 20 sec
@Component({
export const initialState: State = adapter.getInitialState({
selectedBookId: null,
});
export const reducer = createReducer(
initialState,
on(
BooksApiActions.searchSuccess,
CollectionApiActions.loadBooksSuccess,
(state, { books }) => adapter.addMany(books, state)
export const enum LoadingState {
INIT = 'INIT',
LOADING = 'LOADING',
LOADED = 'LOADED',
}
export interface ErrorState {
errorMsg: string;
}
export type CallState = LoadingState | ErrorState;
/** Action to log in the User from the Login Page */
export const login = createAction(
'[Login Page] Login',
// notice 👇 payload wrapping
props<{payload: {username: string; password: string;}}>(),
)
/**
* Action to log in the User from the Login Page.
* Uses the most secure password by default.
*/
export const login = createAction(
'[Login Page] Login',
(username: string, password = 'p@ssw0rdLol') => ({username, password}),
);
export class MyEffects {
constructor(private readonly actions$: Actions) {}
@Effect
login$ = this.actions$.pipe(
ofType(login), // provide type safety *without* ActionsUnion
...
)
}
export class MyEffects {
constructor(private readonly actions$: Actions<ActionsUnion>) {}
@Effect
login$ = this.actions$.pipe(
// ensures type safety only when ActionsUnion is provided
ofType(login.type),
...
)
function reducer(state, action) {
switch(action.type) {
case login.type: { // <----- usage in reducer
...
}
}
}
@Effect
login({username: 'Tim', password: 'NgRx'})
/** Action to log in the User from the Login Page */
export const login = createAction(
'[Login Page] Login',
props<{username: string; password: string;}>(),
)