Skip to content

Commit 961ce87

Browse files
authored
Fix MultiRequest to return instance. Add unit tests. (#80)
1 parent 95d2ab5 commit 961ce87

File tree

4 files changed

+127
-32
lines changed

4 files changed

+127
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.1.2 - 2020-12-09
2+
- [41969](https://www.labkey.org/home/Developer/issues/issues-details.view?issueId=41969): Fix MultiRequest to return instance. Add unit tests.
3+
14
## 1.1.1 - 2020-09-18
25
- Add additional properties exposed via `getServerContext()` to typings
36

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@labkey/api",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "JavaScript client API for LabKey Server",
55
"scripts": {
66
"build": "npm run build:dist && npm run build:docs",

src/labkey/MultiRequest.spec.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2020 LabKey Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { MultiRequest } from './MultiRequest';
17+
18+
describe('MultiRequest', () => {
19+
it('handles array of requests and add', (done) => {
20+
// Arrange
21+
const scope = {};
22+
let sum = 0;
23+
function asyncMethod(config: any) {
24+
sum += config.x;
25+
setTimeout(config.success, config.timeout);
26+
}
27+
28+
const requests = [
29+
[ asyncMethod, { x: 1, timeout: 25 } ],
30+
[ asyncMethod, { x: 2, timeout: 50 } ],
31+
[ asyncMethod, { x: 3, timeout: 75 } ],
32+
[ asyncMethod, { x: 12, timeout: 100 } ],
33+
];
34+
35+
// @ts-ignore
36+
// Calculate the sum prior to mutating the requests
37+
const expectedSum = requests.reduce((total, req) => total + req[1].x, 0);
38+
39+
// Pop the last request for testing add()
40+
const [lastRequestFn, lastRequestConfig] = requests.pop();
41+
42+
function onSend() {
43+
// Assert
44+
expect(sum).toEqual(expectedSum);
45+
expect(this).toEqual(scope);
46+
done();
47+
}
48+
49+
// @ts-ignore
50+
const multi = new MultiRequest(requests);
51+
52+
// Apply the last request using .add()
53+
multi.add(lastRequestFn, lastRequestConfig, scope);
54+
55+
// Act
56+
multi.send(onSend, scope);
57+
});
58+
it('supports listeners config', (done) => {
59+
// Arrange
60+
const scope = {};
61+
let sum = 0;
62+
function asyncMethod(config: any) {
63+
sum += config.x;
64+
setTimeout(config.success, config.timeout);
65+
}
66+
67+
const requests = [
68+
[ asyncMethod, { x: 13, timeout: 25 } ],
69+
[ asyncMethod, { x: 5, timeout: 50 } ],
70+
[ asyncMethod, { x: 71, timeout: 75 } ],
71+
];
72+
73+
// @ts-ignore
74+
const expectedSum = requests.reduce((total, req) => total + req[1].x, 0);
75+
76+
function onSend() {
77+
// Assert
78+
expect(sum).toEqual(expectedSum);
79+
expect(this).toEqual(scope);
80+
done();
81+
}
82+
83+
// Act
84+
// @ts-ignore
85+
new MultiRequest({
86+
requests,
87+
listeners: { done: { fn: onSend }, scope }
88+
});
89+
});
90+
});

src/labkey/MultiRequest.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,11 @@ import { getOnFailure, getOnSuccess, isArray } from './Utils'
8484
* ```
8585
*/
8686
export const MultiRequest = function(config: any) {
87-
config = config || {};
88-
89-
let doneCallbacks: Array<any> = [];
90-
let listeners;
91-
let requests;
87+
let doneCallbacks: any[] = [];
9288
let self = this;
9389
let sending = false;
94-
let sendQ: Array<any> = [];
95-
let waitQ: Array<any> = [];
96-
97-
if (isArray(config)) {
98-
requests = config;
99-
}
100-
else {
101-
requests = config.requests;
102-
listeners = config.listeners;
103-
}
104-
105-
if (requests) {
106-
for (let i = 0; i < requests.length; i++) {
107-
let request = requests[i];
108-
this.add(request[0], request[1]);
109-
}
110-
}
111-
112-
if (listeners && listeners.done) {
113-
applyCallback(listeners.done, listeners.scope);
114-
}
115-
116-
if (waitQ.length && doneCallbacks.length > 0) {
117-
this.send();
118-
}
90+
let sendQ: any[] = [];
91+
let waitQ: any[] = [];
11992

12093
function applyCallback(callback: any, scope: any) {
12194
if (typeof callback == 'function') {
@@ -124,7 +97,7 @@ export const MultiRequest = function(config: any) {
12497
scope
12598
});
12699
}
127-
else if (typeof callback.fn == 'function') {
100+
else if (callback && typeof callback.fn == 'function') {
128101
doneCallbacks.push({
129102
fn: callback.fn,
130103
scope: callback.scope || scope
@@ -234,4 +207,33 @@ export const MultiRequest = function(config: any) {
234207

235208
applyCallback(callback, scope);
236209
};
210+
211+
const cfg = config || {};
212+
let listeners;
213+
let requests;
214+
215+
if (isArray(cfg)) {
216+
requests = cfg;
217+
}
218+
else {
219+
requests = cfg.requests;
220+
listeners = cfg.listeners;
221+
}
222+
223+
if (requests) {
224+
for (let i = 0; i < requests.length; i++) {
225+
let request = requests[i];
226+
this.add(request[0], request[1]);
227+
}
228+
}
229+
230+
if (listeners && listeners.done) {
231+
applyCallback(listeners.done, listeners.scope);
232+
}
233+
234+
if (waitQ.length && doneCallbacks.length > 0) {
235+
this.send();
236+
}
237+
238+
return this;
237239
};

0 commit comments

Comments
 (0)