 Rx = require('rx')
 #Rxdom = require('rx-dom') # may not need
 
-window.app_root = app_root =
+log = console.log.bind(console)
+
+{Observable, Observer, Subject} = Rx
+
+window.app = app =
   ns:
     Rx: Rx
-    #Rxdom: Rxdom
     React: React
   config:
     pkg: pkg
-    rest_api:
+    restAPI:
       sources: "/api/metrics/sources"
       requests: "/api/metrics/meters/requests"
       responseTimes: "/api/metrics/histograms/responseTimes"
-    ws_api:
+    wsAPI:
      requests: "/streams/meters/requests"
      responseTimes: "/streams/metrics/histograms/responseTimes"
   events:
-    ui: new Rx.Subject()
-    requests: new Rx.Subject()
-    responseTimes: new Rx.Subject()
+    ui: new Subject()
+    mouseDemo: new Subject()
+    requests: new Subject()
+    responseTimes: new Subject()
   subscriptions:
     # keep track for disposing them later: off/on
-    requests_ws: null
-    responseTimes_ws: null
-  ui_root: null
+    requestsWS: null
+    responseTimesWS: null
+  uiRoot: null
+
+logWith = (label) ->
+  (args...) -> console.log.apply(console, [label].concat(args))
+
+logAllObserver = (label) ->
+  Observer.create(
+    logWith(label + ' - Value:'),
+    logWith(label + ' - Error:'),
+    logWith(label + ' - Completed'))
 
 wsurl = (path) ->
   host = document.location.hostname

  # or just use
  # https://github.com/Reactive-Extensions/RxJS-DOM/tree/master/doc#rxdomfromwebsocketurl-protocol-observeroronnext
  # but I'm including this as an example of how to create a bidirectional subject
  if url[0] == "/"
    url = wsurl(url)
  ws = new WebSocket(url)
  observable = Rx.Observable.create (obs) ->
    ws.onmessage = obs.onNext.bind(obs)
    ws.onerror = obs.onError.bind(obs)
    ws.onclose = obs.onCompleted.bind(obs)
    # Return way to unsubscribe
    -> ws.close()
  observer = Rx.Observer.create (data) ->
    if ws.readyState == WebSocket.OPEN
      ws.send(data)
  Rx.Subject.create(observer, observable)

RequestsWidget = React.createClass
  getInitialState: ->

    mean:  '-'

  componentWillMount: ->
    @subscription = app.events.requests.subscribe(@setState.bind(@))
  componentWillUnmount: ->
    @subscription.dispose()

  componentDidUpdate: (prevProps, prevState) ->
    null
  componentWillUpdate: (nextProps, nextState) ->
    null

  render: ->
      (R.div {style: {float: "left"}}, [
        (R.h2 {}, "Vert.x Request Metrics"),
        '1m: ' + @state['1m'],
        R.br {},
