UNPKG

5.33 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { Observable } from "../../utilities/index.js";
3var OperationBatcher = (function () {
4 function OperationBatcher(_a) {
5 var batchDebounce = _a.batchDebounce, batchInterval = _a.batchInterval, batchMax = _a.batchMax, batchHandler = _a.batchHandler, batchKey = _a.batchKey;
6 this.batchesByKey = new Map();
7 this.batchDebounce = batchDebounce;
8 this.batchInterval = batchInterval;
9 this.batchMax = batchMax || 0;
10 this.batchHandler = batchHandler;
11 this.batchKey = batchKey || (function () { return ''; });
12 }
13 OperationBatcher.prototype.enqueueRequest = function (request) {
14 var _this = this;
15 var requestCopy = __assign(__assign({}, request), { next: [], error: [], complete: [], subscribers: new Set() });
16 var key = this.batchKey(request.operation);
17 if (!requestCopy.observable) {
18 requestCopy.observable = new Observable(function (observer) {
19 var batch = _this.batchesByKey.get(key);
20 if (!batch)
21 _this.batchesByKey.set(key, batch = new Set());
22 var isFirstEnqueuedRequest = batch.size === 0;
23 var isFirstSubscriber = requestCopy.subscribers.size === 0;
24 requestCopy.subscribers.add(observer);
25 if (isFirstSubscriber) {
26 batch.add(requestCopy);
27 }
28 if (observer.next) {
29 requestCopy.next.push(observer.next.bind(observer));
30 }
31 if (observer.error) {
32 requestCopy.error.push(observer.error.bind(observer));
33 }
34 if (observer.complete) {
35 requestCopy.complete.push(observer.complete.bind(observer));
36 }
37 if (isFirstEnqueuedRequest) {
38 _this.scheduleQueueConsumption(key);
39 }
40 else if (_this.batchDebounce) {
41 clearTimeout(_this.scheduledBatchTimer);
42 _this.scheduleQueueConsumption(key);
43 }
44 if (batch.size === _this.batchMax) {
45 _this.consumeQueue(key);
46 }
47 return function () {
48 var _a;
49 if (requestCopy.subscribers.delete(observer) &&
50 requestCopy.subscribers.size < 1) {
51 if (batch.delete(requestCopy) && batch.size < 1) {
52 _this.consumeQueue(key);
53 (_a = batch.subscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
54 }
55 }
56 };
57 });
58 }
59 return requestCopy.observable;
60 };
61 OperationBatcher.prototype.consumeQueue = function (key) {
62 if (key === void 0) { key = ''; }
63 var batch = this.batchesByKey.get(key);
64 this.batchesByKey.delete(key);
65 if (!batch || !batch.size) {
66 return;
67 }
68 var operations = [];
69 var forwards = [];
70 var observables = [];
71 var nexts = [];
72 var errors = [];
73 var completes = [];
74 batch.forEach(function (request) {
75 operations.push(request.operation);
76 forwards.push(request.forward);
77 observables.push(request.observable);
78 nexts.push(request.next);
79 errors.push(request.error);
80 completes.push(request.complete);
81 });
82 var batchedObservable = this.batchHandler(operations, forwards) || Observable.of();
83 var onError = function (error) {
84 errors.forEach(function (rejecters) {
85 if (rejecters) {
86 rejecters.forEach(function (e) { return e(error); });
87 }
88 });
89 };
90 batch.subscription = batchedObservable.subscribe({
91 next: function (results) {
92 if (!Array.isArray(results)) {
93 results = [results];
94 }
95 if (nexts.length !== results.length) {
96 var error = new Error("server returned results with length ".concat(results.length, ", expected length of ").concat(nexts.length));
97 error.result = results;
98 return onError(error);
99 }
100 results.forEach(function (result, index) {
101 if (nexts[index]) {
102 nexts[index].forEach(function (next) { return next(result); });
103 }
104 });
105 },
106 error: onError,
107 complete: function () {
108 completes.forEach(function (complete) {
109 if (complete) {
110 complete.forEach(function (c) { return c(); });
111 }
112 });
113 },
114 });
115 return observables;
116 };
117 OperationBatcher.prototype.scheduleQueueConsumption = function (key) {
118 var _this = this;
119 this.scheduledBatchTimer = setTimeout(function () {
120 _this.consumeQueue(key);
121 }, this.batchInterval);
122 };
123 return OperationBatcher;
124}());
125export { OperationBatcher };
126//# sourceMappingURL=batching.js.map
\No newline at end of file