here's how I have combines both:
Code:
-- Coroutines management table
local function SetupCoroutineTable()
coroutine_table = { };
end
-- adds a coroutine to the queue
local function AddCoroutine(Item)
table.insert(coroutine_table, Item);
end
-- remove a coroutine to the queue
local function RemoveCoroutine(Index)
table.remove(coroutine_table, Index);
end
-- resumes a coroutine while it is active
local function ResumeCoroutine(Index)
Item = coroutine_table[Index];
if coroutine.status(Item) ~= 'dead' then
coroutine.resume(Item);
end
end
-- loops through the coroutines lists and calls the resume handler
local function ResumeAllCoroutines()
-- make sure there is something to process
if coroutine_table ~= {} then
for Index,Item in ipairs(coroutine_table) do
ResumeCoroutine(Index);
end
else
return
end
end
-- pause timer
function Pause(seconds)
local start = Inspect.Time.Frame()
while start + seconds > Inspect.Time.Frame() do
coroutine.yield()
end
end
-- function that check for the global queue status
local queueStatus = false
local function QueueStatus()
-- inspects the queue status
queueStatus = Inspect.Queue.Status("global")
if queueStatus then return end -- global queue is still backlogged, var is true so exit out
queueStatus = false
end
-- small function that opens an email
local function MailOpen(k)
-- check if the queue is available before opening
if not QueueStatus() then
Command.Mail.Open(k)
else
Pause(0.2)
MailOpen(k)
end
end
-- function that parses each email and store it in a emprary database for further processing
local function Mailboxparser()
print("Starting to read emails")
local status = Inspect.Interaction("mail")
if status == true then
-- get the list of email
mailList = Inspect.Mail.List()
-- checking how many email will be parsed (cannot use table.getn, since this table contains key/values => only way is to iterate throught the table)
mail_number = 0
for k,v in pairs(mailList) do
mail_number = mail_number + 1
end
-- index that stores the nuber of processed emails
processed_mail_count = 1
-- fetch through each emails
for k, v in pairs(mailList) do
-- if email hasn't been parsed previously
if not setContains(mail_history, k) or mail_history == {} then
-- open email to have access to details
mailOpen(k)
Pause(0.2)
-- get details
details = (Inspect.Mail.Detail(k))
-- table that will store the mail content
mail_details = {}
-- feeding the table
table.insert(mail_details, details["from"])
table.insert(mail_details, details["subject"])
table.insert(mail_details, details["body"])
--table.insert(mail_details, os.date)
-- table that will contain teh attachment list if there is any
attachment_list = {}
-- detecty if the is any attachment
if tonumber(details["attachments"]) == nil and details["attachments"] ~= nil then
-- add item ids in a table
for ka, va in pairs(details["attachments"]) do
table.insert(attachment_list, va)
end
end
table.insert(mail_details, attachment_list)
-- detect if mail is actually "read" (only way to declare the mail as processed)
if details["body"] ~= nil then
addToSet(mail_history, k, mail_details)
processed_mail_count = processed_mail_count + 1
end
-- check if we're done processing
if mail_number == processed_mail_count then
print("Email recording complete: " .. processed_mail_count .. " entries saved")
RemoveCoroutine(parsing_coro)
end
end
end
end
end
local function Launch_mailboxparser()
parsing_coro = coroutine.create(Mailboxparser)
AddCoroutine(parsing_coro)
end
SetupCoroutineTable()
table.insert(Event.Queue.Status, {QueueStatus, "aher", "Queue Status"})
table.insert(Event.System.Update.Begin, {function() ResumeAllCoroutines() end, "aher", "OnUpdate" })
launch_mailboxparser() -- just for the sake of calling the function
Re-reading this have me wondering though. "if not QueueStatus() then", I wonder if I should just call the queuestatus variable instead.