Sync to released Jecs 0.5.5-nightly.20250423T001100Z (#42)

Reviewed-on: #42
This commit is contained in:
marked 2025-04-23 02:11:21 +02:00
parent 5c8f32e236
commit 4a0ba9a95f
8 changed files with 199 additions and 126 deletions

View file

@ -101,45 +101,50 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.added = function(_, component, fn)
local listeners = signals.added[component]
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then
listeners = {}
signals.added[component] = listeners
local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_add = function(entity)
for _, listener in listeners do
listener(entity, component)
local function on_add(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
end
end
end
world:set(component, jecs.OnAdd, on_add) end
table.insert(listeners, fn)
end
world.changed = function(_, component, fn)
local listeners = signals.emplaced[component]
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then
listeners = {}
signals.emplaced[component] = listeners
local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_change = function(entity, value)
for _, listener in listeners do
listener(entity, component, value)
local function on_change(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
end
end
world:set(component, jecs.OnChange, on_change)
end
table.insert(listeners, fn)
end
world.removed = function(_, component, fn)
local listeners = signals.removed[component]
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then
listeners = {}
signals.removed[component] = listeners
local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_remove = function(entity)
for _, listener in listeners do
listener(entity, component)
local function on_remove(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
end
end
world:set(component, jecs.OnRemove, on_remove)
end
table.insert(listeners, fn)
end
@ -150,7 +155,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.monitor = monitors_new
return world
return world :: PatchedWorld
end
return observers_add