rewrite(phase5): add docstring

This commit is contained in:
khtsly 2026-02-23 12:02:18 +07:00
parent 85f6aa8b77
commit c4a7934bc4
3 changed files with 459 additions and 404 deletions

View file

@ -29,8 +29,13 @@ local eventSchemas: { [number]: Buffer.SchemaType } = {}
local pendingInvokes: { [string]: thread } = {} local pendingInvokes: { [string]: thread } = {}
local invokeId = 0 local invokeId = 0
--@optional
-- Yields the current thread until the client has successfully initialized and synchronized with the server's replication data (identifier). Its optionally, but highly recommended to call this before firing or connecting to any events to ensure the network is fully ready.
Client.awaitReady = Replication.wait_for_ready Client.awaitReady = Replication.wait_for_ready
--@remoteName string
--@schema Buffer.SchemaType
-- Define a schema for strict data packing on a specific event.
Client.useSchema = function(remoteName: string, schema: Buffer.SchemaType) Client.useSchema = function(remoteName: string, schema: Buffer.SchemaType)
local id = Replication.get_id(remoteName) local id = Replication.get_id(remoteName)
if not id then if not id then
@ -40,6 +45,9 @@ Client.useSchema = function(remoteName: string, schema: Buffer.SchemaType)
eventSchemas[id] = schema eventSchemas[id] = schema
end end
--@remoteName string
--@fn function
-- Connect to an event to receive incoming data from the server.
Client.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?): Connection Client.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?): Connection
local id = Replication.get_id(remoteName) local id = Replication.get_id(remoteName)
if not id then if not id then
@ -66,6 +74,9 @@ Client.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?):
} :: Connection } :: Connection
end end
--@remoteName string
--@fn function
-- Similar to :Connect but automatically disconnects after the first firing.
Client.Once = function(remoteName: string, fn: (...any?) -> ()): Connection Client.Once = function(remoteName: string, fn: (...any?) -> ()): Connection
local connection local connection
connection = Client.Connect(remoteName, function(...: any?) connection = Client.Connect(remoteName, function(...: any?)
@ -77,6 +88,8 @@ Client.Once = function(remoteName: string, fn: (...any?) -> ()): Connection
return connection return connection
end end
--@remoteName string
-- Wait for an event to be triggered. Yields the current thread.
Client.Wait = function(remoteName: string): (number, ...any?) Client.Wait = function(remoteName: string): (number, ...any?)
local thread, t = coroutine.running(), os.clock() local thread, t = coroutine.running(), os.clock()
Client.Once(remoteName, function(...: any?) Client.Once(remoteName, function(...: any?)
@ -85,6 +98,8 @@ Client.Wait = function(remoteName: string): (number, ...any?)
return coroutine.yield() return coroutine.yield()
end end
--@remoteName string
-- Disconnect all connections for a specific event.
Client.DisconnectAll = function(remoteName: string) Client.DisconnectAll = function(remoteName: string)
local id = Replication.get_id(remoteName) local id = Replication.get_id(remoteName)
if not id then if not id then
@ -97,8 +112,13 @@ Client.DisconnectAll = function(remoteName: string)
end end
end end
--@remoteName string
-- Disconnect all connections and remove the event.
Client.Destroy = Client.DisconnectAll Client.Destroy = Client.DisconnectAll
--@remoteName string
--@reliable boolean
-- Fire an event to the server.
Client.Fire = function(remoteName: string, reliable: boolean, ...: any?) Client.Fire = function(remoteName: string, reliable: boolean, ...: any?)
local id = Replication.get_id(remoteName) local id = Replication.get_id(remoteName)
if id then if id then
@ -109,6 +129,9 @@ Client.Fire = function(remoteName: string, reliable: boolean, ...: any?)
end end
end end
--@remoteName string
--@timeout number?
-- Invoke the server with timeout support. Yields the current thread. Returns nil if timeout occurs.
Client.Invoke = function(remoteName: string, timeout: number?, ...: any?): ...any? Client.Invoke = function(remoteName: string, timeout: number?, ...: any?): ...any?
local id = Replication.get_id(remoteName) local id = Replication.get_id(remoteName)
if not id then if not id then

View file

@ -86,7 +86,6 @@ if RunService:IsClient() or RunService:IsRunMode() then
if not obj then if not obj then
warn(`[Replication] timeout: could not find identifier '{name}' after {timeout}s.`) warn(`[Replication] timeout: could not find identifier '{name}' after {timeout}s.`)
end end
return obj return obj
end end

View file

@ -37,16 +37,25 @@ local players_ready: { Player }, player_bytes: { [Player]: number } = {}, {}
local pendingInvokes: { [string]: thread } = {} local pendingInvokes: { [string]: thread } = {}
local invokeId = 0 local invokeId = 0
--@namespaces { string }
--@optional
-- Register namespaces to ensure all of the namespaces is being registered earlier on the server to prevent any unexpected issues on the client.
Server.reg_namespaces = function(namespaces: { string }) Server.reg_namespaces = function(namespaces: { string })
for _, name: string in namespaces do for _, name: string in namespaces do
Identifier.get_id(name) Identifier.get_id(name)
end end
end end
--@remoteName string
--@schema Buffer.SchemaType
-- Define a schema for strict data packing on a specific event.
Server.useSchema = function(remoteName: string, schema: Buffer.SchemaType) Server.useSchema = function(remoteName: string, schema: Buffer.SchemaType)
eventSchemas[Identifier.get_id(remoteName)] = schema eventSchemas[Identifier.get_id(remoteName)] = schema
end end
--@remoteName string
--@fn function
-- Connect to an event to receive incoming data from clients.
Server.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?): Connection Server.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?): Connection
local detail = { local detail = {
i = Identifier.get_id(remoteName), i = Identifier.get_id(remoteName),
@ -68,6 +77,9 @@ Server.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?):
} :: Connection } :: Connection
end end
--@remoteName string
--@fn function
-- Similar to :Connect but automatically disconnects after the first firing.
Server.Once = function(remoteName: string, fn: (...any?) -> ()): Connection Server.Once = function(remoteName: string, fn: (...any?) -> ()): Connection
local connection local connection
connection = Server.Connect(remoteName, function(...: any?) connection = Server.Connect(remoteName, function(...: any?)
@ -79,6 +91,8 @@ Server.Once = function(remoteName: string, fn: (...any?) -> ()): Connection
return connection return connection
end end
--@remoteName string
-- Wait for an event to be triggered. Yields the current thread.
Server.Wait = function(remoteName: string): (number, ...any?) Server.Wait = function(remoteName: string): (number, ...any?)
local thread, t = coroutine.running(), os.clock() local thread, t = coroutine.running(), os.clock()
Server.Once(remoteName, function(...: any?) Server.Once(remoteName, function(...: any?)
@ -87,6 +101,8 @@ Server.Wait = function(remoteName: string): (number, ...any?)
return coroutine.yield() return coroutine.yield()
end end
--@remoteName string
-- Disconnect all connections for a specific event.
Server.DisconnectAll = function(remoteName: string) Server.DisconnectAll = function(remoteName: string)
local id = Identifier.get_id(remoteName) local id = Identifier.get_id(remoteName)
if not id then if not id then
@ -99,8 +115,14 @@ Server.DisconnectAll = function(remoteName: string)
end end
end end
--@remoteName string
-- Disconnect all connections and remove the event.
Server.Destroy = Server.DisconnectAll Server.Destroy = Server.DisconnectAll
--@remoteName string
--@reliable boolean
--@player Player
-- Fire an event to a specific player.
Server.Fire = function(remoteName: string, reliable: boolean, player: Player, ...: any?) Server.Fire = function(remoteName: string, reliable: boolean, player: Player, ...: any?)
local targetQueue = reliable and queueEvent or queueUnreliableEvent local targetQueue = reliable and queueEvent or queueUnreliableEvent
if not targetQueue[player] then if not targetQueue[player] then
@ -112,12 +134,19 @@ Server.Fire = function(remoteName: string, reliable: boolean, player: Player, ..
}) })
end end
--@remoteName string
--@reliable boolean
-- Fire an event to all connected players.
Server.Fires = function(remoteName: string, reliable: boolean, ...: any?) Server.Fires = function(remoteName: string, reliable: boolean, ...: any?)
for _, player: Player in players_ready do for _, player: Player in players_ready do
Server.Fire(remoteName, reliable, player, ...) Server.Fire(remoteName, reliable, player, ...)
end end
end end
--@remoteName string
--@reliable boolean
--@except { Player }
-- Fire an event to all players except specified ones.
Server.FireExcept = function(remoteName: string, reliable: boolean, except: { Player }, ...: any?) Server.FireExcept = function(remoteName: string, reliable: boolean, except: { Player }, ...: any?)
for _, player: Player in players_ready do for _, player: Player in players_ready do
if table.find(except, player) then continue end if table.find(except, player) then continue end
@ -125,6 +154,10 @@ Server.FireExcept = function(remoteName: string, reliable: boolean, except: { Pl
end end
end end
--@remoteName string
--@player Player
--@timeout number?
-- Invoke a client with timeout support. Yields the current thread. Returns nil if timeout occurs.
Server.Invoke = function(remoteName: string, player: Player, timeout: number?, ...: any?): ...any? Server.Invoke = function(remoteName: string, player: Player, timeout: number?, ...: any?): ...any?
invokeId += 1 invokeId += 1
local reqId, thread = `{invokeId}`, coroutine.running() local reqId, thread = `{invokeId}`, coroutine.running()