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