Set signal with hook

This commit is contained in:
Ukendio 2025-04-22 04:49:52 +02:00
parent 42401f93ae
commit ef0f69ac6d

View file

@ -104,13 +104,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
if not listeners then if not listeners then
listeners = {} listeners = {}
signals.added[component] = listeners signals.added[component] = listeners
local idr = jecs.id_record_ensure(world, component) local function on_add(entity: number, id: number, value: any)
idr.hooks.on_add = function(entity) for _, listener in listeners :: any do
for _, listener in listeners do listener(entity, id, value)
listener(entity, component)
end
end end
end end
world:set(component, jecs.OnAdd, on_add) end
table.insert(listeners, fn) table.insert(listeners, fn)
end end
@ -119,12 +118,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
if not listeners then if not listeners then
listeners = {} listeners = {}
signals.emplaced[component] = listeners signals.emplaced[component] = listeners
local idr = jecs.id_record_ensure(world, component) local function on_change(entity: number, id: number, value: any)
idr.hooks.on_change = function(entity, value) for _, listener in listeners :: any do
for _, listener in listeners do listener(entity, id, value)
listener(entity, component, value)
end end
end end
world:set(component, jecs.OnChange, on_change)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
end end
@ -134,12 +133,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
if not listeners then if not listeners then
listeners = {} listeners = {}
signals.removed[component] = listeners signals.removed[component] = listeners
local idr = jecs.id_record_ensure(world, component) local function on_remove(entity: number, id: number, value: any)
idr.hooks.on_remove = function(entity) for _, listener in listeners :: any do
for _, listener in listeners do listener(entity, id, value)
listener(entity, component)
end end
end end
world:set(component, jecs.OnRemove, on_remove)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
end end
@ -150,7 +149,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.monitor = monitors_new world.monitor = monitors_new
return world return world :: PatchedWorld
end end
return observers_add return observers_add