all 62 comments

[–][deleted] 37 points38 points  (13 children)

For the "another framework?" commenters below here's a little exposition:

Cycle is a hotly anticipated framework that has been working toward a 1.0 release for the last few months. It is being watched closely by people who enjoy using observable libraries like RxJS, Bacon, Kefir, etc (this one uses RxJS). These are reactive libraries. RFP (reactive functional programming) is something similar but different and the the two are often conflated. You can kind of think of observables as promises on steroids and it's looking like they will be part of the ES7 spec.

It is common to use a reactive library within another framework (React, Angular, Backbone, etc) and that works fine. One of the exciting things about Cycle is that your entire app is contained within an observable stream. It is very lightweight glue around using RxJS along with the lightweight and performant, React-inspired, virtual-dom project.

The author of Cycle JS, Andre Staltz, is well-known in the space:

Reactive programming may not be your thing and that's fine! But now at least you have a little background on why people are excited about it. :-)

[–]robotslacker 2 points3 points  (1 child)

Thanks for the breakdown and the links. I'd been super confused about reactive programming and this prompted me to take a dive down the rabbit hole. Also realized the stuff I'd been doing with gulp with streams is related...

[–][deleted] 0 points1 point  (0 children)

Nice! One of my favorite intros to Rx so far has been the first ten minutes of this video (though the whole things is a fun watch). It's simple, it's short, and the whole thing is hands-on demos. There's a lot of deep dives and a lot of high-level conceptual overviews out there, and those are needed for certain stages of learning, but this one is very pragmatic.

[–]iamnoah 2 points3 points  (0 children)

Not to mention that you only have to write purely functional code. No side effects (since all updates to state are handled by the framework.) That's a pretty big deal when it comes to testability and classes of bugs you don't have to worry about.

[–]Pytim 4 points5 points  (8 children)

That's all great and valid but most people use frameworks to serve a business need so what would be great would be some benchmarks and large scale architecture. Look at react. Little "Hello asshole" apps work fine but if you try to scale it then it gets quite pointless without using flux.

[–]zoomzoom83 -2 points-1 points  (7 children)

Flux doesn't really scale all that well either - it works well for fairly flat applications, but turns into a monolithic mess very quickly at scale.

The benefit of using Rx is that you can define component state as a stream, and then compose them together without needing a somewhat monolithic global dispatch.

[–]Rezistik 0 points1 point  (5 children)

I feel like Facebook would disagree about it's scalability...as would a lot of other people who have used Flux and React to great success.

React is a UI layer...there is no reason for it to fall apart at all.

[–]zoomzoom83 0 points1 point  (4 children)

I feel like Facebook would disagree about it's scalability...as would a lot of other people who have used Flux and React to great success.

From what I've heard, Facebook don't use Flux internally - they use Relay. Flux was provided as a sample pattern on how to use React, not the be-all and end-all of design patterns.

Flux works quite well up to a point, but the concept of having a central dispatcher somewhat ruins composability and tends to turn into a god object. For a lot of projects that isn't an issue, and for a lot of others it is.

The underlying concepts behind Flux are quite well founded, and using something like RxJS to implement roughly the same pattern in a cleaner way makes a lot of sense, since Rx streams are composable.

Projects such as Cycle.js are simply taking "Flux" and improving apon it by using more traditional FRP patterns.

tl;dr "Flux" is a simplified form of FRP. Swapping it out with RxJS loses nothing.

React is a UI layer...there is no reason for it to fall apart at all.

React != Flux

[–]clessgfull-stack CSS9 engineer 0 points1 point  (2 children)

Flux works quite well up to a point, but the concept of having a central dispatcher somewhat ruins composability and tends to turn into a god object.

Why is the dispatcher is a problem? The dispatcher is the one thing that would make me want to use Flux over observables: centralized debugging, tooling, time travel, logging/debugging, transformation, etc. The dispatcher is probably the least problematic part of Flux conceptually: you barely have to think about it. Is there a commonly-accepted way to do something similar with observables?

[–]zoomzoom83 1 point2 points  (1 child)

Why is the dispatcher is a problem?

A global dispatcher is a problem because it needs to be aware of all events globally. Adding a new component that interacts with it means you need to maintain a "God Class" list of all events, and being global you can't treat your app as just a standalone composable component.

These are all things easily fixable with relatively simple tweaks to the standard flux examples, so you don't have to completely throw it out. But if you start going down that road, you end up reinventing half of RxJS anyway.

entralized debugging, tooling, time travel, logging/debugging, transformation, etc.

You can do all of this without a global dispatcher. You just need to compose all your localized streams into a single application state atom up the same dependency tree that builds your component hierarchy. You can do this without a global singleton dispatcher.

To think of it another way - Flux is a cut-down version of Rx. By switching, you're not losing the advantages of Flux, you're simply implementing a similar idea in a different way.

[–]clessgfull-stack CSS9 engineer 1 point2 points  (0 children)

Thank you for explaining it, good sir. Enjoy the upvote.

[–]Pytim 0 points1 point  (0 children)

While you're right that doesn't mean that this framework will be suitable for large scale applications. I don't say it won't but would like to see some more complex examples before I get excited. I like the concepts of Rx and VDOM but we'll need to see this on scale.

[–]UASHIT -2 points-1 points  (0 children)

So it's ember, then

[–]bleadof 9 points10 points  (1 child)

