Skip to content

Commit

Permalink
refactor(effects): use class properties syntax instead of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed Sep 14, 2018
1 parent 130193b commit f8cc284
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 63 deletions.
30 changes: 13 additions & 17 deletions src/app/core/auth/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@ export class AuthEffects {
) {}

@Effect({ dispatch: false })
login() {
return this.actions$.pipe(
ofType<ActionAuthLogin>(AuthActionTypes.LOGIN),
tap(() =>
this.localStorageService.setItem(AUTH_KEY, { isAuthenticated: true })
)
);
}
login = this.actions$.pipe(
ofType<ActionAuthLogin>(AuthActionTypes.LOGIN),
tap(() =>
this.localStorageService.setItem(AUTH_KEY, { isAuthenticated: true })
)
);

@Effect({ dispatch: false })
logout() {
return this.actions$.pipe(
ofType<ActionAuthLogout>(AuthActionTypes.LOGOUT),
tap(() => {
this.router.navigate(['']);
this.localStorageService.setItem(AUTH_KEY, { isAuthenticated: false });
})
);
}
logout = this.actions$.pipe(
ofType<ActionAuthLogout>(AuthActionTypes.LOGOUT),
tap(() => {
this.router.navigate(['']);
this.localStorageService.setItem(AUTH_KEY, { isAuthenticated: false });
})
);
}
40 changes: 18 additions & 22 deletions src/app/examples/stock-market/stock-market.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,22 @@ export class StockMarketEffects {
) {}

@Effect()
retrieveStock() {
return this.actions$.pipe(
ofType<ActionStockMarketRetrieve>(StockMarketActionTypes.RETRIEVE),
tap(action =>
this.localStorageService.setItem(STOCK_MARKET_KEY, {
symbol: action.payload.symbol
})
),
distinctUntilChanged(),
debounceTime(500),
switchMap((action: ActionStockMarketRetrieve) =>
this.service
.retrieveStock(action.payload.symbol)
.pipe(
map(stock => new ActionStockMarketRetrieveSuccess({ stock })),
catchError(error =>
of(new ActionStockMarketRetrieveError({ error }))
)
)
)
);
}
retrieveStock = this.actions$.pipe(
ofType<ActionStockMarketRetrieve>(StockMarketActionTypes.RETRIEVE),
tap(action =>
this.localStorageService.setItem(STOCK_MARKET_KEY, {
symbol: action.payload.symbol
})
),
distinctUntilChanged(),
debounceTime(500),
switchMap((action: ActionStockMarketRetrieve) =>
this.service
.retrieveStock(action.payload.symbol)
.pipe(
map(stock => new ActionStockMarketRetrieveSuccess({ stock })),
catchError(error => of(new ActionStockMarketRetrieveError({ error })))
)
)
);
}
14 changes: 6 additions & 8 deletions src/app/examples/todos/todos.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ export class TodosEffects {
) {}

@Effect({ dispatch: false })
persistTodos() {
return this.actions$.pipe(
ofType<ActionTodosPersist>(TodosActionTypes.PERSIST),
tap(action =>
this.localStorageService.setItem(TODOS_KEY, action.payload.todos)
)
);
}
persistTodos = this.actions$.pipe(
ofType<ActionTodosPersist>(TodosActionTypes.PERSIST),
tap(action =>
this.localStorageService.setItem(TODOS_KEY, action.payload.todos)
)
);
}
4 changes: 2 additions & 2 deletions src/app/settings/settings.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('SettingsEffects', () => {

const persistAction = new ActionSettingsPersist({ settings: settings });
actions$ = cold('a', { a: persistAction });
effects.persistSettings().subscribe(() => {
effects.persistSettings.subscribe(() => {
expect(localStorageService.setItem).toHaveBeenCalledWith(
SETTINGS_KEY,
persistAction.payload.settings
Expand All @@ -79,7 +79,7 @@ describe('SettingsEffects', () => {
);

actions$ = cold('a-|', { a: nonPersistAction });
effects.persistSettings().subscribe(undefined, undefined, () => {
effects.persistSettings.subscribe(undefined, undefined, () => {
expect(localStorageService.setItem).not.toHaveBeenCalled();
expect(animationsService.updateRouteAnimationType).not.toHaveBeenCalled();
});
Expand Down
26 changes: 12 additions & 14 deletions src/app/settings/settings.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ export class SettingsEffects {
) {}

@Effect({ dispatch: false })
persistSettings() {
return this.actions$.pipe(
ofType<ActionSettingsPersist>(SettingsActionTypes.PERSIST),
tap(action => {
const { settings } = action.payload;
const { pageAnimations, elementsAnimations } = settings;
this.localStorageService.setItem(SETTINGS_KEY, settings);
this.animationsService.updateRouteAnimationType(
pageAnimations,
elementsAnimations
);
})
);
}
persistSettings = this.actions$.pipe(
ofType<ActionSettingsPersist>(SettingsActionTypes.PERSIST),
tap(action => {
const { settings } = action.payload;
const { pageAnimations, elementsAnimations } = settings;
this.localStorageService.setItem(SETTINGS_KEY, settings);
this.animationsService.updateRouteAnimationType(
pageAnimations,
elementsAnimations
);
})
);
}

0 comments on commit f8cc284

Please sign in to comment.