By now, you should have some idea of what cycle run does and what the domDriver is. It's important that you feel that there's no magic going on here. We really just have instructions for write effects coming out as sinks here, these streams, and instructions for read effects coming in as sources. These are also streams.
Enough with building our own cycle.js. We can start learning how to solve problems using cycle.js. I'm going to delete this old code here. We're going to start blank, we don't need the log driver either. This is usually how we start. We have an empty main function, we have some drivers, and we connect them.
We're also going to need the div helper function to create a virtual DOM element for div. Then as well label input hr and h1. I recommend you start by first returning the sinks object. We're going to have one sink stream for each driver that takes a write effect.
Here we're going to give to the domDriver a stream of just one value. That value will be the div that has inside it label saying name and then input. Here we can give attributes of type. We want text. Then we're going to have a horizontal line and a header which will say hello, and exclamation mark.
For now, we don't know what to put here but we're just going to do one thing at a time and see if this still works, and it does. This virtual DOM value ended up creating the elements that we wanted there.
This doesn't yet react to our events. We need to take those as read effects. Right now, we're passing the write effects to the domDriver and it's creating this, but we need to detect the reads. We do that through sources. Sources is an object like sinks that has the DOM property coming from the domDriver.
There we can select under input, that would be the tag name for this element here. Instead actually we can pass here a string that has some class names or even IDs if you want. Once I pass this string this function knows how to parse that and detect these class names. Then I can use those same class names here and we can do .events of type input.
This will return to us a stream of input events. I'm going to store that in a variable called input event stream. So far, this is not connected to anything else so this is the same application. There is no difference.
What we need to do is create another stream and call it name stream, which will be input event mapped, each event to event target. That would give us the element here for this input. Then we say event.target.value. This gives us the string for this input event.
For instance, every time I type here that would give me the string w and then the string wo, string wor, that type of stuff. Now that we have name stream, if we plot that overtime it would look something like this. We have w, wo, wor.
If we get each name and we map each name string to a virtual DOM tree which displays that, maybe then we're able to accomplish our app, because we just literally map state to view. For each of those strings we'd have the corresponding div that displays that.
Once I hit this, it's curious that we don't actually see anything here. That happens because, notice that the DOM sink here is mapped from the name stream, which in turn is mapped from the input event stream. If we plot that one it looks like this as a marble diagram.
Initially, we don't have any event happening, so it's like that. Once we map that we get this as name stream and then we get this as the DOM sink. That's why we're basically telling the DOM driver show nothing overtime. That's what's actually happening here.
In order to fix this, we need to kick start our app with something. Here we need to basically pre-append the name stream with some string. Here I'm giving the empty string, I have that in the beginning. Then I can map this to something useful.
That's what we're going to have here. That's why we see our app displayed here. I could actually replace this with, let's say, world, and then we would see world initially. This should now work. Once I type here it does change the header.
This is how you should go about building a basic cycle.js application. You start by writing sinks and then just hooking together streams that are based on the sources. That's why it's also called cycle.js because applications are a loop of write effects and read effects.