dynatrace-api-balancer
Version:
A wrapper around Axios that balances and throttles requests across tenants, clusters and cluster nodes.
125 lines (99 loc) • 4.09 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lib/Cancellables.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: lib/Cancellables.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const EventEmitter = require('events');
/**
* A cancellable event emitter at which the progress of a request
* can be monitored and at which the request can be cancelled. See the NodeJS
* [EventEmitter]{@link https://nodejs.org/api/events.html#events_class_eventemitter}
* documentation for more information.
* @extends EventEmitter
*/
class CancellableEventEmitter extends EventEmitter {
/**
* Creates an instance.
* @param {object} request - An object that should expose an 'cancel()' method
* that accepts an optional reason (string).
*/
constructor(request) {
super();
this.request = request || { cancel: () => {} };
this.on('error', () => {}); // One MUST be registered. Otherwise... BOOM!
}
/**
* Cancel the request. If a reason is provided, it will cause the
* {@link RequestCallback} provided to the {@link BalancedAPIRequest#fetch fetch()} method
* to be called with a {@link RequestError} as it single argument, containing
* status 512 and this reason as its message.
*
* @param {string} reason - The reason for the cancellation. If omitted, the request will be cancelled or aborted silently.
*/
cancel(reason) {
try {
this.request.cancel(reason);
}
catch (ex) {
/* NOP */
}
return null;
}
}
/**
* A cancellable Promise tat which the request can be cancelled.
* @extends Promise
*/
class CancellablePromise extends Promise {
#onCancel = () => {};
constructor(executor) {
const setOnCancel = (cancel) => {
// Async because we can't use "this" before super().
setTimeout(() => { this.#onCancel = cancel });
}
const cancellableExecutor = (resolve, reject) => {
executor(resolve, reject, setOnCancel);
}
super(cancellableExecutor);
}
/**
* Cancel the request. If a reason is provided, it will cause the
* {@link CancellablePromise} returned by the {@link BalancedAPIRequest#fetch fetch()}
* method to be called with a {@link RequestError} as it single argument,
* containing status 512 and this reason as its message.
*
* @param {string} reason - The reason for the cancellation. If omitted, the request will be cancelled or aborted silently.
*/
cancel(reason) {
this.#onCancel(reason);
}
}
module.exports = { CancellableEventEmitter, CancellablePromise }</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BalancedAPIRequest.html">BalancedAPIRequest</a></li><li><a href="CancellableEventEmitter.html">CancellableEventEmitter</a></li><li><a href="CancellablePromise.html">CancellablePromise</a></li><li><a href="DirectAPIRequest.html">DirectAPIRequest</a></li><li><a href="Throttle.html">Throttle</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sun Dec 19 2021 10:28:39 GMT-0600 (Central Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>