Update tween.luau

fix test fail again! yes i'm a retard
This commit is contained in:
sindri 2026-03-03 13:53:49 +01:00 committed by GitHub
parent 8ca9e9b440
commit 4edaddf151
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,47 +1,62 @@
-- src/tween.luau -- src/tween.luau
local TweenService = game and game:GetService("TweenService") or nil local TweenService = game and game:GetService("TweenService") or nil
local HasTweenInfo = (TweenInfo ~= nil) local HasTweenInfo = (TweenInfo ~= nil)
--cleanup and action injection so tweens are stopped on scope destruction
-- Factory: inject action + cleanup so tweens are stopped on scope destruction
return function(action, cleanup) return function(action, cleanup)
local tween = {} local tween = {}
-- Presets (simple, useful) -- Presets: only available when TweenInfo exists (Roblox runtime)
if HasTweenInfo then if HasTweenInfo then
tween.presets = { tween.presets = {
instant = TweenInfo.new(0), instant = TweenInfo.new(0),
fast = TweenInfo.new(0.12, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), fast = TweenInfo.new(0.12, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
smooth = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), smooth = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
snappy = TweenInfo.new(0.16, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), snappy = TweenInfo.new(0.16, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),
} }
else
tween.presets = {}
end
-- Imperative helper (for events): vide.tweenTo(instance, {Position=...}, info) -- Imperative helper: plays a tween (or applies instantly in tests)
function tween.tweenTo(instance, goals, info) function tween.tweenTo(instance, goals, info)
if not instance or not goals then
return nil
end
-- Non-Roblox / unit tests: apply instantly
if not TweenService then if not TweenService then
-- Non-Roblox / tests: just apply instantly for k, v in pairs(goals) do
if instance and goals then instance[k] = v
for k, v in pairs(goals) do
instance[k] = v
end
end end
return nil return nil
end end
local tweenInfo = info or tween.presets.smooth
local tweenInfo = info
if tweenInfo == nil then
if tween.presets.smooth then
tweenInfo = tween.presets.smooth
else
-- Fallback in case presets are not available for any reason
tweenInfo = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
end
end
local tw = TweenService:Create(instance, tweenInfo, goals) local tw = TweenService:Create(instance, tweenInfo, goals)
tw:Play() tw:Play()
return tw return tw
end end
-- Action helper (for create trees): -- Action helper: attach tweening in create() trees
-- create "Frame" { tween({Position=...}, presets.fast), ... }
function tween.tween(goals, info) function tween.tween(goals, info)
return action(function(instance) return action(function(instance)
if not goals then if not goals then
return return
end end
-- If TweenService not available, apply instantly (tests outside of roblox environment) -- Non-Roblox / unit tests: apply instantly
if not TweenService then if not TweenService then
for k, v in pairs(goals) do for k, v in pairs(goals) do
instance[k] = v instance[k] = v
@ -49,11 +64,19 @@ return function(action, cleanup)
return return
end end
local tweenInfo = info or tween.presets.smooth local tweenInfo = info
if tweenInfo == nil then
if tween.presets.smooth then
tweenInfo = tween.presets.smooth
else
tweenInfo = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
end
end
local tw = TweenService:Create(instance, tweenInfo, goals) local tw = TweenService:Create(instance, tweenInfo, goals)
tw:Play() tw:Play()
-- Stop on scope destruction to avoid leaking connections/tweens -- Ensure tween is cancelled when the scope is destroyed
cleanup(function() cleanup(function()
pcall(function() pcall(function()
tw:Cancel() tw:Cancel()