Skip to content

Support chainable hooks #111

@pi0

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions