-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
44 lines (39 loc) · 1.14 KB
/
service.ts
File metadata and controls
44 lines (39 loc) · 1.14 KB
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
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class GithubButtonService {
private cached: { [url: string]: any } = {};
private _notify = new BehaviorSubject<{ [url: string]: any }>(null);
get notify() {
return this._notify.asObservable();
}
constructor(private http: HttpClient) {}
getKey(namespace: string, repo: string, query: string): string {
return `${namespace}_${repo}_${query}`;
}
req(namespace: string, repo: string, token: string, query: string): void {
const key = this.getKey(namespace, repo, query);
if (this.cached[key] === null) {
this._notify.next(this.cached[key]);
return;
}
this.cached[key] = null;
this.http
.post(
`https://api.github.com/graphql`,
{
query,
},
{
headers: {
// Authorization: `bearer ${token}`,
},
},
)
.subscribe((res: any) => {
this.cached[key] = { key, data: res.data };
this._notify.next(this.cached[key]);
});
}
}