Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support chainable hooks #111

Open
pi0 opened this issue Jan 21, 2025 · 1 comment
Open

Support chainable hooks #111

pi0 opened this issue Jan 21, 2025 · 1 comment
Labels
discussion enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Jan 21, 2025

Describe the feature

Hookable hooks will be called sequentially by default with original arguments. But result of each step is not preserved.

We could export a built-in chainable hook runner.

Example from unjs/community#15 (@ChrisGV04)

  const chainableCaller = async (hooks, args) => {
      let hookResult: Order = args[0];

      for (const hook of hooks) {
        hookResult = await hook(hookResult);
      }

      return hookResult;
    }
  /** Before create hook */
  const orderData = await hooks.callHookWith(chainableCaller, 'orders:before-create', originalOrder);

Some ideas:

  • We could export a built-in chainableCaller
  • We could support hookable.callHookChained(name, <initial>) that chains first arg
  • Hooks can be registred as chainable <-- I prefer this most / to define strategy on register instead of use but needs few initial refactors
@pi0 pi0 added discussion enhancement New feature or request labels Jan 21, 2025
@ChrisGV04
Copy link

I like your preferred proposal to register a hook as chainable, since you should already know beforehand if you are going to need it or not.

But any of the three proposals look fine to me. Specially if we can achieve the same goal and not create any breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants