Update hook retrieval

This commit is contained in:
Ukendio 2025-07-14 00:45:01 +02:00
parent f6731069aa
commit 9ae32bcce8

View file

@ -1,28 +1,46 @@
--!strict --!strict
local jecs = require("@jecs") local jecs = require("@jecs")
type Description<T...> = jecs.Query<T...>
type ObserverArm = (<a>(
PatchedWorld,
{
query: jecs.Query<a>,
callback: ((jecs.Entity) -> ())?
}
) -> () -> () -> (jecs.Entity)) & (<a, b>(
PatchedWorld,
{
query: jecs.Query<a, b>,
callback: ((jecs.Entity) -> ())?
}
) -> () -> () -> (jecs.Entity)) & (<a, b, c>(
PatchedWorld,
{
query: jecs.Query<a, b, c>,
callback: ((jecs.Entity) -> ())?
}
) -> () -> () -> (jecs.Entity))
export type PatchedWorld = jecs.World & { export type PatchedWorld = jecs.World & {
added: <T>(PatchedWorld, jecs.Id<T>, <e>(e: jecs.Entity<e>, id: jecs.Id<T>, value: T?) -> ()) -> () -> (), added: <T>(PatchedWorld, jecs.Id<T>, <e>(e: jecs.Entity<e>, id: jecs.Id<T>, value: T?) -> ()) -> () -> (),
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (), removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
changed: <T>(PatchedWorld, jecs.Id<T>, <e>(e: jecs.Entity<e>, id: jecs.Id<T>, value: T) -> ()) -> () -> (), changed: <T>(PatchedWorld, jecs.Id<T>, <e>(e: jecs.Entity<e>, id: jecs.Id<T>, value: T) -> ()) -> () -> (),
observer: <T...>( observer: ObserverArm & any,
PatchedWorld, monitor: ObserverArm & any
jecs.Query<T...>,
(<a>(jecs.Entity, jecs.Id<a>, a) -> ())?
) -> () -> (jecs.Entity),
monitor: (
PatchedWorld,
any,
(<a>(jecs.Entity, jecs.Id<a>) -> ())?
) -> ()
} }
local function observers_new( local function observers_new(
world: PatchedWorld, world: PatchedWorld,
description: {
query: any, query: any,
callback: (<T, a>(jecs.Entity<T>, jecs.Id<a>, value: a?) -> ())? callback: (<T, a>(jecs.Entity<T>, jecs.Id<a>, value: a?) -> ())?
}
) )
query = query:cached() local query = description.query:cached()
local callback = description.callback
local archetypes = {} local archetypes = {}
local terms = query.ids local terms = query.ids
@ -46,8 +64,7 @@ local function observers_new(
id: jecs.Id<a>, id: jecs.Id<a>,
value: a? value: a?
) )
local r = jecs.entity_index_try_get_fast( local r = entity_index.sparse_array[jecs.ECS_ID(entity)]
entity_index, entity :: any) :: jecs.Record
local archetype = r.archetype local archetype = r.archetype