diff --git a/jecs/addons/observers.luau b/jecs/addons/observers.luau index ef85d85..5f3f1f5 100644 --- a/jecs/addons/observers.luau +++ b/jecs/addons/observers.luau @@ -6,9 +6,9 @@ type Observer = { } export type PatchedWorld = jecs.World & { - added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (), - removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (), - changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (), + 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) -> (), } @@ -70,7 +70,6 @@ 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) @@ -90,6 +89,62 @@ 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 @@ -137,23 +192,18 @@ local function monitors_new(world, description) end end -local function observers_add(world: jecs.World): PatchedWorld +local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld type Signal = { [jecs.Entity]: { (...any) -> () } } - - local world_mut = world :: jecs.World & {[string]: any} - local signals = { added = {} :: Signal, emplaced = {} :: Signal, removed = {} :: Signal } - world_mut.added = function( - _: jecs.World, - component: jecs.Id, - fn: (e: jecs.Entity, id: jecs.Id, value: T) -> () - ) + 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 @@ -163,16 +213,7 @@ local function observers_add(world: jecs.World): PatchedWorld listener(entity, id, value) end end - 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 + world:set(component, jecs.OnAdd, on_add) end table.insert(listeners, fn) return function() @@ -183,12 +224,10 @@ local function observers_add(world: jecs.World): PatchedWorld end end - world_mut.changed = function( - _: jecs.World, - component: jecs.Id, - fn: (e: jecs.Entity, id: jecs.Id, value: T) -> () - ) + 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 @@ -197,16 +236,7 @@ local function observers_add(world: jecs.World): PatchedWorld listener(entity, id, value) end end - 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 + world:set(component, jecs.OnChange, on_change) end table.insert(listeners, fn) return function() @@ -217,12 +247,10 @@ local function observers_add(world: jecs.World): PatchedWorld end end - world_mut.removed = function( - _: jecs.World, - component: jecs.Id, - fn: (e: jecs.Entity, id: jecs.Id) -> () - ) + 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 @@ -231,16 +259,7 @@ local function observers_add(world: jecs.World): PatchedWorld listener(entity, id, value) end end - 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 + world:set(component, jecs.OnRemove, on_remove) end table.insert(listeners, fn) return function() @@ -251,15 +270,15 @@ local function observers_add(world: jecs.World): PatchedWorld end end - world_mut.signals = signals + world.signals = signals - world_mut.observer = observers_new + world.observer = observers_new - world_mut.monitor = monitors_new + world.monitor = monitors_new - world_mut.trackers = {} + world.trackers = {} - return world_mut :: PatchedWorld + return world :: PatchedWorld end return observers_add diff --git a/jecs/build.txt b/jecs/build.txt index 7f1df49..ff9399c 100644 --- a/jecs/build.txt +++ b/jecs/build.txt @@ -1,2 +1,2 @@ -modified = ["addons/observers.luau", "jecs.luau"] -version = "0.6.0-rc.1-nightly.20250508T001059Z" +modified = ["jecs.luau"] +version = "0.5.5-nightly.20250429T001100Z" diff --git a/jecs/jecs.luau b/jecs/jecs.luau index 35cf16a..3d61e96 100644 --- a/jecs/jecs.luau +++ b/jecs/jecs.luau @@ -2403,9 +2403,9 @@ export type ComponentRecord = { flags: number, size: number, hooks: { - on_add: ((entity: Entity, id: Entity, value: T) -> ())?, - on_change: ((entity: Entity, id: Entity, value: T) -> ())?, - on_remove: ((entity: Entity, id: Entity) -> ())?, + on_add: ((entity: Entity) -> ())?, + on_set: ((entity: Entity, data: any) -> ())?, + on_remove: ((entity: Entity) -> ())?, }, } export type ComponentIndex = Map diff --git a/jecs/pesde-rbx.toml b/jecs/pesde-rbx.toml index 31e700d..aed318d 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.6.0-rc.1-nightly.20250508T001059Z" +version = "0.5.5-nightly.20250429T001100Z" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/jecs/pesde.toml b/jecs/pesde.toml index 6efefea..17546f9 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.6.0-rc.1-nightly.20250508T001059Z" +version = "0.5.5-nightly.20250429T001100Z" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/jecs/test.txt b/jecs/test.txt index 5d7566c..dbaa44c 100644 --- a/jecs/test.txt +++ b/jecs/test.txt @@ -1,2 +1,2 @@ passed = true -timestamp = "20250508T001102Z" +timestamp = "20250507T001102Z" diff --git a/jecs/test_fulllog.txt b/jecs/test_fulllog.txt index 5bcdf18..dd3e963 100644 --- a/jecs/test_fulllog.txt +++ b/jecs/test_fulllog.txt @@ -1,9 +1,6 @@ -7.2 us  3 kB│ delete children of entity -9.0 us  1 kB│ remove friends of entity -393 ns  0  B│ simple deletion of entity -repro -NONE│  - +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 world:add() PASS│ idempotent PASS│ archetype move @@ -121,5 +118,5 @@ PASS│ #2 PASS│ #3 -74/74 test cases passed in 32.600 ms. +73/73 test cases passed in 32.637 ms. 0 fails diff --git a/jecs/wally.toml b/jecs/wally.toml index e09d156..8ee99e2 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.6.0-rc.1-nightly.20250508T001059Z" +version = "0.5.5-nightly.20250429T001100Z"