question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(FEATURE FEEDBACK): Hub middleware

See original GitHub issue

Is your feature request related to a problem? Please describe. In order to minimize the number of dependencies in our apps, we’ve been using Hub as our default pub-sub event broker. It works well with low latency for the most part, but I think it could be made more robust with middleware.

Describe the solution you’d like The ability to apply middleware to Hub that will allow for things like custom logging (e.g. on listener groups), and modifying listeners at runtime, grouping listeners that share logic in order to attach that logic to the listener group (DRY principle), garbage collection, etc.

For instance, consider a set of Show(.*) and Hide(.*) listeners (e.g. ShowAppSplashScreen and HideAppSplashScreen), each being created on a different Component that (as a constraint) has the same name as the capture group in the regex. Suppose also that each of these components, or their containers, provide show() and hide() methods. Then any listener that matches Show(.*) is going to execute its holder object’s show() method when that event is sent.

Rather than duplicating that logic across every component by repetition in onHubCapsule, it would be cleaner to give Hub listener groups (perhaps by regex as in this example) and apply hooks that replace duplicated code.

In extreme cases, we have listeners that every component is listening to, like “UIReset”, with every component that listens to it having a corresponding reset() method.

Something like the addition of these methods:

Hub.createGroup(listenerNames, groupName) - create a group aliased groupName from list listenerNames Hub.createGroupByRegex(regex, groupName), with a use like this:

Hub.createGroupByRegex(/Show(.*)/, "ShowListeners") - create a group referencable with ShowListeners that middleware can be attached to

Hub.attachMiddlewareToGroup(groupName, f) - apply function f to any listener l in group on Hub.dispatch(l, data), with a sample use case:

Hub.attachMiddlewareToGroup("ShowListeners", (capsule) => {
    let holder = capsule.holder //assume that every emit includes a reference to the holder object
    holder.show();
}

Hub.attachMiddlewareToGroup("HideListeners", (capsule) => {
    let holder = capsule.holder //assume that every emit includes a reference to the holder object
    holder.hide();
}

Then for the extreme use case, a similar method that lets you apply middleware to a singel listener:

Hub.attachMiddlewareToListener("UIReset", (capsule) => {
    let holder = capsule.holder //assume that every emit includes a reference to the holder object
    holder.reset();
}

This would allow us to put a lot of repetitive logic inside Hub and not repeat it across many components. If the holder reference for each object in the group could be provided by Hub as an additional parameter to the attachMiddleware* function(s), that would allow attaching middleware without modifying existing dispatch code. Like this:

Hub.attachMiddlewareToGroup("ShowListeners", (capsule, holder) => {
    holder.show();
    //process the capsule if necessary
}

Thoughts? I’m sure this idea can be drastically improved.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
undefobjcommented, Feb 23, 2019

Hello @jkeys-ecg-nmsu. I’ve looked into this a bit. We had already been looking into some possible improvements for Hub so your suggestions are timely. There have been some patterns we were considering including changing Hub into a full blown Observable, but right now we don’t want to introduce any breaking changes and are unsure of the long term benefits here. However I think we can make some changes to still support your goals.

I have created a new version of the Hub module with some sample usage and posted it in the following repo: https://github.com/undefobj/hub_reboot

You’ll find a readme there with sample usage, and you can also download it and run some tests. Please let me know your feedback if this is in the direction you were looking for to improve your usage of the module, any changes in functionality you might require, or any future ideas for improvement. Thanks.

1reaction
undefobjcommented, Mar 28, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Send feedback to Microsoft with the Feedback Hub app
The Feedback Hub app lets you tell Microsoft about any problems you run into while using Windows 10. You can also send suggestions...
Read more >
What is Middleware? - IBM
Robotics middleware simplifies the process of integrating robotic hardware, firmware and software from multiple manufacturers and locations.
Read more >
Integration Hub | Connect seamlessly - District CMS
Districts Integration Hub is a middleware platform that simplifies connecting services and provides a growing list of third party integrations ...
Read more >
ScaleUp: middleware for intelligent environments - PMC - NCBI
Middleware may vary in terms of functionality, with different ... For master devices, the hub liaises with the communication system created ...
Read more >
Middleware Modernization - Boomi
Expand your integration capabilities beyond the core IT team. Accelerated Innovation. Drive customer-centric experiences based on data and feedback loops.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found