UNPKG

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