Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 68x 68x 28x 28x 28x 28x 3x 3x 3x 3x 3x 2x 2x 2x 2x 3x 3x | import { Injectable } from '@angular/core';
import { makeAutoObservable, observable, runInAction } from 'mobx';
import { catchError, Observable, of, shareReplay, Subject, throwError } from 'rxjs';
import { Oembed } from '../model/oembed';
import { OEmbedService } from '../service/api/oembed.service';
@Injectable({
providedIn: 'root'
})
export class OembedStore {
cache = new Map<string, Observable<Oembed | null>>();
private loading: (() => void)[] = [];
constructor(
private oembeds: OEmbedService,
) {
makeAutoObservable(this, {
cache: observable.ref,
});
}
get(url: string, theme?: string, maxwidth?: number, maxheight?: number) {
const key = `${url}-${theme}-${maxwidth}-${maxheight}`;
Eif (!this.cache.has(key)) {
const sub = new Subject<Oembed | null>();
this.cache.set(key, sub.pipe(shareReplay(1)));
this.loading.push(() => this.oembeds.get(url, theme, maxwidth, maxheight).pipe(
catchError(() => of(null)),
).subscribe(o => {
sub.next(o);
runInAction(() => this.loading.shift());
Iif (this.loading.length) this.loading[0]!();
}));
Eif (this.loading.length === 1) this.loading[0]!();
}
return this.cache.get(key)!.pipe(
catchError(err => {
if (err.status === 404) {
this.cache.delete(key);
return of({ url } as Oembed);
}
return throwError(() => err);
}),
);
}
}
|