@akala/core
Version:
176 lines • 5.84 kB
JavaScript
import { AsyncTeardownManager } from "../teardown-manager.js";
;
/**
* Implements a configurable interval-based retry strategy.
* Retries with a delay that can be adjusted after each attempt using a step function.
*/
export class IntervalRetry {
handler;
startDelayInMs;
step;
/**
* Creates a new interval retry strategy.
* @param handler Function to call on each retry attempt
* @param startDelayInMs Initial delay in milliseconds before the first retry
* @param step Function that calculates the next delay given the current delay
*/
constructor(handler, startDelayInMs, step) {
this.handler = handler;
this.startDelayInMs = startDelayInMs;
this.step = step;
}
/**
* Creates an IntervalRetry with a fixed delay between retries.
* @param handler Function to call on each retry attempt
* @param delayInMs Fixed delay in milliseconds between retries
* @returns A new IntervalRetry instance with fixed interval
*/
static fixedInterval(handler, delayInMs) {
return new IntervalRetry(handler, delayInMs, () => delayInMs);
}
timeout;
/**
* Starts the retry strategy by scheduling the first attempt.
*/
start() {
const nextRetry = (delayInMs) => {
this.timeout = setTimeout(() => {
this.handler();
nextRetry(this.step(delayInMs));
}, delayInMs);
};
if (!this.timeout)
nextRetry(this.startDelayInMs);
}
/**
* Stops the retry strategy by clearing any pending timeout.
*/
stop() {
if (this.timeout)
clearTimeout(this.timeout);
this.timeout = undefined;
}
}
export class SocketTransformerWithConnectionRetry extends AsyncTeardownManager {
constructor(socket, strategy) {
super([socket.on('close', () => this.shouldRetry ? strategy.start() : strategy.stop())]);
}
/**
* Flag indicating whether retries should be attempted on disconnection.
*/
shouldRetry = true;
receive(data, self) {
return [data];
}
send(data, self) {
return data;
}
close(socket) {
this.shouldRetry = false;
return Promise.resolve();
}
}
/**
* Wraps a socket adapter to automatically retry on connection loss.
* Combines a socket adapter with a retry strategy to handle automatic reconnection attempts.
* @template T The type of messages handled by the socket
*/
export class SocketWithConnectionRetry extends AsyncTeardownManager {
inner;
strategy;
/**
* Flag indicating whether retries should be attempted on disconnection.
*/
shouldRetry = true;
/**
* Creates a new socket adapter with connection retry capability.
* @param inner The underlying socket adapter to wrap
* @param strategy The retry strategy to use on connection loss
*/
constructor(inner, strategy) {
super([inner.on('close', () => this.shouldRetry ? strategy.start() : strategy.stop())]);
this.inner = inner;
this.strategy = strategy;
}
/**
* Gets the open status of the wrapped socket adapter.
*/
get open() {
return this.inner.open;
}
;
/**
* Closes the socket and prevents further retry attempts.
*/
close() {
this.shouldRetry = false;
return this.inner.close();
}
/**
* Sends a message through the wrapped socket adapter.
* @param data The message to send
*/
send(data) {
return this.inner.send(data);
}
/**
* Pipes incoming messages to another socket adapter.
* @param socket The destination socket to send messages to
*/
pipe(socket) {
return this.inner.pipe(socket);
}
/**
* Checks if a listener is registered for the specified event.
* @param name The event name to check
* @returns true if a listener is registered for the event
*/
hasListener(name) {
return this.inner.hasListener(name);
}
/**
* Gets the list of defined event types.
*/
get definedEvents() {
return this.inner.definedEvents;
}
/**
* Emits an event to all registered listeners.
* @param event The event type to emit
* @param args Arguments to pass to event listeners
* @returns The result of the emit operation
*/
emit(event, ...args) {
return this.inner.emit(event, ...args);
}
/**
* Registers an event listener on the wrapped socket adapter.
* @param event The event type to listen for
* @param handler The callback to invoke when the event occurs
* @param options Optional event listener configuration
* @returns A subscription function to unsubscribe from the event
*/
on(event, handler, options) {
return this.inner.on(event, handler, options);
}
/**
* Registers an event listener that fires only once on the wrapped socket adapter.
* @param event The event type to listen for
* @param handler The callback to invoke when the event occurs
* @param options Optional event listener configuration (excluding 'once')
* @returns A subscription function to unsubscribe from the event
*/
once(event, handler, options) {
return this.inner.once(event, handler, options);
}
/**
* Removes an event listener from the wrapped socket adapter.
* @param event The event type to remove the listener from
* @param handler The event handler to remove, or undefined to remove all handlers
* @returns true if the listener was successfully removed
*/
off(event, handler) {
return this.inner.off(event, handler);
}
}
//# sourceMappingURL=shared.socket-transformer-with-connection-retry.js.map