Essentially, when the .ofType()
method is invoked, it subscribes to the source stream of actions and only passes matching actions to the resulting stream. This means that it is called just once.
If we delve into the source code, we can see that behind the scenes, the ofType
function utilizes the filter
operator from the rxjs
library. In essence, this.action$.ofType(CREATE_TASK)
can be equated to:
this.action$.filter(action => action.type === CREATE_TASK)
To understand how the filter
operation functions, you can refer to the documentation provided by rxjs
here:
Similar to the familiar Array.prototype.filter
method, this operator extracts values from the source Observable, applies them through a predicate
function, and emits only those values that resulted in true
.
It's important to note that each effect accepts an observable (this.action$
) as input and yields a fresh observable which is subscribed to only once during initialization. The returned observable dictates how actions from the input observable are altered, without impacting the source observable itself.
In your scenario, the ofType()
method generates a new observable that monitors the this.action$
stream and emits solely actions meeting the condition action.type === CREATE_TASK
. Following that is the map
operator, which also produces a new observable observing the one returned by the ofType()
invocation and modifies each received action according to a specified projection function. However, all these observables are established once at initialization and simply allow actions to pass through, filtering and transforming them as needed upon dispatch.
If you wish to enhance your comprehension of rxjs
, I recommend delving into André Staltz's talk on "You will learn RxJS". It should provide valuable insights into the world of observables and their functionality.