Skip to content

Commit

Permalink
fix: immediate should fire events after last completion
Browse files Browse the repository at this point in the history
  • Loading branch information
littensy committed Jan 3, 2025
1 parent da2dfd4 commit 6d98249
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/createMotion.luau
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ local function createMotion<T>(initialValue: T, options: types.MotionOptions?):
local value = self:get()
local complete = self:isComplete()

if not wasComplete or not complete then
if not complete or not wasComplete or lastCompleteValue ~= value then
for _, listener in onStepListeners do
task.spawn(listener, value, deltaTime)
end
Expand Down
67 changes: 64 additions & 3 deletions src/solvers/immediate.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return function()
local motion = createMotion({ x = 0, y = 0 })

motion:to(immediate({ x = 1, y = 1 }))
motion:step(0)
motion:step(1)

expect(motion:get()).to.be.ok()
expect(motion:get().x).to.equal(1)
Expand All @@ -18,19 +18,80 @@ return function()
local motion = createMotion({ x = 0, y = 0 })

motion:to({ x = immediate(1), y = immediate(1) })
motion:step(0)
motion:step(1)

expect(motion:get()).to.be.ok()
expect(motion:get().x).to.equal(1)
expect(motion:get().y).to.equal(1)
expect(motion:isComplete()).to.equal(true)

motion:to({ x = immediate(2), y = immediate(2) })
motion:step(0)
motion:step(1)

expect(motion:get()).to.be.ok()
expect(motion:get().x).to.equal(2)
expect(motion:get().y).to.equal(2)
expect(motion:isComplete()).to.equal(true)
end)

it("should wake spring after last completion", function()
local motion = createMotion(0)

motion:linear(1, { speed = 1 })
motion:step(1)

expect(motion:isComplete()).to.equal(true)

local firedOnStep = 0
local firedOnComplete = 0

motion:onStep(function()
firedOnStep += 1
end)

motion:onComplete(function()
firedOnComplete += 1
end)

motion:immediate(0)

expect(motion:isComplete()).to.equal(false)

motion:step(1)

expect(motion:isComplete()).to.equal(true)
expect(firedOnStep).to.equal(1)
expect(firedOnComplete).to.equal(1)

motion:step(1)

expect(firedOnStep).to.equal(1)
expect(firedOnComplete).to.equal(1)
end)

it("should trigger events", function()
local motion = createMotion(0)
local firedOnStep = 0
local firedOnComplete = 0

motion:onStep(function()
firedOnStep += 1
end)

motion:onComplete(function()
firedOnComplete += 1
end)

motion:immediate(0)
motion:step(1)

expect(motion:isComplete()).to.equal(true)
expect(firedOnStep).to.equal(1)
expect(firedOnComplete).to.equal(1)

motion:step(1)

expect(firedOnStep).to.equal(1)
expect(firedOnComplete).to.equal(1)
end)
end
2 changes: 1 addition & 1 deletion wally.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies = []

[[package]]
name = "littensy/ripple"
version = "0.8.1"
version = "0.9.1"
dependencies = [["Promise", "evaera/[email protected]"], ["TestEZ", "roblox/[email protected]"]]

[[package]]
Expand Down

0 comments on commit 6d98249

Please sign in to comment.