-- ReplicaController functions:


function ReplicaController.RequestData() -- Call after all client controllers are loaded and before CoreReadySignal is fired
	if DataRequestStarted == true then
		return
	end
	DataRequestStarted = true
	task.spawn(function() -- In case the initial rev_ReplicaRequestData signal was lost (Highly unlikely)
		while game:IsLoaded() == false do
			task.wait()
		end
		rev_ReplicaRequestData:FireServer()
		while task.wait(SETTINGS.RequestDataRepeat) do
			if ReplicaController.InitialDataReceived == true then
				break
			end
			rev_ReplicaRequestData:FireServer()
		end
	end)
end

function ReplicaController.ReplicaOfClassCreated(replica_class, listener) --> [ScriptConnection] listener(replica)
	if type(replica_class) ~= "string" then
		error("[ReplicaController]: replica_class must be a string")
	end
	if type(listener) ~= "function" then
		error("[ReplicaController]: Only a function can be set as listener in ReplicaController.ReplicaOfClassCreated()")
	end
	-- Getting listener table for replica class:
	local signal = ClassListeners[replica_class]
	if signal == nil then
		signal = Madwork.NewScriptSignal()
		ClassListeners[replica_class] = signal
	end
	return signal:Connect(listener, function()
		-- Cleanup script signals that are no longer used:
		if signal:GetListenerCount() == 0 and ClassListeners[replica_class] == signal then
			ClassListeners[replica_class] = nil
		end
	end)
end

function ReplicaController.GetReplicaById(replica_id)
	return Replicas[replica_id]
end
