Git Product home page Git Product logo

reactive-flux's Introduction

reactive-flux

Fluxish model implemented with RxJS

I was trying to use React + Flux to build a Dashboard but while reading Flux tutorials I already started to dislike the switch statements and constants that are all over the place.

React and the Flux architecture seem to fit well with RxJS, so this is my take on it.

This is my first JavaScript code so feel free to send pull requests and let me know of any bugs or improvements you can think of. It's inspired by several libraries that try to combine React with Rx but they didn't seem to do exactly what I had in mind.

Installation

npm install reactive-flux

Description

The dispatcher is completely gone. Instead we are using Rx.Subjects as Actions and Stores.

  • An Action can have many Stores subscribed to it
  • A Store can subscribe to many Actions, each with its own handler
  • Additionally it can request to be notified after other stores in case there are any dependencies (Dispatcher.waitFor())
  • React components can subscribe to many Stores as they are subclasses of Rx.Subject

Changelog

  • 0.1.2: Custom function argument for Action. Fixed problem that the library could not be required correctly.

Example

let ReactiveFlux = require('reactive-flux'),
	request = require('superagent'),
	React = require('react'),
	Action = ReactiveFlux.Action,
	Store = ReactiveFlux.Store;

let LoginSucceededAction = Action.make();
let LoginFailedAction = Action.make();

let LoginAction = Action.make((username, password) => {
	request
		.post('/login')
		.send({ username: username, password: password })
		.end(function(err, res){
			if (res.status == 200) {
				LoginSucceededAction(res.body.token);
			} else {
				LoginFailedAction();
			}
		});
});

let LOGIN_TOKEN_KEY = 'token';

class LoginStore extends Store {
	constructor() {
		super();

		// yes, I agree this is superfluous and needs to be changed :)
		this.init();
	}

	init() {
		this.observe(LoginSucceededAction, this.onLoginSucceeded);
		this.observe(LoginFailedAction, this.onLoginFailed);
	}

	onLoginSucceeded(token) {
		window.localStorage.setItem(LOGIN_TOKEN_KEY, token);
	}

	onLoginFailed() {
		window.localStorage.removeItem(LOGIN_TOKEN_KEY);
	}

	getToken() {
		return window.localStorage.getItem(LOGIN_TOKEN_KEY);
	}

	isLoggedIn() {
		return !!this.getToken();
	}
}

var LoginComponent = React.createClass({
	getInitialState() {
		return { isLoggedIn: LoginStore.isLoggedIn() };
	},

	_subscription: {},

	componentDidMount() {
		self = this;
		this._subscription = LoginStore.subscribe(function () {
			if (LoginStore.isLoggedIn()) {
			   // do something useful
			}
		});
	},

	componentWillUnmount() {
		this._subscription.dispose();
	},

	render() {
		return (
			// something
		);
	},

	handleSubmit(e) {
		e.preventDefault();

		let username = this.refs.username.getValue().trim();
		let password = this.refs.password.getValue().trim();

		if (!username || !password) {
			return;
		}

		// call our action
		LoginAction(username, password);
	}
});

// Actions are subjects so we can subscribe to an API or similar
var source = Rx.Observable.fromEvent(document, 'mousemove');
source.subscribe(SomeAction); // All mousemove events will be send to subscribing stores of SomeAction

reactive-flux's People

Contributors

codesuki 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  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

qwtel appleseedez

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.