Skip to content

Commit

Permalink
fix(types): generate types from jsdoc comments so ts projects also ha…
Browse files Browse the repository at this point in the history
…ve the textual descriptions in addition to the type definitions
  • Loading branch information
lionralfs committed Aug 12, 2021
1 parent b12b278 commit f5fc3ff
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 39 deletions.
56 changes: 56 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @typedef {Object} PrecurringController
* @property {() => void} start Starts the interval
* @property {() => void} stop Stops the interval
*/
/**
* @template K
* @typedef {Object} PrecurringOptions
* @property {(...args: any[]) => Promise<K>} fn The function you want to repeatedly call, returning a Promise
* @property {number} interval Time in ms representing the time span between the last Promise settling and the next one firing
* @property {number} [timeout] The maximum amount of time in ms which the promise is allowed to take
* @property {(value: K) => any} onSuccess The success callback
* @property {(reason: K | Error) => any} onError The error callback
*/
/**
* @function
* @template K
*
* Creates a new instance
* @param {PrecurringOptions<K>} options An options object
*
* @returns {PrecurringController}
*/
export default function _default<K>({ fn, interval, timeout, onSuccess, onError }: PrecurringOptions<K>): PrecurringController;
export type PrecurringController = {
/**
* Starts the interval
*/
start: () => void;
/**
* Stops the interval
*/
stop: () => void;
};
export type PrecurringOptions<K> = {
/**
* The function you want to repeatedly call, returning a Promise
*/
fn: (...args: any[]) => Promise<K>;
/**
* Time in ms representing the time span between the last Promise settling and the next one firing
*/
interval: number;
/**
* The maximum amount of time in ms which the promise is allowed to take
*/
timeout?: number;
/**
* The success callback
*/
onSuccess: (value: K) => any;
/**
* The error callback
*/
onError: (reason: K | Error) => any;
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"umd:main": "dist/precurring.umd.js",
"module": "dist/precurring.mjs",
"source": "src/index.js",
"typings": "typings.d.ts",
"typings": "index.d.ts",
"scripts": {
"test": "npm run build && jest",
"build": "rm -rf dist/ && microbundle --generateTypes=false src/index.js",
"build": "rm -rf dist/ && microbundle src/index.js",
"dev": "rm -rf dist/ && microbundle watch"
},
"keywords": [
Expand All @@ -35,7 +35,7 @@
"files": [
"src",
"dist",
"typings.d.ts"
"index.d.ts"
],
"jest": {
"verbose": true,
Expand Down
36 changes: 22 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,38 @@ function callWithTimeout(fn, timeout) {
}

/**
* @typedef {Object} PrecurringController
* @property {() => void} start Starts the interval
* @property {() => void} stop Stops the interval
*/

/**
* @template K
* @typedef {Object} PrecurringOptions
* @property {(...args: any[]) => Promise<K>} fn The function you want to repeatedly call, returning a Promise
* @property {number} interval Time in ms representing the time span between the last Promise settling and the next one firing
* @property {number} [timeout] The maximum amount of time in ms which the promise is allowed to take
* @property {(value: K) => any} onSuccess The success callback
* @property {(reason: K | Error) => any} onError The error callback
*/

/**
* @function
* @template K
*
* Creates a new instance
* @param {object} options An options object
* @param {() => Promise} options.fn A promise
* @param {number} options.interval Time in ms representing the time span between the promise resolving and the next one firing
* @param {number} options.timeout The maximum amount of time in ms which the promise is allowed to take
* @param {(value: any) => any} options.onSuccess The success callback
* @param {(reason: any) => any} options.onError The error callback
* @param {PrecurringOptions<K>} options An options object
*
* @returns {{start: () => void, stop: () => void}}
* @returns {PrecurringController}
*/
export default function ({ fn, interval, timeout, onSuccess, onError }) {
let running = false;
return {
/**
* Starts the interval
*/
start: () => {
running = true;
(function run() {
if (!running) return;

callWithTimeout(fn, timeout)
.then((res) => {
if (running) {
Expand All @@ -85,9 +96,6 @@ export default function ({ fn, interval, timeout, onSuccess, onError }) {
});
})();
},
/**
* Stops the interval
*/
stop: () => {
running = false;
},
Expand Down
8 changes: 8 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import precurring from '.';

precurring({
fn: () => fetch('/'),
interval: 1000,
onError: (reason) => console.log(reason),
onSuccess: (val) => console.log(val),
});
22 changes: 0 additions & 22 deletions typings.d.ts

This file was deleted.

0 comments on commit f5fc3ff

Please sign in to comment.