vlio20 / utils-decorators

Decorators for web and node applications
https://vlio20.github.io/utils-decorators/
MIT License
216 stars 13 forks source link

Request for New Feature: Enhancing Decorator Functionality with Queuing Mechanism #163

Closed xziy closed 1 month ago

xziy commented 9 months ago

Feature Request:

Overview:

The current decorator function, queued(), aims to provide a queuing mechanism for asynchronous operations. However, it could be improved to offer enhanced functionality and broader applicability.

Code Sample:

interface QueueItem {
  queue: Promise<void>;
}

function queued() {
  return function (_target: any, _key: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>) {
    const operationQueue: QueueItem = { queue: Promise.resolve() };

    const method = descriptor.value!;
    descriptor.value = async function (...args: any[]) {
      const operation = operationQueue.queue.then(async () => {
        return await method.apply(this, args);
      });
      operationQueue.queue = operation.catch(() => {});
      return operation;
    };
    return descriptor;
  };
}

Additional Information:

Possible Implementation:

Your contributions and insights to enhance this decorator are highly appreciated. Thank you for considering this request.

i found it here: https://gist.github.com/RomkeVdMeulen/f774324202d3fb8b710e7e2b1dcfdeb0#file-operationqueue-ts

vlio20 commented 9 months ago

Hi, can't you achive the same withthe ThrottleAsync decorator: https://vlio20.github.io/utils-decorators/#throttleAsync