Change monitor to be iterable

This commit is contained in:
Ukendio 2025-07-06 09:48:11 +02:00
parent a9891abf6d
commit 169ec09ed5

View file

@ -12,7 +12,7 @@ export type PatchedWorld = jecs.World & {
monitor: ( monitor: (
PatchedWorld, PatchedWorld,
any, any,
(jecs.Entity, jecs.Id) -> () (<a>(jecs.Entity, jecs.Id<a>) -> ())?
) -> () ) -> ()
} }
@ -120,7 +120,9 @@ local function monitors_new(world, query, callback)
end end
local entity_index = world.entity_index :: any local entity_index = world.entity_index :: any
local function emplaced(entity: jecs.Entity) local i = 0
local entities = {}
local function emplaced(entity, id, value)
local r = jecs.entity_index_try_get_fast( local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any) entity_index, entity :: any)
@ -131,13 +133,19 @@ local function monitors_new(world, query, callback)
local archetype = r.archetype local archetype = r.archetype
if jecs.query_match(query, archetype) then if jecs.query_match(query, archetype) then
callback(entity, jecs.OnAdd) i += 1
entities[i] = entity
if callback ~= nil then
callback(entity, id, value)
end
end end
end end
local function removed(entity: jecs.Entity, component: jecs.Id) local function removed(entity: jecs.Entity, component: jecs.Id)
local EcsOnRemove = jecs.OnRemove :: jecs.Id local EcsOnRemove = jecs.OnRemove :: jecs.Id
callback(entity, EcsOnRemove) if callback ~= nil then
callback(entity, EcsOnRemove)
end
end end
for _, term in terms do for _, term in terms do