From 28a8f0ed3ce4cf47d7820834a6ddd32d2781a9be Mon Sep 17 00:00:00 2001 From: AshleyFlow Date: Mon, 28 Oct 2024 20:32:56 +0330 Subject: [PATCH 1/6] types for users --- src/Loop.luau | 8 ++++++-- src/Types.luau | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/Types.luau diff --git a/src/Loop.luau b/src/Loop.luau index 77df51b..660b844 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) @@ -28,14 +29,17 @@ local function systemName(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 diff --git a/src/Types.luau b/src/Types.luau new file mode 100644 index 0000000..5af9edf --- /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: () -> (), name: string } | () -> () + +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 {} From 155f6464caf02f31165ecaee4b19b7d64e982e2b Mon Sep 17 00:00:00 2001 From: AshleyFlow Date: Mon, 28 Oct 2024 20:36:20 +0330 Subject: [PATCH 2/6] typed params for hooks --- src/topoRuntime.luau | 2 +- src/useEvent.luau | 6 ++++-- src/useThrottle.luau | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) 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..e7a4413 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) @@ -155,7 +157,7 @@ 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, event: string): () -> (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 From f2e8eeee961db34c9eb8e5fa318bac1525b338a7 Mon Sep 17 00:00:00 2001 From: AshleyFlow Date: Mon, 28 Oct 2024 20:39:29 +0330 Subject: [PATCH 3/6] include states in system type --- src/Types.luau | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Types.luau b/src/Types.luau index 5af9edf..d480888 100644 --- a/src/Types.luau +++ b/src/Types.luau @@ -5,7 +5,7 @@ export type Entity = number & { __T: T, } -export type System = { system: () -> (), name: string } | () -> () +export type System = { system: (T...) -> (), name: string } | (T...) -> () export type Loop = { phases: { @@ -15,7 +15,7 @@ export type Loop = { PreAnimation: Entity, }, phase: (after: Entity) -> Entity, - schedule: (self: Loop, system: System, phase: number?) -> System, + schedule: (self: Loop, system: System, phase: number?) -> System, begin: (self: Loop) -> { [any]: any }, } From 4693337cc2ff62705a85fe7f84ba7fbfef39cf65 Mon Sep 17 00:00:00 2001 From: AshleyFlow Date: Mon, 28 Oct 2024 20:44:53 +0330 Subject: [PATCH 4/6] custom system name --- src/Loop.luau | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Loop.luau b/src/Loop.luau index 660b844..2a730fc 100644 --- a/src/Loop.luau +++ b/src/Loop.luau @@ -25,6 +25,9 @@ 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 From 285417ae8c303856bbf245ff44cb2186d12c7e83 Mon Sep 17 00:00:00 2001 From: AshleyFlow Date: Mon, 28 Oct 2024 20:47:49 +0330 Subject: [PATCH 5/6] pass states to scheduler --- src/Loop.luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Loop.luau b/src/Loop.luau index 2a730fc..dda4c70 100644 --- a/src/Loop.luau +++ b/src/Loop.luau @@ -175,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)) From efcfad2815d7c7d3aacec8ac8ce0778c72a3b63d Mon Sep 17 00:00:00 2001 From: AshleyFlow Date: Mon, 28 Oct 2024 20:59:11 +0330 Subject: [PATCH 6/6] correctly type useEvent --- src/useEvent.luau | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/useEvent.luau b/src/useEvent.luau index e7a4413..b698f2b 100644 --- a/src/useEvent.luau +++ b/src/useEvent.luau @@ -82,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 @@ -92,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 @@ -157,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: Instance, event: string): () -> (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")