Code Monkey home page Code Monkey logo

flocks.js's Introduction

flocks

A radically simpler alternative to Flux - opinionated React state and rendering management

Language   Platform   License   Status

NPM Downloads

What the hell is this?

So flux is pretty cool. But, I wanted something simpler, because I wanted to move faster. So, I created flocks - a flux alternative.

The primary goal of flocks.js is extreme simplicity.

There is a propaganda page developing at the website http://flocks.rocks/.

You can be up and running in five minutes. Try the Flocks quick start.

What's the big deal

The goal of the flux model is unidirectional state. That's admirable. It leads to large simplifications, and has a big positive impact on debugging.

However, it's also a very large amount of boilerplate, requires the implementation of a bunch of patterns to get anything done, and provides an eight point flowchart on how to update a value, which in practice is an under-sell of the complexity.

I wanted something simpler. I wanted something that felt like a global, that gave me transactionality, atomicity, and that didn't have me reimplementing things every time I started.

I wanted something where just calling a function got the job done.

So I created flocks.

Get on with it; show some code

This is a complete flocks app which shares data between three simple controls, all fully defined in this snippet. The only code not present in this example is the copies of react and flocks being pulled from CDN.

<!doctype html>
<html>

  <head>

    <meta charset="utf-8">
    <title>Example Spinner App</title>

    <style type="text/css">
      body   { font-size: 600%; font-family: helvetica neue, sans-serif; }
      button { font-size: 50%; margin: 2em; }
    </style>

    <script defer src="http://fb.me/JSXTransformer-0.12.2.js"></script>
    <script defer src="http://fb.me/react-0.12.2.js"></script>
    <script defer src="http://cdnjs.cloudflare.com/ajax/libs/flocks.js/0.15.1/flocks.js"></script>

    <script defer type="text/jsx">

      // up button control
      var Up = flocks.createClass({
        inc:    function() { this.fset('value', this.fctx['value'] + 1) },
        render: function() { return <button onClick={this.inc}></button>; }
      });

      // down button control
      var Down = flocks.createClass({
        dec:    function() { this.fset('value', this.fctx['value'] - 1) },
        render: function() { return <button onClick={this.dec}></button>; }
      });

      // the application root control
      var SpinnerApp = flocks.createClass({
        render: function() { return <div><Up/>{this.fctx['value']}<Down/></div>; }
      });

      // telling flocks where to put the app control, and what that control is
      var FlocksConfig = { target: document.body, control: SpinnerApp };
      var InitialState = { value: 0 };

      // and mount the app 😄
      flocks.mount(FlocksConfig, InitialState);

    </script>

  </head>

  <body></body>

</html>

Why is this different?

There's basically no boilerplate, and updating is easy.

There're no dispatchers; there're no actions; there're no stores.

It's all automatic. You don't need to manage anything, or worry about conflicts between actions.

Your applications become atomic, and transactional, very similar to state machines, very close to being purely declarative. You can validate states before they go into place.

The simplicity is unmatched.

var Config = {
      target  : document.body,
      control : YourControl
    },

    Data = {
      logged_in: true,
      user_name: "Bob Dobbs",
      icon: "http://upload.wikimedia.org/wikipedia/en/a/a3/Bobdobbs.png"
    },

    Update = Flocks.mount(Config, Data);

Done.

Want to change the state of the app?

Update.set("user_name", "JR \"Bob\" Dobbs");

Done.

If you want that from inside the control, or even inside the heirarchy, that's fine; props, state, and contexts will cascade down safely and normally.

Now, you just write your app as a single control, and keep state 100% outside of the control heirarchy. :)

How is this better?

It's:

  • Simpler
  • Centralized
  • Atomic
  • Transactional

Simpler

Learn two functions and one member of this, and you're off to the races.

Learn more tools as they become convenient.

Centralized

Your entire render state - maybe your entire state, if you like - in one simple place.

Read and write the whole thing at once, if convenient. This is surprisingly often convenient.

Atomic

Batch writes and locking let you prevent the rendering of intermediate states.

All the convenience of piecewise and group update with none of the flash of content.

Transactional

That's right, dust off your SQL brain, because you can write a function that invalidates and rejects application update sets.

Imagine what this will do for your debugging. 😹

Would you explain more clearly?

What Flocks actually does is to provide you with an update function.

To get started, tell Flocks where the mount point is and what control to use. Then, Flocks will create that control in place, and provide you an update function.

The idea is that you're supposed to let Flocks manage the part of your application's state that gets visibly rendered. (You can let it manage the whole state, if you want to; I often do. But you don't have to.)

Once that's done, the whole shebang is just taken off your shoulders. No update calls. No dispatching. No events. No broadcasting. No registration or deregistration.

Just simple prop-style updates. It's quite liberating.

Example plox

There is an example page developing at the Flocks propaganda site http://flocks.rocks/.

Endnotes

Are you using my lib? Please let me know. I'm curious!

If you want to reach me, try stonecypher at gmail dot com, please. Thanks :)

Polemic :neckbeard:

flocks is MIT licensed, because viral licenses and newspeak language modification are evil. Free is only free when it's free for everyone.

flocks.js's People

Contributors

acrispino avatar mkb avatar stonecypher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flocks.js's Issues

Implement reject

Lock and unlock are cool.

There should be a reject, to throw away all changes since a lock began, to establish full transactionality.

commute to global

@glenjamin recommends to move the library off of window into a global, to make referring to it more convenient. I agree, and will do so.

Can't initialize in Chrome

Here's some example code: https://github.com/acrispino/flockstest

Everything works as expected in Firefox. On Chrome, the calling Flocks.create fails:

flocks.jsx:69 Flocks2 [1] Flocks2 root creation begins
flocks.jsx:69 Flocks2 [2]  - Flocks2 engaging auto-context
flocks.jsx:143 Uncaught TypeError: Converting circular structure to JSON

The issue seems to be that the flocks context contains a copy of the config object, which contains a reference to the target element for the root component. In Chrome, this causes the call to JSON.stringify to fail.

Firefox and IE9+ seem to serialize elements as objects without any of the DOM properties. IE8 returns undefined.

sc-eslint and gulp-strip-react-dom-comment should be devDependencies

sc-eslint and gulp-strip-react-dom-comment don't seem to be necessary to actually run flocks.

This would allow npm users to not install sc-eslint, gulp-strip-react-dom-comment, and their dependencies to use flocks. This probably affects me more than others because I prefer to vendor my node_modules.

Related, I feel that react should be a dependency or peerDependency.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.