Skip to content

Commit 5494a70

Browse files
committed
refactor into simpler code and API
1 parent 8077bb7 commit 5494a70

File tree

2 files changed

+85
-92
lines changed

2 files changed

+85
-92
lines changed

index.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
module.exports = function (fn, lever, opts) {
2-
var stress = 1
3-
var cycle
4-
var options = Object.assign({
5-
modifier: 1.20, timeout: 1000, min: 1, max: 0
6-
}, opts)
7-
var increase = function () {
8-
var s = stress * options.modifier
9-
return (s < options.max || !options.max)? s: options.max
10-
}
11-
var decrease = function () {
12-
var s = stress / options.modifier
13-
return (s > options.min)? s: options.min
14-
}
15-
var balance = function () {
16-
var s = stress
17-
stress = (lever())? increase(): decrease()
18-
return stress !== s
19-
}
20-
var end = function () {
21-
clearInterval(cycle)
22-
}
23-
var run = function () {
24-
var timeout = options.timeout * stress
25-
var runInterval = function () {
26-
fn({ stress: stress, timeout: timeout })
27-
if (balance()) run()
1+
module.exports = function (fn, relay, options = {}) {
2+
const {modifier = 1.20, timeout = 1000, min = 1, max = 0} = options
3+
let stress = 1
4+
let cycle
5+
function increase () {
6+
const nextStress = stress * modifier
7+
return (nextStress < max || !max) ? nextStress : max
288
}
29-
end()
30-
cycle = setInterval(runInterval, timeout)
31-
}
32-
run()
33-
return {
34-
end: end
35-
}
9+
function decrease () {
10+
const nextStress = stress / modifier
11+
return nextStress > min ? nextStress : min
12+
}
13+
function balance () {
14+
const prevStress = stress
15+
stress = relay() ? increase() : decrease()
16+
return prevStress !== stress
17+
}
18+
function start (nextTimeout) {
19+
cycle = setInterval(run, nextTimeout)
20+
}
21+
function run () {
22+
const nextTimeout = timeout * stress
23+
fn({stress, timeout: nextTimeout, cycle})
24+
if (balance()) {
25+
clear()
26+
start(nextTimeout)
27+
}
28+
}
29+
function clear () {
30+
if (cycle) {
31+
clearInterval(cycle)
32+
}
33+
}
34+
start()
35+
return clear
3636
}

test/cycle.js

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,62 @@ var test = require('tape')
22
var loop = require('../index.js')
33

44
test('cycle defaults', function (t) {
5-
t.plan(10)
6-
var runs = 0
7-
var busy = true
8-
var cycle
9-
var validate = function (f) {
10-
switch (runs) {
11-
case 0: t.equal(f.timeout, 1000); break
12-
case 1: t.equal(f.timeout, 1200); break
13-
case 2: t.equal(f.timeout, 1440); break
14-
case 3: t.equal(f.timeout, 1728); break
15-
case 4: t.equal(f.timeout, 2073.6); break
16-
case 5: t.equal(f.timeout, 1728); break
17-
case 6: t.equal(f.timeout, 1440); break
18-
case 7: t.equal(f.timeout, 1200); break
19-
case 8: t.equal(f.timeout, 1000); break
20-
case 9: t.equal(f.timeout, 1000); break
5+
t.plan(10)
6+
let runs = 0
7+
function validate (f) {
8+
switch (runs) {
9+
case 0: t.equal(f.timeout, 1000); break
10+
case 1: t.equal(f.timeout, 1200); break
11+
case 2: t.equal(f.timeout, 1440); break
12+
case 3: t.equal(f.timeout, 1728); break
13+
case 4: t.equal(f.timeout, 2073.6); break
14+
case 5: t.equal(f.timeout, 1728); break
15+
case 6: t.equal(f.timeout, 1440); break
16+
case 7: t.equal(f.timeout, 1200); break
17+
case 8: t.equal(f.timeout, 1000); break
18+
case 9: t.equal(f.timeout, 1000); break
19+
}
2120
}
22-
}
23-
var execute = function (f) {
24-
if (runs === 10) {
25-
cycle.end()
26-
t.end()
21+
function execute (f) {
22+
if (runs === 10) {
23+
clearLoop()
24+
t.end()
25+
}
26+
else {
27+
validate(f)
28+
runs++
29+
}
2730
}
28-
else {
29-
validate(f)
30-
runs++
31-
}
32-
}
33-
cycle = loop(execute, function () {
34-
return runs < 5
35-
})
31+
let clearLoop = loop(execute, () => runs < 5)
3632
})
3733

3834
test('cycle with max', function (t) {
39-
t.plan(10)
40-
var runs = 0
41-
var busy = true
42-
var cycle
43-
var validate = function (f) {
44-
switch (runs) {
45-
case 0: t.equal(f.timeout, 10); break
46-
case 1: t.equal(f.timeout, 20); break
47-
case 2: t.equal(f.timeout, 40); break
48-
case 3: t.equal(f.timeout, 40); break
49-
case 4: t.equal(f.timeout, 40); break
50-
case 5: t.equal(f.timeout, 20); break
51-
case 6: t.equal(f.timeout, 10); break
52-
case 7: t.equal(f.timeout, 10); break
53-
case 8: t.equal(f.timeout, 10); break
54-
case 9: t.equal(f.timeout, 10); break
55-
}
56-
}
57-
var execute = function (f) {
58-
if (runs === 10) {
59-
cycle.end()
60-
t.end()
35+
t.plan(10)
36+
let runs = 0
37+
function validate (f) {
38+
switch (runs) {
39+
case 0: t.equal(f.timeout, 10); break
40+
case 1: t.equal(f.timeout, 20); break
41+
case 2: t.equal(f.timeout, 40); break
42+
case 3: t.equal(f.timeout, 40); break
43+
case 4: t.equal(f.timeout, 40); break
44+
case 5: t.equal(f.timeout, 20); break
45+
case 6: t.equal(f.timeout, 10); break
46+
case 7: t.equal(f.timeout, 10); break
47+
case 8: t.equal(f.timeout, 10); break
48+
case 9: t.equal(f.timeout, 10); break
49+
}
6150
}
62-
else {
63-
validate(f)
64-
runs++
51+
function execute (f) {
52+
if (runs === 10) {
53+
clearInterval(f.cycle)
54+
t.end()
55+
}
56+
else {
57+
validate(f)
58+
runs++
59+
}
6560
}
66-
}
67-
cycle = loop(execute, function () {
68-
return runs < 5
69-
}, { timeout: 10, max: 4, modifier: 2 })
61+
const options = {timeout: 10, max: 4, modifier: 2}
62+
loop(execute, () => runs < 5, options)
7063
})

0 commit comments

Comments
 (0)