Knit icon indicating copy to clipboard operation
Knit copied to clipboard

.GetService() and .GetController() work on uninitialized services and controllers

Open Weldify opened this issue 2 years ago • 4 comments

When using .GetService() or .GetController() the service/controller you're getting doesn't need to exist at the time. This reduces boilerplate.

local service = Knit.CreateService {
   Name = "Service";
}

local otherService

function service:KnitStart()
   otherService = Knit.GetService("OtherService")
   --otherService is ready to use
end

is now

local service = Knit.CreateService {
   Name = "Service";
}

local otherService = Knit.GetService("OtherService")

function service:KnitStart()
   --otherService is ready to use
end

If you use .GetService() or .GetController() and never create the said service, Knit will error while starting:

Service ServiceName is not defined
Controller ControllerName is not defined

Everything should work pretty well, but the types might be a bit wack. So if you plan on merging this then I suggest you take a look at the types and maybe tweak them around a bit.

Weldify avatar May 22 '22 12:05 Weldify

I think the goal of Knit was to not have weird metatable hacks like AGF did.

howmanysmall avatar May 22 '22 19:05 howmanysmall

I think the goal of Knit was to not have weird metatable hacks like AGF did.

This doesn't utilize any metatables or hacks, just plain old table behavior :)

Weldify avatar May 22 '22 19:05 Weldify

oh wow, impressive

howmanysmall avatar May 22 '22 19:05 howmanysmall

But you will be need to use controller/service in KnitStart method anyway. Also this solution will make code bigger and more complex. But your solution is great anyway.

Insadem avatar Jul 18 '22 17:07 Insadem