Learn RxJS in 60 Minutes for Beginners - Free Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone Gary Simon here and welcome to this brand-new 100% free course and crash course really on rxjs so rxjs is the JavaScript implementation of reactive X well what is reactive X it's an API for asynchronous programming with observable streams if you've never heard of or remember never messed around with this before that probably means nothing to you as well what the hell is that that's okay though because I'm going to show you exactly from scratch what all this stuff means and so at reactive xio which is the official website for it we can see we have our languages Java JavaScript which is what we're going to be focusing on Nets gala etc so if we go to /r XJS we'll have the official documentation for reactive X library and we will be referring to this especially when it comes to understanding what are called marble diagrams and you'll understand that stuff very soon as we go through throughout this crash course so I there's also a written version of pretty much everything I'm teaching here feel free to check that out it's linked and let's get started okay so we're gonna get started by setting up a quick development environment so that we can start learning rxjs and there are frameworks like angular that already have it integrated but for this series of tutorials for this course I really wanted to make it AI framework agnostic we're just gonna be working with in pure vanilla JavaScript I'm not gonna be choosing any frameworks so with that said I'm gonna go ahead and go to the console alright so we're just going to create a new folder called rxjs and Ivan learn and then we'll also CD into it so CD rxjs Ivan learn all right and I'm going to use yarn for this I although you could use NPM the node package manager for installing the necessary project dependencies so I'm just going to type yarn a knit and - why that will create a package JSON file with all of the answers being yes for that so then we're going to use yarn to add rxjs itself and then web pack which we'll use to bundle everything together and then also type script although you don't have to use typescript in order to use rxjs but eventually I'm going to be leading into angular so might as well just get used to some type script right so then yarn add rxjs web pack web pack - dev - server this will launch a dev server for us on localhost 808 oh by default and then also typescript and TS or typescript loader all right so I'm just gonna wait until this installs alright and then finally we're going to add as a development dependency in our package JSON file I a package called web pack - CLI yarn add web pack - CLI and then we add the dev flag alright great so now I'm just going to go ahead and clear this out and type in code and a period this will launch Visual Studio code if you haven't installed from to me online in our current rxjs learned folder okay so first thing we want to do is open up the package.json file and let me make this a little bit larger so everybody can see what's happening here and inside of here I we just want to add a new property called scripts so scripts and we're gonna open this up and we'll say start so when we run a command called yarn run well what's it called start well upgrade later there and what would be what command do we want to be run when we call start it'll be web pack dev server and then also we're gonna add a mode development here if you just I was running we're web pack dev server itself and the output mentioned I need to specify a mode here that's why I'm adding that here alright and in a comma at the end save that so now we need to set up web pack itself so we're gonna create a file here called web pack dot config das by the way I everything I'm doing right now in terms of the code if you don't want to follow along you can grab the github code there's a link here provided and also all the codes in the written version of this tutorial which is also linked here ok so I really at this point I'm just going to copy from the written tutorial this section right here again this is all linked so I'm just gonna copy all of this because I'm not going to go through all this this configuration set up simply because it's not relevant to the purpose of this course alright so the only thing that's really happening here that you know it's worth mentioning is that our entry point it's going to be looking at this for this folder in a file called code TS which is for typescript that's where all of our code is going to be throughout the pretty much the duration of this course to learn rxjs alright and then of course the extensions that we're using TS for typescript javascript any testing we're not going to be doing that and it just bundles everything to a bundle Jas and into a path of the dist:4 der alright alright then we want to create another file for a typescript so it's going to be TS config Jason and once again I'll be copying the code for that and this is the code for that it's just specifying our output of dist we're able to use I the es 2017 otherwise known as es 8 JavaScript while compiling down to 2015 or es6 right here so we'll save both these files alright and then we need to create our index.html alright and here are in Visual Studio code you just put a exclamation point and hit enter that gives just some basic boilerplate hit control be a guru that sidebar and also in the written version of the tutorial I do have a couple adjustments I made to this basic document for instance I I'm going to change this document here the title I'm also going to add just some three CSS rule sets right here again I'm grabbing all this from the written version and then for our body we're going to have a single unordered list element with an ID of output and then outside of here we're gonna have our script source equals forward slash bundle j/s alright so this idea of output right here we're going to be using that to display the output of our observables while we're learning rxjs I we could have just console.log it but I think that looks ugly it's a lot easier just to have something that's kind of styled and it looks well in the browser for us so that's the purpose of this unordered list element again it's not necessary but it'll make life a little bit easier in prettier so let's go ahead now and create that source folder so create a new folder right here called source inside of it will create a file called code TS alright so just to be sure that everything's working and that we have rxjs you know integrated as we want it to be we're gonna put import all as rx from and we'll say rxjs four slash observable and then we'll simply console.log rx for now alright so now if we go back to our console which by the way you don't have to use a separate console you could go to view integrated terminal and just run this far line following command which is yarn yarn run and start so if I do that everything should work here as expected and then we'll be able to visit our localhost 808 Oh control shift I and we'll see we have our object right here which this means correctly that rxjs has been integrated and we're ready to rock all right so now we're going to actually begin learning about the exciting world of rxjs and a great starting point would be to understand what a stream is and an observable so a stream is simply just to represent it's a concept it's basically values or events as they're called that are emitted over time so think of for instance Thai you know a bunch of users chatting in a chatroom an individual filling out a form you know through through multiple different inputs these are all values or events that are emitted over time essentially and their relation to an observable and observable is actually what facilitates the stream it provides you with a function and a means to both emit these values and also respond to them all right so that's I don't like a lot of conceptual stuff so let's get a better understanding by actually doing so I'm going to go back to our editor here and we're gonna make some adjustments to this so first you know you could just import everything the entire kitchen sink here as we're doing but I'd rather not do that because you know chances are you're never gonna need to import this much stuff and that would just be a waste so what we're gonna do is what is it that we want to experiment with and learn well in observable so we're gonna import observable from rxjs for slash observable if I could spell it there we go and then we're going to create an observable very simple we'll simply say var observable equals observable dot create method all right so I should warn you there are multiple ways to create an observable and to create method is just one of which are one method that you can do so to create one from scratch alright so this in and of itself is an observable but this create method accepts a single argument which is a SUBSCRIBE function and the SUBSCRIBE function accepts what's called an observer argument so adding to line three right here we're going to pass in a function called subscribe and then we pass in that observer alright and then we can open up that function within our squiggly braces and to emit a value from this part right here what we'll do by the way it's giving us this red squiggly line because of typescript we have enabled so I just put any and that will go away so to emit a value we simply reference observer dot next and then we pass in whatever it is that we want to emit so I'll just put in a string value of something like hey guys all right so I dissonant of itself if we were to save this we wouldn't see anything in the console or not console logging nothing's really happening here by the way what we're gonna do this here we can make this less verbose if we simply get rid of function subscribe alright and then we convert this to an arrow function which is what we're gonna do throughout the rest of the duration of this course but I did just want to show you the initial we're there alright so next if we want to grab the value we have to define what's called an observer right so we haven't yet done that so the way we can do that is we can say observable dot subscribe alright and then we're going to take whatever value is being emitted from next so we can reference that as I don't know X of type any our error function and then we can just console log X for now so now if we were to save this and go back and view the console hey guys we can see R it right there and this is ugly and as I mentioned I did want to output these type of results over here in the actual web page started using the console log so what I'm going to do based on the written tutorial is just copy this little piece of code right here this function and we're going to use that function throughout this entire course essentially so function add item all we're doing is using vanilla JavaScript to create a list item element and we're going to be passing in this value that we pass in to it so we can change this console.log here to add item and then X so now we save it and there we go we have something that's much better to look at alright so let's take a closer look at observers and we can see right here this here is in observers every time you subscribe you you are an observer and you create what's called a subscription all right so once again these observers they read values coming from the observer observable up here so an observer is simply a set of callbacks that accept notifications coming from this observer which include next error and complete alright so in our current example we've only provided the next callback which technically is right here however we can come and separate these to also include error and also complete so let's make this a little bit more I you know I did say demonstrative of the possibilities of observables and observers so we have our observer next hey guys right here well let's replicate that with shift all in the down arrow key if you're using visual studio code we'll say how are you alright and then we're gonna call observer dot complete and then we'll copy one of these and we'll say this will not send alright next before I describe what's happening here let's go ahead and open this up alright so this is the next function that's handled right here let's also handle the other two functions which would be error and complete so we'll say error we will put add item error and then we'll also say complete we don't need to pass in anything here we can just say add item and completed so now at this point if we save it we'll see that it says hey guys how are you completed however we can see that because we completed the observer right here this will not send alright great so hopefully you have a pretty good idea of observables at least at a very basic level all right so one thing if we want to demonstrate error and how that works is we can wrap our observable code here in try-catch blocks so try and we'll put this here catch and then observer dot error that's how we can define an error through the error method and then just put er R and we want to catch that all right and that's how you would communicate error and make this fire right here if there is an error we'll see if we save it it still works there is no error at all okay so let's talk about cancelling a subscription so when you subscribe to an observable within observer you've created what's called a subscription as I mentioned earlier and you're given the ability to cancel that subscription the event that you no longer need to receive the emitted values from the azure observer so up here we completed this observable but what if we wanted just one of our subscribers in our case we only have one right here what if we don't need to receive that those emitted values anymore all right so let's set up our I our example here just so we can demonstrate this so we're gonna use I right here a set interval function and this will just repeat whatever is inside it here for whether whatever duration that we specify oops alright so what we'll do is I'll say observer in the dot next we'll change this to I am good alright so this is going to continually spit out I am good right here for every two seconds let's also get rid of this section right here these two lines and then we'll go ahead let's rename this just so we can understand what it is a little bit better instead of renaming I mean we're going to create a variable out of it so that we can reference it so we'll call this var and was call this an observer then we can create a set timeout and inside of here we're gonna call observer dot unsubscribe in 6,001 milliseconds or six seconds plus one millisecond so if we save this I am good I am good I am good and then it's been unsubscribed and we will no longer receive any other emitted values all right so that's how you cancel a actual observer from receiving any further emitted values all right so also let's talk about creating multiple subscriptions so you're able to create multiple subscriptions on the same observable very easily so this could be useful if you have different areas of your your user interface where you're using the same data but you're displaying it potentially in a different way so what we'll do is let's copy this right here and we'll call this observer two and we don't need to have the error or complete callbacks there and now if we save this you'll see we'll have duplicates of everything all right so also notice that we will still receive the I am good just from one of the observers simply because the set timeout is only unsubscribing from the observer the first observer up here all right so if we want to tie these two together then we get into what are called child subscriptions so what if we wanted to unsubscribe both of our subscriptions if the one has been unsubscribed so to do that all we have to do is call observer dot add and then we add observer two so if now we save this all right here it goes now none of them will emit any more values because it's been added and you can also call subscription dot remove and also remove a previously added observer if you need that functionality now the next most important thing to understand once you understand the basics of what an observable is is whether or not an observable is hot or cold all right so in the case of a cold observable like the type that we've been working with so far right here it's an observable whose producer and by the way a producer is this observer or the subscribed function right here because it produces or it emits these values or events it's an observer who's producer is activated once a subscription like down here has been created all right so to better demonstrate this let's go ahead and adjust our code so we're gonna leave all this the same up here and down here let's go ahead and get rid of just about all of this stuff and inside of here we'll make this by the way just one second and we're going to create a second observer so we'll say our observer two equals observable dot subscribe and then we will pass in just as we have before our next callback so we'll call it X of type any and we'll add item and we'll say subscriber two and then plus X which is the return the emitted value here essentially all right so if we go ahead and save this you'll see now that we it has hey guys how are you and then after one second the subscriber receives hey guys how are you etc and this means that the observable is cold and so an observable is hot when the producer is emitting values outside of the observable up here so we can actually make this cold observe hot and technically this is more of what's called a warm approach or a warm observable with just a few changes so if we import rxjs add operator share and we're going to learn about operators later on in the course but we're doing it just for this relevant purpose here if we had the share operator at the very end of our observable and then we save this we'll see it says hey guys how are you I am good subscriber to starts receiving I am good and not the very initial values that were already emitted and this would make it a warm or a hot observable all right so an example of a truly hot observable would be mouse movements for instance that are made by a user so let's remove all the current code with exception to the add item function all right so let's remove all of this let's get rid of our share operator here and we're going to import from event from rxjs observable from event all right so we're going to create what's called a truly hot observable so we're gonna save our observable equals from event document and mouse move this right here is technically an observable and were created creating an observable from this from event function so you can create observables through these creation operators as they're referred to as so then we're going to set timeout and we're going to create a subscription or an observer unobservable dot subscribe and the same thing as before with our next call back we're gonna add item right here and then we're also going to give this a delay down here of two thousand seconds or milliseconds rather all right so this is an example of what would be a truly hot observable because if we save this and for the first two seconds we were already emitting values and this observable was receiving those values already but once we come over here only refresh see it's there already Stephen it's only gonna start receiving from that point at which the observer was created alright so there are also like I mentioned other creation operators that we'll get into perhaps in later on in the course and you know that we just from event is simply one of probably about ten different creation operators you can create observables from ranges and timers from a promise just from a lot of stuff so at this point hopefully you should have a fairly strong and basic understanding of what streams are and really a stream is just a concept observables observable observers and subscriptions all right so now let's talk about subjects and you should really only you know get yourself to this point if you truly understand what an observable is so if you're ending up on this from the internet from this page without watching the previous lesson where I discuss what observables are then make sure you watch out first because a subject is just a different type of observable that has different capabilities alright so going back to the code that we've been working on we're gonna go ahead and just cut everything with exception to our add item function and what I'm going to do is get rid of this and this and we're going to import our subject from rxjs subject all right so let's create a subject and by the way a subject you know in contrast to an observable is simply an observer that's also able to emit values so it's both an observable and an observer simultaneously and this is unlike an observable because as an observer that's subscribed to an observer or rather an observable it can only read values emitted from an observable so if that sounds confusing and word solid just I'll hold your horses for a second we're gonna create a subject new subject alright that's how easy it is to create a subject and now we have to subscribe to it just as we would a regular observable so think of this just as an observable alright so what we'll do is subject dot subscribe and we have our three different functions here so we can say our data add item observer one and we'll pass in the data and then we can have our error of add item and our complete callback item observer one completed alright now if we save this project nothing's going to have that or nothing's going to happen next because we have to use that next method to start emitting values from the observer so now we can say subject dot next the first thing has been sent now we weren't able to do this before like in the previous lesson when we were talking about just pure observables I the the actual subscriptions or the observers or subscribers can't actually emit values like we're doing right here so if we save this and go back observer one the first thing has been sent all right so now that's simple enough but let's make it more interesting by defining another observer and then pushing some more values alright so underneath the first thing has been sent let's create a second observer so far this time we'll create a variable out of it so observer two subject dot subscribe and we'll say data same thing over here add item and we'll say observer two and plus data all right and then let's go ahead and reference our subject and make two more calls so the second thing has been sent and shift alt and down arrow key that and then also a third thing has been set let's save that and this is the result that we get so we can see that the second observer doesn't receive the first thing has been sent but only the values that came after it was created so it doesn't receive this simply because this hasn't yet been created so it's created then it receives both of these values down here so this subject observers also have the following methods which are add remove and unsubscribe so let's try unsubscribing the second observer and sending a value after it and this will demonstrate you know when it what it takes to unsubscribe and what happens as a result so what we'll do is we'll reference our observer two right here and we'll say we don't need that one anymore to subscribe so we'll say observer to unsubscribe and then let's push one more event and we'll just say a final thing has been sent all right so if we save this and we go back we'll see that I the observer two receives the two values that were emitted right here in here and then observer two is unsubscribed and observer one will still receive all of the events that are coming from the subject itself so now that you understand what a subject is let's talk about the variations of subjects so there's three different variations and the first one is called the behavior subject and they're all very similar but they offer different capabilities so going back here basically a behavior subject is a special type of subject whose only difference is that it will MIT the last value upon a new observers subscription so what does that mean alright so for instance in the above example that we have here of a regular subject when observer to subscribed and we can look at this in a result it only received the value that was emitted after it or the values rather plural that were submitted after it as we could see in the case of a behavior subject this observer too will receive the last or the previous value that was emitted from the from the behavior subject so in this case it would have received the very first thing that we received was right here the first thing that has been sent so let's better demonstrate this so you can get a better grip of what exactly is happening here we're gonna change subject simply to behavior subject and we're gonna import it from behavior subject and then on line three instead of a new subject this is a behavior subject right here alright so we also have to pass in in the first argument right here we'll just say first because it needs any an initial argument here so what we want to do then to better demonstrate what's happening just after this very first eye event that's emitted we're gonna say subject dot next observer two is about to subscribe alright so now let's go ahead and save this and we're going to see in the browser we have our observer one and three instances of this where it has those first three pushes so it has the very first you appear and then the first thing has been sent observer 2 is about to subscribe then we can see observer 2 right here and this is the first time we see it and it gets the last event that was emitted from the the behavior subject so right here observer 2 is about to subscribe observe it to receives that in the case of a subject you simply wouldn't get that previous value and that really is one of the only differences between behavior subject in a subject itself so now let's talk about the second type of subject and that's called a replay subject all right so it's like behavior subject except it allows you to specify a buffer or number of emitted values to dispatch to observers so behavior subject only dispatches the last emitted value as we can see here and replay subject allows you to dispatch any designated number of values so let's try to go ahead and give this a try in our project so we're gonna change this here from behavior to replay alright same old stuff right here and then in this first parameter we can specify the number of events or values to dispatch to new observers so let's just say - alright so and then let's say what's once again make adjustments to our next calls alright so we'll just say the first thing has been sent let's replicate that and we're gonna change this to another thing has been sent and this is just to give us 3 just to show that it will only return the last two to the new subscriber so it saved that alright so looks let's look for observer 2 we can see the very first thing it receives is another thing has been sent and not the first thing has been sent and not the observer - alright so hopefully you're understanding the differences between subject behavior subject and replay subjects so far they're all very similar I in the most flexibility so far to the plus at this point is basically in replay subject where you can specify any X amount of events that a new observer or a new subscription would see okay all right so now let's go ahead and also talk about another really cool thing that a replay subject is able to do all right it accepts in optional second argument up here and upon creation which is referred to as the window window time and it's defined in milliseconds so it allows you to define a maximum number of events to return in the first argument as we've already done and then the second argument is the time in milliseconds so sounds confusing so to demonstrate this let's make a number of changes here we're gonna change we'll say we want it to return a maximum number of 30 events within a 200 millisecond ibuffer time here all right so then we're going to just make an adjustment right down here we're gonna get rid of these next things right here so we're gonna save our I equals 1 and then our int we're going to set interval if I can type and we're going to say subject dot next so we're going to push a value on an interval and the value that we're going to push is going to be that I and we're going to increment it every 100 milliseconds and then we're going to take our observer to that's created right here and we're going to wrap it in a set timeout and we'll make this timeout I have 500 milliseconds all right so now if we go ahead and let's go ahead and get rid of that unsubscription right there and we can go ahead and get rid of everything else down here as well now if we go ahead and save this alright so we could see that in the browser we have our observer of 1 two three four and five that the observer one receives then after 500 milliseconds the observer - is created and observer two will only receive four in five all right so hopefully that makes sense I we can go ahead and change for instance the window time on line three from 200 to 500 and in this case we'll see that observer two once it's created right here receives all of them all right now the final type of subject that we're going to discuss is called the async subject and this is the last subject variation and it's perhaps the most simple to understand so async subject only emits the very last value and will only do so once the complete method has been called upon the subject so using the example from the above let's just make some slight adjustments we're going to import async subject all right and we can get rid of this sector this table don't need any arguments here and we're going to simply come down here and in our set timeout we're going to call in an order for a synched subject to work we have to call subject doc complete so now let's save that and what has happened well we can see that I the only the very last value has will be sent to each of the observers and they will only be sent for instance if we remove that nothing will happen they only get sent once complete has been called upon the subject all right so for the final section we're going to discuss operators and in our rxjs operators are perhaps what might be used most outside of observables and in and of themselves all right so there's over a hundred plus I don't know I think it's like something like 120 different operators to use and it would take forever to go through all of them so the goal of this specific tutorial is to not go over every single one of them but to help you understand how to read the official documentation and how to read what are called the marble diagrams so that you can have confidence going forward so we still are going to use several of these operators so you can you know get some muscle memory and understand how they're used in relation to subjects and observables alright so I what I want to do first to spend a little bit of time just discussing what an actual operator is alright so there are simply methods that can be you can use on observables and subjects and remember subjects are just special types of observables that allow you to change the original observable in some manner and return a new observable so these operators they don't change the existing observable they simply modify it and return a new one so operators like this are known as pure functions which are functions I can JavaScript that they don't modify the variables outside of its scope all right so the only other two things to understand here in the context of operators are that static operators and then the instance operator so the instance operators right here these are methods on observable instances and these account for the large majority of rxjs operators then you also have static operators and these operators are usually used to create observables and you'll find these mainly under the creation operators all right next I want to show this part the official documentation when it comes to these operators we'll use what is called a marble right here all right so this here is just an excerpt from the official documentation that describes what's happening with a marble diagram and so at the top here this represents the original observable this whole portion right here this arrow is time that's flowing from left to right and these here are events or the values that are emitted from the observables right here and then this vertical line represents the complete notification as we've already discussed and demonstrated how you can complete an observable and here in the middle is the actual operator all right so we're going to be passing the original observable to some type of operator and it could be multiple operators not just one I and then this shows the output like what happened to these events based on whatever happened to this I you know whichever type of operator was passed into all right so they're using an example called x 10 so you know 4 becomes 40 6 becomes 60 and then the X represents an error emitted by the output observable so they're just kind of throwing the kitchen sink at you you're not going to see all this type of stuff with all these marble diagrams so it's really important you know to understand and we're gonna take a look at a few examples and actually integrate them in a code here going forward ok so let's talk about using an operator so the official documentation has a section where you can see categories of operators let me try to find it just kind of lost a page there we go categories of operators so there like I said there's a time there's a creation there's transformation operators there's filtering combination multi casting aerial handling I that utility conditional boolean and mathematical navigate so just just a time that are here and so depending on what you want to do with the incoming observable you would choose an operator from one of these categories so let's say for instance that we wanted to I wanted to merge two separate observables into one well you would use what's called the merge operator and so that's all about combining two observables and it falls under the the combination category in the documentation I got rid of that I shouldn't have done that but let me come back here we'll find it real quickly just so I can show you the marble diagram and then we'll actually work with it in the code so we'll have our let's see our combination there we go right here and we'll find merge and this is what the official documentation looks like it gives you a description and then it shows you that Marvel diagram so here when you have two arrows it's representing two different observables so the events coming from observable one are a B and C observable two would be de and F now in the if you pass it through a merge operator then it becomes and outputs a single observable right here that has all of these values combined so let's actually do that and of course it does give you you know more information as needed okay so to do that let's go ahead and we're going to import just in observable and let's create and observable right here all right so it takes in an observer arrow function and we'll say server next to emit a value of hey guys we're going to go back to the original demonstration here we're gonna say observable to alright and we'll say absorber next would be something like how is it going by the way you don't have to use this create eye operator for creating an observable you can use one of the other creation operators which will do shortly you could use from a lot of different ones there's like ten or eleven all right so this wannabe how's it going is this observable too what if we wanted to merge these two together let's go ahead and by the way get rid of all of this stuff right here and let's create a new observable of our new mobs observable and then we simply call by the way before we can call merge we have to import rxjs observable merge and we'll say merge and then simply the observables that we want to merge so observable and observable - and by the way I screwed that up and need to import merge from there there we go and then finally we have to subscribe to this new observable right here so this would be new of OBS subscribe X of type any typescript stuff there and we'll say add item X all right so we have two different values hey guys how is it going to different observables we're gonna merge them and then we subscribe to them so let's save that and there we go our single observable that has been merged now takes and emits both of these values here all right so let's get more practice just by working with these operators so we can help with muscle memory so a common operator that's used a lot is the map operator and you're probably familiar with it if you've used angular and any degree so let's take a look at that real quick in terms of the marble diagram so in this eye map operator this is the marble diagram for it so we have a single observable up here with the values or the events of one two and three and they pass in the map operator and you pass in whatever events are coming from it being X and then you transform it in some manner and so in this particular case they're going to multiply it by some byte by ten so one becomes 10 20 and 30 so we could do a lot of different things so we could append a string to the end of the vents or make all the results capital or just do anything you know Under the Sun so basically it's a transformation type of operator so let's go ahead and and actually I get experience with just making a real simple adjustment here so we're gonna put in map and then we're going to call observable dot create and I want to do this to show you a less verbose way if you don't necessarily need to create variables out of these so get rid of this and right here just get rid of all that stuff we can say dot map actually will come down dot map and then we'll call will transform the value of which we know is going to be hey guys and we'll just call a javascript function of to uppercase so we're transforming it in some way and then we can subscribe to it and add item X oh and I forgot we need to adjust that slightly this is not the correct location it is ad and then operator map sorry about that now if we go back there we go it's transformed it in some way and in our case it's just to make it uppercase I so again very very simple stuff to understand these operators let's try another one called pluck and so let's say your observable emits an array of objects with multiple properties but you only need a single property so you can pluck that property from the original observable and return a new observable with just that property so this is what the marble diagram looks like for that coming down here pluck alright so we have an object with a property name of one with a value of 1 and also here same thing these are all called V you want to pluck just V well here we go we have the values that are returned are 1 2 & 3 so let's go ahead and try that eye by adjusting our code a bit more so this time instead of just creating an observable from observable let's use one of the other creation operators we haven't used so you can create an and observable using what's called from and this is a creation operator so from and then we're going to include pluck instead of add or map rather and then let's go ahead and get rid of all this and we're going to say from and we're gonna pass in an array of objects so the first will be first we'll name the property first so first name would be Gary last would be Simon age 34 I'm getting old and then I'll use shift alt and down twice and I'll just change these two properties here so Jane and John I'm gonna leave the other ones the same I don't feel like adjusting those so then we can use the pluck operator on this so we'll say pluck first being the name of the for the property that's from this array of objects so we're just going to take that one and then we're going to subscribe to this newly transformed I'm observable so the the original observable is still here but we just this right here will return a new observable that we're going to subscribe to so xne you know the drill at this point and we'll say add item X so now it's only going to return Garry Jane and John the values from the first property alright let's try one more and so we're gonna try one called skip until and this is an operator it's a little bit more complex than the previous examples but it's the last one we're gonna try and it allows you to start emitting events from one observable based on when a second observable begins emitting events so let's try updating our code i from here so we're just going to use an observable from observable and let's replicate this two more times the second observable let's go ahead and use a subject this time and again it's an observable it's just a special type of observable and then we're also going to import interval and you'll see how this comes into play all right and then we're going to import the operator we want to use skip until all right so now let's go ahead I'm going to gut everything here and we're going to say observable one is observable and this is one that's gonna be created just from the create operator data type any and inside of here of our I equals one and then we're going to say set interval data dot next I plus plus so it's just going to start event I'm omitting these events or which are simply going to be introvert all right integers that increase by one and they will do that every one second all right so that's the first observable now let's go ahead and create a subject observer or observable rather so var observable to equals new subject and then we're going to create this subject after a certain amount of time so set timeout and we'll say observable too and we're not going to create it rather I meant we're going to emit a value after three seconds so observable to and because if you remember subjects can as they can act as observers and that they can produce so we can put next hey and I'll do that in three seconds and then we're going to use our skip until so we're going to create a new observable from observable one dot skip until observable to begins emitting its first value alright so then we can subscribe to new observable so we'll say subscribe X any I keep on doing now to put an underline or an underscore rather and then we'll put ad item X alright so the thing that an important thing to note here is that observable to does get created at the same time but it doesn't actually emit a value so it has to emit a value in order for observable one to actually start being displayed and you'll see that here so if we say that we go through three seconds and then it starts producing values and it will only produce a value until this right here gets met that this this subject starts producing values so if you change this to five seconds you count one two three four five there it goes it starts emitting those values so this of course is just one of many operators that you can use that dramatically allow you to control how your observables and how you respond to the events and values that the observables are emitting over time all right so hopefully you found that crash course into rxjs useful because now going forward especially on my own channel we're going to be expanding on be covering rxjs i in relation to angular and i'm also going to be creating actual practical practical example projects using what we've learned with these observables and the subjects in operators alright so as always if you enjoyed it make sure you subscribe to the youtube channel check out course cetera comm and I'll see you soon
Info
Channel: DesignCourse
Views: 296,467
Rating: 4.869483 out of 5
Keywords: rxjs, rxjs tutorial, reactivex, reactivex tutorial, javascript tutorial, javascript, observables tutorial, observers tutorial, javascript observables, what is an observable, how to install rxjs, rxjs observables, observables, rxjs operators, operators, tutorial, reactivex javascript, rxjs javascript, course, rxjs course, angular 6, learn rxjs in 60 minutes, rxjs in 60 minutes, rxjs 60 minutes, rsjx
Id: PhggNGsSQyg
Channel Id: undefined
Length: 59min 50sec (3590 seconds)
Published: Fri Mar 30 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.