--[[ @brief Performs an operation on heartbeat based on a condition.
     @param callback The callback function for each heartbeat step.
     @param condition The condition we should check before running callback.
     @param yield Whether this function should yield or return immediately.
--]]

function AnimateLib.ConditionalOnHeartbeat(callback, condition, yield, callbackOnComplete)
	local cxn;
	local event = Utils.new("Event");
	local startTime = tick();
	cxn = game:GetService("RunService").Heartbeat:connect(function(step)
		if condition() then
			callback(step, tick() - startTime)
		else
			cxn:disconnect();
			event:Fire();
			if callbackOnComplete then
				callbackOnComplete();
			end
		end
	end);
	if yield then
		event:wait();
	end
end
