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