Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Aug 12, 2016
1 parent b8515e5 commit b8f251a
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015", "stage-0"],
"plugins": ["babel-plugin-add-module-exports"]
}
63 changes: 63 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"env": {
"browser": true,
"node": true,
"jasmine": true,
"es6": true
},
"plugins": [],
"ecmaFeatures": {
"destructuring": true,
"spread": true,
"experimentalObjectRestSpread": true
},
"parser": "babel-eslint",
"rules": {
"comma-dangle": 0,

"curly": 2,
"guard-for-in": 2,
"no-void": 2,
"radix": 2,
"wrap-iife": 2,
"no-alert": 2,
"no-console": 2,
"no-unused-vars": 2,
"no-extra-semi": 2,

"no-use-before-define": [2, "nofunc"],
"no-redeclare": 0,
"no-undef": 2,
"object-curly-spacing": [2, "always"],

"brace-style": [2, "stroustrup", { "allowSingleLine": true }],
"indent": [2, 4, {"SwitchCase": 1}], // NB! Disabled at the moment because of extra indentation in browserify branch
"linebreak-style": [2, "unix"],
"quotes": 0,
"space-infix-ops": 0,
"no-underscore-dangle": 0,
"one-var": [2, "never"],
"operator-linebreak": [2, "after"],
"keyword-spacing": 2,
"space-before-blocks": 2,
"space-before-function-paren": [2, "never"],
"spaced-comment": 2,
"strict": [2, "global"],

"arrow-spacing": 2,
"constructor-super": 2,
"no-const-assign": 2,
"no-duplicate-imports": 2,
"no-this-before-super": 2,
"no-useless-constructor": 2,
"no-var": 2,
"prefer-const": 2,
"prefer-rest-params": 2,
"prefer-spread": 2,
"require-yield": 2,

"no-bitwise": 2,
"max-len": [2, 205, 4],
"arrow-parens": ["error", "as-needed"],
}
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
dist/
lib/
coverage/

.idea/
50 changes: 50 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "rx-connect",
"version": "0.1.0",
"description": "Connect React components to Redux store with RxJS-based HOC",
"main": "./lib/index.js",
"scripts": {
"clean": "rimraf lib dist",
"build:lib": "babel src --out-dir lib",
"build:umd": "cross-env NODE_ENV=development webpack src/index.js dist/rx-connect.js",
"build:umd:min": "cross-env NODE_ENV=production webpack src/index.js dist/rx-connect.min.js",
"build": "npm run build:lib && npm run build:umd && npm run build:umd:min",
"prepublish": "npm run clean && npm run build"
},
"keywords": [
"redux",
"react",
"rx",
"rxjs"
],
"author": "Sergei Egorov <bsideup@gmail.com>",
"homepage": "https://github.com/bsideup/rx-connect",
"license": "MIT",
"repository": "bsideup/rx-connect",
"bugs": {
"url": "https://github.com/bsideup/rx-connect/issues"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-core": "^6.13.2",
"babel-eslint": "^6.1.2",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.13.2",
"babel-preset-stage-0": "^6.5.0",
"cross-env": "^2.0.0",
"eslint": "^3.2.2",
"eslint-loader": "^1.5.0",
"react-redux": "^4.4.5",
"redux": "^3.5.2",
"rimraf": "^2.5.4",
"rx": "^4.1.0",
"webpack": "^1.13.1",
"yargs": "^4.8.1"
},
"peerDependencies": {
"react-redux": "^4.4.5",
"redux": "^3.5.2",
"rx": "^4.1.0"
}
}
55 changes: 55 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react";
import Rx from "rx";

export default function rxConnect(selectState) {
return WrappedComponent => class RxConnector extends React.PureComponent {

static displayName = 'RxConnector';

static contextTypes = {
store: React.PropTypes.object.isRequired
};

stateSubscription = undefined;

store = undefined;

state$ = undefined;

state = {
props: {}
};

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

this.props$ = new Rx.BehaviorSubject(props);

this.store = props.store || context.store;

this.state$ = Rx.Observable
.create(observer => this.store.subscribe(() => observer.onNext(this.store.getState())))
.startWith(this.store.getState())
.distinctUntilChanged();
}

componentWillMount() {
this.stateSubscription = selectState(this.props$, this.state$, this.store.dispatch).subscribe(props => this.setState({ props }));
}

componentWillUnmount() {
this.stateSubscription.dispose();
}

componentWillReceiveProps(nextProps) {
this.props$.onNext(nextProps);
}

render() {
return React.createElement(WrappedComponent, {
...this.props,
...this.state.props
});
}
};
}
69 changes: 69 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var webpack = require("webpack");
var path = require("path");

var env = process.env.NODE_ENV

var reactExternal = {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}

var rxExternal = {
root: 'Rx',
commonjs2: 'rx',
commonjs: 'rx',
amd: 'rx'
}

var config = {
externals: {
'react': reactExternal,
'rx': rxExternal
},
output: {
library: "rx-connect",
libraryTarget: "umd"
},
module: {
loaders: [
{
test: /\.js$/,
loader: "babel",
exclude: /(node_modules)/
},
{
test: /\.js$/,
loader: "eslint-loader",
exclude: /node_modules/
}
]
},
resolve: {
root: path.resolve("./src"),
extensions: ["", ".js"]
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(env)
})
]
};

if (env === "production") {
config.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compressor: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
screw_ie8: true,
warnings: false
}
})
)
}

module.exports = config;

0 comments on commit b8f251a

Please sign in to comment.