Comparing version 3.2.1 to 3.2.2
@@ -1,2 +0,2 @@ | ||
Copyright 2022 Philipp Rudloff | ||
Copyright 2023 Philipp Rudloff | ||
@@ -3,0 +3,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
101
package.json
{ | ||
"name": "poll", | ||
"version": "3.2.1", | ||
"license": "MIT", | ||
"description": "A simple poll function based on async, await, and an infinite loop", | ||
"author": { | ||
"name": "Philipp Rudloff", | ||
"url": "https://kleinfreund.de" | ||
}, | ||
"homepage": "https://github.com/kleinfreund/poll", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/kleinfreund/poll.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/kleinfreund/poll/issues" | ||
}, | ||
"keywords": [ | ||
"poll", | ||
"async", | ||
"await" | ||
], | ||
"type": "module", | ||
"exports": "./dist/poll.js", | ||
"main": "./dist/poll.js", | ||
"module": "./dist/poll.js", | ||
"types": "./types/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"types" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"build": "rollup --config", | ||
"test": "jest", | ||
"prebuild": "npm run test", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^8.5.0", | ||
"@types/jest": "^29.1.2", | ||
"jest": "^29.2.0", | ||
"jest-environment-jsdom": "^29.2.0", | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"ts-jest": "^29.0.3", | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.8.4" | ||
} | ||
"name": "poll", | ||
"version": "3.2.2", | ||
"license": "MIT", | ||
"description": "A simple poll function based on async, await, and an infinite loop", | ||
"author": { | ||
"name": "Philipp Rudloff", | ||
"url": "https://kleinfreund.de" | ||
}, | ||
"homepage": "https://github.com/kleinfreund/poll", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/kleinfreund/poll.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/kleinfreund/poll/issues" | ||
}, | ||
"keywords": [ | ||
"poll", | ||
"async", | ||
"await" | ||
], | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./types/index.d.ts", | ||
"default": "./dist/poll.js" | ||
}, | ||
"./types/index.d.ts": "./types/index.d.ts" | ||
}, | ||
"main": "./dist/poll.js", | ||
"module": "./dist/poll.js", | ||
"types": "./types/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"types" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"build": "rollup --config", | ||
"test": "vitest run --coverage", | ||
"prebuild": "npm run test", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"@rollup/plugin-typescript": "^11.1.1", | ||
"@vitest/coverage-c8": "^0.31.1", | ||
"jsdom": "^22.0.0", | ||
"rollup": "^3.22.0", | ||
"tslib": "^2.5.1", | ||
"typescript": "^5.0.4", | ||
"vitest": "^0.31.1" | ||
} | ||
} |
174
README.md
@@ -14,3 +14,3 @@ # poll | ||
- [**npmjs.com**/package/poll](https://www.npmjs.com/package/poll) | ||
- [on BundlePhobia](https://bundlephobia.com/result?p=poll) | ||
- [on BundlePhobia](https://bundlephobia.com/result?p=poll) | ||
- [**github.com**/kleinfreund/poll](https://github.com/kleinfreund/poll) | ||
@@ -21,11 +21,11 @@ | ||
- [Installation & usage](#installation-&-usage) | ||
- [npm package](#npm-package) | ||
- [Plain file](#plain-file) | ||
- [As npm package](#as-npm-package) | ||
- [As plain JS file](#as-plain-js-file) | ||
- [Documentation](#documentation) | ||
- [Syntax](#syntax) | ||
- [Syntax](#syntax) | ||
- [Examples](#examples) | ||
- [Minimal](#minimal) | ||
- [Stop polling](#stop-polling) | ||
- [Stop polling using asynchronous `shouldStopPolling` function](#stop-polling-using-asynchronous-shouldstoppolling-function) | ||
- [Exponential backoff: increase polling interval with every cycle](#exponential-backoff-increase-polling-interval-with-every-cycle) | ||
- [Minimal](#minimal) | ||
- [Stop polling](#stop-polling) | ||
- [Stop polling using asynchronous `shouldStopPolling` function](#stop-polling-using-asynchronous-shouldstoppolling-function) | ||
- [Exponential backoff: increase polling interval with every cycle](#exponential-backoff-increase-polling-interval-with-every-cycle) | ||
- [Versioning](#versioning) | ||
@@ -36,44 +36,44 @@ - [Update package version](#update-package-version) | ||
### npm package | ||
### As npm package | ||
1. Install the `poll` package. | ||
```sh | ||
npm install poll | ||
``` | ||
```sh | ||
npm install poll | ||
``` | ||
2. Import the `poll` function and use it. | ||
```js | ||
// “poll” is mapped to “poll/dist/poll.js” by Node.js via the package’s “exports” field. | ||
import { poll } from 'poll' | ||
```js | ||
// “poll” is mapped to “poll/dist/poll.js” by Node.js via the package’s “exports” field. | ||
import { poll } from 'poll' | ||
function fn() { | ||
console.log('Hello, beautiful!') | ||
} | ||
function fn() { | ||
console.log('Hello, beautiful!') | ||
} | ||
poll(fn, 1000) | ||
``` | ||
poll(fn, 1000) | ||
``` | ||
### Plain file | ||
### As plain JS file | ||
1. Download the `poll` module. | ||
```sh | ||
curl -O https://raw.githubusercontent.com/kleinfreund/poll/main/dist/poll.js | ||
``` | ||
```sh | ||
curl -O 'https://cdn.jsdelivr.net/npm/poll@latest/dist/poll.js' | ||
``` | ||
2. Import the `poll` function and use it. | ||
```html | ||
<script type="module"> | ||
import { poll } from './poll.js' | ||
```html | ||
<script type="module"> | ||
import { poll } from './poll.js' | ||
function fn() { | ||
console.log('Hello, beautiful!') | ||
} | ||
function fn() { | ||
console.log('Hello, beautiful!') | ||
} | ||
poll(fn, 1000) | ||
</script> | ||
``` | ||
poll(fn, 1000) | ||
</script> | ||
``` | ||
@@ -91,22 +91,22 @@ ## Documentation | ||
- **Name**: `fn`<br> | ||
**Type**: `() => any`<br> | ||
**Required**: Yes<br> | ||
**Description**: A function to be called every `delay` milliseconds. No parameters are passed to `fn` upon calling it. | ||
**Type**: `() => any`<br> | ||
**Required**: Yes<br> | ||
**Description**: A function to be called every `delay` milliseconds. No parameters are passed to `fn` upon calling it. | ||
- **Name**: `delayOrDelayCallback`<br> | ||
**Type**: `number | (() => number)`<br> | ||
**Required**: Yes<br> | ||
**Description**: The delay (in milliseconds) to wait before calling the function `fn` again. If a function is provided instead of a number, it is evaluated during every polling cycle right before the wait period. If the delay is a negative number, zero will be used instead. | ||
**Type**: `number | (() => number)`<br> | ||
**Required**: Yes<br> | ||
**Description**: The delay (in milliseconds) to wait before calling the function `fn` again. If a function is provided instead of a number, it is evaluated during every polling cycle right before the wait period. If the delay is a negative number, zero will be used instead. | ||
- **Name**: `shouldStopPolling`<br> | ||
**Type**: `() => boolean | Promise<boolean>`<br> | ||
**Required**: No<br> | ||
**Default**: `() => false`<br> | ||
**Description**: A function (or a promise resolving to a function) indicating whether to stop the polling process by returning a truthy value (e.g. `true`). The `shouldStopPolling` callback function is called twice during one polling cycle: | ||
**Type**: `() => boolean | Promise<boolean>`<br> | ||
**Required**: No<br> | ||
**Default**: `() => false`<br> | ||
**Description**: A function (or a promise resolving to a function) indicating whether to stop the polling process by returning a truthy value (e.g. `true`). The `shouldStopPolling` callback function is called twice during one polling cycle: | ||
- After the result of the call to `fn` was successfully awaited (right before triggering a new delay period). | ||
- After the `delay` has passed (right before calling `fn` again). | ||
- After the result of the call to `fn` was successfully awaited (right before triggering a new delay period). | ||
- After the `delay` has passed (right before calling `fn` again). | ||
This guarantees two things: | ||
This guarantees two things: | ||
- A currently active execution of `fn` will be completed. | ||
- No new calls to `fn` will be triggered. | ||
- A currently active execution of `fn` will be completed. | ||
- No new calls to `fn` will be triggered. | ||
@@ -127,6 +127,6 @@ **Return value**: | ||
async function getStatusUpdates() { | ||
const pokemonId = Math.floor(Math.random() * 151 + 1) | ||
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonId}/`) | ||
const pokemon = await response.json() | ||
console.log(pokemon.name) | ||
const pokemonId = Math.floor(Math.random() * 151 + 1) | ||
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonId}/`) | ||
const pokemon = await response.json() | ||
console.log(pokemon.name) | ||
} | ||
@@ -148,7 +148,7 @@ | ||
function fn() { | ||
console.log('Hello, beautiful!') | ||
console.log('Hello, beautiful!') | ||
} | ||
setTimeout(() => { | ||
stopPolling = true | ||
stopPolling = true | ||
}, 1000) | ||
@@ -168,13 +168,13 @@ | ||
const shouldStopPolling = () => new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(stopPolling) | ||
}, 100) | ||
setTimeout(() => { | ||
resolve(stopPolling) | ||
}, 100) | ||
}) | ||
function fn() { | ||
console.log('Hello, beautiful!') | ||
console.log('Hello, beautiful!') | ||
} | ||
setTimeout(() => { | ||
stopPolling = true | ||
stopPolling = true | ||
}, 1000) | ||
@@ -198,13 +198,13 @@ | ||
async function getStatusUpdates() { | ||
const pokemonId = Math.floor(Math.random() * 151 + 1) | ||
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonId}/`) | ||
const pokemon = await response.json() | ||
const seconds = (Date.now() - startTime) / 1000 | ||
console.log('Seconds passed:', seconds, pokemon.name) | ||
const pokemonId = Math.floor(Math.random() * 151 + 1) | ||
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonId}/`) | ||
const pokemon = await response.json() | ||
const seconds = (Date.now() - startTime) / 1000 | ||
console.log('Seconds passed:', seconds, pokemon.name) | ||
} | ||
const delayCallback = () => { | ||
const currentDelay = delay | ||
delay *= 2 | ||
return currentDelay | ||
const currentDelay = delay | ||
delay *= 2 | ||
return currentDelay | ||
} | ||
@@ -223,6 +223,6 @@ | ||
```sh | ||
npm test | ||
npm run build | ||
``` | ||
```sh | ||
npm test | ||
npm run build | ||
``` | ||
@@ -232,28 +232,28 @@ 2. Commit the changes. | ||
```sh | ||
npm whomai | ||
``` | ||
```sh | ||
npm whomai | ||
``` | ||
If you’re not authenticated, do so using `npm login`. | ||
If you’re not authenticated, do so using `npm login`. | ||
4. Change the package’s version locally. | ||
```sh | ||
# See `npm version --help` for more options | ||
npm version minor | ||
``` | ||
```sh | ||
# See `npm version --help` for more options | ||
npm version minor | ||
``` | ||
This changes the version number in the package.json file and adds a new git tag matching the new version. | ||
This changes the version number in the package.json file and adds a new git tag matching the new version. | ||
5. Push your changes and the updated git tags separately. | ||
```sh | ||
git push | ||
git push --tags | ||
``` | ||
```sh | ||
git push | ||
git push --tags | ||
``` | ||
6. Publish the package. | ||
```sh | ||
npm publish | ||
``` | ||
```sh | ||
npm publish | ||
``` |
@@ -5,16 +5,16 @@ /** | ||
export declare function poll( | ||
/** | ||
* The function to call. | ||
*/ | ||
fn: () => any, | ||
/** | ||
* The function to call. | ||
*/ | ||
fn: () => any, | ||
/** | ||
* The delay (in milliseconds) to wait before calling the function again. Can be a function. | ||
*/ | ||
delayOrDelayCallback: number | (() => number), | ||
/** | ||
* The delay (in milliseconds) to wait before calling the function again. Can be a function. | ||
*/ | ||
delayOrDelayCallback: number | (() => number), | ||
/** | ||
* A callback function indicating whether to stop polling. | ||
*/ | ||
shouldStopPolling?: () => boolean | Promise<boolean> | ||
/** | ||
* A callback function indicating whether to stop polling. | ||
*/ | ||
shouldStopPolling?: () => boolean | Promise<boolean> | ||
): Promise<void> |
8
10316