<commit_before>import 'rxjs/Rx';

import {Injectable} from '@angular/core';
import {ReplaySubject} from 'rxjs/ReplaySubject';

import {ErrorHandlingService} from './error-handling.service';

import {Profile} from 'generated';
import {ProfileService} from 'generated';


@Injectable()
export class ProfileStorageService {
  private activeCall = false;
  private profile = new ReplaySubject<Profile>(1);
  public profile$ = this.profile.asObservable();

  constructor(private profileService: ProfileService,
              private errorHandlingService: ErrorHandlingService) {
    this.reload();
  }

  reload() {
    if (!this.activeCall) {
      this.activeCall = true;
      this.profileService.getMe().subscribe((profile) => {
        this.profile.next(profile);
        this.activeCall = false;
      }, () => {
        this.errorHandlingService.profileLoadError = true;
      });
    }
  }
}<commit_msg>[RW-1010][risk=low] Retry on getProfile 409s (#1025)<commit_after>import {Observable} from 'rxjs/Observable';
import {timer} from 'rxjs/observable/timer';
import 'rxjs/Rx';

import {Injectable} from '@angular/core';
import {ReplaySubject} from 'rxjs/ReplaySubject';

import {ErrorHandlingService} from './error-handling.service';

import {Profile} from 'generated';
import {ProfileService} from 'generated';


@Injectable()
export class ProfileStorageService {
  private static readonly RETRY_DELAY_MS = 500;

  private activeCall = false;
  private profile = new ReplaySubject<Profile>(1);
  public profile$ = this.profile.asObservable();

  constructor(private profileService: ProfileService,
              private errorHandlingService: ErrorHandlingService) {
    this.reload();
  }

  reload() {
    if (this.activeCall) {
      return;
    }
    this.activeCall = true;

    // Conflict 409s are not retries automatically. These have a likelihood to
    // occur on initial user login since we lazily initialize several things via
    // the /me profile endpoint.
    let retries = 0;
    this.profileService.getMe().retryWhen((errs) => {
      return errs.flatMap((err) => {
        if (err.status !== 409) {
          throw err;
        }
        if (++retries >= 3) {
          throw err;
        }
        return timer(retries * ProfileStorageService.RETRY_DELAY_MS);
      });
    }).subscribe((profile) => {
      this.profile.next(profile);
      this.activeCall = false;
    }, (err) => {
      this.errorHandlingService.profileLoadError = true;
    });
  }
}
{'fraction_non_alphanumeric': 0.12469733656174334, 'fraction_numerical': 0.009685230024213076, 'mean_word_length': 3.3798586572438163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3740703', 'n_tokens_mistral': 750, 'n_tokens_neox': 718, 'n_words': 174}