refactored tsched.lua with checkArgs and fail-safes
This commit is contained in:
@@ -14,6 +14,7 @@ local gpu = component.gpu
|
|||||||
local log = require("log")
|
local log = require("log")
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.runAsTask(path, ...)
|
function _G._PUBLIC.tsched.runAsTask(path, ...)
|
||||||
|
checkArg(1, path, "string")
|
||||||
local args = { ... }
|
local args = { ... }
|
||||||
local function taskFunction()
|
local function taskFunction()
|
||||||
local result, errorMessage = xpcall(function(...)
|
local result, errorMessage = xpcall(function(...)
|
||||||
@@ -59,9 +60,11 @@ function _G._PUBLIC.tsched.runAsTask(path, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.addTask(func, name)
|
function _G._PUBLIC.tsched.addTask(func, name)
|
||||||
|
checkArg(1, func, "function")
|
||||||
|
checkArg(2, name, "string")
|
||||||
local task = coroutine.create(func)
|
local task = coroutine.create(func)
|
||||||
local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = idCounter }
|
local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = idCounter }
|
||||||
if currentTask and type(currentTask.id) == "number" then
|
if type(currentTask) == "table" and type(currentTask.id) == "number" then
|
||||||
taskInfo.parent = currentTask.id
|
taskInfo.parent = currentTask.id
|
||||||
end
|
end
|
||||||
table.insert(tsched.tasks, taskInfo)
|
table.insert(tsched.tasks, taskInfo)
|
||||||
@@ -77,6 +80,7 @@ function _G._PUBLIC.tsched.addTask(func, name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.removeTask(id)
|
function _G._PUBLIC.tsched.removeTask(id)
|
||||||
|
checkArg(1, id, "number")
|
||||||
-- TODO: Check for user permissions before running
|
-- TODO: Check for user permissions before running
|
||||||
for index, task in pairs(tsched.tasks) do
|
for index, task in pairs(tsched.tasks) do
|
||||||
if task.id == id then
|
if task.id == id then
|
||||||
|
|||||||
Reference in New Issue
Block a user