1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var tslib = require('tslib');
|
6 | var apolloLink = require('apollo-link');
|
7 |
|
8 | var DedupLink = (function (_super) {
|
9 | tslib.__extends(DedupLink, _super);
|
10 | function DedupLink() {
|
11 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
12 | _this.inFlightRequestObservables = new Map();
|
13 | _this.subscribers = new Map();
|
14 | return _this;
|
15 | }
|
16 | DedupLink.prototype.request = function (operation, forward) {
|
17 | var _this = this;
|
18 | if (operation.getContext().forceFetch) {
|
19 | return forward(operation);
|
20 | }
|
21 | var key = operation.toKey();
|
22 | if (!this.inFlightRequestObservables.get(key)) {
|
23 | var singleObserver_1 = forward(operation);
|
24 | var subscription_1;
|
25 | var sharedObserver = new apolloLink.Observable(function (observer) {
|
26 | if (!_this.subscribers.has(key))
|
27 | _this.subscribers.set(key, new Set());
|
28 | _this.subscribers.get(key).add(observer);
|
29 | if (!subscription_1) {
|
30 | subscription_1 = singleObserver_1.subscribe({
|
31 | next: function (result) {
|
32 | var subscribers = _this.subscribers.get(key);
|
33 | _this.subscribers.delete(key);
|
34 | _this.inFlightRequestObservables.delete(key);
|
35 | if (subscribers) {
|
36 | subscribers.forEach(function (obs) { return obs.next(result); });
|
37 | subscribers.forEach(function (obs) { return obs.complete(); });
|
38 | }
|
39 | },
|
40 | error: function (error) {
|
41 | var subscribers = _this.subscribers.get(key);
|
42 | _this.subscribers.delete(key);
|
43 | _this.inFlightRequestObservables.delete(key);
|
44 | if (subscribers) {
|
45 | subscribers.forEach(function (obs) { return obs.error(error); });
|
46 | }
|
47 | },
|
48 | });
|
49 | }
|
50 | return function () {
|
51 | if (_this.subscribers.has(key)) {
|
52 | _this.subscribers.get(key).delete(observer);
|
53 | if (_this.subscribers.get(key).size === 0) {
|
54 | _this.inFlightRequestObservables.delete(key);
|
55 | if (subscription_1)
|
56 | subscription_1.unsubscribe();
|
57 | }
|
58 | }
|
59 | };
|
60 | });
|
61 | this.inFlightRequestObservables.set(key, sharedObserver);
|
62 | }
|
63 | return this.inFlightRequestObservables.get(key);
|
64 | };
|
65 | return DedupLink;
|
66 | }(apolloLink.ApolloLink));
|
67 |
|
68 | exports.DedupLink = DedupLink;
|
69 |
|