Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed May 29, 2015
0 parents commit 8bc1465
Show file tree
Hide file tree
Showing 22 changed files with 408 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stage": 0
}
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"ecmaFeatures": {
"jsx": true,
"modules": true
},
"env": {
"browser": true,
"node": true
},
"parser": "babel-eslint",
"rules": {
"quotes": [2, "single"],
"strict": [2, "never"],
"react/jsx-uses-react": 2,
"react/jsx-uses-vars": 2,
"react/react-in-jsx-scope": 2
},
"plugins": [
"react"
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
.DS_Store
6 changes: 6 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"node": true,
"browser": true,
"esnext": true,
"newcap": false
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Dan Abramov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
redux
=====================

Work in progress
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>Sample App</title>
</head>
<body>
<div id='root'>
</div>
</body>
<script src="/static/bundle.js"></script>
</html>
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "redux",
"version": "1.0.0",
"description": "Work in progress",
"scripts": {
"start": "node server.js",
"lint": "eslint src"
},
"repository": {
"type": "git",
"url": "https://github.com/gaearon/redux.git"
},
"keywords": [
"react",
"reactjs",
"hot",
"reload",
"hmr",
"live",
"edit",
"webpack",
"flux"
],
"author": "Dan Abramov <dan.abramov@me.com> (http://github.com/gaearon)",
"license": "MIT",
"bugs": {
"url": "https://github.com/gaearon/redux/issues"
},
"homepage": "https://github.com/gaearon/redux",
"devDependencies": {
"babel-core": "^5.4.7",
"babel-eslint": "^3.1.9",
"babel-loader": "^5.1.2",
"eslint-plugin-react": "^2.3.0",
"react-hot-loader": "^1.2.7",
"webpack": "^1.9.6",
"webpack-dev-server": "^1.8.2"
},
"dependencies": {
"react": "^0.13.0"
}
}
15 changes: 15 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
if (err) {
console.log(err);
}

console.log('Listening at localhost:3000');
});
13 changes: 13 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';
import Counter from './Counter';
import flux from './redux/flux';
import dispatcher from './dispatcher';

@flux(dispatcher)

This comment has been minimized.

Copy link
@doodzik

doodzik Jun 1, 2015

I haven't found anything about the @ symbol in javascript.
@gaearon can you provide any resources or point me in the right direction on what it does?

This comment has been minimized.

Copy link
@gaearon

gaearon Jun 1, 2015

Author Contributor

It's an experimental syntax extension proposed for ES7:
https://github.com/wycats/javascript-decorators

This comment has been minimized.

Copy link
@doodzik

doodzik Jun 1, 2015

Thank you :)

export default class App extends Component {
render() {
return (
<Counter />
);
}
}
20 changes: 20 additions & 0 deletions src/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import connect from './redux/connect';

@connect(
stores => ({
counterState: stores.CounterStore
}),
actions => ({
increment: actions.increment
})
)
export default class Counter {
render() {
return (
<button onClick={this.props.increment}>
{this.props.counterState.counter}
</button>
);
}
}
7 changes: 7 additions & 0 deletions src/actions/CounterActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { INCREMENT_COUNTER } from '../constants/ActionTypes';

export function increment() {
return {
type: INCREMENT_COUNTER
};
}
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CounterActions';
1 change: 1 addition & 0 deletions src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
12 changes: 12 additions & 0 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as stores from './stores/index';
import * as actions from './actions/index';
import createDispatcher from './redux/createDispatcher';

const state = module.hot && module.hot.data && module.hot.data.state;
const dispatcher = createDispatcher(stores, actions, state);

module.hot.dispose((data) => {
data.state = dispatcher.getState();
});

export default dispatcher;
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import App from './App';

React.render(<App />, document.getElementById('root'));
48 changes: 48 additions & 0 deletions src/redux/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { Component, PropTypes } from 'react';

const contextTypes = {
observeStores: PropTypes.func.isRequired,
bindActions: PropTypes.func.isRequired
};

