-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Description
Hey humans. I keep hitting a logic wall where I need to wait for hubot to finish processing middleware, but the only way to do that currently is to add more middleware, which doesn't work with my requirements. That approach could also be buggy, as you'd still want to resolve the promise even if the first middleware prevents others from executing.
Use case 1:
Unit testing expects res.reply strings to be transformed by middleware.
Currently I have to wait and observe the test helper's messages for a matching pattern, calling the test's done when the reply middleware stack finishes and finally outputs a message, because res.reply returns immediately but the transform happens asynchronously. Very pain.
If middleware.execute returned a promise, it would cascade up through runWithMiddleware and all the response methods that invoke it, like send, emote, reply etc. Much wow.
Then I could very simply do something like this:
it 'capitalises a string', (done) ->
res.reply 'sent string'
.then (context) ->
expect(context.strings).to.equal ['Sent String']
done()
Use case 2
after a user sends something to hubot, listener middleware stores the match and performs some lookup for correlating data. I need to use that data in the listener callback. I can't move the middleware logic to the callback (because reasons), but I can't reply until I know the middleware has completed.
I've heard that async logic support will be improved in the next version, but this is a pretty small change that will really increase the versatility of my bots, so I'm hoping I can get some help with a PR and get it through without waiting for broader roadmap stuff.
I'll post PR to follow up, but the middleware logic looks like black magic to me and I'm unfamiliar with the async library, so it won't be accepted without feedback. Thanks in advance.