/**
 * Created by tushar.mathur on 18/06/16.
 */

'use strict'

import {Observable as O} from 'rx'
import {mux} from 'muxer'
import R from 'ramda'

export const ev = R.curry((event, message) => message.event === event)

export const RequestParams = R.curry((request, params) => {
  return O.create((observer) => request(params)
    .on('data', (message) => observer.onNext({event: 'data', message}))
    .on('response', (message) => observer.onNext({event: 'response', message}))
    .on('complete', () => observer.onCompleted())
    .on('error', (error) => observer.onError(error))
  )
})

export const Request = R.curry((request, requestParams$) => {
  const response$ = requestParams$.flatMap(RequestParams(request))
  return mux({
    response$: response$.filter(ev('response')),
    data$: response$.filter(ev('data'))
  })
})

@@ -172,50 +172,66 @@
 ry((
+$, 
 event
-, message) =%3E message.event === event
+) =%3E $.filter(R.whereEq(%7Bevent%7D)).pluck('message')
 )%0A%0Ae
@@ -641,30 +641,22 @@
 equest, 
-requestP
+p
 arams
-$
 ) =%3E %7B%0A 
@@ -662,17 +662,17 @@
   const 
-r
+R
 esponse$
@@ -674,17 +674,20 @@
 onse$ = 
-r
+ev(R
 equestPa
@@ -694,39 +694,15 @@
 rams
-$.flatMap(RequestParams(request
+(params
 ))%0A 
@@ -730,25 +730,25 @@
 ponse$: 
-r
+R
 esponse$
 .filter(
@@ -739,26 +739,16 @@
 esponse$
-.filter(ev
 ('respon
@@ -751,17 +751,16 @@
 sponse')
-)
 ,%0A    da
@@ -764,25 +764,25 @@
  data$: 
-r
+R
 esponse$
 .filter(
@@ -777,26 +777,16 @@
 nse$
-.filter(ev
 ('data')
 )%0A  
@@ -781,17 +781,16 @@
 ('data')
-)
 %0A  %7D)%0A%7D)

use request as params instead of a stream