import { Injectable, Inject } from '@angular/core';
import { createEffect, Actions, ofType } from '@ngrx/effects';
import { applyInstantUpdate } from './actions';
import { UCAP_NATIVE_SERVICE, NativeService } from '@ucap-webmessenger/native';
import { tap } from 'rxjs/operators';

@Injectable()
export class Effects {
  applyInstantUpdate$ = createEffect(
    () => {
      return this.actions$.pipe(
        ofType(applyInstantUpdate),
        tap(() => {
          this.nativeService.applyInstantUpdates();
        })
      );
    },
    { dispatch: false }
  );

  constructor(
    private actions$: Actions,
    @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService
  ) {}
}
