Fix toobusy-js types, add docs

This commit is contained in:
Dimitri Benin 2018-12-05 15:25:22 +01:00
parent e1a9b84984
commit b4cc8ebfab
2 changed files with 64 additions and 9 deletions

View File

@ -1,19 +1,66 @@
// Type definitions for toobusy-js 0.5
// Project: https://github.com/STRML/node-toobusy
// Definitions by: Arne Schubert <https://github.com/atd-schubert>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export = toobusy_js;
/**
* Main export function.
* @return True if node process is too busy.
*/
declare function toobusy_js(): boolean;
declare namespace toobusy_js {
function interval(newInterval: number): number;
/**
* Sets or gets the current check interval.
* If you want more sensitive checking, set a faster (lower) interval. A lower maxLag can also create a more
* sensitive check.
* @param [newInterval] New interval to set. If not provided, will return the existing interval.
* @return New or existing interval.
*/
function interval(newInterval?: number): number;
/**
* Returns last lag reading from last check interval.
* @return Lag in ms.
*/
function lag(): number;
function maxLag(newLag: number): number;
function smoothingFactor(newFactor: number): number;
/**
* Set or get the current max latency threshold. Default is 70ms.
*
* Note that if event loop lag goes over this threshold, the process is not always 'too busy' - the farther
* it goes over the threshold, the more likely the process will be considered too busy.
*
* The percentage is equal to the percent over the max lag threshold. So 1.25x over the maxLag will indicate
* too busy 25% of the time. 2x over the maxLag threshold will indicate too busy 100% of the time.
* @param [newLag] New maxLag (highwater) threshold.
* @return New or existing maxLag (highwater) threshold.
*/
function maxLag(newLag?: number): number;
/**
* Set or get the smoothing factor. Default is 0.3333....
*
* The smoothing factor per the standard exponential smoothing formula "αtn + (1-α)tn-1"
* See: https://en.wikipedia.org/wiki/Exponential_smoothing
*
* @param [newFactor] New smoothing factor.
* @return New or existing smoothing factor.
*/
function smoothingFactor(newFactor?: number): number;
/**
* Shuts down toobusy.
*
* Not necessary to call this manually, only do this if you know what you're doing. `unref()` is called
* on toobusy's check interval, so it will never keep the server open.
*/
function shutdown(): void;
/**
* Registers an event listener for lag events,
* optionally specify a minimum value threshold for events being emitted
* @param fn Function of form onLag(value: number) => void
* @param [threshold=maxLag] Optional minimum lag value for events to be emitted
*/
function onLag(fn: (lag: number) => void, threshold?: number): void;
function started(): boolean;
}

View File

@ -1,4 +1,4 @@
import toobusy = require("toobusy-js");
import toobusy = require('toobusy-js');
let numberValue = 1;
let booleanValue = true;
@ -7,11 +7,19 @@ booleanValue = toobusy();
booleanValue = toobusy.started();
numberValue = toobusy.interval(numberValue);
numberValue = toobusy.interval();
numberValue = toobusy.lag();
numberValue = toobusy.maxLag(numberValue);
numberValue = toobusy.maxLag();
numberValue = toobusy.smoothingFactor(numberValue);
toobusy.onLag((duration: number) => {});
toobusy.onLag((duration: number) => {}, numberValue);
numberValue = toobusy.smoothingFactor();
toobusy.shutdown();
toobusy.onLag(duration => {
// $ExpectType number
duration;
});
toobusy.onLag(duration => {
// $ExpectType number
duration;
}, numberValue);