Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Loop.luau
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local IS_CLIENT = RunService:IsClient()

local Jecs = require(script.Parent.Parent.Jecs)
local Jabby = require(script.Parent.Parent.Jabby)
local Types = require(script.Parent.Types)

local pair = Jecs.pair
local topoRuntime = require(script.Parent.topoRuntime)
Expand All @@ -24,18 +25,24 @@ local function systemFn(system)
end

local function systemName(system)
if typeof(system) == "table" then
return system.name
end
local fn = systemFn(system)
return debug.info(fn, "s") .. "->" .. debug.info(fn, "n")
end

function Loop.new(params: { name: string, debug: any, world: any }, ...)
function Loop.new<T...>(
params: { name: string, debug: Types.Entity<string>, world: Types.World },
...: T...
): Types.Loop<T...>
local self = setmetatable({}, Loop)

self._name = params.name
self._debug = params.debug or Jecs.Name
self._running = false

self._state = { ... }
self._state = table.pack(...)
self._stateLength = select("#", ...)

self._world = params.world
Expand Down Expand Up @@ -168,7 +175,7 @@ function Loop:begin()

local commitFailed = false
local thread = coroutine.create(function(...)
self._reporter:run(system.jabbyId, system.callback)
self._reporter:run(system.jabbyId, system.callback, ...)
end)

local success, errorValue = coroutine.resume(thread, unpack(self._state, 1, self._stateLength))
Expand Down
22 changes: 22 additions & 0 deletions src/Types.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local Jecs = require(script.Parent.Parent.Jecs)

export type World = typeof(Jecs.World.new())
export type Entity<T> = number & {
__T: T,
}

export type System<T...> = { system: (T...) -> (), name: string } | (T...) -> ()

export type Loop<T...> = {
phases: {
RenderStepped: Entity<number>,
Heartbeat: Entity<number>,
PreSimulation: Entity<number>,
PreAnimation: Entity<number>,
},
phase: (after: Entity<number>) -> Entity<number>,
schedule: (self: Loop<T...>, system: System<T...>, phase: number?) -> System<T...>,
begin: (self: Loop<T...>) -> { [any]: any },
}

return {}
2 changes: 1 addition & 1 deletion src/topoRuntime.luau
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ end
@param discriminator? any -- A unique value to additionally key by
@param cleanupCallback (storage: {}) -> boolean? -- A function to run when the storage for this hook is cleaned up
]=]
local function useHookState(discriminator, cleanupCallback): {}
local function useHookState(discriminator: any, cleanupCallback: (storage: {}) -> ()): {}
local file, line = debug.info(3, "sl")
local fn = debug.info(2, "f")

Expand Down
22 changes: 20 additions & 2 deletions src/useEvent.luau
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ local function connect(object, callback, event)
end
end

error("Couldn't connect to event as no valid connect methods were found! Ensure the passed event has a 'Connect' or an 'on' method!")
error(
"Couldn't connect to event as no valid connect methods were found! Ensure the passed event has a 'Connect' or an 'on' method!"
)
end

local function disconnect(connection)
Expand Down Expand Up @@ -80,6 +82,13 @@ end
A connection object returned by a custom event must be either a table with any of the following methods, or a cleanup function.
]=]

type ConnectionObject = {
Disconnect: (() -> ())?,
Destroy: (() -> ())?,
disconnect: (() -> ())?,
destroy: (() -> ())?,
} | () -> ()

--[=[
@interface CustomEvent
@within Matter
Expand All @@ -90,6 +99,12 @@ end
A custom event must have any of these 3 methods.
]=]

type CustomEvent = {
Connect: ((...any) -> ConnectionObject)?,
on: ((...any) -> ConnectionObject)?,
connect: ((...any) -> ConnectionObject)?,
}

--[=[
@within Matter
:::info Topologically-aware function
Expand Down Expand Up @@ -155,7 +170,10 @@ end
@param instance Instance | { [string]: CustomEvent } | CustomEvent -- The instance or the custom event, or a table that has the event you want to connect to
@param event string | RBXScriptSignal | CustomEvent -- The name of, or the actual event that you want to connect to
]=]
local function useEvent(instance, event): () -> (number, ...any)
local function useEvent(
instance: Instance | { [string]: CustomEvent } | CustomEvent,
event: string | RBXScriptSignal | CustomEvent
): () -> (number, ...any)
assert(instance ~= nil, "Instance is nil")
assert(event ~= nil, "Event is nil")

Expand Down
2 changes: 1 addition & 1 deletion src/useThrottle.luau
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end
@param discriminator? any -- A unique value to additionally key by
@return boolean -- returns true every x seconds, otherwise false
]=]
local function useThrottle(seconds, discriminator)
local function useThrottle(seconds: number, discriminator: any)
local storage = topoRuntime.useHookState(discriminator, cleanup)

if storage.time == nil or os.clock() - storage.time >= seconds then
Expand Down