observation-js
Version:
A fully-typed TypeScript client for the waarneming.nl API.
42 lines (41 loc) • 1.11 kB
JavaScript
/**
* @internal
* Manages a collection of interceptors.
*/
export class InterceptorManager {
handlers = [];
/**
* Adds a new interceptor to the manager.
*
* @param onFulfilled - The function to run when the promise is fulfilled.
* @param onRejected - The function to run when the promise is rejected.
* @returns An ID that can be used to remove the interceptor later.
*/
use(onFulfilled, onRejected) {
this.handlers.push({ onFulfilled, onRejected });
return this.handlers.length - 1;
}
/**
* Removes an interceptor from the manager by its ID.
*
* @param id - The ID of the interceptor to remove.
*/
eject(id) {
if (this.handlers[id]) {
this.handlers[id] = null;
}
}
/**
* Iterates over all registered interceptors.
*
* @param fn - The function to execute for each interceptor.
* @internal
*/
forEach(fn) {
this.handlers.forEach((handler) => {
if (handler !== null) {
fn(handler);
}
});
}
}