Refraction – JS library to make modules independent, testable and re-usable
github.com
github.com
Using centralized message passing/routing/registering is just being lazy on handling dependency, it is like abandoning structure programming and starts using "goto" for every branch, which makes the logic flow eventually untraceable. For a system of N modules, it ideally should have around O(N) dependency edges, but a centralized hub is essentially modeling a possibly O(N^2). It only makes sense to introduce a hub when everyone potentially wants to talk to everyone.
And worst of all, every module using the framework now depends on the "hub" now. While the actual logic-related dependencies are all converted to "dynamically" instantiated ones, this "static" dependency on the hub becomes extremely hard to remove in the long term. It's like a cancer. It is not a good "pattern".
You are still going to introduce conceptual dependencies between modules. I'd personally rather have explicit control over those dependencies and their interfaces rather than passing it through a bus.
But anyway, y'all, if class A depends on class B, formalize the interface between the two (throw an error when expectations aren't met) and then inject the instance!
We had an event bus in an early implementation of our application and it was a huge pain later when we didn't know every place that was using and relying on a given event.
We switched to a much more explicit structure with defined dependencies and more boilerplate to reduce the "magic". It is much easier to debug and maintain over the long haul. The boilerplate stuff sucks to write, but when you are going to maintain the application for more than a few months, it takes MUCH less time to go back and understand than the alternatives.
Do you have a simple example of this for something like https://mbasso.github.io/refraction/docs/basics/Publishing.h... in react? Would you hard code in the dependencies if you have "multiple" modules that respond to "onUserLogin" "event".
Instead of fixed calls to other modules all modules use this library to call other modules' functions?
I think everyone has that idea at some point. I started writing my own but ended up going back to rigid coupling because unless I really decouple the system, for example to place modules in different processes or even machines, or letting different versions of certain modules "plug in" simply by loading them (run-time configurable) instead of having to change module names all over the code (needs to be done before deployment), it didn't really give me any benefit (at that early stage). I still use it for logging: I don't have to call a specific method, instead it's all just "messages", and when I have a "logger" listening for certain messages it logs them on the console, in production I'd leave the calls in but send them to a server, simply by loading another logger module, without code changes.
My statement is only for my own specific project, I found that whether to use such a method or not is highly specific.
So my only point is that trying to use such an approach "because it sounds cool" turned out not to be sufficient reason. IMHO the provided "Motivation" document is not sufficient (https://mbasso.github.io/refraction/docs/introduction/Motiva...). I think more could (should?) be said and explained.
Yeah, it looks like a pubsub system. The use of the word "module" here is misleading. It's not about ecmascript modules.
And as a bonus its not hard to imagine adding RPC capabilities to this library if one of the modules has performance characteristics that require a different type of scaling, so you want to move that module out to its own process running on a different machine.
Suggestions?