This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 347
/
dedupLink.ts
82 lines (72 loc) · 2.46 KB
/
dedupLink.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
ApolloLink,
Operation,
NextLink,
FetchResult,
Observable,
} from 'apollo-link';
/*
* Expects context to contain the forceFetch field if no dedup
*/
export class DedupLink extends ApolloLink {
private inFlightRequestObservables: Map<
string,
Observable<FetchResult>
> = new Map();
private subscribers: Map<string, any> = new Map();
public request(
operation: Operation,
forward: NextLink,
): Observable<FetchResult> {
// sometimes we might not want to deduplicate a request, for example when we want to force fetch it.
if (operation.getContext().forceFetch) {
return forward(operation);
}
const key = operation.toKey();
if (!this.inFlightRequestObservables.get(key)) {
// this is a new request, i.e. we haven't deduplicated it yet
// call the next link
const singleObserver = forward(operation);
let subscription;
const sharedObserver = new Observable(observer => {
// this will still be called by each subscriber regardless of
// deduplication status
if (!this.subscribers.has(key)) this.subscribers.set(key, new Set());
this.subscribers.get(key).add(observer);
if (!subscription) {
subscription = singleObserver.subscribe({
next: result => {
const subscribers = this.subscribers.get(key);
this.subscribers.delete(key);
this.inFlightRequestObservables.delete(key);
if (subscribers) {
subscribers.forEach(obs => obs.next(result));
subscribers.forEach(obs => obs.complete());
}
},
error: error => {
const subscribers = this.subscribers.get(key);
this.subscribers.delete(key);
this.inFlightRequestObservables.delete(key);
if (subscribers) {
subscribers.forEach(obs => obs.error(error));
}
},
});
}
return () => {
if (this.subscribers.has(key)) {
this.subscribers.get(key).delete(observer);
if (this.subscribers.get(key).size === 0) {
this.inFlightRequestObservables.delete(key);
if (subscription) subscription.unsubscribe();
}
}
};
});
this.inFlightRequestObservables.set(key, sharedObserver);
}
// return shared Observable
return this.inFlightRequestObservables.get(key);
}
}