specifies the type of a function that receives an observable as a parameter and returns another observable:
OperatorFunction: 接收兩個型別引數,T 代表原始 Observable 包裹的型別,A 表示返回的新的 Observable 包含的型別。
最終返回一個新的 Observable,型別為 B.
# Store.dispatch(event)
> It signals that an event that requires state changes is sent from the UI(e.g a smart component).
> Store.dispatch() will push the action(event) into an actions stream(which is different from the one that belongs to the effects):
# Action
可以理解成指令,透過 UI / Service / Effects 來 dispatch.
> Creator is simply a function takes up a parameter of type P and returns an object of type R.
# reducer
Reducers are pure functions that are responsible for state changes.
reducer 的 interface 定義:
```typescript
export interface ActionReducer {
(state: T | undefined, action: V): T;
}
```
上述語法定義了一個 interface,該 interface 描述了一個函式型別,大括號內是函式型別的具體定義,小括號為這個函式的輸入介面定義,該函式接收兩個輸入引數,形參為 state 和 action,型別分別為 T(也可以接受 undefined) 和 V, 小括號後面的冒號,定義了返回引數型別也應該為 T.
很明顯,這個函式就是一個狀態機,基於當前狀態和輸入的 action(可以理解成指令,觸發狀態遷移的指令),返回新的狀態。
TypeScript 裡透過定義介面來描述函式 signature 的這種方式,已經和 ABAP 裡的 interface 很不一樣了。
以 address-verification.reducer.ts 裡的 reducer 為例:該 reducer 如何被 setup?
![](https://img-blog.csdnimg.cn/img_convert/4abd6441d3fe9d9a3571caae8443b5b1.png)
在 index.ts 裡,透過 import * as 來區分這些同名的 reducer:
![](https://img-blog.csdnimg.cn/img_convert/84da8a47f364f634c511007cb9ad9dca.png)
透過 getReducers 統一返回:
![](https://img-blog.csdnimg.cn/img_convert/f605b993a586e10744da792f6079793f.png)
透過 reducerToken 和 getReducers 提供 provider 資訊:
![](https://img-blog.csdnimg.cn/img_convert/7a10f896db98972bfb8f86acf01ca9d2.png)
Provider 在 @NgModule 提供的後設資料裡使用:
![](https://img-blog.csdnimg.cn/img_convert/9eae7850e0486b2879939a0b06dd39ea.png)
# Store
並不儲存資料,只是一箇中介軟體。
原始碼:
```typescript
export class Store extends Observable implements Observer {
constructor(
state$: StateObservable,
private actionsObserver: ActionsSubject,
private reducerManager: ReducerManager
) {
super();
this.source = state$;
}
/* ... */
}
```
Store 從外界接受資料,即 state$.
> Every time the source (state$) emits, the Store class will send the value to its subscribers.
```javascript
allows consumer ↔️ state communication
⬆️
|
|
----------- newState -----------
| | | |
| | Store.dispatch() | |
----------- -----------
| ⬆️
Action | | newState
| |
⬇️ |
-------------
| |
| Reducer |