import Bacon from "baconjs"
import React from "react"

export default React.createClass({
  getInitialState() {
    return {}
  },
  tryDispose() {
    const {dispose} = this.state
    if (dispose) {
      dispose()
      this.replaceState({})
    }
  },
  trySubscribe(tDOM) {
    this.tryDispose()
    if (tDOM)
      this.setState(
        {dispose: (tDOM instanceof Bacon.Observable ? tDOM :
                   Bacon.combineTemplate(tDOM instanceof Array
                                         ? <div>{tDOM}</div>
                                         : tDOM))
         .onValue(DOM => this.setState({DOM}))})
  },
  componentWillReceiveProps(nextProps) {
    this.trySubscribe(nextProps.children)
  },
  componentWillMount() {
    this.trySubscribe(this.props.children)
    const {willMount} = this.props
    !willMount || willMount(this)
  },
  componentDidMount() {
    const {didMount} = this.props
    !didMount || didMount(this)
  },
  shouldComponentUpdate(nextProps, nextState) {
    return nextState.DOM !== this.state.DOM
  },
  componentWillUnmount() {
    this.tryDispose()
    const {willUnmount} = this.props
    !willUnmount || willUnmount(this)
  },
  render() {
    const {DOM} = this.state
    return DOM ? DOM : null
  }
})

@@ -263,20 +263,21 @@
 bscribe(
-tDOM
+props
 ) %7B%0A    
@@ -302,145 +302,97 @@
     
-if (tDOM)%0A      this.setState(%0A        %7Bdispose: (tDOM instanceof Bacon.Observable ? tDOM :%0A                   Bacon.combineTemplate(tDOM
+const %7Bchildren%7D = props%0A    if (children) %7B%0A      let stream = children%0A      if (stream
  ins
@@ -404,16 +404,19 @@
 of Array
+) %7B
 %0A       
@@ -420,53 +420,97 @@
     
-                                 ? %3Cdiv%3E%7BtDOM
+const %7BclassName, id%7D = props%0A        stream = %3Cdiv className=%7BclassName%7D id=%7Bid%7D%3E%7Bstream
 %7D%3C/d
@@ -523,61 +523,142 @@
     
+%7D%0A
       
-                             : tDOM))%0A         
+if (!(stream instanceof Bacon.Observable))%0A        stream = Bacon.combineTemplate(stream)%0A      this.setState(%7Bdispose: stream
 .onV
@@ -693,16 +693,22 @@
 OM%7D))%7D)%0A
+    %7D%0A
   %7D,%0A  c
@@ -772,33 +772,24 @@
 be(nextProps
-.children
 )%0A  %7D,%0A  com
@@ -844,17 +844,8 @@
 rops
-.children
 )%0A  
@@ -1287,31 +1287,182 @@
     
-return DOM ? DOM : null
+if (!DOM)%0A      return null%0A    if (!(DOM instanceof Array))%0A      return DOM%0A    const %7BclassName, id%7D = this.props%0A    return %3Cdiv className=%7BclassName%7D id=%7Bid%7D%3E%7BDOM%7D%3C/div%3E
 %0A  %7D

Support className & id attrs and stream of arrays.