Advertisement
Jakey4543

NetworkService

Feb 19th, 2022
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. local events,modules = {},{}
  2.  
  3. local globalConnections = {}
  4. local initalisedServices = {}
  5.  
  6. local HttpService = game:GetService("HttpService")
  7. local RunService = game:GetService("RunService")
  8.  
  9. local module = {}
  10. module.__index = module
  11.  
  12.     function module.new(m,e)
  13.         events = e
  14.         modules = m
  15.        
  16.         local self = {
  17.             ["_connections"] = {}
  18.         }
  19.        
  20.         setmetatable(self,module)
  21.         table.insert(initalisedServices,self)
  22.        
  23.         return self
  24.     end
  25.    
  26.     function module:ListenToEvent(Args)
  27.         local EventName,Callback = table.unpack(Args)
  28.         local EventObject = events[EventName]
  29.        
  30.         local Assert = modules.Utils.Assert
  31.        
  32.         Assert(self._connections[EventName],("Warning: this event is already being listened to by this function (%s)"):format(EventName))
  33.        
  34.         if Assert(not Callback,("Callback required %s"):format(EventName)) then
  35.             return false
  36.         end
  37.        
  38.         if Assert(not EventObject,("Cannot find event %s"):format(EventName)) then -- checks if the event exists
  39.             Callback(nil) -- send error to the callback
  40.             return false
  41.         end
  42.        
  43.         local EventCallType = (if EventObject:IsA("RemoteEvent") then 1 else 2)
  44.        
  45.         local connection = nil
  46.         local key = HttpService:GenerateGUID(false)
  47.        
  48.         if RunService:IsServer() then
  49.             if EventCallType == 1 then
  50.                 connection = EventObject.OnServerEvent:Connect(function(...) -- we use all of these to be sure we get all possible data from the event
  51.                     self._connections[EventName..key] = connection
  52.                     globalConnections[EventName..key] = connection
  53.  
  54.                     Callback(...)
  55.                 end)
  56.             else
  57.                 -- theres no connection for remote functions safly
  58.                 EventObject.OnServerInvoke = function(...)
  59.                     local args = table.pack(...)
  60.  
  61.  
  62.                     if Callback then
  63.                         task.spawn(function()
  64.                             local Return_Data = Callback(table.unpack(args)) -- we have to use this instead of ... or it errors
  65.  
  66.                             repeat
  67.                                 task.wait()
  68.                             until Return_Data
  69.  
  70.                             return Return_Data
  71.                         end)
  72.                     end
  73.                 end
  74.             end
  75.         elseif RunService:IsClient() then
  76.             if EventCallType == 1 then
  77.                 connection = EventObject.OnClientEvent:Connect(function(...) -- we use all of these to be sure we get all possible data from the event
  78.                     self._connections[EventName..key] = connection
  79.                     globalConnections[EventName..key] = connection
  80.  
  81.                     Callback(...)
  82.                 end)
  83.             else
  84.                 -- theres no connection for remote functions safly
  85.                 EventObject.OnClientInvoke = function(...)
  86.                     local args = table.pack(...)
  87.  
  88.  
  89.                     if Callback then
  90.                         task.spawn(function()
  91.                             local Return_Data = Callback(table.unpack(args)) -- we have to use this instead of ... or it errors
  92.  
  93.                             repeat
  94.                                 task.wait()
  95.                             until Return_Data
  96.  
  97.                             return Return_Data
  98.                         end)
  99.                     end
  100.                 end
  101.             end
  102.         end
  103.     end
  104.    
  105.     function module:Cleanup(global)
  106.         if modules.Utils.Assert(global == true,"Starting global disconnection") then
  107.             for k,connection in pairs(globalConnections) do
  108.                 warn(("Disconnecting %s"):format(k))
  109.                 connection:Disconnect()
  110.             end
  111.            
  112.             for _,service in pairs(initalisedServices) do -- clear all the _connections of all initialised services
  113.                 warn("Removing service connections")
  114.                 service._connections = {}
  115.             end
  116.         else
  117.             warn("Starting local disconnection")
  118.             for k,connection in pairs(self._connections) do
  119.                 warn(("Disconnecting %s"):format(k))
  120.                 connection:Disconnect()
  121.                 globalConnections[k] = nil -- remove this connection from the global connetions
  122.             end
  123.         end
  124.     end
  125.    
  126.     function module._Cleanup() -- we dont need a global arg for this because if its called it can only be global
  127.         for k,connection in pairs(globalConnections) do
  128.             warn(("Disconnecting %s"):format(k))
  129.             connection:Disconnect()
  130.         end
  131.        
  132.         for _,service in pairs(initalisedServices) do -- clear all the _connections of all initialised services
  133.             warn("Removing service connections")
  134.             service._connections = {}
  135.         end
  136.     end
  137.  
  138. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement