tapable icon indicating copy to clipboard operation
tapable copied to clipboard

Do something with hook results

Open wmertens opened this issue 7 years ago • 12 comments

In my app, I would like to return early if one of the hooks returns some specific value. Would that be possible?

So for example hook.callUntil({args: [...], condition: result => result.foo}).

wmertens avatar Feb 05 '18 10:02 wmertens

You could use a do/while loop for that.

ooflorent avatar Feb 05 '18 12:02 ooflorent

@oofloren how would that work? There is no way to iterate hook subscribers, is there?

wmertens avatar Feb 08 '18 18:02 wmertens

Sorry, the notification got lost!

let result
do {
  result = hook.call(…)
} while (result.foo)

We use a similar technique in webpack:

https://github.com/webpack/webpack/blob/3024078e1e8ec3df00020acad2e2de8b199b27fc/lib/Compilation.js#L828-L834

ooflorent avatar Mar 20 '18 15:03 ooflorent

No, that calls all subscribers. The idea is to stop after the first subscriber that returns something specific.

is that what callTapsSeries does, can that be called directly?

wmertens avatar Mar 20 '18 15:03 wmertens

I think all call* methods are private APIs. They are designed to be called by the code factories. Not user-land code.

ooflorent avatar Mar 20 '18 16:03 ooflorent

So there is no way to achieve this without extending the public API?

On Tue, Mar 20, 2018 at 5:09 PM Florent Cailhol [email protected] wrote:

I think all call* methods are private APIs. They are designed to be called by the code factory. Not user-land code.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/webpack/tapable/issues/47#issuecomment-374656403, or mute the thread https://github.com/notifications/unsubscribe-auth/AADWlvABIV0pGkH90evPktrHcHpsYSFKks5tgSmggaJpZM4R5Mur .

wmertens avatar Mar 20 '18 17:03 wmertens

The idea is to stop after the first subscriber that returns something specific.

The Bail hooks stop after the first plugin that returns something.

sokra avatar Nov 27 '18 09:11 sokra

@sokra but that still doesn't allow checking for a specific value, and there's no way to wrap the hooks…

wmertens avatar Nov 27 '18 15:11 wmertens

@wmertens you can use bail hook if the the value is just what you need, just return it.

kisoua avatar May 31 '19 06:05 kisoua

@MoonYaph that would require wrapping all the subscribers to a hook and there's no way to do that.

wmertens avatar May 31 '19 07:05 wmertens

@wmertens Hi!

Did you manage to find a solution to this issue? I'd like to do the same thing, as well.

Telokis avatar Apr 20 '20 15:04 Telokis

@Telokis I ended up rewriting it with a similar API but without all the browser optimizations.

https://github.com/StratoKit/strato-runner/blob/new-new-config/packages/launcher/src/tapable.js

wmertens avatar Apr 22 '20 14:04 wmertens