UNPKG

chrono-forge

Version:

A comprehensive framework for building resilient Temporal workflows, advanced state management, and real-time streaming activities in TypeScript. Designed for a seamless developer experience with powerful abstractions, dynamic orchestration, and full cont

39 lines (38 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Debounce = Debounce; const workflow_1 = require("@temporalio/workflow"); function Debounce(ms) { let currentScope = null; const executeOriginalMethod = async (originalMethod, args, context) => { return await workflow_1.CancellationScope.nonCancellable(async () => { const result = originalMethod.apply(context, args); return result instanceof Promise ? await result : result; }); }; const debounceMethod = async (originalMethod, args, context) => { if (currentScope !== null) { currentScope.cancel(); } try { return await workflow_1.CancellationScope.cancellable(async () => { currentScope = workflow_1.CancellationScope.current(); await (0, workflow_1.sleep)(ms); const result = await executeOriginalMethod(originalMethod, args, context); currentScope = null; return result; }); } catch (e) { if (!(0, workflow_1.isCancellation)(e)) { throw e; } } }; return (target, propertyKey, descriptor) => { const originalMethod = descriptor.value; descriptor.value = async function (...args) { return await Promise.resolve().then(() => debounceMethod(originalMethod, args, this)); }; }; }