32 points · by BinaryIdiot · 11y ago · 18 comments
Not sure if others will find it useful but figured I’d see what feedback hacker news would have, if any :)
Both of them are battle tested and have a big community.
Also my curiosity in writing one as I originally wrote one almost two years ago called intend.js but, because of my job, I very rarely get time to write open source stuff so it took a while to move to msngr.js, refactor and release.
Also, what do you mean by live-binding ?
That's the problem. It's hard to get this done. Developers like to develop, it's like pulling teeth to get us to document things. I've found its much easier and faster to have a codebase that speaks-for-itself, in a way.
Don't get me wrong, we definitely do document things. But the miniscule things like implementation details are just too small to be worth spending time on.
We've been in production for about 7 years now. Our team has pretty much completely turned over in that time. Knowledge and conventions get lost, much better to have the codebase dictate how things should be done. A developer will read the code+comments before they read the documentation.
Hmm how to explain live-binding... Angular's $scope and CanJS' Observables are examples of live-binding. Basically, you can pass around a live-binding object and changes made to it in one module are immediately reflected in another. Whereas with a pub/sub model, you publish messages out to a hub and it is up to the module to decide how to react to the message.
A good example of how we use live-binding is with sessions. We have a global live-bound object that holds the session data. Each module that needs to use session info simply refers to this global object. If we perform an API call that returns 401 and kills the session, our API module kills the session object and all our modules immediately respond to this change. We don't have to declare any listeners, or subscribe to any pubs; we just use the object.
msngr.register({ topic: "Save", category: "Profile", dataType: "application/json" }, function (payload) {
console.log("Save profile information here");
});
msngr.emit({ topic: "Save", category: "Profile", dataType: "application/json" }, {
name: "Kris",
country: "United States"
});
Rather than: function saveProfile(payload) {
console.log("Save profile information here");
}
saveProfile({
name: "Kris",
country: "United States"
});The idea behind this is so you can create message receivers that can respond to a variety of items without even knowing the origin. Much like Android intents where intents can come from internally or externally these messages can be handled the same way (hence my primary use case is using them with web components; I can make a web component publish specific messages and have my business logic handle them without ever touching anything in the UI layer).
I started working on an experiment where I wrote two controllers that handled the UI with msngr.bind(), one for a web browser and one for a native application using node-qt (which is basically a dead project now and one of the reasons I never finished my experiment but I digress). The business logic never had to chance at all but the controllers could be swapped.
It's not hugely different than your example but I like the separation and descriptiveness it gives me. I also have plans to eventually revisit old code I have written where messages can cross web browser windows and the client-server boundary but not sure when I'll get to that.
Hope that helps!