天天看點

ngrx Effect學習筆記

SAP Spartacus的connector通過NgRx的Effect從SAP Commerce Cloud背景取資料。

官網位址:

https://ngrx.io/guide/effects

Effects的五大要點

Effects are injectable service classes with distinct parts:

(1) An injectable Actions service that provides an observable stream of all actions dispatched after the latest state has been reduced.

ngrx Effect學習筆記
ngrx Effect學習筆記

每個injectable Effects都有一個action service actions$,在store最新的狀态被reduce之後,能夠dispatch一個observable stream.

(2) Metadata is attached to the observable streams using the createEffect function. The metadata is used to register the streams that are subscribed to the store. Any action returned from the effect stream is then dispatched back to the Store.

使用createEffect将Metadata添加到observable streams上去。Metadata的作用是将streams注冊到store上去。從Effect stream傳回的action會重新dispatch到store中去。

(3) Actions are filtered using a pipeable ofType operator. The ofType operator takes one or more action types as arguments to filter on which actions to act upon.

Actions通過ofType操作符進行過濾。ofType操作符接受一個或多個Action type作為輸入參數,過濾某個effect應該響應哪一個action.

(4) Effects are subscribed to the Store observable.

Effects訂閱到store Observable上面。

(5) Services are injected into effects to interact with external APIs and handle streams.

服務注冊到effects裡,同外部API互動。

關于Observable真正開始工作的時間點

官網提到:

This means that calling subscribe is actually the moment when Observable starts its work, not when it is created

調用Observable的subscribe方法,才是Observable真正開始工作的時間點,而不是它被建立的時候。

繼續閱讀