Skip to content

Commit c5fe940

Browse files
committed
Merge branch 'develop' into scales-configurable
2 parents d22ac38 + 751318c commit c5fe940

File tree

5 files changed

+60
-60
lines changed

5 files changed

+60
-60
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
|Master |[![Build Status](https://travis-ci.com/nut-tree/nut.js.svg?branch=master)](https://travis-ci.com/nut-tree/nut.js)|[![Build status](https://ci.appveyor.com/api/projects/status/iohwxc5t46gcuvby/branch/master?svg=true)](https://ci.appveyor.com/project/s1hofmann/nut-js/branch/master)|
66
|Develop|[![Build Status](https://travis-ci.com/nut-tree/nut.js.svg?branch=develop)](https://travis-ci.com/nut-tree/nut.js)|[![Build status](https://ci.appveyor.com/api/projects/status/iohwxc5t46gcuvby/branch/develop?svg=true)](https://ci.appveyor.com/project/s1hofmann/nut-js/branch/develop)|
77

8-
8+
![Supported node versions](https://img.shields.io/badge/node-v10.x%20--%20v14.x-green)
99
[![SonarCloud badge](https://sonarcloud.io/api/project_badges/measure?project=nut-tree%3Anut.js&metric=alert_status)](https://sonarcloud.io/dashboard?id=nut-tree%3Anut.js)
1010
[![SonarCloud Coverage](https://sonarcloud.io/api/project_badges/measure?project=nut-tree%3Anut.js&metric=coverage)](https://sonarcloud.io/component_measures?id=nut-tree%3Anut.js&metric=coverage)
1111

@@ -35,6 +35,9 @@ but additionally gives you the possibility to navigate the screen based on image
3535

3636
[nut-tree/trailmix](https://github.com/nut-tree/trailmix) contains a set of ready to use examples which demo the usage ot nut.js.
3737

38+
# Discussion
39+
40+
In [nut-tree/rfc](https://github.com/nut-tree/rfc) documents regarding larger design / implementation changes in nut.js are up for discussion.
3841
# Modules
3942

4043
This list gives an overview on currently implemented and planned functionality.

lib/provider/opencv/find-edges.function.spec.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

lib/provider/opencv/find-edges.function.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/util/poll-action.function.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,25 @@ describe("poll-action", () => {
118118
expect(action).toBeCalledTimes(1);
119119
expect((end - start)).toBeLessThan(updateInterval);
120120
});
121+
122+
it("should fail if action does not resolve within timeout", async () => {
123+
// GIVEN
124+
const updateInterval = 100;
125+
const maxDuration = 200;
126+
const action = jest.fn(() => {
127+
return new Promise((_, reject) => {
128+
setTimeout(() => reject(), 300);
129+
})
130+
});
131+
132+
// WHEN
133+
try {
134+
await timeout(updateInterval, maxDuration, action);
135+
} catch (e) {
136+
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
137+
}
138+
139+
// THEN
140+
expect(action).toBeCalledTimes(1);
141+
});
121142
});

lib/util/poll-action.function.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
export function timeout<R>(updateIntervalMs: number, maxDurationMs: number, action: (...params: any) => Promise<R>): Promise<R> {
22
return new Promise<R>((resolve, reject) => {
33
let interval: NodeJS.Timeout;
4-
const maxTimeout = setTimeout(
5-
() => {
6-
clearTimeout(maxTimeout);
7-
if (interval) {
8-
clearTimeout(interval);
9-
}
10-
reject(`Action timed out after ${maxDurationMs} ms`);
11-
},
12-
maxDurationMs
13-
);
14-
const startInterval = () => {
15-
interval = setTimeout(function intervalFunc() {
16-
action().then((result) => {
17-
if (!result) {
18-
interval = setTimeout(intervalFunc, updateIntervalMs);
19-
} else {
20-
clearTimeout(maxTimeout);
21-
clearTimeout(interval);
22-
resolve(result);
23-
}
24-
}).catch(() => {
25-
interval = setTimeout(intervalFunc, updateIntervalMs);
26-
});
27-
}, updateIntervalMs);
28-
};
4+
let timerCleaned = false
5+
6+
function executeInterval() {
7+
action().then(validateResult).catch(handleRejection);
8+
}
299

30-
action().then((result) => {
10+
function validateResult(result: R){
3111
if (!result) {
32-
startInterval();
12+
interval = setTimeout(executeInterval, updateIntervalMs);
3313
} else {
34-
clearTimeout(maxTimeout);
14+
cleanupTimer();
3515
resolve(result);
3616
}
37-
}).catch(() => {
38-
startInterval();
39-
});
17+
}
18+
19+
function handleRejection() {
20+
if(!timerCleaned){
21+
interval = setTimeout(executeInterval, updateIntervalMs);
22+
}
23+
}
24+
25+
function cleanupTimer(){
26+
timerCleaned = true
27+
if(maxTimeout){
28+
clearTimeout(maxTimeout);
29+
}
30+
if(interval){
31+
clearTimeout(interval);
32+
}
33+
}
34+
35+
const maxTimeout = setTimeout(
36+
() => {
37+
cleanupTimer();
38+
reject(`Action timed out after ${maxDurationMs} ms`);
39+
},
40+
maxDurationMs
41+
);
42+
43+
executeInterval()
4044
});
4145
}

0 commit comments

Comments
 (0)