We've got our Singleton put together.
But now the big question is how do we actually use this from somewhere else inside of our project.
Well the first thing I want to do is improve the semantics around accessing the client inside of your
a little bit more.
Right now we have Mark the client instance variable as private which means nothing else inside of application
can access this client.
Remember that our ultimate goal here is inside of our new ticket root handler.
So here is roots new the ultimate goal is to yield to call ticket create a publisher and provide the
client like the actual Nats client as an argument to it.
So we do need to somehow expose this client to the outside world but just keep in mind that this clan
thing might at some point times be undefined.
So I want to expose this to outside people but I also want to somehow throw an air or something very
similar to that if someone tries to access this client before it is actually ready to be used.
So in other words before we have actually called Connect so to do so we're going to use a typescript
getter that we're going to assign to this class inside the class I'm going to add in get and then client.
So the idea here is that we're going to access the Nats client by using this client property and if
we tried to call this thing or make use this thing before we have actually use connect I want to throw
in air so I can stay inside of your if not this dot underscore a client row new air and we're gonna
throw an error with a text or something like an OT access Nats client before connecting
if we have already defined the client so in other words if we have called Connect already then asked
if statement we'll return this dot underscore client so now we can go back over to our new root handler
inside the roots directory inside the new file at the very top we can import Nats wrapper from up one
directory Nats wrapper and now when we attempt to publish that event rather than passing and just client
right there we'll do it Nats wrapper dots client like so so that's how we make use of a getter in typescript
we do not do client with parentheses because that is not a function we just defined it as a getter it's
going to define the client property on the instance so it's going to be just client by itself if we
try to access this client before it's actually been defined or before we've connected to Nats.
Like I said this function is gonna throw an error and so we'll see that light up and we'll know oh we
tried to access the Nats client before we actually tried to connect now with that get her in place we
can also make a little update inside the promise down here it's now rather than trying to say this dot
underscore our client and have to put in the nasty exclamation right there we can instead just do this
dot.
Clients in both them
and so if for some reason we assigned underscore client right here and then that thing got unassigned
very unlikely extremely unlikely not really gonna happen but if that happens between that assignment
and a running the body of this promise well this will make sure we throw an error and say hey something
really funny is going on OK so that is just about it.
So we've now got this singleton class.
It has a connect method that we can await that connection on which is fantastic the Connect method is
going to take these connection properties also great.
We only have one instance of this class floating around because we exported a class at the bottom so
that means that we can connect from inside of our index not to yes file.
We can then access the client from any other file inside of our project and we do not have to worry
about any cyclical dependencies.
So the singleton is now complete and it is looking pretty good we're pretty much done with this as far
as the kind of Singleton side goes.
But there is one last thing we're going to take care of inside of here.
You might recall that back on our Nats test little mini project if we take a look at that listener Test
File once again you'll recall that we made a really big deal around making sure that we cleaned up our
Nats connection whenever we were about to end this process.
So we need to make sure that we do something very similar inside of our project as well.
Whenever our project is about to be shut down we want to make sure that we clean up our Nats connection
so let's take care of that last little bit of record keeping in the next video.