<commit_before>import { Injectable } from "@angular/core";
import { Http, Headers } from "@angular/http";
import { USER_AUTH_KEY } from "./auth.service";
import { Subject, Observable, Observer } from "rxjs";
import { HttpBackend, HttpRequest } from "@angular/common/http";
import { HttpHeaders } from "@angular/common/http";
import { HttpEventType } from "@angular/common/http";

export class UserAuthInfo {
    token: string = "";
    refreshToken: string = "";
}

@Injectable()
export class TokenService {
    private refreshUrl: string = "api/token/refresh"
    private isRefreshing: boolean = false;
    private refreshingStream: Subject<boolean> = new Subject<boolean>();
    constructor(private http: HttpBackend) { }

    public refreshAuthentication(refreshToken: string): Observable<null> {
        if (!this.isRefreshing) return this.tryRefreshToken(refreshToken);

        return new Observable((observer: Observer<null>) => {
            this.refreshingStream.subscribe(result => {
                if (!result) {
                    observer.next(null);
                    observer.complete();
                }
            })
        })
    }

    private tryRefreshToken(refreshToken: string): Observable<null> {
        this.setRefreshing(true);

        const headers = new HttpHeaders({
            "Authorization": `Bearer ${refreshToken}`,
            "Content-Type": "application/json"
        });

        const request = new HttpRequest("POST", this.refreshUrl, JSON.stringify(refreshToken), { headers, responseType: "json" });

        return this.http.handle(request)
            .filter(event => event.type === HttpEventType.Response)
            .map((res) => {
                console.log(res);
                localStorage.setItem(USER_AUTH_KEY, JSON.stringify(res));
                return null;
            })
            .finally(() => this.setRefreshing(false))
    }

    private setRefreshing(value: boolean) {
        this.isRefreshing = value;
        this.refreshingStream.next(this.isRefreshing);
    }
}<commit_msg>Redone some parts of TokenService<commit_after>import { Injectable } from "@angular/core";
import { Http, Headers } from "@angular/http";
import { USER_AUTH_KEY } from "./auth.service";
import { Subject, Observable, Observer } from "rxjs";
import { HttpBackend, HttpRequest } from "@angular/common/http";
import { HttpHeaders } from "@angular/common/http";
import { HttpEventType } from "@angular/common/http";

export class UserAuthInfo {
    token: string = "";
    refreshToken: string = "";
}

@Injectable()
export class TokenService {
    private refreshUrl: string = "api/token/refresh"
    private isRefreshing: boolean = false;
    private refreshingStream: Subject<boolean> = new Subject<boolean>();
    constructor(private http: HttpBackend) { }

    public refreshAuthentication(refreshToken: string): Observable<null> {
        if (!this.isRefreshing) return this.tryRefreshToken(refreshToken);

        return new Observable((observer: Observer<null>) => {
            this.refreshingStream.subscribe(result => {
                if (!result) {
                    observer.next(null);
                    observer.complete();
                }
            })
        })
    }

    private tryRefreshToken(refreshToken: string): Observable<null> {
        this.setRefreshing(true);

        const headers = new HttpHeaders({
            "Authorization": `Bearer ${refreshToken}`,
            "Content-Type": "application/json"
        });

        const request = new HttpRequest("POST", this.refreshUrl, JSON.stringify(refreshToken), { headers, responseType: "json" });

        let a =  this.http.handle(request)
            .filter(event => event.type === HttpEventType.Response)
            .map((res:any)=>res.body)
            .map((res) => {
                console.log(res);
                localStorage.setItem(USER_AUTH_KEY, JSON.stringify(res));
                this.setRefreshing(false)
                return null;
            })
            .finally(() => this.setRefreshing(false))
        
            a.subscribe(e=>{})

        return a;
    }

    private setRefreshing(value: boolean) {
        this.isRefreshing = value;
        this.refreshingStream.next(this.isRefreshing);
    }
}
{'fraction_non_alphanumeric': 0.1261999531725591, 'fraction_numerical': 0.0, 'mean_word_length': 2.562969140950792, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 25, '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': '6344906', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1131, 'n_words': 246}