js-middleware icon indicating copy to clipboard operation
js-middleware copied to clipboard

Add priorites

Open dikaso opened this issue 5 years ago • 2 comments

Is it possible to add execution order of middleware.

Let's say we have multiple middlewares (middleware1, middlewarr2, middleware3) attached to walk function.

It would be great if I could specify that middlewarr2 should run first.

dikaso avatar Oct 01 '18 09:10 dikaso

Sorry @DinkoMiletic , I just got back from vacation. Middlewares are sequential, if you add middleware2 before middleware1, the middleware2 will be run first. If you don't want to change the order and you still want to run middleware2 first, you can always add an if(..) in your middleware1 to not run it in whatever case. You might have to add additional values to help your if(...). If you're writing a middleware object then you can add values in your class, see https://github.com/unbug/js-middleware#middlewaremethods

unbug avatar Oct 08 '18 09:10 unbug

I ended up with ordering middle-wares before they are run because each part of my app can register own middle-ware but middle-wares should be always executed by priority.


var i;
var hooks = [];
var hook;
var middleware = new Middleware();
// this.hook -> list of callbacks to run with priority property

if (angular.isDefined(this.hooks[name])) {
        hooks = $filter('orderBy')(this.hooks[name], 'priority', true);
        for (i = 0; i < hooks.length; i += 1) {
          hook = hooks[i].callback;
          middleware.use(hook);
        }
}

middleware.go.apply(this, Array.prototype.slice.call(arguments, 1));

Thanks anyway.

dikaso avatar May 06 '19 07:05 dikaso