effect Method

Creates a new stream with a side effect, similar to the RxJS pipe method.

This method allows you to apply a sequence of RxJS operators to the state’s stream, performing actions or side effects whenever the state emits a value. This can be particularly useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param ...operators: OperatorFunction<any, any>[]
A sequence of RxJS operators that define the side effects to be applied to the emitted values

@returns this
The instance of the current state, allowing for method chaining

API

effect(...operators: OperatorFunction<any, any>[]): this;

Example

import {switchMap, of} from 'rxjs';
import {state} from '@bitfiber/rx';
 
const openDialog = state<boolean>(false, s => s
  // Performs an effect each time the emitter emits new value
  .effect(
    switchMap(isOpened => !isOpened ? dialog.open() : of(false)),
  ));