1 00:00:02,080 --> 00:00:03,420 We've got our Singleton put together. 2 00:00:03,430 --> 00:00:07,840 But now the big question is how do we actually use this from somewhere else inside of our project. 3 00:00:07,840 --> 00:00:12,820 Well the first thing I want to do is improve the semantics around accessing the client inside of your 4 00:00:12,850 --> 00:00:13,890 a little bit more. 5 00:00:13,930 --> 00:00:19,150 Right now we have Mark the client instance variable as private which means nothing else inside of application 6 00:00:19,150 --> 00:00:21,160 can access this client. 7 00:00:21,160 --> 00:00:26,020 Remember that our ultimate goal here is inside of our new ticket root handler. 8 00:00:26,050 --> 00:00:31,450 So here is roots new the ultimate goal is to yield to call ticket create a publisher and provide the 9 00:00:31,450 --> 00:00:35,050 client like the actual Nats client as an argument to it. 10 00:00:35,080 --> 00:00:40,230 So we do need to somehow expose this client to the outside world but just keep in mind that this clan 11 00:00:40,230 --> 00:00:43,090 thing might at some point times be undefined. 12 00:00:43,090 --> 00:00:48,520 So I want to expose this to outside people but I also want to somehow throw an air or something very 13 00:00:48,520 --> 00:00:53,140 similar to that if someone tries to access this client before it is actually ready to be used. 14 00:00:53,150 --> 00:00:58,510 So in other words before we have actually called Connect so to do so we're going to use a typescript 15 00:00:58,550 --> 00:01:05,510 getter that we're going to assign to this class inside the class I'm going to add in get and then client. 16 00:01:05,510 --> 00:01:12,140 So the idea here is that we're going to access the Nats client by using this client property and if 17 00:01:12,140 --> 00:01:17,210 we tried to call this thing or make use this thing before we have actually use connect I want to throw 18 00:01:17,210 --> 00:01:25,760 in air so I can stay inside of your if not this dot underscore a client row new air and we're gonna 19 00:01:25,760 --> 00:01:33,910 throw an error with a text or something like an OT access Nats client before connecting 20 00:01:38,090 --> 00:01:42,830 if we have already defined the client so in other words if we have called Connect already then asked 21 00:01:42,860 --> 00:01:51,790 if statement we'll return this dot underscore client so now we can go back over to our new root handler 22 00:01:53,020 --> 00:02:01,060 inside the roots directory inside the new file at the very top we can import Nats wrapper from up one 23 00:02:01,060 --> 00:02:08,650 directory Nats wrapper and now when we attempt to publish that event rather than passing and just client 24 00:02:08,650 --> 00:02:15,510 right there we'll do it Nats wrapper dots client like so so that's how we make use of a getter in typescript 25 00:02:15,550 --> 00:02:20,830 we do not do client with parentheses because that is not a function we just defined it as a getter it's 26 00:02:20,830 --> 00:02:28,510 going to define the client property on the instance so it's going to be just client by itself if we 27 00:02:28,510 --> 00:02:32,360 try to access this client before it's actually been defined or before we've connected to Nats. 28 00:02:32,410 --> 00:02:36,730 Like I said this function is gonna throw an error and so we'll see that light up and we'll know oh we 29 00:02:36,730 --> 00:02:42,510 tried to access the Nats client before we actually tried to connect now with that get her in place we 30 00:02:42,510 --> 00:02:48,260 can also make a little update inside the promise down here it's now rather than trying to say this dot 31 00:02:48,300 --> 00:02:53,130 underscore our client and have to put in the nasty exclamation right there we can instead just do this 32 00:02:53,220 --> 00:02:53,750 dot. 33 00:02:53,750 --> 00:02:55,140 Clients in both them 34 00:02:58,090 --> 00:03:02,650 and so if for some reason we assigned underscore client right here and then that thing got unassigned 35 00:03:02,740 --> 00:03:08,230 very unlikely extremely unlikely not really gonna happen but if that happens between that assignment 36 00:03:08,260 --> 00:03:13,120 and a running the body of this promise well this will make sure we throw an error and say hey something 37 00:03:13,120 --> 00:03:17,770 really funny is going on OK so that is just about it. 38 00:03:17,770 --> 00:03:20,490 So we've now got this singleton class. 39 00:03:20,890 --> 00:03:27,410 It has a connect method that we can await that connection on which is fantastic the Connect method is 40 00:03:27,410 --> 00:03:31,220 going to take these connection properties also great. 41 00:03:31,370 --> 00:03:36,110 We only have one instance of this class floating around because we exported a class at the bottom so 42 00:03:36,110 --> 00:03:39,800 that means that we can connect from inside of our index not to yes file. 43 00:03:39,800 --> 00:03:44,150 We can then access the client from any other file inside of our project and we do not have to worry 44 00:03:44,150 --> 00:03:46,650 about any cyclical dependencies. 45 00:03:46,790 --> 00:03:52,310 So the singleton is now complete and it is looking pretty good we're pretty much done with this as far 46 00:03:52,310 --> 00:03:54,180 as the kind of Singleton side goes. 47 00:03:54,260 --> 00:03:57,920 But there is one last thing we're going to take care of inside of here. 48 00:03:57,920 --> 00:04:03,710 You might recall that back on our Nats test little mini project if we take a look at that listener Test 49 00:04:03,710 --> 00:04:08,930 File once again you'll recall that we made a really big deal around making sure that we cleaned up our 50 00:04:08,930 --> 00:04:14,300 Nats connection whenever we were about to end this process. 51 00:04:14,340 --> 00:04:17,650 So we need to make sure that we do something very similar inside of our project as well. 52 00:04:17,820 --> 00:04:22,980 Whenever our project is about to be shut down we want to make sure that we clean up our Nats connection 53 00:04:24,730 --> 00:04:27,940 so let's take care of that last little bit of record keeping in the next video.