import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { shareReplay } from 'rxjs/operators';

import { RPCService } from '@loafer/ng-rpc';
import { MetaCrawler } from '@overflow/commons-typescript/model/meta';

@Injectable()
export class MetaCrawlerService {
  private metaCrawlerCache$: Observable<MetaCrawler[]>;

  public constructor(
    private rpcService: RPCService,
  ) {

  }

  public readAll(): Observable<MetaCrawler[]> {
    if (!this.metaCrawlerCache$) {
      this.metaCrawlerCache$ = this.rpcService.call('MetaCrawlerService.readAll').pipe(
        shareReplay<MetaCrawler[]>(1),
      );
    }

    return this.metaCrawlerCache$;
  }
}
