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

No control over order of remotes().before hook #3434

Closed
bitmage opened this issue Jun 6, 2017 · 4 comments
Closed

No control over order of remotes().before hook #3434

bitmage opened this issue Jun 6, 2017 · 4 comments
Assignees
Labels

Comments

@bitmage
Copy link
Contributor

bitmage commented Jun 6, 2017

I have two hooks attached to remotes().before, like so:

app.remotes().before('*.*', test1)
app.remotes().before('Company.someAction', test2)

Regardless of which order I put them in, test2 always runs before test1. What is the intended order of operation of these hooks? Should I be able to control it by changing the order I define them in?

@bitmage
Copy link
Contributor Author

bitmage commented Jun 7, 2017

I did some more testing. The order is predictable as long as I have multiple hooks with the *.* pattern. As soon as I introduce a more specific pattern though, it runs before any of the general hooks.

To work around this, I've implemented a custom 'before' hook that intercepts everything with *.* and lets me do my own matching.

const _ = require('lodash')
const matchOrStar = ([p, s]) => p === '*' || p === s
const match = (pattern, string) => {
  if (!~string.indexOf('.')) return false
  const matchup = _.zip(
    pattern.split('.'),
    string.split('.')
  )
  return _.every(matchup, matchOrStar)
}

// Define a custom before hook because loopback changes the ordering based on the specificity of the pattern.
// We don't want that!
const before = (pattern, cb) => {
  app.remotes().before('*.*', (ctx, next) => {
    match(pattern, ctx.methodString) ? cb(ctx, next) : next()
  })
}

before('*.*', lookupAccountId)
before('Company.analysis', cacheStuff)

This works, but it's pretty disapointing that I have to jump through these hoops.

@bajtos
Copy link
Member

bajtos commented Jun 7, 2017

The order is predictable as long as I have multiple hooks with the . pattern. As soon as I introduce a more specific pattern though, it runs before any of the general hooks.

I believe this is by design: we run more specific patterns first, before running general patterns like *.*. See strong-remoting's lib/remote-objects.js

@raymondfeng @ritch could you please confirm?

If you want to setup a "before" hook that's always executed before any other "beforeRemote" hooks, then you should use a custom strong-remoting phase as explained here: https://loopback.io/doc/en/lb3/Using-current-context.html#use-a-custom-strong-remoting-phase

@stale
Copy link

stale bot commented Aug 23, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Sep 6, 2017
@0candy 0candy removed the triaging label Sep 6, 2017
@stale
Copy link

stale bot commented Sep 6, 2017

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

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

No branches or pull requests

3 participants