[Cycle.js] Main function and effects functions

We need to give structure to our application with logic and effects. This lessons shows how we can organize our code into two parts: main() function for logic, and effects functions for effects.

 

// Logic (functional)
function main() {
  return Rx.Observable.timer(0, 1000)
    .map(i => `Seconds elapsed ${i}`);  
}

// Effects (imperative)
function DOMEffect(text$) {
  text$.subscribe(text => {
    const container = document.querySelector('#app');
    container.textContent = text;
  });
}

function consoleLogEffect(msg$) {
  msg$.subscribe(msg => console.log(msg));
}

const sink = main();
DOMEffect(sink);
consoleLogEffect(sink);
  

 

posted @ 2016-02-02 06:54  Zhentiw  阅读(179)  评论(0)    收藏  举报