mirror of
https://github.com/centau/vide.git
synced 2026-10-22 20:54:09 +00:00
Fix actions not running in priority order (#107)
Some checks are pending
unit-test / unit-test (push) Waiting to run
Some checks are pending
unit-test / unit-test (push) Waiting to run
Co-authored-by: centau <83140718+centau@users.noreply.github.com>
This commit is contained in:
parent
dda5e0f2bb
commit
d99dbe1873
2 changed files with 30 additions and 4 deletions
|
|
@ -23,6 +23,9 @@ type Cache = {
|
||||||
Array<(Instance) -> ()> -- action callbacks
|
Array<(Instance) -> ()> -- action callbacks
|
||||||
>,
|
>,
|
||||||
|
|
||||||
|
-- priorities of the actions, in ascending order
|
||||||
|
priorities: Array<number>,
|
||||||
|
|
||||||
-- what to parent the instance to after running actions
|
-- what to parent the instance to after running actions
|
||||||
parent: unknown,
|
parent: unknown,
|
||||||
|
|
||||||
|
|
@ -45,11 +48,19 @@ local function borrow_cache(): Cache
|
||||||
free_cache = nil
|
free_cache = nil
|
||||||
return cache
|
return cache
|
||||||
else
|
else
|
||||||
|
local priorities = {}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
events = {},
|
events = {},
|
||||||
actions = setmetatable({} :: any, { -- lazy init
|
actions = setmetatable({} :: any, { -- lazy init, records new priorities in order
|
||||||
__index = function(self, i) self[i] = {}; return self[i] end
|
__index = function(self, i: number)
|
||||||
|
self[i] = {}
|
||||||
|
table.insert(priorities, i)
|
||||||
|
table.sort(priorities)
|
||||||
|
return self[i]
|
||||||
|
end
|
||||||
}),
|
}),
|
||||||
|
priorities = priorities,
|
||||||
parent = nil,
|
parent = nil,
|
||||||
nested_debug = setmetatable({} :: any, {
|
nested_debug = setmetatable({} :: any, {
|
||||||
__index = function(self, i: number) self[i] = {}; return self[i] end
|
__index = function(self, i: number) self[i] = {}; return self[i] end
|
||||||
|
|
@ -116,6 +127,7 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
||||||
local caches = borrow_cache()
|
local caches = borrow_cache()
|
||||||
local events = caches.events
|
local events = caches.events
|
||||||
local actions = caches.actions
|
local actions = caches.actions
|
||||||
|
local priorities = caches.priorities
|
||||||
local nested_debug = caches.nested_debug
|
local nested_debug = caches.nested_debug
|
||||||
local nested_stack = caches.nested_stack
|
local nested_stack = caches.nested_stack
|
||||||
|
|
||||||
|
|
@ -133,8 +145,8 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
||||||
;(instance :: any)[event_name]:Connect(event_listener)
|
;(instance :: any)[event_name]:Connect(event_listener)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, queued in actions do
|
for _, priority in priorities do
|
||||||
for _, callback in queued do
|
for _, callback in actions[priority] do
|
||||||
callback(instance)
|
callback(instance)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -2469,6 +2469,20 @@ TEST("actions", function()
|
||||||
|
|
||||||
CHECK(testkit.seq(queue, { 1, 2 }))
|
CHECK(testkit.seq(queue, { 1, 2 }))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "non-consecutive priorities"
|
||||||
|
local queue = {}
|
||||||
|
|
||||||
|
create "Frame" {
|
||||||
|
action(function() table.insert(queue, 100) end, 100),
|
||||||
|
action(function() table.insert(queue, 2.5) end, 2.5),
|
||||||
|
action(function() table.insert(queue, -1) end, -1),
|
||||||
|
action(function() table.insert(queue, 0) end, 0),
|
||||||
|
action(function() table.insert(queue, 10) end, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(testkit.seq(queue, { -1, 0, 2.5, 10, 100 }))
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("changed()", wrap_root(function()
|
TEST("changed()", wrap_root(function()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue