Immutable & Redux in Angular Way

Immutable & Redux in Angular Way

寫在前面

AngularJS 1.x版本做爲上一代MVVM的框架取得了巨大的成功,如今一提到Angular,哪怕是已經和1.x版本徹底不兼容的Angular 2.x(目前最新的版本號爲4.2.2),你們仍是把其做爲典型的MVVM框架,MVVM的優勢Angular天然有,MVVM的缺點也變成了Angular的缺點一直被人詬病。javascript

其實,從Angular 2開始,Angular的數據流動徹底能夠由開發者自由控制,所以不管是快速便捷的雙向綁定,仍是如今風頭正盛的Redux,在Angular框架中其實均可以獲得很好的支持。css

Mutable

咱們以最簡單的計數器應用舉例,在這個例子中,counter的數值能夠由按鈕進行加減控制。html

counter.component.ts代碼前端

import { Component, ChangeDetectionStrategy, Input } from '@angular/core';

@Component({
  selector       : 'app-counter',
  templateUrl    : './counter.component.html',
  styleUrls      : []
})
export class CounterComponent {
  @Input()
  counter = {
    payload: 1
  };
  
  increment() {
    this.counter.payload++;
  }

  decrement() {
    this.counter.payload--;
  }

  reset() {
    this.counter.payload = 1;
  }

}

counter.component.html代碼java

<p>Counter: {{ counter.payload }}</p>
<button (click)="increment()">Increment</button>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset</button>

圖片描述

如今咱們增長一下需求,要求counter的初始值能夠被修改,而且將修改後的counter值傳出。在Angular中,數據的流入和流出分別由@Input和@Output來控制,咱們分別定義counter component的輸入和輸出,將counter.component.ts修改成git

import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
  selector   : 'app-counter',
  templateUrl: './counter.component.html',
  styleUrls  : []
})
export class CounterComponent {
  @Input() counter = {
    payload: 1
  };
  @Output() onCounterChange = new EventEmitter<any>();

  increment() {
    this.counter.payload++;
    this.onCounterChange.emit(this.counter);
  }

  decrement() {
    this.counter.payload--;
    this.onCounterChange.emit(this.counter);
  }

  reset() {
    this.counter.payload = 1;
    this.onCounterChange.emit(this.counter);
  }
}

當其餘component須要使用counter時,app.component.html代碼github

<counter [counter]="initCounter" (onCounterChange)="onCounterChange($event)"></counter>

app.component.ts代碼web

import { Component } from '@angular/core';
@Component({
  selector   : 'app-root',
  templateUrl: './app.component.html',
  styleUrls  : [ './app.component.less' ]
})
export class AppComponent {
  initCounter = {
    payload: 1000
  }

  onCounterChange(counter) {
    console.log(counter);
  }
}

在這種狀況下counter數據typescript

  1. 會被當前counter component中的函數修改編程

  2. 也可能被initCounter修改

  3. 若是涉及到服務端數據,counter也能夠被Service修改

  4. 在複雜的應用中,還可能在父component經過@ViewChild等方式獲取後被修改

框架自己對此並無進行限制,若是開發者對數據的修改沒有進行合理的規劃時,很容易致使數據的變動難以被追蹤。

與AngularJs 1.x版本中在特定函數執行時進行髒值檢查不一樣,Angular 2+使用了zone.js對全部的經常使用操做進行了monkey patch,有了zone.js的存在,Angular再也不像以前同樣須要使用特定的封裝函數才能對數據的修改進行感知,例如ng-click或者$timeout等,只須要正常使用(click)或者setTimeout就能夠了。

與此同時,數據在任意的地方能夠被修改給使用者帶來了便利的同時也帶來了性能的下降,因爲沒法預判髒值產生的時機,Angular須要在每一個瀏覽器事件後去檢查更新template中綁定數值的變化,雖然Angular作了大量的優化來保證性能,而且成果顯著(目前主流前端框架的跑分對比),可是Angular也提供了另外一種開發方式。

Immutable & ChangeDetection

在Angular開發中,能夠經過將component的changeDetection定義爲ChangeDetectionStrategy.OnPush從而改變Angular的髒值檢查策略,在使用OnPush模式時,Angular從時刻進行髒值檢查的狀態改變爲僅在兩種狀況下進行髒值檢查,分別是

  1. 當前component的@Input輸入值發生更換

  2. 當前component或子component產生事件

反過來講就是當@Input對象mutate時,Angular將再也不進行自動髒值檢測,這個時候須要保證@Input的數據爲Immutable

將counter.component.ts修改成

import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
@Component({
  selector       : 'app-counter',
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl    : './counter.component.html',
  styleUrls      : []
})
export class CounterComponent {
  @Input() counter = {
    payload: 1
  };
  @Output() onCounterChange = new EventEmitter<any>();

  increment() {
    this.counter.payload++;
    this.onCounterChange.emit(this.counter);
  }

  decrement() {
    this.counter.payload--;
    this.onCounterChange.emit(this.counter);
  }

  reset() {
    this.counter.payload = 1;
    this.onCounterChange.emit(this.counter);
  }
}

將app.component.ts修改成

import { Component } from '@angular/core';
@Component({
  selector   : 'app-root',
  templateUrl: './app.component.html',
  styleUrls  : [ './app.component.less' ]
})
export class AppComponent {
  initCounter = {
    payload: 1000
  }

  onCounterChange(counter) {
    console.log(counter);
  }

  changeData() {
    this.initCounter.payload = 1;
  }
}

將app.component.html修改成

<app-counter [counter]="initCounter" (onCounterChange)="onCounterChange($event)"></app-counter>
<button (click)="changeData()">change</button>

圖片描述

這個時候點擊change發現counter的值不會發生變化。

將app.component.ts中changeData修改成

changeData() {
  this.initCounter = {
    ...this.initCounter,
    payload: 1
  }
}

counter值的變化一切正常,以上的代碼使用了Typescript 2.1開始支持的 Object Spread,和如下代碼是等價的

changeData() {
  this.initCounter = Object.assign({}, this.initCounter, { payload: 1 });
}

在ChangeDetectionStrategy.OnPush時,能夠經過ChangeDetectorRef.markForCheck()進行髒值檢查,官網範點擊此處,手動markForCheck能夠減小Angular進行髒值檢查的次數,可是不只繁瑣,並且也不能解決數據變動難以被追蹤的問題。

經過保證@Input的輸入Immutable能夠提高Angular的性能,可是counter數據在counter component中並非Immutable,數據的修改一樣難以被追蹤,下一節咱們來介紹使用Redux思想來構建Angular應用。

Redux & Ngrx Way

Redux來源於React社區,時至今日已經基本成爲React的標配了。Angular社區實現Redux思想最流行的第三方庫是ngrx,借用官方的話來講RxJS poweredinspired by Redux,靠譜。

若是你對RxJS有進一步瞭解的興趣,請訪問https://rxjs-cn.github.io/rxj...

圖片描述

基本概念

和Redux同樣,ngrx也有着相同View、Action、Middleware、Dispatcher、Store、Reducer、State的概念。使用ngrx構建Angular應用須要捨棄Angular官方提供的@Input和@Output的數據雙向流動的概念。改用Component->Action->Reducer->Store->Component的單向數據流動。
圖片描述

如下部分代碼來源於CounterNgrx這篇文章

咱們使用ngrx構建一樣的counter應用,與以前不一樣的是此次須要依賴@ngrx/core@ngrx/store

Component

app.module.ts代碼,將counterReducer經過StoreModule import

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';

import {AppComponent} from './app.component';
import {StoreModule} from '@ngrx/store';
import {counterReducer} from './stores/counter/counter.reducer';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    StoreModule.provideStore(counterReducer),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}

在NgModule中使用ngrx提供的StoreModule將咱們的counterReducer傳入

app.component.html

<p>Counter: {{ counter | async }}</p>
<button (click)="increment()">Increment</button>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset</button>

注意多出來的async的pipe,async管道將自動subscribe Observable或Promise的最新數據,當Component銷燬時,async管道會自動unsubscribe。

app.component.ts

import {Component} from '@angular/core';
import {CounterState} from './stores/counter/counter.store';
import {Observable} from 'rxjs/observable';
import {Store} from '@ngrx/store';
import {DECREMENT, INCREMENT, RESET} from './stores/counter/counter.action';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  counter: Observable<number>;

  constructor(private store: Store<CounterState>) {
    this.counter = store.select('counter');
  }

  increment() {
    this.store.dispatch({
      type: INCREMENT,
      payload: {
        value: 1
      }
    });
  }

  decrement() {
    this.store.dispatch({
      type: DECREMENT,
      payload: {
        value: 1
      }
    });
  }

  reset() {
    this.store.dispatch({type: RESET});
  }
}

在Component中能夠經過依賴注入ngrx的Store,經過Store select獲取到的counter是一個Observable的對象,天然能夠經過async pipe顯示在template中。

dispatch方法傳入的內容包括typepayload兩部分, reducer會根據typepayload生成不一樣的state,注意這裏的store其實也是個Observable對象,若是你熟悉Subject,你能夠暫時按照Subject的概念來理解它,store也有一個next方法,和dispatch的做用徹底相同。

Action

counter.action.ts

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const RESET     = 'RESET';

Action部分很簡單,reducer要根據dispath傳入的action執行不一樣的操做。

Reducer

counter.reducer.ts

import {CounterState, INITIAL_COUNTER_STATE} from './counter.store';
import {DECREMENT, INCREMENT, RESET} from './counter.action';
import {Action} from '@ngrx/store';

export function counterReducer(state: CounterState = INITIAL_COUNTER_STATE, action: Action): CounterState {
  const {type, payload} = action;

  switch (type) {
    case INCREMENT:
      return {...state, counter: state.counter + payload.value};

    case DECREMENT:
      return {...state, counter: state.counter - payload.value};

    case RESET:
      return INITIAL_COUNTER_STATE;

    default:
      return state;
  }
}

Reducer函數接收兩個參數,分別是state和action,根據Redux的思想,reducer必須爲純函數(Pure Function),注意這裏再次用到了上文提到的Object Spread

Store

counter.store.ts

export interface CounterState {
  counter: number;
}

export const INITIAL_COUNTER_STATE: CounterState = {
  counter: 0
};

Store部分其實也很簡單,定義了couter的Interface和初始化state。

以上就完成了Component->Action->Reducer->Store->Component的單向數據流動,當counter發生變動的時候,component會根據counter數值的變化自動變動。

總結

一樣一個計數器應用,Angular其實提供了不一樣的開發模式

  1. Angular默認的數據流和髒值檢查方式其實適用於絕大部分的開發場景。

  2. 當性能遇到瓶頸時(基本不會遇到),能夠更改ChangeDetection,保證傳入數據Immutable來提高性能。

  3. 當MVVM再也不能知足程序開發的要求時,能夠嘗試使用Ngrx進行函數式編程。

這篇文章總結了不少Ngrx優缺點,其中我以爲比較Ngrx顯著的優勢是

  1. 數據層不只相對於component獨立,也相對於框架獨立,便於移植到其餘框架

  2. 數據單向流動,便於追蹤

Ngrx的缺點也很明顯

  1. 實現一樣功能,代碼量更大,對於簡單程序而言使用Immutable過分設計,下降開發效率

  2. FP思惟和OOP思惟不一樣,開發難度更高

參考資料

  1. Immutability vs Encapsulation in Angular Applications

  2. whats-the-difference-between-markforcheck-and-detectchanges

  3. Angular 也走 Redux 風 (使用 Ngrx)

  4. Building a Redux application with Angular 2

相關文章
相關標籤/搜索