function ServiceBag._addServiceType(A,Q)if not A._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(tostring(Q)))return end if A._services[Q]then return end local P=setmetatable({},{__index=Q})A._services[Q]=P A:_ensureInitialization(Q)end function ServiceBag._ensureInitialization(A,Q)if A._initializedServiceTypeSet[Q]then return end if A._initializing then A._serviceTypesToInitializeSet[Q]=nil A._initializedServiceTypeSet[Q]=true A:_initService(Q)elseif A._serviceTypesToInitializeSet then A._serviceTypesToInitializeSet[Q]=true else error("\091\083\101\114\118\105\099\101\066\097\103\046\095\101\110\115\117\114\101\073\110\105\116\105\097\108\105\122\097\116\105\111\110\093 - \067\097\110\110\111\116 \105\110\105\116\105\097\108\105\122\101 \112\097\115\116 \105\110\105\116\105\097\108\105\122\105\110\103 \112\104\097\115\101 ")end end function ServiceBag._initService(A,Q)local P=assert(A._services[Q],"\078\111 \115\101\114\118\105\099\101")if P.Init then local Q task.spawn(function()Q=coroutine.running()P:Init(A)end)assert(coroutine.status(Q)=="\100\101\097\100","\073\110\105\116\105\097\108\105\122\105\110\103 \115\101\114\118\105\099\101 \121\105\101\108\100\101\100")end table.insert(A._serviceTypesToStart,Q)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(tostring(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
		error("[ServiceBag._ensureInitialization] - Cannot initialize past initializing phase ")
	end
end

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

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

		assert(coroutine.status(current) == "dead", "Initializing service yielded")
	end

	table.insert(self._serviceTypesToStart, serviceType)
end
