import React, { Component, PropTypes } from 'react';

export default function connect(ReactComponent, { subscriptions = {}, mutations = {} }) {
  return class extends Component {
    static contextTypes = {
      hz: PropTypes.func
    }
    constructor(props, context) {
      super(props, context);
      this._subscriptions = [];
      this._mutations = {};
      this.state = Object.keys(subscriptions)
        .reduce((initialState, qname) => {
          initialState[qname] = [];
          return initialState;
        }, {})
    }
    componentWillMount() {

      const hz = this.context.hz;

      Object.keys(subscriptions)
        .forEach((qname) => {
          const q = subscriptions[qname];
          const subscription = q(hz, this.props).watch().subscribe((data) => this.setState({ [qname]: data }))
          this._subscriptions.push(subscription)
        })
      Object.keys(mutations)
        .forEach((mname) => {
          this._mutations[mname] = mutations[mname](hz)
        })
    }
    componentWillUnmount() {
      this._subscriptions.forEach((q) => q.unsubscribe())
    }
    render() {
      return <ReactComponent {...this.props} {...this.state} {...this._mutations} />;
    }
  }
}

@@ -45,16 +45,57 @@
 'react';
+%0Aimport shallowequal from 'shallowequal';
 %0A%0Aexport
@@ -302,24 +302,25 @@
  context) %7B%0A
+%0A
       super(
@@ -336,16 +336,17 @@
 ntext);%0A
+%0A
       th
@@ -397,16 +397,17 @@
 s = %7B%7D;%0A
+%0A
       th
@@ -572,60 +572,620 @@
  %7B%7D)
+;%0A
 %0A    
-%7D%0A    componentWillMount() %7B%0A%0A      const hz = 
+  this._subscribe = this._subscribe.bind(this);%0A      this._unsubscribe = this._unsubscribe.bind(this);%0A      this._createMutations = this._createMutations.bind(this);%0A    %7D%0A    shouldComponentUpdate(nextProps, nextState) %7B%0A      return shallowequal(nextProps, this.props) === false %7C%7C%0A             shallowequal(nextState, this.state) === false;%0A    %7D%0A    componentWillReceiveProps(nextProps) %7B%0A      this._unsubscribe(this._subscriptions);%0A      this._subscribe(this.context.hz, nextProps);%0A    %7D%0A    componentWillMount() %7B%0A      this._subscribe(this.context.hz, this.props);%0A      this._createMutations(
 this
@@ -1195,18 +1195,133 @@
 ntext.hz
+)
 ;%0A
+    %7D%0A    componentWillUnmount() %7B%0A      this._unsubscribe(this._subscriptions);%0A    %7D%0A    _subscribe(hz, props) %7B
 %0A      O
@@ -1451,29 +1451,24 @@
 ion = q(hz, 
-this.
 props).watch
@@ -1523,16 +1523,17 @@
 data %7D))
+;
 %0A       
@@ -1573,16 +1573,17 @@
 ription)
+;
 %0A       
@@ -1581,24 +1581,151 @@
 ;%0A        %7D)
+;%0A    %7D%0A    _unsubscribe(subscriptions) %7B%0A      subscriptions.forEach((q) =%3E q.unsubscribe());%0A    %7D%0A    _createMutations(hz) %7B
 %0A      Objec
@@ -1827,16 +1827,17 @@
 ame%5D(hz)
+;
 %0A       
@@ -1843,101 +1843,9 @@
   %7D)
-%0A    %7D%0A    componentWillUnmount() %7B%0A      this._subscriptions.forEach((q) =%3E q.unsubscribe())
+;
 %0A   

update subscriptions with new props