export default function connect(pickStores, pickActions) {
return function (DecoratedComponent) {
const wrappedDisplayName =
DecoratedComponent.displayName ||
DecoratedComponent.name ||
'Component';

return class extends Component {
static displayName = `ReduxConnect(${wrappedDisplayName})`;
static contextTypes = contextTypes;

constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);

this.actions = this.context.bindActions(pickActions);
this.unobserve = this.context.observeStores(pickStores, this.handleChange);
}

handleChange(state) {
if (this.state) {
this.setState(state);
} else {
this.state = state;
}
}

componentWillUnmount() {
this.unobserve();
}

render() {
return (
<DecoratedComponent {...this.props}
{...this.state}
{...this.actions} />
);
}
};
};
}
99 changes: 99 additions & 0 deletions src/redux/createDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
function mapValues(obj, fn) {
const result = {};
Object.keys(obj).forEach(key =>
result[key] = fn(obj[key], key)
);
return result;
}

export default function createDispatcher(stores, actionCreators, initialState) {
// To compute the next state, combine the next states of every store
function computeState(state, action) {
return mapValues(stores,
(store, key) => store(state[key], action)
);
}

// Keep all store observers by key
const observers = mapValues(stores, () => []);
function emitChange(changedKeys) {
if (!changedKeys.length) {
return;
}

// Gather the affected observers
const notifyObservers = [];
changedKeys.forEach(key => {
observers[key].forEach(o => {
if (notifyObservers.indexOf(o) === -1) {
notifyObservers.push(o);
}
});
});

// Emit change
notifyObservers.forEach(o => o());
}

// Reassign the current state on each dispatch
let currentState = initialState || computeState({});
function dispatch(action) {
// Swap the state
const previousState = currentState;
currentState = computeState(currentState, action);

// Notify the observers
const changedKeys = Object.keys(currentState).filter(key =>
currentState[key] !== previousState[key]
);
emitChange(changedKeys);
}

// Provide subscription and unsubscription
function observeStores(pickStores, onChange) {
// Map store keys into the desired shape
const observedKeys = pickStores(
mapValues(stores, (store, key) => key)
);

// Emit the state update in the desired shape
function handleChange() {
const observedState = mapValues(observedKeys, key => currentState[key]);
onChange(observedState);
}

// Synchronously emit the initial value
handleChange();

// Register the observer for each relevant key
mapValues(observedKeys, key => {
observers[key].push(handleChange);
});

// Let it unregister when the time comes
return () => {
mapValues(observedKeys, key => {
const index = observers[key].indexOf(handleChange);
observers[key].splice(index, 1);
});
};
}

// Provide dispatching
function bindActions(pickActions) {
return mapValues(
pickActions(actionCreators),
(actionCreator) => (...args) => dispatch(actionCreator(...args))
);
}

function getState() {
return currentState;
}

return {
bindActions,
observeStores,
getState
};
}
33 changes: 33 additions & 0 deletions src/redux/flux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { PropTypes } from 'react';

const childContextTypes = {
observeStores: PropTypes.func.isRequired,
bindActions: PropTypes.func.isRequired
};

export default function flux(dispatcher) {
const { observeStores, bindActions } = dispatcher;
const childContext = { observeStores, bindActions };

return function (DecoratedComponent) {
const wrappedDisplayName =
DecoratedComponent.displayName ||
DecoratedComponent.name ||
'Component';

return class {
static displayName = `ReduxProvide(${wrappedDisplayName})`;
static childContextTypes = childContextTypes;

getChildContext() {
return childContext;
}

render() {
return (
<DecoratedComponent {...this.props} />
);
}
};
};
}
Loading

7 comments on commit 8bc1465

@granmoe
Copy link

@granmoe granmoe commented on 8bc1465 Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy belated first birthday :-)

@CiaranReen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy extra-belated second birthday :)

@YussufElarif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy extra-extra-belated third birthday :D

@ayhandoslu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy extra-extra-extra-belated fourth birthday :))

@adityaketkar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy extra-extra-extra-extra-belated fifth birthday 💯

@Manish-kumar-DEV
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Continuing the trend]
Happy (extra * 6) belated seventh birthday 🎉🥳🎊🎂

@leedom92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy eighth birthday ^_^

Please sign in to comment.