[Cycle.js] generalizing run () function for more types of sources

Source: Internet
Author: User

Our application is able to produce write effects, through sinks, and is able to receive read effects, through the DOM so Urces. However, the main function only gets the domsource as input. This lessons shows how we can generalize main to receive a object of sources, containing all kinds of the read effects that W E can use.

Code to be chagned:

function Run (MAINFN, drivers) {  new  rx.subject ();   = Mainfn (proxydomsource);   = drivers. DOM (sinks. DOM);   = = Proxydomsource.onnext (click)); //    Object.keys (drivers). ForEach (key = {//     Drivers[key] (Sinks[key]); //    });}

This is the hard code now and make it more fixable:

functionRun (MAINFN, drivers) {const Proxysource= {}; //for each driver, we need to ProxysourceObject.keys (drivers). ForEach (key) ={Proxysource[key]=NewRx.subject ();  } ); //Get sinks (output effect)Const SINKS =MAINFN (Proxysource); Object.keys (drivers). ForEach ((key)={    //Create a source for thatConst SOURCE =Drivers[key] (Sinks[key]); Source.subscribe (x=Proxysource[key].onnext (x)); } );}

---------------------

Code:

//Logic (functional)functionMain (Sources) {const click$=Sources.dom; return{dom:click$. Startwith (NULL). Flatmaplatest (()=Rx.Observable.timer (0, 1000). Map (i=' Seconds elapsed ${i} '), Log:Rx.Observable.timer (0). map (i =i),};} Const drivers={dom:domdriver, log:consolelogdriver,}//Bproxy = ...//a = f (bproxy)//B = g (a)//Bproxy.imitate (b)functionRun (MAINFN, drivers) {const Proxysource= {}; Object.keys (drivers). ForEach ((key)={Proxysource[key]=NewRx.subject ();  } ); Const Sinks=MAINFN (Proxysource); Object.keys (drivers). ForEach ((key)={Const Source=Drivers[key] (Sinks[key]); Source.subscribe (x=Proxysource[key].onnext (x)); } );} Run (main, drivers);//source:input (read) effects//sink:output (write) Effects//Effects (Imperative)functionDomdriver (text$) {text$.subscribe (text={Const container= Document.queryselector (' #app '); Container.textcontent=text;  }); Const Domsource= Rx.Observable.fromEvent (document, ' click '); returnDomsource;}functionConsolelogdriver (msg$) {msg$.subscribe (msg=Console.log (msg));}

[Cycle.js] generalizing run () function for more types of sources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.