source: frontend/node_modules/tryer/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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