-
-
Notifications
You must be signed in to change notification settings - Fork 99
Allow us to get access to uninitialized services / controllers #210
Comments
This is not possible because controllers and services are only added to Knit when required, meaning that Knit has no knowledge of the controller or service existing beforehand. |
This is possible. And I, in fact, know how to implement it as well. The concept goes as such: |
The concept it self I strongly don't support because it actually ruins the main point of Knit (to avoid nasty race conditions, even if one may not occur at a specific point). Additionally an additional method |
One potential solution to this is to have Issues such as non-existent services/controllers, indexing them before they are ready, etc, can be enforced using metamethods. |
Out of curiosity, would incorporating this function that I just wrote up into local function getKnitService(service: string)
return setmetatable({}, {
__index = function(dummyTable)
local success = Knit.OnStart():timeout(1/60):await() -- Hacky, but this is out of Knit's scope.
if not success then
error("Knit has not been initialized yet!")
end
local exists, serviceObjectOrWarning = pcall(function()
return Knit.GetService(service)
end)
if not exists then
setmetatable(dummyTable, {}) -- Don't create repetitive errors.
error(serviceObjectOrWarning)
end
for k, v in pairs(serviceObjectOrWarning) do
rawset(dummyTable, k, v)
end
setmetatable(dummyTable, {})
end
})
end |
Made a PR. |
I think I'm a little uneasy with services existing in an unpopulated state. In other words, the returned service is not actually the service yet at all. The table ref returned is expected to be mutated (in an ideal world, services should have no mutations IMO, but Knit v2 will address that) |
@Sleitnick as mentioned, your uneasy with the idea of unpopulated services ~ instead could we introduce a method to get premature services? (This would keep services - as services, but premature services as a way to define a reference to an not-yet-set service; Which would also avoid tampering with Knit services as a whole. ) I primarily want this change to help make the codebase cleaner with the definitions of services and controllers references completed at the top local Knit = require(path.to.Knit)
local SessionService = Knit.GetPrematureService("SessionService") |
Services/controllers should not be accessed if they're uninitialized |
Currently, to get access to a service/controller, you have to use
.GetService()
and.GetController()
respectively in the:Start()
method of another controller. It looks something like this:While this is fine, I don't personally like spending 2 lines to get access to each controller and I feel like there could be a better way. It could go something like this:
I suggest adding an optional argument to
.GetService()
and.GetController()
that allows you to access a service/controller before it gets initialized.Let me know what you think. I'd be happy to implement this and do a PR.
The text was updated successfully, but these errors were encountered: