jecs/demo/src/ReplicatedStorage/std/ref.luau
Marcus ec4fa3ff3e
Add cached queries (#166)
* Initial commit

* Add tests

* Dedup observers

* Handle filters on table creation

* Handle Archetype deletion

* Remove print

* Fix type errors

* Cleanup code

* Manually inline code

* Build terms for cached queries

* Specialized cached query iterator

* Remove shadowed variable

* Inverse statement

* Rework demo

* Fix metatable

* Use generalized iteration
2024-12-26 06:15:41 +01:00

26 lines
529 B
Text

local world = require(script.Parent.world)
local jecs = require(game:GetService("ReplicatedStorage").ecs)
local refs: {[any]: jecs.Entity} = {}
local function fini(key): () -> ()
return function()
refs[key] = nil
end
end
local function noop() end
local function ref(key): (jecs.Entity, () -> ())
if not key then
return world:entity(), noop
end
local e = refs[key]
if not e then
e = world:entity()
refs[key] = e
end
-- Cannot cache handles because they will get invalidated
return e, fini(key)
end
return ref