Home Reference Source Test Repository

buildDocs/delay.js

  1. import { delay } from 'lodash';
  2. import { DecoratorConfig, DecoratorFactory } from './factory';
  3. import { PreValueApplicator } from './applicators';
  4. /**
  5. * Invokes func after wait milliseconds. Any additional arguments are provided to func when it's invoked.
  6. *
  7. * @param {number} wait The number of milliseconds to delay invocation.
  8. * @param {...*} [args] Additional arguments to invoke the function with
  9. * @example
  10. *
  11. * class MyClass {
  12. * value = 100;
  13. *
  14. * @Delay(20)
  15. * add(a) {
  16. * this.value += a;
  17. * }
  18. * }
  19. *
  20. * const myClass = new MyClass();
  21. *
  22. * myClass.add(10);
  23. *
  24. * myClass.value; // => 100;
  25. *
  26. * setTimeout(() => {
  27. * myClass.value; // => 110;
  28. * }, 30);
  29. */
  30. export const Delay = DecoratorFactory.createDecorator(new DecoratorConfig(function (value, wait, ...args) {
  31. return function (...invokeArgs) {
  32. return delay(value.bind(this), wait, ...invokeArgs, ...args);
  33. };
  34. }, new PreValueApplicator(), { setter: true }));
  35. export { Delay as delay };
  36. export default Delay;