* Creates a handler factory with shared max-count gating and processedCount state. * * This eliminates the duplicated scaffolding across safe-output handlers: * - Extracts maxCount from config (default 10) * - Resolves staged mode from config * - Maintains processedCount in closure * - Gates e
({ handlerType, setup })
| 32 | * @returns {HandlerFactoryFunction} Handler factory function compatible with the handler manager |
| 33 | */ |
| 34 | function createCountGatedHandler({ handlerType, setup }) { |
| 35 | return async function main(config = {}) { |
| 36 | const maxCount = config.max || 10; |
| 37 | const isStaged = isStagedMode(config); |
| 38 | let processedCount = 0; |
| 39 | |
| 40 | const handleItem = await setup(config, maxCount, isStaged); |
| 41 | |
| 42 | /** @type {MessageHandlerFunction} */ |
| 43 | return async function handler(message, resolvedTemporaryIds) { |
| 44 | if (processedCount >= maxCount) { |
| 45 | core.warning(`Skipping ${handlerType}: max count of ${maxCount} reached`); |
| 46 | return { |
| 47 | success: false, |
| 48 | error: `Max count of ${maxCount} reached`, |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | processedCount++; |
| 53 | return handleItem(message, resolvedTemporaryIds); |
| 54 | }; |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | module.exports = { createCountGatedHandler }; |
no test coverage detected