mirror of
https://github.com/Ukendio/jecs.git
synced 2025-06-20 00:09:18 +00:00
Fix contrived bug with observer
Some checks failed
Some checks failed
This commit is contained in:
parent
5dba6e7bac
commit
bb03e88d3d
1 changed files with 33 additions and 30 deletions
|
@ -2,15 +2,20 @@ local jecs = require("@jecs")
|
||||||
|
|
||||||
type Observer = {
|
type Observer = {
|
||||||
callback: (jecs.Entity) -> (),
|
callback: (jecs.Entity) -> (),
|
||||||
query: jecs.Query<...jecs.Id>,
|
query: jecs.Query<...any>,
|
||||||
|
}
|
||||||
|
|
||||||
|
type Monitor = {
|
||||||
|
callback: (jecs.Entity, jecs.Entity) -> (),
|
||||||
|
queyr: jecs.Query<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PatchedWorld = jecs.World & {
|
export type PatchedWorld = jecs.World & {
|
||||||
added: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
|
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||||
removed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id) -> ()) -> () -> (),
|
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
|
||||||
changed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
|
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||||
observer: (PatchedWorld, Observer) -> (),
|
observer: (PatchedWorld, Observer) -> (),
|
||||||
monitor: (PatchedWorld, Observer) -> (),
|
monitor: (PatchedWorld, Monitor) -> (),
|
||||||
}
|
}
|
||||||
|
|
||||||
local function observers_new(world, description)
|
local function observers_new(world, description)
|
||||||
|
@ -45,7 +50,6 @@ local function observers_new(world, description)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function join(world, component)
|
local function join(world, component)
|
||||||
local sparse_array = {}
|
local sparse_array = {}
|
||||||
local dense_array = {}
|
local dense_array = {}
|
||||||
|
@ -163,16 +167,16 @@ local function observers_add(world: jecs.World): PatchedWorld
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local idr = world.component_index[component]
|
local existing_hook = world:get(component, jecs.OnAdd)
|
||||||
if idr then
|
if existing_hook then
|
||||||
local idr_hook_existing = idr.hooks.on_add
|
table.insert(listeners, existing_hook)
|
||||||
if idr_hook_existing then
|
local idr = world.component_index[component]
|
||||||
table.insert(listeners, idr_hook_existing)
|
if idr then
|
||||||
|
idr.hooks.on_add = on_add
|
||||||
end
|
end
|
||||||
idr.hooks.on_add = on_add :: any
|
|
||||||
else
|
|
||||||
world:set(component, jecs.OnAdd, on_add)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
world:set(component, jecs.OnAdd, on_add)
|
||||||
end
|
end
|
||||||
table.insert(listeners, fn)
|
table.insert(listeners, fn)
|
||||||
return function()
|
return function()
|
||||||
|
@ -197,16 +201,15 @@ local function observers_add(world: jecs.World): PatchedWorld
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local idr = world.component_index[component]
|
local existing_hook = world:get(component, jecs.OnChange)
|
||||||
if idr then
|
if existing_hook then
|
||||||
local idr_hook_existing = idr.hooks.on_change
|
table.insert(listeners, existing_hook)
|
||||||
if idr_hook_existing then
|
local idr = world.component_index[component]
|
||||||
table.insert(listeners, idr_hook_existing)
|
if idr then
|
||||||
|
idr.hooks.on_change = on_change
|
||||||
end
|
end
|
||||||
idr.hooks.on_change = on_change :: any
|
|
||||||
else
|
|
||||||
world:set(component, jecs.OnChange, on_change)
|
|
||||||
end
|
end
|
||||||
|
world:set(component, jecs.OnChange, on_change)
|
||||||
end
|
end
|
||||||
table.insert(listeners, fn)
|
table.insert(listeners, fn)
|
||||||
return function()
|
return function()
|
||||||
|
@ -231,16 +234,16 @@ local function observers_add(world: jecs.World): PatchedWorld
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local idr = world.component_index[component]
|
local existing_hook = world:get(component, jecs.OnRemove)
|
||||||
if idr then
|
if existing_hook then
|
||||||
local idr_hook_existing = idr.hooks.on_remove
|
table.insert(listeners, existing_hook)
|
||||||
if idr_hook_existing then
|
local idr = world.component_index[component]
|
||||||
table.insert(listeners, idr_hook_existing)
|
if idr then
|
||||||
|
idr.hooks.on_remove = on_remove
|
||||||
end
|
end
|
||||||
idr.hooks.on_remove = on_remove :: any
|
|
||||||
else
|
|
||||||
world:set(component, jecs.OnRemove, on_remove)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
world:set(component, jecs.OnRemove, on_remove)
|
||||||
end
|
end
|
||||||
table.insert(listeners, fn)
|
table.insert(listeners, fn)
|
||||||
return function()
|
return function()
|
||||||
|
|
Loading…
Reference in a new issue