UNPKG

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