UNPKG

6.73 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var tslib = require('tslib');
6var core = require('../core');
7var utilities = require('../../utilities');
8
9var OperationBatcher = (function () {
10 function OperationBatcher(_a) {
11 var batchDebounce = _a.batchDebounce, batchInterval = _a.batchInterval, batchMax = _a.batchMax, batchHandler = _a.batchHandler, batchKey = _a.batchKey;
12 this.batchesByKey = new Map();
13 this.scheduledBatchTimerByKey = new Map();
14 this.batchDebounce = batchDebounce;
15 this.batchInterval = batchInterval;
16 this.batchMax = batchMax || 0;
17 this.batchHandler = batchHandler;
18 this.batchKey = batchKey || (function () { return ''; });
19 }
20 OperationBatcher.prototype.enqueueRequest = function (request) {
21 var _this = this;
22 var requestCopy = tslib.__assign(tslib.__assign({}, request), { next: [], error: [], complete: [], subscribers: new Set() });
23 var key = this.batchKey(request.operation);
24 if (!requestCopy.observable) {
25 requestCopy.observable = new utilities.Observable(function (observer) {
26 var batch = _this.batchesByKey.get(key);
27 if (!batch)
28 _this.batchesByKey.set(key, batch = new Set());
29 var isFirstEnqueuedRequest = batch.size === 0;
30 var isFirstSubscriber = requestCopy.subscribers.size === 0;
31 requestCopy.subscribers.add(observer);
32 if (isFirstSubscriber) {
33 batch.add(requestCopy);
34 }
35 if (observer.next) {
36 requestCopy.next.push(observer.next.bind(observer));
37 }
38 if (observer.error) {
39 requestCopy.error.push(observer.error.bind(observer));
40 }
41 if (observer.complete) {
42 requestCopy.complete.push(observer.complete.bind(observer));
43 }
44 if (isFirstEnqueuedRequest || _this.batchDebounce) {
45 _this.scheduleQueueConsumption(key);
46 }
47 if (batch.size === _this.batchMax) {
48 _this.consumeQueue(key);
49 }
50 return function () {
51 var _a;
52 if (requestCopy.subscribers.delete(observer) &&
53 requestCopy.subscribers.size < 1) {
54 if (batch.delete(requestCopy) && batch.size < 1) {
55 _this.consumeQueue(key);
56 (_a = batch.subscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
57 }
58 }
59 };
60 });
61 }
62 return requestCopy.observable;
63 };
64 OperationBatcher.prototype.consumeQueue = function (key) {
65 if (key === void 0) { key = ''; }
66 var batch = this.batchesByKey.get(key);
67 this.batchesByKey.delete(key);
68 if (!batch || !batch.size) {
69 return;
70 }
71 var operations = [];
72 var forwards = [];
73 var observables = [];
74 var nexts = [];
75 var errors = [];
76 var completes = [];
77 batch.forEach(function (request) {
78 operations.push(request.operation);
79 forwards.push(request.forward);
80 observables.push(request.observable);
81 nexts.push(request.next);
82 errors.push(request.error);
83 completes.push(request.complete);
84 });
85 var batchedObservable = this.batchHandler(operations, forwards) || utilities.Observable.of();
86 var onError = function (error) {
87 errors.forEach(function (rejecters) {
88 if (rejecters) {
89 rejecters.forEach(function (e) { return e(error); });
90 }
91 });
92 };
93 batch.subscription = batchedObservable.subscribe({
94 next: function (results) {
95 if (!Array.isArray(results)) {
96 results = [results];
97 }
98 if (nexts.length !== results.length) {
99 var error = new Error("server returned results with length ".concat(results.length, ", expected length of ").concat(nexts.length));
100 error.result = results;
101 return onError(error);
102 }
103 results.forEach(function (result, index) {
104 if (nexts[index]) {
105 nexts[index].forEach(function (next) { return next(result); });
106 }
107 });
108 },
109 error: onError,
110 complete: function () {
111 completes.forEach(function (complete) {
112 if (complete) {
113 complete.forEach(function (c) { return c(); });
114 }
115 });
116 },
117 });
118 return observables;
119 };
120 OperationBatcher.prototype.scheduleQueueConsumption = function (key) {
121 var _this = this;
122 clearTimeout(this.scheduledBatchTimerByKey.get(key));
123 this.scheduledBatchTimerByKey.set(key, setTimeout(function () {
124 _this.consumeQueue(key);
125 _this.scheduledBatchTimerByKey.delete(key);
126 }, this.batchInterval));
127 };
128 return OperationBatcher;
129}());
130
131var BatchLink = (function (_super) {
132 tslib.__extends(BatchLink, _super);
133 function BatchLink(fetchParams) {
134 var _this = _super.call(this) || this;
135 var _a = fetchParams || {}, batchDebounce = _a.batchDebounce, _b = _a.batchInterval, batchInterval = _b === void 0 ? 10 : _b, _c = _a.batchMax, batchMax = _c === void 0 ? 0 : _c, _d = _a.batchHandler, batchHandler = _d === void 0 ? function () { return null; } : _d, _e = _a.batchKey, batchKey = _e === void 0 ? function () { return ''; } : _e;
136 _this.batcher = new OperationBatcher({
137 batchDebounce: batchDebounce,
138 batchInterval: batchInterval,
139 batchMax: batchMax,
140 batchHandler: batchHandler,
141 batchKey: batchKey,
142 });
143 if (fetchParams.batchHandler.length <= 1) {
144 _this.request = function (operation) { return _this.batcher.enqueueRequest({ operation: operation }); };
145 }
146 return _this;
147 }
148 BatchLink.prototype.request = function (operation, forward) {
149 return this.batcher.enqueueRequest({
150 operation: operation,
151 forward: forward,
152 });
153 };
154 return BatchLink;
155}(core.ApolloLink));
156
157exports.BatchLink = BatchLink;
158exports.OperationBatcher = OperationBatcher;
159//# sourceMappingURL=batch.cjs.map