Skip to content

Support chainable hooks #111

Description

@pi0

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions