import Rx from 'rx'
import Cycle from '@cycle/core'
import { makeDOMDriver, div, button, p, h1 } from '@cycle/dom'
import electron from 'electron'

function ipcDriver(request$) {
    request$.subscribe(req => {
        electron.ipcRenderer.send(req.msg, req.payload)
    })

    const ipc$ = Rx.Observable.create((observer) => {
        Rx.Observable
            .fromEvent(electron.ipcRenderer,
                       'ipc-driver-finished',
                       (event, data) => data)
            .subscribe(data => observer.onNext(data))

        return () => { console.log('disposed'); };
    })
    return ipc$;
}

// DOM read effect: Btn clicked
// IPC write effect: Send request
// IPC read effect: Get loaded data
// DOM write effect: Display data
// write effects are sinks
// read effects are coming from sources

function main(sources) {
    const clickEv$ = sources.DOM.select('button').events('click')
    const request$ = clickEv$.map(() => {
        return { msg: 'load-data', payload: { filename: 'default.custom.yaml' } }
    })

    const data$ = sources.IPC.startWith(null);

    return {
        DOM: data$.map(data =>
                 div([
                     h1('Menu'),
                     p(`Page Size: ${data === null ?
                                     '' : data.patch.menu.pageSize}`),
                     button("Load Data"),
                 ])
                ),
        IPC: request$,
    }
}

const drivers = {
    DOM: makeDOMDriver("#app"),
    IPC: ipcDriver,
}

Cycle.run(main, drivers);

@@ -201,26 +201,16 @@
 e(req =%3E
- %7B%0A       
  electro
@@ -249,22 +249,16 @@
 payload)
-%0A    %7D
 )%0A%0A    c
@@ -294,17 +294,16 @@
 ate(
-(
 observer
 ) =%3E
@@ -298,17 +298,16 @@
 observer
-)
  =%3E %7B%0A  
@@ -568,12 +568,10 @@
 ed')
-;
  %7D
-;
 %0A   
@@ -872,16 +872,27 @@
 button')
+%0A          
 .events(
@@ -900,16 +900,84 @@
 click')%0A
+          .startWith(%7B/* Fake a click event for initial loading*/%7D)%0A
     cons
@@ -1026,16 +1026,28 @@
 return %7B
+%0A           
  msg: 'l
@@ -1056,16 +1056,28 @@
 d-data',
+%0A           
  payload
@@ -1113,16 +1113,24 @@
 .yaml' %7D
+%0A       
  %7D%0A    %7D
@@ -1131,17 +1131,16 @@
 %0A    %7D)%0A
-%0A
     cons
@@ -1164,24 +1164,8 @@
 .IPC
-.startWith(null)
 ;%0A%0A 
@@ -1308,38 +1308,73 @@
 data
- === null ?%0A                  
+.patch.menu.pageSize%7D%60),%0A                     h1('Schema List'),%0A
     
@@ -1392,12 +1392,9 @@
     
-'' :
+ 
  dat
@@ -1397,39 +1397,62 @@
  data.patch.
-menu.pageSize%7D%60
+schemaList.map((%7Bschema%7D) =%3E p(schema)
 ),%0A         
@@ -1471,17 +1471,19 @@
 button(%22
-L
+Rel
 oad Data

Refactor renderer/main.js