Skip to content

Commit

Permalink
use new backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdotink committed Nov 30, 2023
1 parent b7ced53 commit 4bfff92
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
24 changes: 8 additions & 16 deletions lib/Event/Client.luau
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
local ClientEvent = require(script.Parent.Parent.ClientEvent)
local Spawn = require(script.Parent.Parent.Parent.Spawn)

local function PackArgs(...: any)
return { ... }
end
local Net = require(script.Parent.Parent.Net)

export type Client<T...> = {
Id: string,
Listening: boolean,
Validate: (...unknown) -> T...,

Fire: (self: Client<T...>, T...) -> (),

On: (self: Client<T...>, Callback: (T...) -> ()) -> (),
}

local function Fire<T...>(self: Client<T...>, ...)
ClientEvent.Fire(self.Id, PackArgs(...))
local function Fire<T...>(self: Client<T...>, ...: T...)
Net.Client.SendReliableEvent(self.Id, table.pack(...))
end

local function On<T...>(self: Client<T...>, Callback: (T...) -> ())
assert(not self.Listening, "Event is already listening")
self.Listening = true

ClientEvent.Listen(self.Id, Callback)
Net.Client.SetListener(self.Id, function(Args)
Spawn(Callback, table.unpack(Args))
end)
end

local function Client<T...>(Id: string, Validate: (...unknown) -> T...): Client<T...>
local function Client<T...>(Id: string): Client<T...>
return {
Id = Id,
Listening = false,
Validate = Validate,

Fire = Fire,
On = On,
Expand Down
39 changes: 17 additions & 22 deletions lib/Event/Server.luau
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
local Players = game:GetService("Players")

local ServerEvent = require(script.Parent.Parent.ServerEvent)
local Spawn = require(script.Parent.Parent.Parent.Spawn)

local function PackArgs(...: any)
return { ... }
end
local Net = require(script.Parent.Parent.Net)

export type Server<T...> = {
Id: string,
Listening: boolean,
Validate: (...unknown) -> T...,

Fire: (self: Server<T...>, Player: Player, T...) -> (),
Expand All @@ -21,60 +18,58 @@ export type Server<T...> = {
}

local function Fire<T...>(self: Server<T...>, Player: Player, ...: T...)
ServerEvent.Fire(Player, self.Id, PackArgs(...))
Net.Server.SendReliableEvent(Player, self.Id, table.pack(...))
end

local function FireAll<T...>(self: Server<T...>, ...: T...)
local Args = PackArgs(...)
local Args = table.pack(...)

for _, Player in Players:GetPlayers() do
ServerEvent.Fire(Player, self.Id, Args)
Net.Server.SendReliableEvent(Player, self.Id, Args)
end
end

local function FireAllExcept<T...>(self: Server<T...>, Except: Player, ...: T...)
local Args = PackArgs(...)
local Args = table.pack(...)

for _, Player in Players:GetPlayers() do
if Player ~= Except then
ServerEvent.Fire(Player, self.Id, Args)
Net.Server.SendReliableEvent(Player, self.Id, Args)
end
end
end

local function FireList<T...>(self: Server<T...>, List: { Player }, ...: T...)
local Args = PackArgs(...)
local Args = table.pack(...)

for _, Player in List do
ServerEvent.Fire(Player, self.Id, Args)
Net.Server.SendReliableEvent(Player, self.Id, Args)
end
end

local function FireWithFilter<T...>(self: Server<T...>, Filter: (Player) -> boolean, ...: T...)
local Args = PackArgs(...)
local Args = table.pack(...)

for _, Player in Players:GetPlayers() do
if Filter(Player) then
ServerEvent.Fire(Player, self.Id, Args)
Net.Server.SendReliableEvent(Player, self.Id, Args)
end
end
end

local function On<T...>(self: Server<T...>, Callback: (Player, T...) -> ())
assert(not self.Listening, "Event is already listening")
self.Listening = true

ServerEvent.Listen(self.Id, function(Player, ...)
if pcall(self.Validate, ...) then
Callback(Player, ...)
end
Net.Server.SetListener(self.Id, function(Player, Args)
Spawn(function(self: typeof(self), Player: Player, ...: any)
if self.Validate(...) then
Callback(Player, ...)
end
end, self, Player, table.unpack(Args))
end)
end

local function Server<T...>(Id: string, Validate: (...unknown) -> T...): Server<T...>
return {
Id = Id,
Listening = false,
Validate = Validate,

Fire = Fire,
Expand Down
2 changes: 1 addition & 1 deletion lib/Event/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local function Client<T...>(self: Event<T...>): ClientEvent.Client<T...>
assert(RunService:IsClient(), "Client events can only be accessed from the client")

if not self.ClientEvent then
self.ClientEvent = ClientEvent(self.Id, self.Validate)
self.ClientEvent = ClientEvent(self.Id) :: any
end

return self.ClientEvent :: any
Expand Down
39 changes: 24 additions & 15 deletions lib/Function.luau
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
local RunService = game:GetService("RunService")

local Future = require(script.Parent.Parent.Future)
local Spawn = require(script.Parent.Parent.Spawn)

local ServerEvent = require(script.Parent.ServerEvent)
local ClientEvent = require(script.Parent.ClientEvent)
local Net = require(script.Parent.Net)
local Identifier = require(script.Parent.Identifier)

local function PackArgs(...: any)
return { ... }
end

export type Function<A..., R...> = {
Id: string,
Validate: (...unknown) -> A...,
Listening: boolean,

SetCallback: (self: Function<A..., R...>, Callback: (Player, A...) -> R...) -> (),
Call: (self: Function<A..., R...>, A...) -> typeof(Future.new(function(): R... end)),
Call: (
self: Function<A..., R...>,
A...
) -> typeof(Future.new(function(): (boolean, R...)
return {} :: any
end)),
}

local function SetCallback<A..., R...>(self: Function<A..., R...>, Callback: (Player, A...) -> R...)
assert(RunService:IsServer(), "Cannot set callback to function on client")
assert(not self.Listening, "Cannot set callback to function multiple times")

self.Listening = true
ServerEvent.Listen(self.Id, function(Player, ...)
if pcall(self.Validate, ...) then
return Callback(Player, ...)
Net.Server.SetListener(self.Id, function(Player, Args)
local CallId = table.remove(Args, 1)

if type(CallId) ~= "string" then
return
end

Spawn(function(Player: Player, CallId: string, ...: any)
if pcall(self.Validate, ...) then
Net.Server.SendCallReturn(Player, CallId, table.pack(pcall(Callback, Player, ...)))
end
end, Player, CallId, unpack(Args))
end)
end

local function Call<A..., R...>(self: Function<A..., R...>, ...: A...)
return ClientEvent.Call(self.Id, PackArgs(...))
return Future.new(function(...: any)
local CallId = Identifier.Unique()

return unpack(Net.Client.CallAsync(self.Id, table.pack(CallId, ...)))
end, ...)
end

local function Function<A..., R...>(
Expand All @@ -45,7 +55,6 @@ local function Function<A..., R...>(
return {
Id = Identifier.Shared(Name):Await(),
Validate = ValidateArg,
Listening = false,

SetCallback = SetCallback,
Call = Call,
Expand Down
4 changes: 2 additions & 2 deletions lib/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ if RunService:IsServer() then
UnreliableRemote.Parent = ReplicatedStorage
end

require(script.ServerEvent).Start()
require(script.Net).Server.Start()
else
ReplicatedStorage:WaitForChild("ReliableRedEvent")
ReplicatedStorage:WaitForChild("UnreliableRedEvent")

require(script.ClientEvent).Start()
require(script.Net).Client.Start()
end

return {
Expand Down

0 comments on commit 4bfff92

Please sign in to comment.