Skip to content

Commit 22df7d4

Browse files
refactor!: return an object instead of throw error when timeout exceeded (#13)
1 parent 98102a8 commit 22df7d4

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

src/__tests__/conrec.test.js

+39-22
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,77 @@ const parsed = convert(data, { noContour: true }).flatten[0];
99
describe('conrec basic test', () => {
1010
it('no result because level too far', () => {
1111
const conrec = new Conrec(parsed.minMax.z);
12-
const basic = conrec.drawContour({
12+
const { contours, timeout } = conrec.drawContour({
1313
contourDrawer: 'basic',
1414
levels: [-1000000000, 1000000000],
1515
timeout: 10000,
1616
});
17-
18-
expect(basic).toStrictEqual([
17+
expect(timeout).toBeFalsy();
18+
expect(contours).toStrictEqual([
1919
{ lines: [], zValue: -1000000000 },
2020
{ lines: [], zValue: 1000000000 },
2121
]);
2222
});
2323

2424
it('2 specified levels', () => {
2525
const conrec = new Conrec(parsed.minMax.z);
26-
const basic = conrec.drawContour({
26+
const { contours, timeout } = conrec.drawContour({
2727
contourDrawer: 'basic',
2828
levels: [-100000, 100000],
2929
timeout: 10000,
3030
});
31-
32-
expect(basic).toHaveLength(2);
33-
expect(basic[0].lines).toHaveLength(36864);
34-
expect(basic[1].lines).toHaveLength(119720);
31+
expect(timeout).toBeFalsy();
32+
expect(contours).toHaveLength(2);
33+
expect(contours[0].lines).toHaveLength(36864);
34+
expect(contours[1].lines).toHaveLength(119720);
3535
});
3636

3737
it('auto select levels', () => {
3838
const conrec = new Conrec(parsed.minMax.z);
39-
const basic = conrec.drawContour({
39+
const { contours, timeout } = conrec.drawContour({
4040
contourDrawer: 'basic',
4141
nbLevels: 10,
4242
timeout: 10000,
4343
});
4444

45-
expect(basic).toHaveLength(10);
46-
expect(basic[0].lines).toHaveLength(0);
47-
expect(basic[1].lines).toHaveLength(4984);
48-
expect(basic[8].lines).toHaveLength(32);
49-
expect(basic[8].lines).toMatchSnapshot();
50-
expect(basic[9].lines).toHaveLength(0);
45+
expect(timeout).toBeFalsy();
46+
expect(contours).toHaveLength(10);
47+
expect(contours[0].lines).toHaveLength(0);
48+
expect(contours[1].lines).toHaveLength(4984);
49+
expect(contours[8].lines).toHaveLength(32);
50+
expect(contours[8].lines).toMatchSnapshot();
51+
expect(contours[9].lines).toHaveLength(0);
5152
});
5253

5354
it('auto select levels with swapAxes', () => {
5455
const conrec = new Conrec(parsed.minMax.z, { swapAxes: true });
55-
const basic = conrec.drawContour({
56+
const { contours, timeout } = conrec.drawContour({
5657
contourDrawer: 'basic',
5758
nbLevels: 10,
5859
timeout: 10000,
5960
});
6061

61-
expect(basic).toHaveLength(10);
62-
expect(basic[0].lines).toHaveLength(0);
63-
expect(basic[1].lines).toHaveLength(4984);
64-
expect(basic[8].lines).toHaveLength(32);
65-
expect(basic[8].lines).toMatchSnapshot();
66-
expect(basic[9].lines).toHaveLength(0);
62+
expect(timeout).toBeFalsy();
63+
expect(contours).toHaveLength(10);
64+
expect(contours[0].lines).toHaveLength(0);
65+
expect(contours[1].lines).toHaveLength(4984);
66+
expect(contours[8].lines).toHaveLength(32);
67+
expect(contours[8].lines).toMatchSnapshot();
68+
expect(contours[9].lines).toHaveLength(0);
69+
});
70+
71+
it('return available contours within 100ms', () => {
72+
const conrec = new Conrec(parsed.minMax.z);
73+
const { contours, timeout } = conrec.drawContour({
74+
contourDrawer: 'basic',
75+
nbLevels: 10,
76+
timeout: 100,
77+
});
78+
expect(timeout).toBeTruthy();
79+
expect(contours).toHaveLength(10);
80+
expect(contours[0].lines).toHaveLength(0);
81+
expect(contours[1].lines.length).toBeLessThan(2000);
82+
expect(contours[8].lines).toHaveLength(0);
83+
expect(contours[9].lines).toHaveLength(0);
6784
});
6885
});

src/__tests__/shape.test.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ describe('shape', () => {
1111
`);
1212

1313
const conrec = new Conrec(matrix);
14-
const shape = conrec.drawContour({
14+
const { contours, timeout } = conrec.drawContour({
1515
contourDrawer: 'shape',
1616
levels: [0.5],
1717
});
18-
expect(shape[0]).toHaveLength(25);
18+
expect(timeout).toBeFalsy();
19+
expect(contours[0]).toHaveLength(25);
1920
});
2021

2122
it('2 squares', () => {
@@ -28,12 +29,13 @@ describe('shape', () => {
2829
`);
2930

3031
const conrec = new Conrec(matrix);
31-
const shape = conrec.drawContour({
32+
const { contours, timeout } = conrec.drawContour({
3233
contourDrawer: 'shape',
3334
levels: [0.5],
3435
});
3536

36-
expect(shape[0]).toHaveLength(25);
37+
expect(timeout).toBeFalsy();
38+
expect(contours[0]).toHaveLength(25);
3739
});
3840

3941
it('2 diagonal squares', () => {
@@ -49,12 +51,12 @@ describe('shape', () => {
4951
`);
5052

5153
const conrec = new Conrec(matrix);
52-
const shape = conrec.drawContour({
54+
const { contours, timeout } = conrec.drawContour({
5355
contourDrawer: 'shape',
5456
levels: [0.5],
5557
});
56-
57-
expect(shape[0]).toHaveLength(49);
58+
expect(timeout).toBeFalsy();
59+
expect(contours[0]).toHaveLength(49);
5860
});
5961
});
6062

src/calculateContour.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ const MINUSEPSILON = 0 - EPSILON;
9999
* @param {number} [options.iub] - index bounds of data matrix
100100
* @param {number} [options.jlb] - index bounds of data matrix
101101
* @param {number} [options.jub] - index bounds of data matrix
102+
* @returns {boolean} - Whether contour generation had to stop early because it reached the timeout
102103
*/
103104
export function calculateContour(matrix, x, y, z, contourDrawer, options = {}) {
104105
const {
@@ -117,13 +118,16 @@ export function calculateContour(matrix, x, y, z, contourDrawer, options = {}) {
117118
const znc1 = z[nc - 1];
118119

119120
const start = Date.now();
121+
120122
/** private */
121123
function xsect(p1, p2) {
122124
return (h[p2] * xh[p1] - h[p1] * xh[p2]) / (h[p2] - h[p1]);
123125
}
126+
124127
function ysect(p1, p2) {
125128
return (h[p2] * yh[p1] - h[p1] * yh[p2]) / (h[p2] - h[p1]);
126129
}
130+
127131
let m1;
128132
let m2;
129133
let m3;
@@ -160,9 +164,8 @@ export function calculateContour(matrix, x, y, z, contourDrawer, options = {}) {
160164
// for (let j = jlb; j < jub; j++) {
161165
for (let j = jub - 1; j >= jlb; j--) {
162166
if (timeout && Date.now() - start > timeout) {
163-
throw new Error(
164-
`timeout: contour generation could not finish in less than ${timeout}ms`,
165-
);
167+
// `timeout: contour generation could not finish in less than ${timeout}ms`
168+
return true;
166169
}
167170
for (let i = ilb; i < iub; i++) {
168171
let dij = matrix[i][j];
@@ -319,4 +322,5 @@ export function calculateContour(matrix, x, y, z, contourDrawer, options = {}) {
319322
}
320323
}
321324
}
325+
return false;
322326
}

src/index.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,22 @@ export class Conrec {
4040
this.hasMinMax = false;
4141
}
4242

43+
/**
44+
* @typedef {Object} Output
45+
* @property {any} contours
46+
* @property {boolean} timeout - Whether contour generation had to stop early because it reached the timeout
47+
*/
48+
4349
/**
4450
*
4551
* @param {number[]} [options.levels]
4652
* @param {number} [options.nbLevels=10]
4753
* @param {string} [options.contourDrawer='basic'] - 'basic' or 'shape'
4854
* @param {number} [options.timeout=0]
49-
* @return {any}
55+
* @return {Output}
5056
*/
5157
drawContour(options) {
52-
options = Object.assign({}, defaultOptions, options);
58+
options = { ...defaultOptions, ...options };
5359

5460
let levels;
5561
if (options.levels) {
@@ -73,11 +79,18 @@ export class Conrec {
7379
} else {
7480
throw new TypeError('contourDrawer must be a string');
7581
}
82+
const isTimeout = calculateContour(
83+
this.matrix,
84+
this.xs,
85+
this.ys,
86+
levels,
87+
contourDrawer,
88+
{
89+
timeout: options.timeout,
90+
},
91+
);
7692

77-
calculateContour(this.matrix, this.xs, this.ys, levels, contourDrawer, {
78-
timeout: options.timeout,
79-
});
80-
return contourDrawer.getContour();
93+
return { contours: contourDrawer.getContour(), timeout: isTimeout };
8194
}
8295

8396
_computeMinMax() {

0 commit comments

Comments
 (0)