ngrx Effect学习笔记

官网地址: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.

在这里插入图片描述

监听currency和language change:

在这里插入图片描述

每个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真正开始工作的时间点,而不是它被创建的时候。