made with esnextbin
Created
December 5, 2016 00:39
-
-
Save briancavalier/3a55407c2aa8db6cce76abb983457d28 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { just, tap, periodic, throwError, recoverWith, delay, join } from 'most' | |
const retry = (ms, s) => nextRetry(ms, ms, s) | |
const nextRetry = (basems, currentms, s) => { | |
let ok = false | |
return recoverWith(() => ok | |
? nextRetry(basems, basems, s) | |
: backoff(basems, currentms, s), | |
tap(() => ok = true, s)) | |
} | |
const backoff = (basems, currentms, s) => | |
join(delay(currentms, just(nextRetry(basems, currentms * 2, s)))) | |
// Create a test stream that fails some number | |
// of times before finally succeeding | |
let attempts = 0 | |
const s = periodic(1000, 'trying') | |
.timestamp() | |
.tap(x => console.log(x)) | |
.chain(x => ++attempts % 5 === 0 ? just(x) : throwError(x)) | |
retry(200, s) | |
.observe(x => console.log('here', x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"most": "1.0.5" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _most = require('most'); | |
var retry = function retry(ms, s) { | |
return nextRetry(ms, ms, s); | |
}; | |
var nextRetry = function nextRetry(basems, currentms, s) { | |
var ok = false; | |
return (0, _most.recoverWith)(function () { | |
return ok ? nextRetry(basems, basems, s) : backoff(basems, currentms, s); | |
}, (0, _most.tap)(function () { | |
return ok = true; | |
}, s)); | |
}; | |
var backoff = function backoff(basems, currentms, s) { | |
return (0, _most.join)((0, _most.delay)(currentms, (0, _most.just)(nextRetry(basems, currentms * 2, s)))); | |
}; | |
// Create a test stream that fails some number | |
// of times before finally succeeding | |
var attempts = 0; | |
var s = (0, _most.periodic)(1000, 'trying').timestamp().tap(function (x) { | |
return console.log(x); | |
}).chain(function (x) { | |
return ++attempts % 5 === 0 ? (0, _most.just)(x) : (0, _most.throwError)(x); | |
}); | |
retry(200, s).observe(function (x) { | |
return console.log('here', x); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment