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

Add priorites #3

Open
dikaso opened this issue Oct 1, 2018 · 2 comments
Open

Add priorites #3

dikaso opened this issue Oct 1, 2018 · 2 comments

Comments

@dikaso
Copy link

dikaso commented Oct 1, 2018

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.

@unbug
Copy link
Owner

unbug commented Oct 8, 2018

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 unbug changed the title [REQUEST] Add priorites Add priorites Oct 8, 2018
@dikaso
Copy link
Author

dikaso commented May 6, 2019

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.

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

No branches or pull requests

2 participants