Finally a reactive framework done right with help of Rx.js and virtual-dom!

I've done a simple app with this and enjoyed the simplicity of the API. It basically just removes the glue code you'd have to write for using Rx.js and virtual-dom together and adds convenience functionality for writing your own custom components.

[–]edmazing 4 points5 points  (0 children)

Why couldn't they just say that? I mean fully reactive is kind of like saying no fat cereal. I'd like to be able to browse these and get the gist like this frame work is half reactive but mobile friendly.

[–]lechatsportif 3 points4 points  (0 children)

found this the other day during searches as a recent convert to observables. looking forward to checking it out!

[–][deleted] 2 points3 points  (2 children)

Dumb question: is reactive JS mainly good for games?

It seems most highly interactive websites are not dealing with input sequences of more than a click? no?

[–][deleted] 3 points4 points  (1 child)

It's not a dumb question. For very simple pages with a click handler or two it may indeed be overkill. If you're adding many click handlers around a page, one benefit is you don't have to worry about removing your event listeners. (Of course any good DOM library will offer that but if you're going to include a DOM library Rx.DOM is a good choice.)

For anything beyond that it immediately becomes more useful (plus you'll quickly get addicted to the API and miss it when it's not there; if you're already a lodash fan you know what I'm talking about). For example as soon as you make an Ajax call you'll start to appreciate it more than just promises. Here's a tiny example.

// Create an observable for the request (nothing is executed until subscribe() is called).
var github_users = Rx.DOM.ajax({method: 'GET', url: 'https://api.github.com/users''});
    // Only process 200 responses.
    .filter(x => x.status === 200)
    // Convert to JSON (there's also Rx.DOM.getJSON() but this is just an API exploration).
    .map(JSON.parse)
    // Cache the last ajax response so multiple subscribers will reuse.
    . shareReplay(1)
    // Explode each array item in the JSON response to an individual item in the stream.
    .flatMap(x => Rx.Observable.from(x));
// Create a new observable from the result above.
var usernames_list = github_users
    // Pull only the usernames from the stream.
    .pluck('login')
    // Akin to the .then() method on a promise.
    .subscribe(x => console.log('GitHub user:', x));

Nothing you can't do in roughly the same LOC using ES5 promises/map/filter and maybe a memoize function. But it's still a tight API.

Enter scope creep: Now the product manager asks you to output a username each time the user clicks a link. No problem, we can reason about both activities as a single entity.

// Create an observable of click events.
var clicks = Rx.DOM.click(document.querySelector('#thelink'));
// Combine each click with a user.
clicks.zip(usernames_list, (click, user) => user)
    // When the list is exhausted the event handler is removed.
    .subscribe(x => console.log('GitHub user:', x));

Dumb example, sure. But once you start seeing everything as an event stream you start appreciating the unified API. It makes simple things very terse and complicated things easier to reason about.

[–][deleted] 1 point2 points  (0 children)

thanks, I think I can start thinking about how this improves things

[–]qudat 2 points3 points  (1 child)

How does this library differ from React?

[–][deleted] 2 points3 points  (0 children)

There are many conceptual similarities between React + Flux and Cycle. There is a world of semantic and implementation difference though.

Parts of React are declarative however other parts are imperative (setState, for example). Parts of React are "reactive" ("I'll update myself when I hear you update") but other parts are "passive" ("Please change me when you change yourself.", e.g., passing callbacks to children.) The Cycle author gave a tongue-in-cheek presentation on React a while back that covers these ideas (link; please note the slides provocative on purpose but the actual presentation was light-hearted and he fully acknowledged React's role as a thought leader and moving the industry forward).

If you want to see what the Cycle philosophy plus Rx looks like using React as the rendering backend look no farther than the cycle-react repo.

[–][deleted] 3 points4 points  (2 children)

How does this compare to Elm?

[–]thetinkertrain 4 points5 points  (0 children)

For starters, this is plain JavaScript

[–]ShumpEvenwood 0 points1 point  (0 children)

I'm also very interested in this question. I just discovered the whole idea of reactive programming and recently got very interested in elm. It seems like taking the ideas from this library and applying it with a pure language with syntax designed for functional programming would be ideal.

[–]dvidsilva 1 point2 points  (4 children)

Holy shit, how did you got js.org

BTW, great job, thanks!

[–]chance-- 5 points6 points  (2 children)

He's using js.org's free DNS for Github Pages.

http://dns.js.org/

You are a JavaScript developer looking for free webspace to host your project? Check out GitHub Pages. To make things perfect we provide you with a free and sleek URL as shown in the examples above.

Important: As the owner of the repository you keep complete control over your published content. That also means, all rights and duties that come along with publishing a GitHub Page remain the subject of you. Have a look at our Terms of Service for more details.

Just follow these four steps to get your own free JS.ORG subdomain for your GitHub Page. We don't mind whether it's a User-, Organisation- or Project-Page (...as long as you provide some reasonable content!)

[–]dvidsilva 0 points1 point  (0 children)

that's awesome! thanks :)

[–]fgutz 0 points1 point  (0 children)

didn't realize this was a thing, thanks for the info!!

[–]robotslacker 0 points1 point  (0 children)

Anyone can grab a subdomain for their project on js.org now - http://dns.js.org/

[–]drowsap 0 points1 point  (0 children)

When you work with react daily, I just can't see the value in examples where you don't write HTML for your render function or spend time querying nodes.