Skip to content

Commit

Permalink
executeIfInternet fires both success and failed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Buchweitz committed Oct 7, 2011
1 parent 854e4e5 commit 7351c2b
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions crawlspaceLib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,20 @@ so access it during the first few seconds of launch.
]]

helpArr.executeIfInternet = 'executeIfInternet(myInternetMethod, myNonInternetMethod)'
local toExecute = {}
local onInternet = {}
local checkForInternet
local executeOnNet = function()
for i=1, #toExecute do local f = table.remove(toExecute); f(); f=nil end
local executeOnNet = function(bool)
if bool then
for i=1, #onInternet do local f = table.remove(onInternet); f.y(); f=nil end
else
for i=1, #onInternet do local f = table.remove(onInternet); f.n(); f=nil end
end
end
-- Sets global variable "internet"
local internetListener = function( event )
if event.isError then _G.internet = false; timer.performWithDelay(30000, checkForInternet, false)
else _G.internet = true; executeOnNet() end
if event.isError then _G.internet = false
else _G.internet = true end
executeOnNet(_G.internet)
return true
end

Expand All @@ -1128,10 +1133,10 @@ checkForInternet = function()
end
checkForInternet()

executeIfInternet = function(f)
if internet then f(); return true
elseif internet == false then return false
elseif internet == nil then toExecute[#toExecute+1] = f end
executeIfInternet = function(y, n)
if internet then y(); return true
elseif internet == false then n(); return false
elseif internet == nil then onInternet[#onInternet+1] = {y=y, n=n} end
end

--[[ ########## Global Information Handling ########## ]--
Expand Down

0 comments on commit 7351c2b

Please sign in to comment.