hookable.ts 776 B

123456789101112131415161718192021222324252627282930313233
  1. import { hooks } from '~/services/dataDeleter/hook/_import'
  2. import { UrlArgs } from '~/types/interfaces'
  3. /**
  4. * Base class for an object which support hooks
  5. */
  6. class Hookable {
  7. /**
  8. * Iterate over the available hooks and invoke the ones
  9. * that support the given args
  10. */
  11. protected async triggerHooks (args: UrlArgs) {
  12. for (const Hook of Hookable.sortedHooks()) {
  13. if (Hook.support(args)) {
  14. await new Hook().invoke(args)
  15. }
  16. }
  17. }
  18. /**
  19. * Sort the available hooks by priority
  20. * @private
  21. */
  22. private static sortedHooks (): Iterable<any> {
  23. return hooks.sort(function (a, b) {
  24. if (a.priority > b.priority) { return 1 }
  25. if (a.priority < b.priority) { return -1 }
  26. return 0
  27. })
  28. }
  29. }
  30. export default Hookable