[Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
Communicating with a remote server via HTTP presents an extra level of complexity as there is an increased chance of race conditions and the need for error handling. There is also the tricky problem of how to handle the user experience as the application is trying to complete the remote request. NX Data Persistence is a library that works with ngrx effects to give us a better way to handle remote server calls as well as improving on the overall shape of the effect itself. In this lesson, we are going to convert our stock effects to use NX Data Persistence and the advantages of doing so by using fetch, optimisticUpdate, and pessimisticUpdate. We will also see how it gives us a neat division in our effect to handle the sequence we want to run as well as any errors that should arise in the process.
Install:
npm i --save @nrwl/nx
app.module.ts:
imports: [
...
NxModule.forRoot(),
...
],
effect:
import { Injectable } from '@angular/core';
import {Effect} from '@ngrx/effects';
import { DataPersistence } from '@nrwl/nx';
import {Action} from '@ngrx/store';
import { RolesService } from '../services/roles.service';
import { Observable } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import * as actions from '../actions';
import { Role } from '../models';
import { DashbaordState } from '../reducers';
@Injectable()
export class RolesEffects {
constructor(
private rolesService: RolesService,
private dataPersistence: DataPersistence<DashbaordState>
) {
}
@Effect()
loadRoles$: Observable<Action> = this.dataPersistence.fetch(
actions.RolesActionTypes.LoadRoles, {
run: (action: actions.LoadRoles) => {
return this.rolesService.roles.pipe(
map((roles: Role[]) => new actions.LoadRolesSuccess(roles))
);
},
onError: (action: actions.LoadRoles, error) => {
console.error('Error', action, error);
}
}
);
}
[Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular的更多相关文章
- [Angular2] @Ngrx/store and @Ngrx/effects learning note
Just sharing the learning experience related to @ngrx/store and @ngrx/effects. In my personal opinio ...
- Angular应用架构设计-3:Ngrx Store
这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...
- [Angular] Ngrx/effects, Action trigger another action
@Injectable() export class LoadUserThreadsEffectService { constructor(private action$: Actions, priv ...
- [Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern
Extracting away the implementation details of ngrx from your components using the facade pattern cre ...
- Identity Server 4 原理和实战(完结)_建立Angular 客户端
https://material.angular.io/ 第一部是安装angular cli --prefix=ac:前缀 --routing:默认使用路由 style=scss:样式使用scss - ...
- [Angular2 Router] Load Data Based on Angular 2 Route Params
You can load resource based on the url using the a combination of ActivatedRouteand Angular 2’s Http ...
- SQL Server Reporting Services 自定义数据处理扩展DPE(Data Processing Extension)
最近在做SSRS项目时,遇到这么一个情形:该项目有多个数据库,每个数据库都在不同的服务器,但每个数据库所拥有的数据库对象(table/view/SPs/functions)都是一模一样的,后来结合网络 ...
- Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题
前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...
- Angular14 Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题、emmet安装
前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...
随机推荐
- php5.5 安装
1.php安装 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum install zl ...
- Windows10 Docker加速
参考地址:https://blog.csdn.net/wanderlustlee/article/details/80216588 在刚开始使用时,有可能因为网络的问题导致整个镜像的下载过程不是太顺畅 ...
- 小试牛刀之Django
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- WAR/EAR 概念
In J2EE application, modules are packaged as EAR, JAR and WAR based on their functionality JAR: EJB ...
- 用Chrome在手机上调试本地网页代码
本文摘自Google 原文地址1:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_sourc ...
- 【数论】bzoj1968 [Ahoi2005]COMMON 约数研究
对于i属于[1,n],i只能成为[1,n]中n/i个数的约数,易证. #include<stdio.h> int n,i; long long ans; int main() { scan ...
- 【DFS】POJ3009-Curling 2.0
[题目大意] 给出一张地图,一旦往一个方向前进就必须一直向前,直到一下情况发生:(1)碰到了block,则停在block前,该block消失:(2)冲出了场地外:(3)到达了终点.改变方向十次以上或者 ...
- 监视scrollview是否滚动到底
//监视scrollview是否滚动到底 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [refreshHeaderViewegoRe ...
- [转]详解spring 每个jar的作用
spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...
- POJ 3057 Evacuation 二分+最大流
Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...