@hotmeshio/hotmesh
Version:
Permanent-Memory Workflows & AI Agents
123 lines (122 loc) • 4.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
// Import the new submodules
const config_1 = require("./config");
const throttling_1 = require("./throttling");
const error_handling_1 = require("./error-handling");
const lifecycle_1 = require("./lifecycle");
const consumption_1 = require("./consumption");
class Router {
constructor(config, stream, logger) {
// Legacy properties for backward compatibility
this.errorCount = 0;
this.counts = {};
this.currentTimerId = null;
this.sleepPromiseResolve = null;
this.innerPromiseResolve = null;
this.isSleeping = false;
this.sleepTimout = null;
this.isUsingNotifications = false;
// Apply configuration defaults
const enhancedConfig = config_1.RouterConfigManager.setDefaults(config);
this.appId = enhancedConfig.appId;
this.guid = enhancedConfig.guid;
this.role = enhancedConfig.role;
this.topic = enhancedConfig.topic;
this.stream = stream;
this.reclaimDelay = enhancedConfig.reclaimDelay;
this.reclaimCount = enhancedConfig.reclaimCount;
this.logger = logger;
this.readonly = enhancedConfig.readonly;
this.retryPolicy = enhancedConfig.retryPolicy;
// Initialize submodule managers
this.throttleManager = new throttling_1.ThrottleManager(enhancedConfig.throttle);
this.errorHandler = new error_handling_1.ErrorHandler();
this.lifecycleManager = new lifecycle_1.LifecycleManager(this.readonly, this.topic, this.logger, this.stream);
this.consumptionManager = new consumption_1.ConsumptionManager(this.stream, this.logger, this.throttleManager, this.errorHandler, this.lifecycleManager, this.reclaimDelay, this.reclaimCount, this.appId, this.role, this, this.retryPolicy);
this.resetThrottleState();
}
// Legacy compatibility methods
get throttle() {
return this.throttleManager.getThrottle();
}
get shouldConsume() {
return this.lifecycleManager.getShouldConsume();
}
set shouldConsume(value) {
this.lifecycleManager.setShouldConsume(value);
}
resetThrottleState() {
this.sleepPromiseResolve = null;
this.innerPromiseResolve = null;
this.isSleeping = false;
this.sleepTimout = null;
}
// Delegated methods to submodules
async createGroup(stream, group) {
return this.consumptionManager.createGroup(stream, group);
}
async publishMessage(topic, streamData, transaction) {
return this.consumptionManager.publishMessage(topic, streamData, transaction);
}
async customSleep() {
return this.throttleManager.customSleep();
}
async consumeMessages(stream, group, consumer, callback) {
return this.consumptionManager.consumeMessages(stream, group, consumer, callback);
}
isStreamMessage(result) {
return this.consumptionManager.isStreamMessage(result);
}
isPaused() {
return this.throttleManager.isPaused();
}
isStopped(group, consumer, stream) {
return this.lifecycleManager.isStopped(group, consumer, stream);
}
async consumeOne(stream, group, id, input, callback) {
return this.consumptionManager.consumeOne(stream, group, id, input, callback);
}
async execStreamLeg(input, stream, id, callback) {
return this.consumptionManager.execStreamLeg(input, stream, id, callback);
}
async ackAndDelete(stream, group, id) {
return this.consumptionManager.ackAndDelete(stream, group, id);
}
async publishResponse(input, output) {
return this.consumptionManager.publishResponse(input, output);
}
shouldRetry(input, output) {
return this.errorHandler.shouldRetry(input, output);
}
structureUnhandledError(input, err) {
return this.errorHandler.structureUnhandledError(input, err);
}
structureUnacknowledgedError(input) {
return this.errorHandler.structureUnacknowledgedError(input);
}
structureError(input, output) {
return this.errorHandler.structureError(input, output);
}
// Static methods for instance management
static async stopConsuming() {
return lifecycle_1.InstanceRegistry.stopAll();
}
async stopConsuming() {
return this.lifecycleManager.stopConsuming(this);
}
cancelThrottle() {
this.throttleManager.cancelThrottle();
this.resetThrottleState();
}
setThrottle(delayInMillis) {
config_1.RouterConfigManager.validateThrottle(delayInMillis);
this.throttleManager.setThrottle(delayInMillis);
}
// Static instances property for backward compatibility
static get instances() {
return lifecycle_1.InstanceRegistry.getInstances();
}
}
exports.Router = Router;