function ServiceBag._addServiceType(W,H)if not W._serviceTypesToInitializeSet then error(("\065\108\114\101\097\100\121 \102\105\110\105\115\104\101\100 \105\110\105\116\105\097\108\105\122\105\110\103\044 \099\097\110\110\111\116 \097\100\100 \037\113"):format(W:_getServiceName(H)))return end if W._services[H]then return end local A=setmetatable({},{__index=H})W._services[H]=A W:_ensureInitialization(H)end function ServiceBag._ensureInitialization(W,H)if W._initializedServiceTypeSet[H]then return end if W._initializing then W._serviceTypesToInitializeSet[H]=nil W._initializedServiceTypeSet[H]=true W:_initService(H)elseif W._serviceTypesToInitializeSet then W._serviceTypesToInitializeSet[H]=true else local A=W:_getServiceName(H)error(string.format("\067\097\110\110\111\116 \105\110\105\116\105\097\108\105\122\101 \115\101\114\118\105\099\101 \037\113 \112\097\115\116 \105\110\105\116\105\097\108\105\122\105\110\103 \112\104\097\115\101",A))end end function ServiceBag._initService(W,H)local A=assert(W._services[H],"\078\111 \115\101\114\118\105\099\101")local T=W:_getServiceName(H)if A.Init then local H task.spawn(function()debug.setmemorycategory(T)H=coroutine.running()A:Init(W)end)local y=coroutine.status(H)=="\100\101\097\100"if not y then error(("\073\110\105\116\105\097\108\105\122\105\110\103 \115\101\114\118\105\099\101 \037\113 \121\105\101\108\100\101\100"):format(T))end end table.insert(W._serviceTypesToStart,H)end
-- Adds a service to this provider only
function ServiceBag:_addServiceType(serviceType)
	if not self._serviceTypesToInitializeSet then
		error(("Already finished initializing, cannot add %q"):format(self:_getServiceName(serviceType)))
		return
	end

	-- Already added
	if self._services[serviceType] then
		return
	end

	-- Construct a new version of this service so we're isolated
	local service = setmetatable({}, { __index = serviceType })
	self._services[serviceType] = service

	self:_ensureInitialization(serviceType)
end

function ServiceBag:_ensureInitialization(serviceType)
	if self._initializedServiceTypeSet[serviceType] then
		return
	end

	if self._initializing then
		self._serviceTypesToInitializeSet[serviceType] = nil
		self._initializedServiceTypeSet[serviceType] = true
		self:_initService(serviceType)
	elseif self._serviceTypesToInitializeSet then
		self._serviceTypesToInitializeSet[serviceType] = true
	else
		local serviceName = self:_getServiceName(serviceType)
		error(string.format("Cannot initialize service %q past initializing phase", serviceName))
	end
end

function ServiceBag:_initService(serviceType)
	local service = assert(self._services[serviceType], "No service")
	local serviceName = self:_getServiceName(serviceType)

	if service.Init then
		local current
		task.spawn(function()
			debug.setmemorycategory(serviceName)
			current = coroutine.running()
			service:Init(self)
		end)

		local isDead = coroutine.status(current) == "dead"
		if not isDead then
			error(("Initializing service %q yielded"):format(serviceName))
		end
	end

	table.insert(self._serviceTypesToStart, serviceType)
end
