Skip to content

Commit f78b17f

Browse files
committed
refactor and remove coverage dependencies
2 parents 8db719a + 91860e7 commit f78b17f

File tree

6 files changed

+115
-2507
lines changed

6 files changed

+115
-2507
lines changed

.travis.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
language: node_js
22
node_js:
3-
- '9'
4-
- '8'
5-
- '7'
6-
- '6'
73
- 'lts/*'
84
- 'node'
95
branches:
106
only:
11-
- master
12-
after_script:
13-
- npm run report
7+
- master

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Elastic Loop
2-
[![Build Status](https://travis-ci.org/grindcode/elastic-loop.svg?branch=master)](https://travis-ci.org/grindcode/elastic-loop) [![Test Coverage](https://codeclimate.com/github/grindcode/elastic-loop/badges/coverage.svg)](https://codeclimate.com/github/grindcode/elastic-loop/coverage) [![Dependency Status](https://david-dm.org/grindcode/elastic-loop.svg)](https://david-dm.org/grindcode/elastic-loop) [![devDependency Status](https://david-dm.org/grindcode/elastic-loop/dev-status.svg)](https://david-dm.org/grindcode/elastic-loop#info=devDependencies)
2+
[![Build Status](https://travis-ci.org/grindcode/elastic-loop.svg?branch=master)](https://travis-ci.org/grindcode/elastic-loop) [![Dependency Status](https://david-dm.org/grindcode/elastic-loop.svg)](https://david-dm.org/grindcode/elastic-loop) [![devDependency Status](https://david-dm.org/grindcode/elastic-loop/dev-status.svg)](https://david-dm.org/grindcode/elastic-loop#info=devDependencies)
33

4-
Runs an interval with elastic timeout. Useful for loops supporting heavy load situations.
4+
Runs an interval with variable timeout. Useful for loops adapting to heavy load or other kind of situations.
55

66
## Get Started
77
```bash
@@ -21,22 +21,22 @@ Runs `function` in loop, mutating timeout depending on `stress`. Returns instanc
2121

2222
### Usage
2323
```javascript
24-
var loop = require('elastic-loop')
25-
var busy = require('node-busy')
24+
const loop = require('elastic-loop')
25+
const busy = require('node-busy')
2626

27-
monitor = busy()
27+
const monitor = busy()
2828

2929
// @cycle: current cycle fingerprint
30-
var run = function (cycle) {
30+
function run (cycle) {
3131
// → @cycle: { timeout: 1000, stress: 1 }
3232
}
3333

34-
var stress = function () {
34+
function stress () {
3535
// returns true if overloaded, false otherwise
3636
return monitor.blocked
3737
}
3838

39-
var cycle = loop(run, stress)
39+
const cycle = loop(run, stress)
4040

4141
// cycle.end()
4242

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
}

0 commit comments

Comments
 (0)