Skip to content

Instantly share code, notes, and snippets.

@vagusX
Forked from acdlite/flux.js
Created September 21, 2016 08:38
Show Gist options
  • Save vagusX/d5d2e7d56f17cafbf06bbcc3e9f88a7e to your computer and use it in GitHub Desktop.
Save vagusX/d5d2e7d56f17cafbf06bbcc3e9f88a7e to your computer and use it in GitHub Desktop.

Revisions

  1. @acdlite acdlite renamed this gist Jun 6, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @acdlite acdlite revised this gist Jun 6, 2015. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. @acdlite acdlite revised this gist Jun 6, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions 1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,10 @@
    /**
    * Basic proof of concept.
    * - Hot reloadable
    * - Stateless stores
    * - Stores and action creators interoperable with Redux.
    */

    import React, { Component } from 'react';

    export default function dispatch(store, atom, action) {
  4. @acdlite acdlite revised this gist Jun 6, 2015. 2 changed files with 2 additions and 1 deletion.
    1 change: 0 additions & 1 deletion flux-library.js → 1.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    import React, { Component } from 'react';


    export default function dispatch(store, atom, action) {
    return store(atom, action);
    }
    2 changes: 2 additions & 0 deletions counter-example.js → 2.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    /**
    * Example usage
    *
    * Based on Redux's counter example
    * https://github.com/gaearon/redux/tree/master/examples/counter
    */
  5. @acdlite acdlite revised this gist Jun 6, 2015. 1 changed file with 81 additions and 0 deletions.
    81 changes: 81 additions & 0 deletions counter-example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    /**
    * Based on Redux's counter example
    * https://github.com/gaearon/redux/tree/master/examples/counter
    */

    import React, { Component, PropTypes } from 'react';
    import { Dispatcher, Injector } from '../';

    const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
    const DECREMENT_COUNTER = 'DECREMENT_COUNTER';

    function counterStore(counter = 0, action) {
    switch (action.type) {
    case INCREMENT_COUNTER:
    return counter + 1;
    case DECREMENT_COUNTER:
    return counter - 10;
    default:
    return counter;
    }
    }

    function increment() {
    return {
    type: INCREMENT_COUNTER
    };
    }

    function decrement() {
    return {
    type: DECREMENT_COUNTER
    };
    }

    export default class CounterApp {
    render() {
    return (
    <Dispatcher
    // Instead of specifying an object of keys mapped to stores, just use a
    // higher-order store!
    store={(state = {}, action) => ({
    counter: counterStore(state.counter, action)
    })}
    >
    {() => (
    <Injector actions={{ increment, decrement }}>
    {({ actions, atom }) => (
    <Counter
    increment={actions.increment}
    decrement={actions.decrement}
    counter={atom.counter}
    />
    )}
    </Injector>
    )}
    </Dispatcher>
    );
    }
    }

    class Counter {
    static propTypes = {
    increment: PropTypes.func.isRequired,
    decrement: PropTypes.func.isRequired,
    counter: PropTypes.number.isRequired
    };

    render() {
    const { increment, decrement, counter } = this.props;

    return (
    <p>
    Clicked: {counter} times
    {' '}
    <button onClick={increment}>+</button>
    {' '}
    <button onClick={decrement}>-</button>
    </p>
    );
    }
    }
  6. @acdlite acdlite renamed this gist Jun 6, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @acdlite acdlite created this gist Jun 6, 2015.
    74 changes: 74 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    import React, { Component } from 'react';


    export default function dispatch(store, atom, action) {
    return store(atom, action);
    }

    export class Dispatcher extends Component {
    static propTypes = {
    store: React.PropTypes.func.isRequired
    };

    static childContextTypes = {
    dispatch: React.PropTypes.func,
    atom: React.PropTypes.any
    };

    getChildContext() {
    return {
    atom: this.state.atom,
    dispatch: this.dispatch.bind(this)
    };
    }

    constructor(props, context) {
    super(props, context);

    this.state = { atom: dispatch(props.store, undefined, {}) };
    }

    dispatch(payload) {
    this.setState(prevState => ({
    atom: dispatch(this.props.store, prevState.atom, payload)
    }));
    }

    render() {
    return typeof this.props.children === 'function'
    ? this.props.children(this.state.atom)
    : this.props.children;
    }
    }

    export class Injector extends Component {
    static contextTypes = {
    dispatch: React.PropTypes.func.isRequired,
    atom: React.PropTypes.any
    };

    static propTypes = {
    actions: React.PropTypes.object
    };

    performAction(actionCreator, ...args) {
    const { dispatch } = this.context;
    const payload = actionCreator(...args);

    return typeof payload === 'function'
    ? payload(dispatch)
    : dispatch(payload);
    };

    render() {
    const { dispatch, atom } = this.context;
    const { actions: _actions } = this.props;

    const actions = Object.keys(_actions).reduce((result, key) => {
    result[key] = this.performAction.bind(this, _actions[key]);
    return result;
    }, {});

    return this.props.children({ actions, atom });
    }
    }