diff --git a/jecs/addons/observers.luau b/jecs/addons/observers.luau index 5f3f1f5..0175060 100644 --- a/jecs/addons/observers.luau +++ b/jecs/addons/observers.luau @@ -1,16 +1,16 @@ local jecs = require("@jecs") -type Observer = { +type Observer = { callback: (jecs.Entity) -> (), - query: jecs.Query, + query: jecs.Query<...jecs.Id>, } export type PatchedWorld = jecs.World & { - added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: any) -> ()) -> (), - removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (), - changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (), - observer: (PatchedWorld, Observer) -> (), - monitor: (PatchedWorld, Observer) -> (), + added: (PatchedWorld, jecs.Id, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (), + removed: (PatchedWorld, jecs.Id, (jecs.Entity, jecs.Id) -> ()) -> () -> (), + changed: (PatchedWorld, jecs.Id, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (), + observer: (PatchedWorld, Observer) -> (), + monitor: (PatchedWorld, Observer) -> (), } local function observers_new(world, description) @@ -70,6 +70,7 @@ local function join(world, component) sparse_array[entity] = nil dense_array[max_id] = nil values[max_id] = nil + max_id -= 1 end) world:changed(component, function(entity, id, value) @@ -89,62 +90,6 @@ local function join(world, component) end end -local function query_changed(world, component) - assert(jecs.IS_PAIR(component) == false) - local callerid = debug.info(2, "sl") - - local tracker = world.trackers[callerid] - if not tracker then - local records = {} - local connections = {} - tracker = { - records = records, - connections = connections - } - world.trackers[callerid] = tracker - - table.insert(connections, world:added(component, function(entity, id, v) - tracker[entity] = { - new = v - } - end)) - table.insert(connections, world:changed(component, function(entity, id, v) - local record = tracker[entity] - record.old = record.new - record.new = v - end)) - - table.insert(connections, world:removed(component, function(entity, id) - local record = tracker[entity] - record.old = record.new - record.new = nil - end)) - end - - local entity = nil - local record = nil - return function() - entity, record = next(tracker, entity) - if entity == nil then - return - end - return entity, record - end -end - -local function spy_on_world_delete(world) - local world_delete = world.delete - world.delete = function(world, entity) - world_delete(world, entity) - for _, tracker in world.trackers do - tracker.records[entity] = nil - for _, connection in tracker.connections do - connection() - end - end - end -end - local function monitors_new(world, description) local query = description.query local callback = description.callback @@ -192,18 +137,23 @@ local function monitors_new(world, description) end end -local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld +local function observers_add(world: jecs.World): PatchedWorld type Signal = { [jecs.Entity]: { (...any) -> () } } + + local world_mut = world :: jecs.World & {[string]: any} + local signals = { added = {} :: Signal, emplaced = {} :: Signal, removed = {} :: Signal } - world.added = function(_, component, fn) + world_mut.added = function( + _: jecs.World, + component: jecs.Id, + fn: (e: jecs.Entity, id: jecs.Id, value: T) -> () + ) 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 @@ -213,7 +163,16 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl listener(entity, id, value) end end - world:set(component, jecs.OnAdd, on_add) + local idr = world.component_index[component] + if idr then + local idr_hook_existing = idr.hooks.on_add + if idr_hook_existing then + table.insert(listeners, idr_hook_existing) + end + idr.hooks.on_add = on_add :: any + else + world:set(component, jecs.OnAdd, on_add) + end end table.insert(listeners, fn) return function() @@ -224,10 +183,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl end end - world.changed = function(_, component, fn) + world_mut.changed = function( + _: jecs.World, + component: jecs.Id, + fn: (e: jecs.Entity, id: jecs.Id, value: T) -> () + ) 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 @@ -236,7 +197,16 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl listener(entity, id, value) end end - world:set(component, jecs.OnChange, on_change) + local idr = world.component_index[component] + if idr then + local idr_hook_existing = idr.hooks.on_change + if idr_hook_existing then + table.insert(listeners, idr_hook_existing) + end + idr.hooks.on_change = on_change :: any + else + world:set(component, jecs.OnChange, on_change) + end end table.insert(listeners, fn) return function() @@ -247,10 +217,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl end end - world.removed = function(_, component, fn) + world_mut.removed = function( + _: jecs.World, + component: jecs.Id, + fn: (e: jecs.Entity, id: jecs.Id) -> () + ) 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 @@ -259,7 +231,16 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl listener(entity, id, value) end end - world:set(component, jecs.OnRemove, on_remove) + local idr = world.component_index[component] + if idr then + local idr_hook_existing = idr.hooks.on_remove + if idr_hook_existing then + table.insert(listeners, idr_hook_existing) + end + idr.hooks.on_remove = on_remove :: any + else + world:set(component, jecs.OnRemove, on_remove) + end end table.insert(listeners, fn) return function() @@ -270,15 +251,15 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl end end - world.signals = signals + world_mut.signals = signals - world.observer = observers_new + world_mut.observer = observers_new - world.monitor = monitors_new + world_mut.monitor = monitors_new - world.trackers = {} + world_mut.trackers = {} - return world :: PatchedWorld + return world_mut :: PatchedWorld end return observers_add diff --git a/jecs/build.txt b/jecs/build.txt index ff9399c..2eb25d5 100644 --- a/jecs/build.txt +++ b/jecs/build.txt @@ -1,2 +1,2 @@ -modified = ["jecs.luau"] -version = "0.5.5-nightly.20250429T001100Z" +modified = ["addons/observers.luau", "jecs.luau"] +version = "0.6.0-rc.1-nightly.20250509T001119Z" diff --git a/jecs/jecs.luau b/jecs/jecs.luau index 3d61e96..f705f0e 100644 --- a/jecs/jecs.luau +++ b/jecs/jecs.luau @@ -780,7 +780,11 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53 local any = dense_array[dense] if dense <= alive_count then - return any + if any ~= entity then + error("Entity ID is already in use with a different generation") + else + return entity + end end local e_swap = dense_array[dense] @@ -790,9 +794,9 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53 r_swap.dense = dense r.dense = alive_count dense_array[dense] = e_swap - dense_array[alive_count] = any + dense_array[alive_count] = entity - return any + return entity else for i = max_id + 1, index do sparse_array[i] = { dense = i } :: ecs_record_t @@ -2403,9 +2407,9 @@ export type ComponentRecord = { flags: number, size: number, hooks: { - on_add: ((entity: Entity) -> ())?, - on_set: ((entity: Entity, data: any) -> ())?, - on_remove: ((entity: Entity) -> ())?, + on_add: ((entity: Entity, id: Entity, value: T) -> ())?, + on_change: ((entity: Entity, id: Entity, value: T) -> ())?, + on_remove: ((entity: Entity, id: Entity) -> ())?, }, } export type ComponentIndex = Map diff --git a/jecs/pesde-rbx.toml b/jecs/pesde-rbx.toml index aed318d..bef1286 100644 --- a/jecs/pesde-rbx.toml +++ b/jecs/pesde-rbx.toml @@ -3,7 +3,7 @@ includes = ["init.luau", "pesde.toml", "README.md", "CHANGELOG.md", "LICENSE", " license = "MIT" name = "marked/jecs_nightly" repository = "https://git.devmarked.win/marked/jecs-nightly" -version = "0.5.5-nightly.20250429T001100Z" +version = "0.6.0-rc.1-nightly.20250509T001119Z" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/jecs/pesde.toml b/jecs/pesde.toml index 17546f9..287766b 100644 --- a/jecs/pesde.toml +++ b/jecs/pesde.toml @@ -3,7 +3,7 @@ includes = ["init.luau", "pesde.toml", "README.md", "CHANGELOG.md", "LICENSE", " license = "MIT" name = "marked/jecs_nightly" repository = "https://git.devmarked.win/marked/jecs-nightly" -version = "0.5.5-nightly.20250429T001100Z" +version = "0.6.0-rc.1-nightly.20250509T001119Z" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/jecs/test.txt b/jecs/test.txt index dbaa44c..db110e7 100644 --- a/jecs/test.txt +++ b/jecs/test.txt @@ -1,2 +1,2 @@ passed = true -timestamp = "20250507T001102Z" +timestamp = "20250509T001121Z" diff --git a/jecs/test_fulllog.txt b/jecs/test_fulllog.txt index dd3e963..4ece543 100644 --- a/jecs/test_fulllog.txt +++ b/jecs/test_fulllog.txt @@ -1,6 +1,9 @@ -7.2 us  2 kB│ delete children of entity -9.6 us  1 kB│ remove friends of entity -347 ns  0  B│ simple deletion of entity +7.0 us  3 kB│ delete children of entity +8.9 us  1 kB│ remove friends of entity +345 ns  10  B│ simple deletion of entity +repro +NONE│  + world:add() PASS│ idempotent PASS│ archetype move @@ -118,5 +121,5 @@ PASS│ #2 PASS│ #3 -73/73 test cases passed in 32.637 ms. +74/74 test cases passed in 30.871 ms. 0 fails diff --git a/jecs/wally.toml b/jecs/wally.toml index 8ee99e2..d0ec6b0 100644 --- a/jecs/wally.toml +++ b/jecs/wally.toml @@ -5,4 +5,4 @@ license = "MIT" name = "mark-marks/jecs-nightly" realm = "shared" registry = "https://github.com/UpliftGames/wally-index" -version = "0.5.5-nightly.20250429T001100Z" +version = "0.6.0-rc.1-nightly.20250509T001119Z"