diff --git a/docs/backoff.html b/docs/backoff.html new file mode 100644 index 0000000..4510588 --- /dev/null +++ b/docs/backoff.html @@ -0,0 +1,206 @@ + + + + + backoff.js + + + + + +
+
+ + + + +
+ + diff --git a/docs/docco.css b/docs/docco.css new file mode 100644 index 0000000..f690a07 --- /dev/null +++ b/docs/docco.css @@ -0,0 +1,500 @@ +/*--------------------- Typography ----------------------------*/ + +@font-face { + font-family: 'aller-light'; + src: url('public/fonts/aller-light.eot'); + src: url('public/fonts/aller-light.eot?#iefix') format('embedded-opentype'), + url('public/fonts/aller-light.woff') format('woff'), + url('public/fonts/aller-light.ttf') format('truetype'); + font-weight: normal; + font-style: normal; +} + +@font-face { + font-family: 'aller-bold'; + src: url('public/fonts/aller-bold.eot'); + src: url('public/fonts/aller-bold.eot?#iefix') format('embedded-opentype'), + url('public/fonts/aller-bold.woff') format('woff'), + url('public/fonts/aller-bold.ttf') format('truetype'); + font-weight: normal; + font-style: normal; +} + +@font-face { + font-family: 'novecento-bold'; + src: url('public/fonts/novecento-bold.eot'); + src: url('public/fonts/novecento-bold.eot?#iefix') format('embedded-opentype'), + url('public/fonts/novecento-bold.woff') format('woff'), + url('public/fonts/novecento-bold.ttf') format('truetype'); + font-weight: normal; + font-style: normal; +} + +/*--------------------- Layout ----------------------------*/ +html { height: 100%; } +body { + font-family: "aller-light"; + font-size: 14px; + line-height: 18px; + color: #30404f; + margin: 0; padding: 0; + height:100%; +} +#container { min-height: 100%; } + +a { + color: #000; +} + +b, strong { + font-weight: normal; + font-family: "aller-bold"; +} + +p, ul, ol { + margin: 15px 0 0px; +} + +h1, h2, h3, h4, h5, h6 { + color: #112233; + line-height: 1em; + font-weight: normal; + font-family: "novecento-bold"; + text-transform: uppercase; + margin: 30px 0 15px 0; +} + +h1 { + margin-top: 40px; +} + +hr { + border: 0; + background: 1px solid #ddd; + height: 1px; + margin: 20px 0; +} + +pre, tt, code { + font-size: 12px; line-height: 16px; + font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace; + margin: 0; padding: 0; +} + .annotation pre { + display: block; + margin: 0; + padding: 7px 10px; + background: #fcfcfc; + -moz-box-shadow: inset 0 0 10px rgba(0,0,0,0.1); + -webkit-box-shadow: inset 0 0 10px rgba(0,0,0,0.1); + box-shadow: inset 0 0 10px rgba(0,0,0,0.1); + overflow-x: auto; + } + .annotation pre code { + border: 0; + padding: 0; + background: transparent; + } + + +blockquote { + border-left: 5px solid #ccc; + margin: 0; + padding: 1px 0 1px 1em; +} + .sections blockquote p { + font-family: Menlo, Consolas, Monaco, monospace; + font-size: 12px; line-height: 16px; + color: #999; + margin: 10px 0 0; + white-space: pre-wrap; + } + +ul.sections { + list-style: none; + padding:0 0 5px 0;; + margin:0; +} + +/* + Force border-box so that % widths fit the parent + container without overlap because of margin/padding. + + More Info : http://www.quirksmode.org/css/box.html +*/ +ul.sections > li > div { + -moz-box-sizing: border-box; /* firefox */ + -ms-box-sizing: border-box; /* ie */ + -webkit-box-sizing: border-box; /* webkit */ + -khtml-box-sizing: border-box; /* konqueror */ + box-sizing: border-box; /* css3 */ +} + + +/*---------------------- Jump Page -----------------------------*/ +#jump_to, #jump_page { + margin: 0; + background: white; + -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777; + -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px; + font: 16px Arial; + cursor: pointer; + text-align: right; + list-style: none; +} + +#jump_to a { + text-decoration: none; +} + +#jump_to a.large { + display: none; +} +#jump_to a.small { + font-size: 22px; + font-weight: bold; + color: #676767; +} + +#jump_to, #jump_wrapper { + position: fixed; + right: 0; top: 0; + padding: 10px 15px; + margin:0; +} + +#jump_wrapper { + display: none; + padding:0; +} + +#jump_to:hover #jump_wrapper { + display: block; +} + +#jump_page { + padding: 5px 0 3px; + margin: 0 0 25px 25px; +} + +#jump_page .source { + display: block; + padding: 15px; + text-decoration: none; + border-top: 1px solid #eee; +} + +#jump_page .source:hover { + background: #f5f5ff; +} + +#jump_page .source:first-child { +} + +/*---------------------- Low resolutions (> 320px) ---------------------*/ +@media only screen and (min-width: 320px) { + .pilwrap { display: none; } + + ul.sections > li > div { + display: block; + padding:5px 10px 0 10px; + } + + ul.sections > li > div.annotation ul, ul.sections > li > div.annotation ol { + padding-left: 30px; + } + + ul.sections > li > div.content { + background: #f5f5ff; + overflow-x:auto; + -webkit-box-shadow: inset 0 0 5px #e5e5ee; + box-shadow: inset 0 0 5px #e5e5ee; + border: 1px solid #dedede; + margin:5px 10px 5px 10px; + padding-bottom: 5px; + } + + ul.sections > li > div.annotation pre { + margin: 7px 0 7px; + padding-left: 15px; + } + + ul.sections > li > div.annotation p tt, .annotation code { + background: #f8f8ff; + border: 1px solid #dedede; + font-size: 12px; + padding: 0 0.2em; + } +} + +/*---------------------- (> 481px) ---------------------*/ +@media only screen and (min-width: 481px) { + #container { + position: relative; + } + body { + background-color: #F5F5FF; + font-size: 15px; + line-height: 21px; + } + pre, tt, code { + line-height: 18px; + } + p, ul, ol { + margin: 0 0 15px; + } + + + #jump_to { + padding: 5px 10px; + } + #jump_wrapper { + padding: 0; + } + #jump_to, #jump_page { + font: 10px Arial; + text-transform: uppercase; + } + #jump_page .source { + padding: 5px 10px; + } + #jump_to a.large { + display: inline-block; + } + #jump_to a.small { + display: none; + } + + + + #background { + position: absolute; + top: 0; bottom: 0; + width: 350px; + background: #fff; + border-right: 1px solid #e5e5ee; + z-index: -1; + } + + ul.sections > li > div.annotation ul, ul.sections > li > div.annotation ol { + padding-left: 40px; + } + + ul.sections > li { + white-space: nowrap; + } + + ul.sections > li > div { + display: inline-block; + } + + ul.sections > li > div.annotation { + max-width: 350px; + min-width: 350px; + min-height: 5px; + padding: 13px; + overflow-x: hidden; + white-space: normal; + vertical-align: top; + text-align: left; + } + ul.sections > li > div.annotation pre { + margin: 15px 0 15px; + padding-left: 15px; + } + + ul.sections > li > div.content { + padding: 13px; + vertical-align: top; + background: #f5f5ff; + border: none; + -webkit-box-shadow: none; + box-shadow: none; + } + + .pilwrap { + position: relative; + display: inline; + } + + .pilcrow { + font: 12px Arial; + text-decoration: none; + color: #454545; + position: absolute; + top: 3px; left: -20px; + padding: 1px 2px; + opacity: 0; + -webkit-transition: opacity 0.2s linear; + } + .for-h1 .pilcrow { + top: 47px; + } + .for-h2 .pilcrow, .for-h3 .pilcrow, .for-h4 .pilcrow { + top: 35px; + } + + ul.sections > li > div.annotation:hover .pilcrow { + opacity: 1; + } +} + +/*---------------------- (> 1025px) ---------------------*/ +@media only screen and (min-width: 1025px) { + + body { + font-size: 16px; + line-height: 24px; + } + + #background { + width: 525px; + } + ul.sections > li > div.annotation { + max-width: 525px; + min-width: 525px; + padding: 10px 25px 1px 50px; + } + ul.sections > li > div.content { + padding: 9px 15px 16px 25px; + } +} + +/*---------------------- Syntax Highlighting -----------------------------*/ + +td.linenos { background-color: #f0f0f0; padding-right: 10px; } +span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } +/* + +github.com style (c) Vasily Polovnyov + +*/ + +pre code { + display: block; padding: 0.5em; + color: #000; + background: #f8f8ff +} + +pre .comment, +pre .template_comment, +pre .diff .header, +pre .javadoc { + color: #408080; + font-style: italic +} + +pre .keyword, +pre .assignment, +pre .literal, +pre .css .rule .keyword, +pre .winutils, +pre .javascript .title, +pre .lisp .title, +pre .subst { + color: #954121; + /*font-weight: bold*/ +} + +pre .number, +pre .hexcolor { + color: #40a070 +} + +pre .string, +pre .tag .value, +pre .phpdoc, +pre .tex .formula { + color: #219161; +} + +pre .title, +pre .id { + color: #19469D; +} +pre .params { + color: #00F; +} + +pre .javascript .title, +pre .lisp .title, +pre .subst { + font-weight: normal +} + +pre .class .title, +pre .haskell .label, +pre .tex .command { + color: #458; + font-weight: bold +} + +pre .tag, +pre .tag .title, +pre .rules .property, +pre .django .tag .keyword { + color: #000080; + font-weight: normal +} + +pre .attribute, +pre .variable, +pre .instancevar, +pre .lisp .body { + color: #008080 +} + +pre .regexp { + color: #B68 +} + +pre .class { + color: #458; + font-weight: bold +} + +pre .symbol, +pre .ruby .symbol .string, +pre .ruby .symbol .keyword, +pre .ruby .symbol .keymethods, +pre .lisp .keyword, +pre .tex .special, +pre .input_number { + color: #990073 +} + +pre .builtin, +pre .constructor, +pre .built_in, +pre .lisp .title { + color: #0086b3 +} + +pre .preprocessor, +pre .pi, +pre .doctype, +pre .shebang, +pre .cdata { + color: #999; + font-weight: bold +} + +pre .deletion { + background: #fdd +} + +pre .addition { + background: #dfd +} + +pre .diff .change { + background: #0086b3 +} + +pre .chunk { + color: #aaa +} + +pre .tex .formula { + opacity: 0.5; +} diff --git a/docs/exponential.html b/docs/exponential.html new file mode 100644 index 0000000..d572d34 --- /dev/null +++ b/docs/exponential.html @@ -0,0 +1,118 @@ + + + + + exponential.js + + + + + +
+
+ + + + +
+ + diff --git a/docs/fibonacci.html b/docs/fibonacci.html new file mode 100644 index 0000000..03ffbdc --- /dev/null +++ b/docs/fibonacci.html @@ -0,0 +1,119 @@ + + + + + fibonacci.js + + + + + +
+
+ + + + +
+ + diff --git a/docs/function_call.html b/docs/function_call.html new file mode 100644 index 0000000..d5cc59b --- /dev/null +++ b/docs/function_call.html @@ -0,0 +1,502 @@ + + + + + function_call.js + + + + + +
+
+ + + + +
+ + diff --git a/doc/backoff_events.png b/docs/img/backoff_events.png similarity index 100% rename from doc/backoff_events.png rename to docs/img/backoff_events.png diff --git a/doc/function_call_events.png b/docs/img/function_call_events.png similarity index 100% rename from doc/function_call_events.png rename to docs/img/function_call_events.png diff --git a/doc/layers.png b/docs/img/layers.png similarity index 100% rename from doc/layers.png rename to docs/img/layers.png diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..ed61bfc --- /dev/null +++ b/docs/index.html @@ -0,0 +1,146 @@ + + + + + index.js + + + + + +
+
+ + + + +
+ + diff --git a/docs/public/fonts/aller-bold.eot b/docs/public/fonts/aller-bold.eot new file mode 100755 index 0000000..1b32532 Binary files /dev/null and b/docs/public/fonts/aller-bold.eot differ diff --git a/docs/public/fonts/aller-bold.ttf b/docs/public/fonts/aller-bold.ttf new file mode 100755 index 0000000..dc4cc9c Binary files /dev/null and b/docs/public/fonts/aller-bold.ttf differ diff --git a/docs/public/fonts/aller-bold.woff b/docs/public/fonts/aller-bold.woff new file mode 100755 index 0000000..fa16fd0 Binary files /dev/null and b/docs/public/fonts/aller-bold.woff differ diff --git a/docs/public/fonts/aller-light.eot b/docs/public/fonts/aller-light.eot new file mode 100755 index 0000000..40bd654 Binary files /dev/null and b/docs/public/fonts/aller-light.eot differ diff --git a/docs/public/fonts/aller-light.ttf b/docs/public/fonts/aller-light.ttf new file mode 100755 index 0000000..c2c7290 Binary files /dev/null and b/docs/public/fonts/aller-light.ttf differ diff --git a/docs/public/fonts/aller-light.woff b/docs/public/fonts/aller-light.woff new file mode 100755 index 0000000..81a09d1 Binary files /dev/null and b/docs/public/fonts/aller-light.woff differ diff --git a/docs/public/fonts/novecento-bold.eot b/docs/public/fonts/novecento-bold.eot new file mode 100755 index 0000000..98a9a7f Binary files /dev/null and b/docs/public/fonts/novecento-bold.eot differ diff --git a/docs/public/fonts/novecento-bold.ttf b/docs/public/fonts/novecento-bold.ttf new file mode 100755 index 0000000..2af39b0 Binary files /dev/null and b/docs/public/fonts/novecento-bold.ttf differ diff --git a/docs/public/fonts/novecento-bold.woff b/docs/public/fonts/novecento-bold.woff new file mode 100755 index 0000000..de558b5 Binary files /dev/null and b/docs/public/fonts/novecento-bold.woff differ diff --git a/docs/public/stylesheets/normalize.css b/docs/public/stylesheets/normalize.css new file mode 100644 index 0000000..73abb76 --- /dev/null +++ b/docs/public/stylesheets/normalize.css @@ -0,0 +1,375 @@ +/*! normalize.css v2.0.1 | MIT License | git.io/normalize */ + +/* ========================================================================== + HTML5 display definitions + ========================================================================== */ + +/* + * Corrects `block` display not defined in IE 8/9. + */ + +article, +aside, +details, +figcaption, +figure, +footer, +header, +hgroup, +nav, +section, +summary { + display: block; +} + +/* + * Corrects `inline-block` display not defined in IE 8/9. + */ + +audio, +canvas, +video { + display: inline-block; +} + +/* + * Prevents modern browsers from displaying `audio` without controls. + * Remove excess height in iOS 5 devices. + */ + +audio:not([controls]) { + display: none; + height: 0; +} + +/* + * Addresses styling for `hidden` attribute not present in IE 8/9. + */ + +[hidden] { + display: none; +} + +/* ========================================================================== + Base + ========================================================================== */ + +/* + * 1. Sets default font family to sans-serif. + * 2. Prevents iOS text size adjust after orientation change, without disabling + * user zoom. + */ + +html { + font-family: sans-serif; /* 1 */ + -webkit-text-size-adjust: 100%; /* 2 */ + -ms-text-size-adjust: 100%; /* 2 */ +} + +/* + * Removes default margin. + */ + +body { + margin: 0; +} + +/* ========================================================================== + Links + ========================================================================== */ + +/* + * Addresses `outline` inconsistency between Chrome and other browsers. + */ + +a:focus { + outline: thin dotted; +} + +/* + * Improves readability when focused and also mouse hovered in all browsers. + */ + +a:active, +a:hover { + outline: 0; +} + +/* ========================================================================== + Typography + ========================================================================== */ + +/* + * Addresses `h1` font sizes within `section` and `article` in Firefox 4+, + * Safari 5, and Chrome. + */ + +h1 { + font-size: 2em; +} + +/* + * Addresses styling not present in IE 8/9, Safari 5, and Chrome. + */ + +abbr[title] { + border-bottom: 1px dotted; +} + +/* + * Addresses style set to `bolder` in Firefox 4+, Safari 5, and Chrome. + */ + +b, +strong { + font-weight: bold; +} + +/* + * Addresses styling not present in Safari 5 and Chrome. + */ + +dfn { + font-style: italic; +} + +/* + * Addresses styling not present in IE 8/9. + */ + +mark { + background: #ff0; + color: #000; +} + + +/* + * Corrects font family set oddly in Safari 5 and Chrome. + */ + +code, +kbd, +pre, +samp { + font-family: monospace, serif; + font-size: 1em; +} + +/* + * Improves readability of pre-formatted text in all browsers. + */ + +pre { + white-space: pre; + white-space: pre-wrap; + word-wrap: break-word; +} + +/* + * Sets consistent quote types. + */ + +q { + quotes: "\201C" "\201D" "\2018" "\2019"; +} + +/* + * Addresses inconsistent and variable font size in all browsers. + */ + +small { + font-size: 80%; +} + +/* + * Prevents `sub` and `sup` affecting `line-height` in all browsers. + */ + +sub, +sup { + font-size: 75%; + line-height: 0; + position: relative; + vertical-align: baseline; +} + +sup { + top: -0.5em; +} + +sub { + bottom: -0.25em; +} + +/* ========================================================================== + Embedded content + ========================================================================== */ + +/* + * Removes border when inside `a` element in IE 8/9. + */ + +img { + border: 0; +} + +/* + * Corrects overflow displayed oddly in IE 9. + */ + +svg:not(:root) { + overflow: hidden; +} + +/* ========================================================================== + Figures + ========================================================================== */ + +/* + * Addresses margin not present in IE 8/9 and Safari 5. + */ + +figure { + margin: 0; +} + +/* ========================================================================== + Forms + ========================================================================== */ + +/* + * Define consistent border, margin, and padding. + */ + +fieldset { + border: 1px solid #c0c0c0; + margin: 0 2px; + padding: 0.35em 0.625em 0.75em; +} + +/* + * 1. Corrects color not being inherited in IE 8/9. + * 2. Remove padding so people aren't caught out if they zero out fieldsets. + */ + +legend { + border: 0; /* 1 */ + padding: 0; /* 2 */ +} + +/* + * 1. Corrects font family not being inherited in all browsers. + * 2. Corrects font size not being inherited in all browsers. + * 3. Addresses margins set differently in Firefox 4+, Safari 5, and Chrome + */ + +button, +input, +select, +textarea { + font-family: inherit; /* 1 */ + font-size: 100%; /* 2 */ + margin: 0; /* 3 */ +} + +/* + * Addresses Firefox 4+ setting `line-height` on `input` using `!important` in + * the UA stylesheet. + */ + +button, +input { + line-height: normal; +} + +/* + * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` + * and `video` controls. + * 2. Corrects inability to style clickable `input` types in iOS. + * 3. Improves usability and consistency of cursor style between image-type + * `input` and others. + */ + +button, +html input[type="button"], /* 1 */ +input[type="reset"], +input[type="submit"] { + -webkit-appearance: button; /* 2 */ + cursor: pointer; /* 3 */ +} + +/* + * Re-set default cursor for disabled elements. + */ + +button[disabled], +input[disabled] { + cursor: default; +} + +/* + * 1. Addresses box sizing set to `content-box` in IE 8/9. + * 2. Removes excess padding in IE 8/9. + */ + +input[type="checkbox"], +input[type="radio"] { + box-sizing: border-box; /* 1 */ + padding: 0; /* 2 */ +} + +/* + * 1. Addresses `appearance` set to `searchfield` in Safari 5 and Chrome. + * 2. Addresses `box-sizing` set to `border-box` in Safari 5 and Chrome + * (include `-moz` to future-proof). + */ + +input[type="search"] { + -webkit-appearance: textfield; /* 1 */ + -moz-box-sizing: content-box; + -webkit-box-sizing: content-box; /* 2 */ + box-sizing: content-box; +} + +/* + * Removes inner padding and search cancel button in Safari 5 and Chrome + * on OS X. + */ + +input[type="search"]::-webkit-search-cancel-button, +input[type="search"]::-webkit-search-decoration { + -webkit-appearance: none; +} + +/* + * Removes inner padding and border in Firefox 4+. + */ + +button::-moz-focus-inner, +input::-moz-focus-inner { + border: 0; + padding: 0; +} + +/* + * 1. Removes default vertical scrollbar in IE 8/9. + * 2. Improves readability and alignment in all browsers. + */ + +textarea { + overflow: auto; /* 1 */ + vertical-align: top; /* 2 */ +} + +/* ========================================================================== + Tables + ========================================================================== */ + +/* + * Remove most spacing between table cells. + */ + +table { + border-collapse: collapse; + border-spacing: 0; +} \ No newline at end of file diff --git a/docs/strategy.html b/docs/strategy.html new file mode 100644 index 0000000..e11ec1e --- /dev/null +++ b/docs/strategy.html @@ -0,0 +1,244 @@ + + + + + strategy.js + + + + + +
+
+ + + + +
+ + diff --git a/index.js b/index.js index 5f184d3..6281fa6 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,5 @@ -/* - * Copyright (c) 2012 Mathieu Turcotte - * Licensed under the MIT license. - */ +// Copyright (c) 2012 Mathieu Turcotte +// Licensed under the MIT license. var Backoff = require('./lib/backoff'); var ExponentialBackoffStrategy = require('./lib/strategy/exponential'); @@ -13,33 +11,17 @@ module.exports.FunctionCall = FunctionCall; module.exports.FibonacciStrategy = FibonacciBackoffStrategy; module.exports.ExponentialStrategy = ExponentialBackoffStrategy; -/** - * Constructs a Fibonacci backoff. - * @param options Fibonacci backoff strategy arguments. - * @return The fibonacci backoff. - * @see FibonacciBackoffStrategy - */ +// Constructs a Fibonacci backoff. module.exports.fibonacci = function(options) { return new Backoff(new FibonacciBackoffStrategy(options)); }; -/** - * Constructs an exponential backoff. - * @param options Exponential strategy arguments. - * @return The exponential backoff. - * @see ExponentialBackoffStrategy - */ +// Constructs an exponential backoff. module.exports.exponential = function(options) { return new Backoff(new ExponentialBackoffStrategy(options)); }; -/** - * Constructs a FunctionCall for the given function and arguments. - * @param fn The function to wrap in a backoff handler. - * @param vargs The function's arguments (var args). - * @param callback The function's callback. - * @return The FunctionCall instance. - */ +// Constructs a FunctionCall for the given function and arguments. module.exports.call = function(fn, vargs, callback) { var args = Array.prototype.slice.call(arguments); fn = args[0]; diff --git a/lib/backoff.js b/lib/backoff.js index cfa13ee..32a36ea 100644 --- a/lib/backoff.js +++ b/lib/backoff.js @@ -1,16 +1,11 @@ -/* - * Copyright (c) 2012 Mathieu Turcotte - * Licensed under the MIT license. - */ +// Copyright (c) 2012 Mathieu Turcotte +// Licensed under the MIT license. var events = require('events'); var util = require('util'); -/** - * Backoff driver. - * @param backoffStrategy Backoff delay generator/strategy. - * @constructor - */ +// A class to hold the state of a backoff operation. Accepts a backoff strategy +// to generate the backoff delays. function Backoff(backoffStrategy) { events.EventEmitter.call(this); @@ -26,11 +21,8 @@ function Backoff(backoffStrategy) { } util.inherits(Backoff, events.EventEmitter); -/** - * Sets a limit, greater than 0, on the maximum number of backoffs. A 'fail' - * event will be emitted when the limit is reached. - * @param maxNumberOfRetry The maximum number of backoffs. - */ +// Sets a limit, greater than 0, on the maximum number of backoffs. A 'fail' +// event will be emitted when the limit is reached. Backoff.prototype.failAfter = function(maxNumberOfRetry) { if (maxNumberOfRetry < 1) { throw new Error('Maximum number of retry must be greater than 0. ' + @@ -40,11 +32,8 @@ Backoff.prototype.failAfter = function(maxNumberOfRetry) { this.maxNumberOfRetry_ = maxNumberOfRetry; }; -/** - * Starts a backoff operation. - * @param err Optional paramater to let the listeners know why the backoff - * operation was started. - */ +// Starts a backoff operation. Accepts an optional parameter to let the +// listeners know why the backoff operation was started. Backoff.prototype.backoff = function(err) { if (this.timeoutID_ !== -1) { throw new Error('Backoff in progress.'); @@ -60,20 +49,14 @@ Backoff.prototype.backoff = function(err) { } }; -/** - * Handles the backoff timeout completion. - * @private - */ +// Handles the backoff timeout completion. Backoff.prototype.onBackoff_ = function() { this.timeoutID_ = -1; this.emit('ready', this.backoffNumber_, this.backoffDelay_); this.backoffNumber_++; }; -/** - * Stops any backoff operation and resets the backoff delay to its inital - * value. - */ +// Stops any backoff operation and resets the backoff delay to its inital value. Backoff.prototype.reset = function() { this.backoffNumber_ = 0; this.backoffStrategy_.reset(); diff --git a/lib/function_call.js b/lib/function_call.js index a7e1a7a..9d6285e 100644 --- a/lib/function_call.js +++ b/lib/function_call.js @@ -1,7 +1,5 @@ -/* - * Copyright (c) 2012 Mathieu Turcotte - * Licensed under the MIT license. - */ +// Copyright (c) 2012 Mathieu Turcotte +// Licensed under the MIT license. var events = require('events'); var util = require('util'); @@ -9,22 +7,12 @@ var util = require('util'); var Backoff = require('./backoff'); var FibonacciBackoffStrategy = require('./strategy/fibonacci'); -/** - * Returns true if the specified value is a function - * @param val Variable to test. - * @return Whether variable is a function. - */ +// Checks whether the given value is a function. function isFunction(val) { return typeof val == 'function'; } -/** - * Manages the calling of a function in a backoff loop. - * @param fn Function to wrap in a backoff handler. - * @param args Array of function's arguments. - * @param callback Function's callback. - * @constructor - */ +// Wraps a function to be called in a backoff loop. function FunctionCall(fn, args, callback) { events.EventEmitter.call(this); @@ -51,83 +39,65 @@ function FunctionCall(fn, args, callback) { } util.inherits(FunctionCall, events.EventEmitter); -/** - * Enum of states in which the FunctionCall can be. - * @private - */ +// States in which the call can be. FunctionCall.State_ = { + // Call isn't started yet. PENDING: 0, + // Call is in progress. RUNNING: 1, + // Call completed successfully which means that either the wrapped function + // returned successfully or the maximal number of backoffs was reached. COMPLETED: 2, + // The call was aborted. ABORTED: 3 }; -/** - * @return Whether the call is pending. - */ +// Checks whether the call is pending. FunctionCall.prototype.isPending = function() { return this.state_ == FunctionCall.State_.PENDING; }; -/** - * @return Whether the call is in progress. - */ +// Checks whether the call is in progress. FunctionCall.prototype.isRunning = function() { return this.state_ == FunctionCall.State_.RUNNING; }; -/** - * @return Whether the call is completed. - */ +// Checks whether the call is completed. FunctionCall.prototype.isCompleted = function() { return this.state_ == FunctionCall.State_.COMPLETED; }; -/** - * @return Whether the call is aborted. - */ +// Checks whether the call is aborted. FunctionCall.prototype.isAborted = function() { return this.state_ == FunctionCall.State_.ABORTED; }; -/** - * Sets the backoff strategy. - * @param strategy The backoff strategy to use. - * @return Itself for chaining. - */ +// Sets the backoff strategy to use. Can only be called before the call is +// started otherwise an exception will be thrown. FunctionCall.prototype.setStrategy = function(strategy) { if (!this.isPending()) { throw new Error('FunctionCall in progress.'); } this.strategy_ = strategy; - return this; + return this; // Return this for chaining. }; -/** - * Returns all intermediary results returned by the wrapped function since - * the initial call. - * @return An array of intermediary results. - */ +// Returns all intermediary results returned by the wrapped function since +// the initial call. FunctionCall.prototype.getResults = function() { return this.results_.concat(); }; -/** - * Sets the backoff limit. - * @param maxNumberOfRetry The maximum number of backoffs. - * @return Itself for chaining. - */ +// Sets the backoff limit. FunctionCall.prototype.failAfter = function(maxNumberOfRetry) { if (!this.isPending()) { throw new Error('FunctionCall in progress.'); } this.failAfter_ = maxNumberOfRetry; - return this; + return this; // Return this for chaining. }; -/** - * Aborts the call. - */ +// Aborts the call. FunctionCall.prototype.abort = function() { if (this.isCompleted()) { throw new Error('FunctionCall already completed.'); @@ -140,11 +110,8 @@ FunctionCall.prototype.abort = function() { this.state_ = FunctionCall.State_.ABORTED; }; -/** - * Initiates the call to the wrapped function. - * @param backoffFactory Optional factory function used to create the backoff - * instance. - */ +// Initiates the call to the wrapped function. Accepts an optional factory +// function used to create the backoff instance; used when testing. FunctionCall.prototype.start = function(backoffFactory) { if (this.isAborted()) { throw new Error('FunctionCall aborted.'); @@ -170,10 +137,7 @@ FunctionCall.prototype.start = function(backoffFactory) { this.doCall_(); }; -/** - * Calls the wrapped function. - * @private - */ +// Calls the wrapped function. FunctionCall.prototype.doCall_ = function() { var eventArgs = ['call'].concat(this.arguments_); events.EventEmitter.prototype.emit.apply(this, eventArgs); @@ -181,21 +145,15 @@ FunctionCall.prototype.doCall_ = function() { this.function_.apply(null, this.arguments_.concat(callback)); }; -/** - * Calls the wrapped function's callback with the last result returned by the - * wrapped function. - * @private - */ +// Calls the wrapped function's callback with the last result returned by the +// wrapped function. FunctionCall.prototype.doCallback_ = function() { var args = this.results_[this.results_.length - 1]; this.callback_.apply(null, args); }; -/** - * Handles wrapped function's completion. This method acts as a replacement - * for the original callback function. - * @private - */ +// Handles wrapped function's completion. This method acts as a replacement +// for the original callback function. FunctionCall.prototype.handleFunctionCallback_ = function() { if (this.isAborted()) { return; @@ -213,13 +171,7 @@ FunctionCall.prototype.handleFunctionCallback_ = function() { } }; -/** - * Handles backoff event. - * @param number Backoff number. - * @param delay Backoff delay. - * @param err The error that caused the backoff. - * @private - */ +// Handles the backoff event by reemitting it. FunctionCall.prototype.handleBackoff_ = function(number, delay, err) { this.emit('backoff', number, delay, err); }; diff --git a/lib/strategy/exponential.js b/lib/strategy/exponential.js index 08a4746..9e19f60 100644 --- a/lib/strategy/exponential.js +++ b/lib/strategy/exponential.js @@ -1,16 +1,11 @@ -/* - * Copyright (c) 2012 Mathieu Turcotte - * Licensed under the MIT license. - */ +// Copyright (c) 2012 Mathieu Turcotte +// Licensed under the MIT license. var util = require('util'); var BackoffStrategy = require('./strategy'); -/** - * Exponential backoff strategy. - * @extends BackoffStrategy - */ +// Exponential backoff strategy. function ExponentialBackoffStrategy(options) { BackoffStrategy.call(this, options); this.backoffDelay_ = 0; @@ -18,14 +13,12 @@ function ExponentialBackoffStrategy(options) { } util.inherits(ExponentialBackoffStrategy, BackoffStrategy); -/** @inheritDoc */ ExponentialBackoffStrategy.prototype.next_ = function() { this.backoffDelay_ = Math.min(this.nextBackoffDelay_, this.getMaxDelay()); this.nextBackoffDelay_ = this.backoffDelay_ * 2; return this.backoffDelay_; }; -/** @inheritDoc */ ExponentialBackoffStrategy.prototype.reset_ = function() { this.backoffDelay_ = 0; this.nextBackoffDelay_ = this.getInitialDelay(); diff --git a/lib/strategy/fibonacci.js b/lib/strategy/fibonacci.js index 602b07f..7c71c03 100644 --- a/lib/strategy/fibonacci.js +++ b/lib/strategy/fibonacci.js @@ -1,16 +1,11 @@ -/* - * Copyright (c) 2012 Mathieu Turcotte - * Licensed under the MIT license. - */ +// Copyright (c) 2012 Mathieu Turcotte +// Licensed under the MIT license. var util = require('util'); var BackoffStrategy = require('./strategy'); -/** - * Fibonacci backoff strategy. - * @extends BackoffStrategy - */ +// Fibonacci backoff strategy. function FibonacciBackoffStrategy(options) { BackoffStrategy.call(this, options); this.backoffDelay_ = 0; @@ -18,7 +13,6 @@ function FibonacciBackoffStrategy(options) { } util.inherits(FibonacciBackoffStrategy, BackoffStrategy); -/** @inheritDoc */ FibonacciBackoffStrategy.prototype.next_ = function() { var backoffDelay = Math.min(this.nextBackoffDelay_, this.getMaxDelay()); this.nextBackoffDelay_ += this.backoffDelay_; @@ -26,7 +20,6 @@ FibonacciBackoffStrategy.prototype.next_ = function() { return backoffDelay; }; -/** @inheritDoc */ FibonacciBackoffStrategy.prototype.reset_ = function() { this.nextBackoffDelay_ = this.getInitialDelay(); this.backoffDelay_ = 0; diff --git a/lib/strategy/strategy.js b/lib/strategy/strategy.js index f5bf7d4..7adfd75 100644 --- a/lib/strategy/strategy.js +++ b/lib/strategy/strategy.js @@ -1,7 +1,5 @@ -/* - * Copyright (c) 2012 Mathieu Turcotte - * Licensed under the MIT license. - */ +// Copyright (c) 2012 Mathieu Turcotte +// Licensed under the MIT license. var events = require('events'); var util = require('util'); @@ -10,15 +8,14 @@ function isDef(value) { return value !== undefined && value !== null; } -/** - * Abstract class defining the skeleton for all backoff strategies. - * @param options Backoff strategy options. - * @param options.randomisationFactor The randomisation factor, must be between - * 0 and 1. - * @param options.initialDelay The backoff initial delay, in milliseconds. - * @param options.maxDelay The backoff maximal delay, in milliseconds. - * @constructor - */ +// Abstract class defining the skeleton for the backoff strategies. Accepts an +// object holding the options for the backoff strategy: +// +// * `randomisationFactor`: The randomisation factor which must be between 0 +// and 1 where 1 equates to a randomization factor of 100% and 0 to no +// randomization. +// * `initialDelay`: The backoff initial delay in milliseconds. +// * `maxDelay`: The backoff maximal delay in milliseconds. function BackoffStrategy(options) { options = options || {}; @@ -44,26 +41,18 @@ function BackoffStrategy(options) { this.randomisationFactor_ = options.randomisationFactor || 0; } -/** - * Retrieves the maximal backoff delay. - * @return The maximal backoff delay, in milliseconds. - */ +// Gets the maximal backoff delay. BackoffStrategy.prototype.getMaxDelay = function() { return this.maxDelay_; }; -/** - * Retrieves the initial backoff delay. - * @return The initial backoff delay, in milliseconds. - */ +// Gets the initial backoff delay. BackoffStrategy.prototype.getInitialDelay = function() { return this.initialDelay_; }; -/** - * Template method that computes the next backoff delay. - * @return The backoff delay, in milliseconds. - */ +// Template method that computes and returns the next backoff delay in +// milliseconds. BackoffStrategy.prototype.next = function() { var backoffDelay = this.next_(); var randomisationMultiple = 1 + Math.random() * this.randomisationFactor_; @@ -71,26 +60,19 @@ BackoffStrategy.prototype.next = function() { return randomizedDelay; }; -/** - * Computes the next backoff delay. - * @return The backoff delay, in milliseconds. - * @protected - */ +// Computes and returns the next backoff delay. Intended to be overridden by +// subclasses. BackoffStrategy.prototype.next_ = function() { throw new Error('BackoffStrategy.next_() unimplemented.'); }; -/** - * Template method that resets the backoff delay to its initial value. - */ +// Template method that resets the backoff delay to its initial value. BackoffStrategy.prototype.reset = function() { this.reset_(); }; -/** - * Resets the backoff delay to its initial value. - * @protected - */ +// Resets the backoff delay to its initial value. Intended to be overridden by +// subclasses. BackoffStrategy.prototype.reset_ = function() { throw new Error('BackoffStrategy.reset_() unimplemented.'); }; diff --git a/package.json b/package.json index f0a62ef..69b857b 100644 --- a/package.json +++ b/package.json @@ -12,9 +12,11 @@ "devDependencies": { "sinon": "1.7", "nodeunit": "0.8", - "jshint": "2.0" + "jshint": "2.0", + "docco": "0.6" }, "scripts": { + "docco" : "node_modules/.bin/docco lib/*.js lib/strategy/* index.js", "pretest": "node_modules/jshint/bin/jshint lib/ tests/ examples/ index.js", "test": "node_modules/nodeunit/bin/nodeunit tests/" },