web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
6.2 KiB

  1. # tryer
  2. [![Build status](https://gitlab.com/philbooth/tryer/badges/master/pipeline.svg)](https://gitlab.com/philbooth/tryer/pipelines)
  3. [![Package status](https://img.shields.io/npm/v/tryer.svg)](https://www.npmjs.com/package/tryer)
  4. [![Downloads](https://img.shields.io/npm/dm/tryer.svg)](https://www.npmjs.com/package/tryer)
  5. [![License](https://img.shields.io/npm/l/tryer.svg)](https://opensource.org/licenses/MIT)
  6. Because everyone loves a tryer!
  7. Conditional
  8. and repeated
  9. function invocation
  10. for node
  11. and browser.
  12. * [Say what?](#say-what)
  13. * [What size is it?](#what-size-is-it)
  14. * [How do I install it?](#how-do-i-install-it)
  15. * [How do I use it?](#how-do-i-use-it)
  16. * [Loading the library](#loading-the-library)
  17. * [Calling the exported function](#calling-the-exported-function)
  18. * [Examples](#examples)
  19. * [How do I set up the dev environment?](#how-do-i-set-up-the-dev-environment)
  20. * [What license is it released under?](#what-license-is-it-released-under)
  21. ## Say what?
  22. Sometimes,
  23. you want to defer
  24. calling a function
  25. until a certain
  26. pre-requisite condition is met.
  27. Other times,
  28. you want to
  29. call a function
  30. repeatedly
  31. until some post-requisite condition
  32. is satisfied.
  33. Occasionally,
  34. you might even want
  35. to do both
  36. for the same function.
  37. To save you writing
  38. explicit conditions
  39. and loops
  40. on each of those occasions,
  41. `tryer` implements
  42. a predicate-based approach
  43. that hides the cruft
  44. behind a simple,
  45. functional interface.
  46. Additionally,
  47. it allows you to easily specify
  48. retry intervals
  49. and limits,
  50. so that your code
  51. doesn't hog the CPU.
  52. It also supports
  53. exponential backoff
  54. of retry intervals,
  55. which can be useful
  56. when handling
  57. indefinite error states
  58. such as network failure.
  59. ## What size is it?
  60. 5.6 kb unminified with comments, 1.1 kb minified, 0.5 kb minified + gzipped.
  61. ## How do I install it?
  62. Via npm:
  63. ```
  64. npm i tryer --save
  65. ```
  66. Or if you just want the git repo:
  67. ```
  68. git clone git@gitlab.com:philbooth/tryer.git
  69. ```
  70. ## How do I use it?
  71. ### Loading the library
  72. If you are running in
  73. Node.js
  74. or another CommonJS-style
  75. environment,
  76. you can `require`
  77. tryer like so:
  78. ```javascript
  79. const tryer = require('tryer');
  80. ```
  81. It also the supports
  82. the AMD-style format
  83. preferred by Require.js.
  84. If you are
  85. including `tryer`
  86. with an HTML `<script>` tag,
  87. or neither of the above environments
  88. are detected,
  89. it will be exported globally as `tryer`.
  90. ### Calling the exported function
  91. `tryer` is a function
  92. that can be invoked to
  93. call other functions
  94. conditionally and repeatedly,
  95. without the need for
  96. explicit `if` statements
  97. or loops in your own code.
  98. `tryer` takes one argument,
  99. an options object
  100. that supports
  101. the following properties:
  102. * `action`:
  103. The function that you want to invoke.
  104. If `action` returns a promise,
  105. iterations will not end
  106. until the promise is resolved or rejected.
  107. Alternatively,
  108. `action` may take a callback argument, `done`,
  109. to signal that it is asynchronous.
  110. In that case,
  111. you are responsible
  112. for calling `done`
  113. when the action is finished.
  114. If `action` is not set,
  115. it defaults to an empty function.
  116. * `when`:
  117. A predicate
  118. that tests the pre-condition
  119. for invoking `action`.
  120. Until `when` returns true
  121. (or a truthy value),
  122. `action` will not be called.
  123. Defaults to
  124. a function that immediately returns `true`.
  125. * `until`:
  126. A predicate
  127. that tests the post-condition
  128. for invoking `action`.
  129. After `until` returns true
  130. (or a truthy value),
  131. `action` will no longer be called.
  132. Defaults to
  133. a function that immediately returns `true`.
  134. * `fail`:
  135. The error handler.
  136. A function
  137. that will be called
  138. if `limit` falsey values
  139. are returned by `when` or `until`.
  140. Defaults to an empty function.
  141. * `pass`:
  142. Success handler.
  143. A function
  144. that will be called
  145. after `until` has returned truthily.
  146. Defaults to an empty function.
  147. * `limit`:
  148. Failure limit,
  149. representing the maximum number
  150. of falsey returns from `when` or `until`
  151. that will be permitted
  152. before invocation is deemed to have failed.
  153. A negative number
  154. indicates that the attempt
  155. should never fail,
  156. instead continuing
  157. for as long as `when` and `until`
  158. have returned truthy values.
  159. Defaults to `-1`.
  160. * `interval`:
  161. The retry interval,
  162. in milliseconds.
  163. A negative number indicates
  164. that each subsequent retry
  165. should wait for twice the interval
  166. from the preceding iteration
  167. (i.e. exponential backoff).
  168. The default value is `-1000`,
  169. signifying that
  170. the initial retry interval
  171. should be one second
  172. and that each subsequent attempt
  173. should wait for double the length
  174. of the previous interval.
  175. ### Examples
  176. ```javascript
  177. // Attempt to insert a database record, waiting until `db.isConnected`
  178. // before doing so. The retry interval is 1 second on each iteration
  179. // and the call will fail after 10 attempts.
  180. tryer({
  181. action: () => db.insert(record),
  182. when: () => db.isConnected,
  183. interval: 1000,
  184. limit: 10,
  185. fail () {
  186. log.error('No database connection, terminating.');
  187. process.exit(1);
  188. }
  189. });
  190. ```
  191. ```javascript
  192. // Attempt to send an email message, optionally retrying with
  193. // exponential backoff starting at 1 second. Continue to make
  194. // attempts indefinitely until the call succeeds.
  195. let sent = false;
  196. tryer({
  197. action (done) {
  198. smtp.send(email, error => {
  199. if (! error) {
  200. sent = true;
  201. }
  202. done();
  203. });
  204. },
  205. until: () => sent,
  206. interval: -1000,
  207. limit: -1
  208. });
  209. ```
  210. ```javascript
  211. // Poll a device at 30-second intervals, continuing indefinitely.
  212. tryer({
  213. action: () => device.poll().then(response => handle(response)),
  214. interval: 30000,
  215. limit: -1
  216. });
  217. ```
  218. ## How do I set up the dev environment?
  219. The dev environment relies on
  220. [Chai],
  221. [JSHint],
  222. [Mocha],
  223. [please-release-me],
  224. [spooks.js] and
  225. [UglifyJS].
  226. The source code is in
  227. `src/tryer.js`
  228. and the unit tests are in
  229. `test/unit.js`.
  230. To install the dependencies:
  231. ```
  232. npm i
  233. ```
  234. To run the tests:
  235. ```
  236. npm t
  237. ```
  238. To lint the code:
  239. ```
  240. npm run lint
  241. ```
  242. To regenerate the minified lib:
  243. ```
  244. npm run minify
  245. ```
  246. ## What license is it released under?
  247. [MIT](COPYING)
  248. [chai]: http://chaijs.com/
  249. [jshint]: http://jshint.com/
  250. [mocha]: http://mochajs.org/
  251. [please-release-me]: https://gitlab.com/philbooth/please-release-me
  252. [spooks.js]: https://gitlab.com/philbooth/spooks.js
  253. [uglifyjs]: http://lisperator.net/uglifyjs/
  254. [license]: COPYING