Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #110 from Sleitnick/module-lifecycle
Browse files Browse the repository at this point in the history
Module lifecycle
  • Loading branch information
Sleitnick authored Dec 15, 2019
2 parents c4426a1 + 684268d commit 53d9e7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/ServerScriptService/Aero/Internal/AeroServer.server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local remoteServices = Instance.new("Folder")
remoteServices.Name = "AeroRemoteServices"

local players = {}
local modulesAwaitingStart = {}

local FastSpawn = require(internalFolder.FastSpawn)

Expand Down Expand Up @@ -113,7 +114,11 @@ function AeroServer:WrapModule(tbl)
tbl:Init()
end
if (type(tbl.Start) == "function" and not tbl.__aeroPreventStart) then
FastSpawn(tbl.Start, tbl)
if (modulesAwaitingStart) then
modulesAwaitingStart[#modulesAwaitingStart + 1] = tbl
else
FastSpawn(tbl.Start, tbl)
end
end
end

Expand Down Expand Up @@ -266,6 +271,14 @@ local function Init()
end
end

-- Start modules that were already loaded:
local function StartLoadedModules()
for _,tbl in pairs(modulesAwaitingStart) do
FastSpawn(tbl.Start, tbl)
end
modulesAwaitingStart = nil
end

--------------------------------------------------------------------

players = game:GetService("Players"):GetPlayers()
Expand All @@ -281,6 +294,7 @@ local function Init()
InitAllServices(AeroServer.Services)
ScanRemoteFoldersForEmpty(remoteServices)
StartAllServices(AeroServer.Services)
StartLoadedModules()

-- Expose server framework to client and global scope:
remoteServices.Parent = game:GetService("ReplicatedStorage").Aero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local modulesFolder = script.Parent.Parent:WaitForChild("Modules")
local sharedFolder = game:GetService("ReplicatedStorage"):WaitForChild("Aero"):WaitForChild("Shared")
local internalFolder = game:GetService("ReplicatedStorage").Aero:WaitForChild("Internal")

local modulesAwaitingStart = {}

local FastSpawn = require(internalFolder:WaitForChild("FastSpawn"))

local function PreventEventRegister()
Expand Down Expand Up @@ -56,7 +58,11 @@ function Aero:WrapModule(tbl)
tbl:Init()
end
if (type(tbl.Start) == "function" and not tbl.__aeroPreventStart) then
FastSpawn(tbl.Start, tbl)
if (modulesAwaitingStart) then
modulesAwaitingStart[#modulesAwaitingStart + 1] = tbl
else
FastSpawn(tbl.Start, tbl)
end
end
end

Expand Down Expand Up @@ -185,6 +191,14 @@ local function Init()
end
end

-- Start modules that were already loaded:
local function StartLoadedModules()
for _,tbl in pairs(modulesAwaitingStart) do
FastSpawn(tbl.Start, tbl)
end
modulesAwaitingStart = nil
end

------------------------------------------------------

-- Lazy load modules:
Expand All @@ -198,6 +212,7 @@ local function Init()
LoadAllControllers(controllersFolder, Aero.Controllers)
InitAllControllers(Aero.Controllers)
StartAllControllers(Aero.Controllers)
StartLoadedModules()

-- Expose client framework globally:
_G.Aero = Aero
Expand Down

0 comments on commit 53d9e7d

Please sign in to comment.