@prodo-ai/js-timing
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -8,9 +8,11 @@ "use strict"; | ||
var _constants = require("./constants"); | ||
var _duration = require("./duration"); | ||
var _errors = require("./errors"); | ||
var _constants = require("./constants"); | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
const repeatedly = exports.repeatedly = (behaviour, defaultDelay, onError = _errors.DefaultErrorHandler) => { | ||
const repeatedly = exports.repeatedly = (behaviour, defaultDelay = (0, _duration.duration)(1, _constants.MILLISECOND), onError = _errors.DefaultErrorHandler) => { | ||
let pendingCancellation = false; | ||
@@ -17,0 +19,0 @@ let cancelResolvers = []; |
{ | ||
"name": "@prodo-ai/js-timing", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "JS utilities for timing", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
@@ -16,5 +17,6 @@ "lib" | ||
"flow": "flow status", | ||
"prepack": "npm run prepack:babel && npm run prepack:flow", | ||
"prepack": "npm run prepack:babel && npm run prepack:flow && npm run prepack:ts", | ||
"prepack:babel": "babel src/ -d lib", | ||
"prepack:flow": "find src -name *.js -type f | cut -d'/' -f2 | xargs -I '{}' cp src/{} lib/{}.flow", | ||
"prepack:ts": "cp index.d.ts lib/", | ||
"test": "npm run test:ava && npm run test:eslint && npm run test:flow", | ||
@@ -29,2 +31,3 @@ "test:ava": "ava --color --verbose --timeout=3000 test", | ||
"devDependencies": { | ||
"@prodo-ai/eslint-plugin": "^1.0.0", | ||
"ava": "^0.23.0", | ||
@@ -38,3 +41,3 @@ "babel-cli": "^6.24.0", | ||
"babel-preset-stage-0": "^6.22.0", | ||
"eslint": "^4.19.1", | ||
"eslint": "^v5.0.0-alpha.2", | ||
"eslint-config-prettier": "^2.9.0", | ||
@@ -41,0 +44,0 @@ "eslint-plugin-babel": "^5.1.0", |
# JS Timing | ||
Utility library for handling time and timing. | ||
## Usage | ||
Most of the functions in this library rely on a single main type, `Duration`. To create a duration, use the `duration` method: | ||
```javascript | ||
import {duration, HOUR, SECONDS} from "@prodo-ai/js-timing"; | ||
const tenSeconds = duration(10, SECONDS); | ||
const oneHour = duration(1, HOUR); | ||
``` | ||
You can then modify durations, or convert them to different units: | ||
```javascript | ||
const threeHours = oneHour.times(3); | ||
const waitInMinutes = (numMinutes: number) => {/*...*/}; | ||
waitInMinutes(oneHour.in(MINUTES)); | ||
``` | ||
### waitFor | ||
Returns a `Promise` that will return after a provided duration; | ||
```javascript | ||
import {waitFor} from "@prodo-ai/js-timing"; | ||
waitFor(duration(10, SECONDS)).then(() => console.log("Done.")); | ||
``` | ||
### repeatedly | ||
Runs a callback again and again. The optional second parameter specifies how much delay should be included between each execution: | ||
```javascript | ||
import {repeatedly} from "@prodo-ai/js-timing"; | ||
const callback = () => { | ||
console.log("foo") | ||
}; | ||
const cancel = repeatedly(callback); | ||
waitFor(duration(10, SECONDS)).then(cancel); | ||
const cancel2 = repeatedly(callback, duration(1, SECOND)); | ||
waitFor(duration(10, SECONDS)).then(cancel2); | ||
``` | ||
You can control the duration between each execution by returning a `Duration` from your callback - the execution of the next callback will only happen after that duration has passed: | ||
```javascript | ||
const callback = () => { | ||
console.log("foo"); | ||
return duration(1, SECOND); | ||
}; | ||
const cancel = repeatedly(callback); | ||
waitFor(duration(10, SECONDS)).then(cancel); // The callback will executed | ||
``` | ||
You can also specify an error handler: | ||
```javascript | ||
const callback = () => { | ||
throw new Error("Failed."); | ||
}; | ||
const errorHandler = (error) => { | ||
console.error(error.message); | ||
}; | ||
const cancel = repeatedly(callback, duration(1, MILLISECOND), errorHandler); | ||
``` | ||
### every | ||
Actually, this is just the same as repeatedly. | ||
## Information | ||
Owner: Prodo Tech Ltd | ||
Maintainer: [tdawes](https://github.com/tdawes) | ||
License: UNLICENSED (for now) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13061
19
179
89
16