cujojs / most

Ultra-high performance reactive programming
MIT License
3.49k stars 206 forks source link

Recommended pattern for joining Backbone to MostJS #463

Closed far-blue closed 7 years ago

far-blue commented 7 years ago

Summary

Backbone has events that fire when model properties change, models are added to collections etc. and it seems to me it would be useful if those events could become streams and that streams could be consumed to update models etc.

Firstly, as I'm pretty new to mostjs, do people think this is reasonable or have I missed a concept?

Secondly, has anyone already tackled this and do people have thoughts or recommendations on the best architecture? For instance, would a single stream of mixed events be a good idea or would event-type-specific streams be more sensible? Should the model instance itself be on the stream or only a representation of the before and after values for the properties that changed?

Maybe a generic solution isn't worth it and a case-by-case approach is best?

briancavalier commented 7 years ago

Hi @far-blue. It seems perfectly reasonable to me.

I've done a little work with Backbone, but not much recently. It's hard for me to say what will be a better general approach (model instances vs. before/after). A concrete use case might be a good way to start, as a guide toward a more general solution. Do you have a relatively simple use case we could use as a starting point?

My guess is that keeping things simple and emulating Backbone's own event payloads may be a good way to go.

As far as a single stream of "mixed" events goes, I don't know what will work best for Backbone. However, heterogeneous stream tend to end up causing more problems that they solve in many cases. We tend to prefer homogeneous (see the TypeScript definitions).

Also, this may be the kind of question that is helpful to work through in gitter. There are plenty of knowledgeable folks in there, and some may have more knowledge of Backbone, or may have even used it with most.js.

Hope that's helpful in moving this forward.

far-blue commented 7 years ago

I'll certainly try out gitter on monday when I'm back at work :)

I actually don't use Backbone itself - I use Type-R and NestedTypes - but they have a 'backbone compatible' event system (https://github.com/Volicon/Type-R). I've raised the suggestion of supporting Observable interop with them (https://github.com/Volicon/Type-R/issues/21) and they seem interested in the idea. Being pretty new to mostjs I thought it best to ask for input and suggestions from others with more experience and I'll do my best to summarise recommendations to put to the Type-R developers :)

I also put together a gist with very basic Observable interop for Type-R (https://gist.github.com/far-blue/05c679b4e4cb5fe1df1d36006e8a21fa). I wrote it to generically support any event but given your comments about mixed events and the discussion in the Type-R issue linked above, I'd be interested in recommended approaches for avoiding mixed event streams through specifying the kind of events a stream should have when you create the Observable (if that makes sense!). Specifically, MixtureJS (which Type-R is based on) can trigger and listen to custom events (Backbone event mixin style) while Type-R has some built-in events such as 'change' (Backbone Model and Collection style).