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