UNPKG

durabull

Version:

A durable workflow engine built on top of BullMQ and Redis

121 lines (120 loc) 4.6 kB
"use strict"; /** * Decorators for workflow methods */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isWebhookMethod = exports.getWebhookMethods = exports.WebhookMethod = exports.getQueryMethods = exports.getSignalMethods = exports.QueryMethod = exports.SignalMethod = exports.WEBHOOK_METHODS = exports.QUERY_METHODS = exports.SIGNAL_METHODS = void 0; exports.SIGNAL_METHODS = Symbol('durabull:signalMethods'); exports.QUERY_METHODS = Symbol('durabull:queryMethods'); exports.WEBHOOK_METHODS = Symbol('durabull:webhookMethods'); const ensureMetadata = (ctor, key) => { if (!ctor[key]) { ctor[key] = []; } return ctor[key]; }; function SignalMethod() { return function signalDecorator(targetOrValue, propertyKeyOrContext) { if (typeof propertyKeyOrContext === 'string' || typeof propertyKeyOrContext === 'symbol') { const target = targetOrValue; const ctor = target.constructor; ensureMetadata(ctor, exports.SIGNAL_METHODS).push(propertyKeyOrContext.toString()); return; } const context = propertyKeyOrContext; if (!context || context.kind !== 'method') { return; } context.addInitializer(function () { const ctor = this.constructor; ensureMetadata(ctor, exports.SIGNAL_METHODS).push(String(context.name)); }); }; } exports.SignalMethod = SignalMethod; /** * Mark a workflow method as a query (read-only, does not advance replay) */ function QueryMethod() { return function queryDecorator(targetOrValue, propertyKeyOrContext) { if (typeof propertyKeyOrContext === 'string' || typeof propertyKeyOrContext === 'symbol') { const target = targetOrValue; const ctor = target.constructor; ensureMetadata(ctor, exports.QUERY_METHODS).push(propertyKeyOrContext.toString()); return; } const context = propertyKeyOrContext; if (!context || context.kind !== 'method') { return; } context.addInitializer(function () { const ctor = this.constructor; ensureMetadata(ctor, exports.QUERY_METHODS).push(String(context.name)); }); }; } exports.QueryMethod = QueryMethod; /** * Get signal methods for a workflow class */ function getSignalMethods(workflowClass) { if (typeof workflowClass !== 'object' && typeof workflowClass !== 'function') { return []; } const store = workflowClass; return Array.isArray(store[exports.SIGNAL_METHODS]) ? store[exports.SIGNAL_METHODS] : []; } exports.getSignalMethods = getSignalMethods; /** * Get query methods for a workflow class */ function getQueryMethods(workflowClass) { if (typeof workflowClass !== 'object' && typeof workflowClass !== 'function') { return []; } const store = workflowClass; return Array.isArray(store[exports.QUERY_METHODS]) ? store[exports.QUERY_METHODS] : []; } exports.getQueryMethods = getQueryMethods; /** * Mark a workflow method as webhook-accessible * Can be applied to workflow start (execute) or signal methods */ function WebhookMethod() { return function webhookDecorator(targetOrValue, propertyKeyOrContext) { if (typeof propertyKeyOrContext === 'string' || typeof propertyKeyOrContext === 'symbol') { const target = targetOrValue; const ctor = target.constructor; ensureMetadata(ctor, exports.WEBHOOK_METHODS).push(propertyKeyOrContext.toString()); return; } const context = propertyKeyOrContext; if (!context || context.kind !== 'method') { return; } context.addInitializer(function () { const ctor = this.constructor; ensureMetadata(ctor, exports.WEBHOOK_METHODS).push(String(context.name)); }); }; } exports.WebhookMethod = WebhookMethod; /** * Get webhook methods for a workflow class */ function getWebhookMethods(workflowClass) { if (typeof workflowClass !== 'object' && typeof workflowClass !== 'function') { return []; } const store = workflowClass; return Array.isArray(store[exports.WEBHOOK_METHODS]) ? store[exports.WEBHOOK_METHODS] : []; } exports.getWebhookMethods = getWebhookMethods; /** * Check if a specific method is webhook-accessible */ function isWebhookMethod(workflowClass, methodName) { const webhookMethods = getWebhookMethods(workflowClass); return webhookMethods.includes(methodName); } exports.isWebhookMethod = isWebhookMethod;