How to skip first value of an observable with RxJS?

netdjw

In my Angular project I use this component what is replresent an input field in a form. The goal is to save the value of the input field on-the-fly if user changed it:

export class SettingInputComponent implements OnInit, OnDestroy {
  @Input() model: SettingInterface;

  private subscriptions: Subscription = new Subscription();

  private appDataService: AppDataServiceInterface<SettingInterface> =
    new AppDataFactoryService<SettingInterface>().get(Setting);

  private value$: BehaviorSubject<any> = new BehaviorSubject(this.model.value);

  constructor() { }

  ngOnInit(): void {
    this.subscriptions.add(this.saveSetting().subscribe());
  }

  ngOnDestroy(): void {
    this.subscriptions.unsubscribe();
  }

  emitToSave(): void {
    this.value$.next(this.model.value);
  }

  private saveSetting(): Observable<any> {
    return this.value$.pipe(
      debounceTime(500),

      distinctUntilChanged(),

      switchMap(() => this.appDataService.save(this.model)
    );
  }
}

HTML:

<third-party-input
  type="text"
  [value]="model.value"
  (change)="emitToSave()"
></third-party-input>

The problem is when the component initializing the saveSetting() observable will fire. I want to avoid this behavior and fire only when user change the model's value property.

How can I avoid the firing on initialization and fireing only when user change the value?

Józef Podlecki

I think skip method would help here

Example

const { of, iif, pipe , BehaviorSubject, Subscription, operators: { distinctUntilChanged, skip, switchMap, debounceTime } } = rxjs;

const value$ = new BehaviorSubject("initial");
const model = {}

const appDataService = {
    save(model) {      
      return of(model);
    }
}

const saveSetting = () => value$.pipe(
      skip(1),
      debounceTime(500),
      distinctUntilChanged(),
      switchMap((model) => appDataService.save(model))
    );

saveSetting().subscribe(console.log);

setInterval(() => {
value$.next(Math.random().toString());
}, 2000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

RxJS - Observable is not emitting value with .next() on first call

Rxjs: How to implement Observable.first()?

Combining two observables, skip first value emitted by one observable if it is null

How to get current value of RxJS Subject or Observable?

How to assign an initial value to Subject RxJS observable?

RxJS: Combine two observables so the observable only emits if the value of the first observable is true

Skip concatenating the first value

How to trigger an rxjs observable

How to chain rxjs observable

How can i get value from an Rxjs observable in my code?

How to use the value from an RxJS observable subscribe call

How do I emit the value of an observable to an rxjs Subject?

RxJS, Observable, how to preserve value and switch map to another one

RxJs: How to only maintain the latest value until inner observable complete

How do I update the value of an RxJS Observable created using of(myArray)

how to change the data of the first observable using data from another observable including the condition in angular/rxjs

RxJs get value from observable

Rxjs combinelatest observable value is undefined

Rxjs: shorthand of create observable from value or observable

RXjs - How to wait for new value on observable A, every time observable B says its okay to start 'watching'

RxJS first() for Observable.of() - no elements in sequence

RxJS - split Observable into two, wait for first to finish

RxJS: why is inner observable firing first?

RXJS Emit first result and then wait for another observable

Rxjs execute and exit the observable on the first match

RxJS Sequential Request and return Observable<> first response

How to create an Observable that depends on another Observable in RxJS

How to chain the result of observable to the below observable in rxjs

RXJS How to convert Observable<T[]> to Observable<T>