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

Commit

Permalink
Coroutines and deprecation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Sep 20, 2018
1 parent 2aa10bc commit 59899a2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ As always, you can also check the commit history for a given version as well, an

| Version | Date | Description |
| ---|---|--- |
| [1.2.4](#1.2.4) | 2018-09-20 | <ul><li>Using `coroutine` yielding/resuming where applicable</li><li>Cleaned up deprecated code in TaskScheduler</li></ul>
| [1.2.3](#1.2.3) | 2018-08-15 | <ul><li>Added `WrapModule` method to Server and Client main scripts.</li></ul> |
| [1.2.2](#1.2.2) | 2018-08-15 | <ul><li>Added Failed events for DataService.</li><li>Added Failed event for DataStoreCache.</li><li>Added Failed event for SafeDataStore.</li></ul> |

### Version History Notes

#### <a name="1.2.4"> Version 1.2.4
Instead of using `while` loops to wait for code to complete, proper usage of `coroutine.yield` and `coroutine.resume` have been implemented. This change reflects best practices on Roblox. Doing this was not possible before a recent Roblox update. The overall behavior of the code remains entirely the same.

Some deprecated code has also been fixed in the TaskScheduler. The functionality remains entirely the same.

#### <a name="1.2.3"></a> Version 1.2.3
Added the `WrapModule` method to both the AeroServer and AeroClient scripts. This method takes a `table` and will set its metatable to the same metatable used by other Aero-based modules/controllers/services. This allows you to easily integrate custom modules into the framework if needed.

Expand Down
15 changes: 6 additions & 9 deletions src/ReplicatedStorage/Aero/Shared/Event.modulescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,13 @@ end


function Event:Wait()
local c, returnArgs = nil, nil
local be = Instance.new("BindableEvent")
c = self:Connect(function(...)
c:Disconnect()
returnArgs = {...}
be:Fire()
local thread = coroutine.running()
local connection
connection = self:Connect(function(...)
connection:Disconnect()
coroutine.resume(thread, ...)
end)
be.Event:Wait()
be:Destroy()
return unpack(returnArgs)
return coroutine.yield()
end


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ end

function Cache:FlushAllConcurrent()
if (not self.DataStore) then return end
local thread = coroutine.running()
local numData = 0
local numFlushed = 0
for key,_ in pairs(self.Data) do
Expand All @@ -106,9 +107,12 @@ function Cache:FlushAllConcurrent()
spawn(function()
self:Flush(key, true)
numFlushed = (numFlushed + 1)
if (numFlushed == numData) then
coroutine.resume(thread)
end
end)
end
while (numFlushed < numData) do wait() end
coroutine.yield()
end


Expand Down
21 changes: 16 additions & 5 deletions src/ServerStorage/Aero/Services/DataService.modulescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ end


function DataService:FlushAllConcurrent()
local thread = coroutine.running()
local numCaches = 0
local numFlushed = 0
for _ in pairs(playerCaches) do
Expand All @@ -220,20 +221,26 @@ function DataService:FlushAllConcurrent()
for _,cache in pairs(customCaches) do
numCaches = (numCaches + 1)
end
local function IncFlushed()
numFlushed = (numFlushed + 1)
if (numFlushed == numCaches) then
coroutine.resume(thread)
end
end
for player,cache in pairs(playerCaches) do
spawn(function()
cache:FlushAllConcurrent()
numFlushed = (numFlushed + 1)
IncFlushed()
end)
end
for _,cache in pairs(customCaches) do
spawn(function()
cache:FlushAll()
numFlushed = (numFlushed + 1)
IncFlushed()
end)
end
globalCache:FlushAll()
while (numFlushed < numCaches) do wait() end
coroutine.yield()
end


Expand Down Expand Up @@ -261,17 +268,21 @@ function DataService:Start()
self.GameClosing = false

local function FireBoundToCloseCallbacks()
local thread = coroutine.running()
local numBinded = #boundToCloseFuncs
local numCompleted = 0
local maxWait = 20
local start = tick()
for _,func in pairs(boundToCloseFuncs) do
coroutine.wrap(function()
func()
pcall(func)
numCompleted = (numCompleted + 1)
if (numCompleted == numBinded) then
coroutine.resume(thread)
end
end)()
end
while (numCompleted < numBinded and (tick() - start) < maxWait) do wait() end
coroutine.yield()
end

-- Flush cache:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
-- Author: EchoReaper
-- ROBLOX Link: https://www.roblox.com/Task-Scheduler-item?id=348019935
-- Roblox Link: https://www.roblox.com/Task-Scheduler-item?id=348019935
-- Publically released January 25, 2016

-- Optimizations and edits made by Crazyman32
-- Changes from EchoReaper's version:
-- Changes made from EchoReaper's version:
-- GetCurrentFPS() method removed
-- FPS is only tracked when the 'Loop' function is running for performance reasons
-- Styled code in consistency with the rest of the Team Crazy Game Framework codebase
-- Styled code in consistency with the rest of the AeroGameFramework codebase

--[[
Expand All @@ -22,13 +21,13 @@



local TaskScheduler = {}

local lastIteration
local frameUpdateTable = {}

local runService = game:GetService("RunService")

local TaskScheduler = {}

--[[
param targetFps Task scheduler won't run a task if it'd make the FPS drop below this amount
(WARNING) this only holds true if it is used properly. If you try to complete 10 union operations
Expand Down Expand Up @@ -60,7 +59,7 @@ local TaskScheduler = {}
plugin:Union({partA, partB}):Destroy()
totalOperations = totalOperations + 1
print("Times unioned:", totalOperations)
if totalOperations == 50 then
if (totalOperations == 50) then
scheduler:Pause()
paused = true
end
Expand All @@ -71,6 +70,8 @@ local TaskScheduler = {}
wait(2)
scheduler:Resume()
--]]


function TaskScheduler:CreateScheduler(targetFps)

local scheduler = {}
Expand All @@ -81,7 +82,7 @@ function TaskScheduler:CreateScheduler(targetFps)
local updateFrameTableEvent = nil

local start = tick()
runService.RenderStepped:wait()
runService.RenderStepped:Wait()

local function UpdateFrameTable()
lastIteration = tick()
Expand All @@ -92,7 +93,7 @@ function TaskScheduler:CreateScheduler(targetFps)
end

local function Loop()
updateFrameTableEvent = runService.RenderStepped:connect(UpdateFrameTable)
updateFrameTableEvent = runService.RenderStepped:Connect(UpdateFrameTable)
while (true) do
if (sleeping) then break end
local fps = (((tick() - start) >= 1 and #frameUpdateTable) or (#frameUpdateTable / (tick() - start)))
Expand All @@ -105,10 +106,10 @@ function TaskScheduler:CreateScheduler(targetFps)
break
end
else
runService.RenderStepped:wait()
runService.RenderStepped:Wait()
end
end
updateFrameTableEvent:disconnect()
updateFrameTableEvent:Disconnect()
updateFrameTableEvent = nil
end

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.3
v1.2.4

0 comments on commit 59899a2

Please sign in to comment.