Skip to content

Commit

Permalink
Iter should not drain by default (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Sep 16, 2024
1 parent 87754c1 commit 62ac6e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
21 changes: 14 additions & 7 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -956,14 +956,16 @@ local EMPTY_QUERY = {
setmetatable(EMPTY_QUERY, EMPTY_QUERY)

local function query_init(query)
local world_query_iter_next = query.iter_next
if world_query_iter_next then
return world_query_iter_next
local world_query_iter_next

if query.should_drain then
world_query_iter_next = query.iter_next
if world_query_iter_next then
return world_query_iter_next
end
end

local compatible_archetypes = query.compatible_archetypes
local ids = query.ids
local A, B, C, D, E, F, G, H, I = unpack(ids)
local a, b, c, d, e, f, g, h
local lastArchetype = 1
local archetype = compatible_archetypes[1]
if not archetype then
Expand All @@ -972,8 +974,12 @@ local function query_init(query)
local columns = archetype.columns
local entities = archetype.entities
local i = #entities

local records = archetype.records

local ids = query.ids
local A, B, C, D, E, F, G, H, I = unpack(ids)
local a, b, c, d, e, f, g, h

if not B then
a = columns[records[A].column]
elseif not C then
Expand Down Expand Up @@ -1216,6 +1222,7 @@ end
local function query_drain(query)
local query_iter_next = query_init(query)
query.next = query_iter_next
query.should_drain = true
return query
end

Expand Down
22 changes: 20 additions & 2 deletions test/tests.luau
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ TEST("world:add()", function()
end)

TEST("world:query()", function()
do CASE "multiple iter"
local world = jecs.World.new()
local A = world:component()
local B = world:component()
local e = world:entity()
world:add(e, A, "a")
world:add(e, B)
local q = world:query(A, B)
local counter = 0
for x in q:iter() do
counter += 1
end
for x in q:iter() do
counter += 1
end
print(counter)
CHECK(counter == 2)
end
do CASE "tag"
local world = jecs.World.new()
local A = world:entity()
Expand Down Expand Up @@ -545,8 +563,8 @@ TEST("world:query()", function()

do CASE("should error when setting invalid pair")
local world = jecs.World.new()
local Eats = world:entity()
local Apples = world:entity()
local Eats = world:component()
local Apples = world:component()
local bob = world:entity()

world:delete(Apples)
Expand Down
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ukendio/jecs"
version = "0.3.0-rc.1"
version = "0.3.0-rc.2"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
include = [
Expand Down

0 comments on commit 62ac6e6

Please sign in to comment.