diff --git a/addons/observers.luau b/addons/observers.luau index fdf256e..b008222 100755 --- a/addons/observers.luau +++ b/addons/observers.luau @@ -1,28 +1,46 @@ --!strict local jecs = require("@jecs") + +type Description = jecs.Query + +type ObserverArm = (( + PatchedWorld, + { + query: jecs.Query, + callback: ((jecs.Entity) -> ())? + } +) -> () -> () -> (jecs.Entity)) & (( + PatchedWorld, + { + query: jecs.Query, + callback: ((jecs.Entity) -> ())? + } +) -> () -> () -> (jecs.Entity)) & (( + PatchedWorld, + { + query: jecs.Query, + callback: ((jecs.Entity) -> ())? + } +) -> () -> () -> (jecs.Entity)) + 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) -> ()) -> () -> (), - observer: ( - PatchedWorld, - jecs.Query, - ((jecs.Entity, jecs.Id, a) -> ())? - ) -> () -> (jecs.Entity), - monitor: ( - PatchedWorld, - any, - ((jecs.Entity, jecs.Id) -> ())? - ) -> () + observer: ObserverArm & any, + monitor: ObserverArm & any } local function observers_new( world: PatchedWorld, - query: any, - callback: ((jecs.Entity, jecs.Id, value: a?) -> ())? + description: { + query: any, + callback: ((jecs.Entity, jecs.Id, value: a?) -> ())? + } ) - query = query:cached() + local query = description.query:cached() + local callback = description.callback local archetypes = {} local terms = query.ids @@ -46,8 +64,7 @@ local function observers_new( id: jecs.Id, value: a? ) - local r = jecs.entity_index_try_get_fast( - entity_index, entity :: any) :: jecs.Record + local r = entity_index.sparse_array[jecs.ECS_ID(entity)] local archetype = r.archetype @@ -68,9 +85,9 @@ local function observers_new( return function() local row = i return function() - if row == 0 then - i = 0 - table.clear(entities) + if row == 0 then + i = 0 + table.clear(entities) end local entity = entities[row] row -= 1