import { HttpClient } from '@angular/common/http'; import { inject, Injectable } from '@angular/core'; import { environment } from '../../../environments/environment'; import TwitchRedemption from '../models/twitch-redemption'; import { of } from 'rxjs'; import EventService from './EventService'; @Injectable({ providedIn: 'root' }) export class TwitchRedemptionService { private readonly http = inject(HttpClient); private readonly events = inject(EventService); private twitchRedemptions: TwitchRedemption[] = []; private loaded = false; constructor() { this.events.listen('logoff', () => { this.twitchRedemptions = []; this.loaded = false; }); } fetch(force: boolean = false) { if (!force && this.loaded) return of(this.twitchRedemptions); const $ = this.http.get(environment.API_HOST + '/twitch/redemptions', { headers: { 'Authorization': 'Bearer ' + localStorage.getItem('jwt'), } }); $.subscribe(d => { this.twitchRedemptions = d; this.loaded = true; }); return $; } }