diff --git a/src/Loop.luau b/src/Loop.luau index 77df51b..dda4c70 100644 --- a/src/Loop.luau +++ b/src/Loop.luau @@ -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) @@ -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( + params: { name: string, debug: Types.Entity, world: Types.World }, + ...: T... +): Types.Loop 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 @@ -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)) diff --git a/src/Types.luau b/src/Types.luau new file mode 100644 index 0000000..d480888 --- /dev/null +++ b/src/Types.luau @@ -0,0 +1,22 @@ +local Jecs = require(script.Parent.Parent.Jecs) + +export type World = typeof(Jecs.World.new()) +export type Entity = number & { + __T: T, +} + +export type System = { system: (T...) -> (), name: string } | (T...) -> () + +export type Loop = { + phases: { + RenderStepped: Entity, + Heartbeat: Entity, + PreSimulation: Entity, + PreAnimation: Entity, + }, + phase: (after: Entity) -> Entity, + schedule: (self: Loop, system: System, phase: number?) -> System, + begin: (self: Loop) -> { [any]: any }, +} + +return {} diff --git a/src/topoRuntime.luau b/src/topoRuntime.luau index 2a5e70a..d2d0145 100644 --- a/src/topoRuntime.luau +++ b/src/topoRuntime.luau @@ -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") diff --git a/src/useEvent.luau b/src/useEvent.luau index 27dc59e..b698f2b 100644 --- a/src/useEvent.luau +++ b/src/useEvent.luau @@ -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) @@ -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 @@ -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 @@ -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") diff --git a/src/useThrottle.luau b/src/useThrottle.luau index bfd79e9..a79fbe4 100644 --- a/src/useThrottle.luau +++ b/src/useThrottle.luau @@ -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