[RXJS实战]使用RXJS管理Angular应用状态

使用RXJS管理Angular应用状态


==由于Node.js出现而衍生的前端框架逐渐走向成熟,各个框架以前互相借鉴良好的编程思想已经成为常态,目前React与Vue分别都有Redux,Vuex工具来管理应用程序数据状态,惟独Angular没有出现成熟稳定的状态管理工具,虽然NGRX是专门为Angular设计,可是其上手较难,因此本人不想使用,因此仍是使用比较熟练的RXJS来定制.==前端

数据中心(Store)

cancellation.data.service.ts编程

@Injectable()
export class CancellationDataService {
    cancellData: CancellationInfo;

    constructor() { }

    /**
     * When access the page each time, then need initial Center Data.
     */
    initPoAdjusterData(): CancellationInfo {
        this.cancellData = new CancellationInfo();
        return this.cancellData;
    }
}

状态管理(State)

state就是当前的状态,那么store和state是什么关系呢? 咱们能够认为 store 对象包括全部数据,若是想获得某个时点的数据,就要对 Store 生成快照。这种时点的数据集合,就叫作 State。
@Injectable()
export class CancellationStateService {

    mainSubject: Subject<ActionConstucter>;

    constructor() { }

    initMainSubject() {
        this.mainSubject = new Subject<ActionConstucter>();
    }

    /**
     * Dispatch one action
     */
    dispatchAction(actionType: Action) {
        const callAction = new ActionConstucter(actionType);
        this.mainSubject.next(callAction);
    }

    /**
     * Dispatch multiple actions at the same time.
     */
    dispatchActions(lstSubjectInfo: ActionConstucter[]) {
        _.each(lstSubjectInfo, (subjectInfo: ActionConstucter) => {
            this.mainSubject.next(subjectInfo);
        });
    }

    /**
     * When portal destory, then unsubscribe
     */
    unsubscribeMainSubject() {
        this.mainSubject.unsubscribe();
    }

}

动做(Action)

/**
 * operation action subject (for containing each action type)
 */
export type Action =
    'QueryResult'
    | 'ReprocessException'
    | 'ReFreshCancellation'
    | 'ReFreshReschedule'
    /**
     * refresh cancellation reschedule two tab at the same time
     */
    | 'ReFreshCancellReschedule'
    /**
     * refresh queryResult's tab number (cancellation,reschedule,new po,exception)
     */
    | 'RefreshTabTotalNumber'
    /**
     * refresh New order todo table and submit table data
     */
    | 'ReFreshNewOrder'
    /**
     * refresh manual creation return list data
     * and create template data Source(like material...)
     */
    | 'RefreshManualCreation'
    /**
     * get Material list for create po template material option
     */
    |'RefreshPurchasingList' // New PO Submit List Submit Refresh PurchasingList;
action creater
经过view来改变state的惟一方式就是触发action来改变store中的数据,而且这个action是一个对象,可是每每view的触发不少,而且有可能触发的类型相同只是传递的内容不一样,那么咱们每次都来写一个对象(而且写这个对象时每次都要写相同的类型),是很麻烦的。 因此咱们能够定义一个函数来生成 action (实际上就是传递必要的参数返回一个符合action的对象)。
export class ActionConstucter {
    actionType: Action;
    constructor(actionType: Action) {
        this.actionType = actionType;
    }
}

reducer(动做状态处理器)==未应用==

JavaScript来理解。reduce属于一种高阶函数,它将其中的回调函数reducer递归应用到数组的全部元素上并返回一个独立的值。这也就是“缩减”或“折叠”的意义所在了。
总而言之一句话,redux当中的reducer之因此叫作reducer,是由于它和 Array.prototype.reduce 当中传入的回调函数很是类似

实战使用

  • 订阅State
subscribeAction() {
        const { mainSubject } = this.stateSvc;
        mainSubject.takeUntil(this.compInstance.destroy$)
            .filter(item => item.actionType === 'QueryResult' ||
                item.actionType === 'ReFreshCancellation' || item.actionType === 'ReFreshCancellReschedule')
            .subscribe((item) => {
                this.refreshUI();
            });
    }
  • 调度Action
this.stateSvc.dispatchAction('ReFreshCancellReschedule');
  • 更新数据
refreshUI() {
        this.compInstance.UICtrl = new UICtrlInfo();
        this.compInstance.UIDisplay = new UIDisplayInfo();
        this.compInstance.UIJsPanel = new CancellationTableJspanelInfo();

        this.closeAllExpand();
        const { cancellData } = this.dataService;
        this.compInstance.UICtrl.categoryTypeList = cancellData.categoryTypeList;
        this.compInstance.UICtrl.categoryCodeMap = cancellData.categoryCodeMap;
        const cancellationList = this.compInstance.submit ?
            cancellData.cancellationList.cancel_submit :
            cancellData.cancellationList.cancel_to_do as CancellationTableInputConfig[];
        if (!_.isEmpty(cancellationList)) {
            this.compInstance.UIDisplay.lstData = this.getContentList(cancellationList);
            this.cfgSvc.refreshStatus();
        }
    }
const { cancellData } = this.dataService;
        this.compInstance.UICtrl.categoryTypeList = cancellData.categoryTypeList;
        this.compInstance.UICtrl.categoryCode
相关文章
相关标签/搜索