[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;

    constructor() { }

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

    /**
     * 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

你可能感兴趣的:(前端,javascript,angular,rxjs)